PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ LWGEOM_asGeoJson()

Datum LWGEOM_asGeoJson ( PG_FUNCTION_ARGS  )

Definition at line 374 of file lwgeom_export.c.

375{
376 GSERIALIZED *geom;
377 LWGEOM *lwgeom;
378 char *geojson;
379 text *result;
380 int precision = DBL_DIG;
381 int output_bbox = LW_FALSE;
382 int output_long_crs = LW_FALSE;
383 int output_short_crs = LW_FALSE;
384 int output_guess_short_srid = LW_FALSE;
385 char *srs = NULL;
386 int32_t srid;
387
388 /* Get the geometry */
389 if (PG_ARGISNULL(0))
390 PG_RETURN_NULL();
391
392 geom = PG_GETARG_GSERIALIZED_P(0);
393 srid = gserialized_get_srid(geom);
394
395 /* Retrieve precision if any (default is max) */
396 if ( PG_NARGS() > 1 && !PG_ARGISNULL(1) )
397 {
398 precision = PG_GETARG_INT32(1);
399 if ( precision > DBL_DIG )
400 precision = DBL_DIG;
401 else if ( precision < 0 )
402 precision = 0;
403 }
404
405 /* Retrieve output option
406 * 0 = without option
407 * 1 = bbox
408 * 2 = short crs
409 * 4 = long crs
410 * 8 = guess if CRS is needed (default)
411 */
412 if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
413 {
414 int option = PG_GETARG_INT32(2);
415 output_guess_short_srid = (option & 8) ? LW_TRUE : LW_FALSE;
416 output_short_crs = (option & 2) ? LW_TRUE : LW_FALSE;
417 output_long_crs = (option & 4) ? LW_TRUE : LW_FALSE;
418 output_bbox = (option & 1) ? LW_TRUE : LW_FALSE;
419 }
420 else
421 output_guess_short_srid = LW_TRUE;
422
423 if (output_guess_short_srid && srid != WGS84_SRID && srid != SRID_UNKNOWN)
424 output_short_crs = LW_TRUE;
425
426 if (srid != SRID_UNKNOWN && (output_short_crs || output_long_crs))
427 {
428 srs = getSRSbySRID(fcinfo, srid, !output_long_crs);
429
430 if (!srs)
431 {
432 elog(ERROR, "SRID %i unknown in spatial_ref_sys table", srid);
433 PG_RETURN_NULL();
434 }
435 }
436
437 lwgeom = lwgeom_from_gserialized(geom);
438 geojson = lwgeom_to_geojson(lwgeom, srs, precision, output_bbox);
439 lwgeom_free(lwgeom);
440
441 if (srs) pfree(srs);
442
443 result = cstring_to_text(geojson);
444 lwfree(geojson);
445
446 PG_FREE_IF_COPY(geom, 0);
447 PG_RETURN_TEXT_P(result);
448}
static uint8_t precision
Definition cu_in_twkb.c:25
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
#define WGS84_SRID
Definition liblwgeom.h:163
#define LW_FALSE
Definition liblwgeom.h:108
void lwgeom_free(LWGEOM *geom)
Definition lwgeom.c:1138
void lwfree(void *mem)
Definition lwutil.c:242
#define LW_TRUE
Return types for functions with status returns.
Definition liblwgeom.h:107
#define SRID_UNKNOWN
Unknown SRID value.
Definition liblwgeom.h:229
char * lwgeom_to_geojson(const LWGEOM *geo, char *srs, int precision, int has_bbox)
Takes a GEOMETRY and returns a GeoJson representation.
char * getSRSbySRID(FunctionCallInfo fcinfo, int32_t srid, bool short_crs)

References getSRSbySRID(), gserialized_get_srid(), LW_FALSE, LW_TRUE, lwfree(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_to_geojson(), precision, SRID_UNKNOWN, and WGS84_SRID.

Referenced by composite_to_geojson(), and LWGEOM_asGeoJson_old().

Here is the call graph for this function:
Here is the caller graph for this function: