PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ LWGEOM_asGeoJson()

Datum LWGEOM_asGeoJson ( PG_FUNCTION_ARGS  )

Definition at line 442 of file lwgeom_export.c.

443 {
444  GSERIALIZED *geom;
445  LWGEOM *lwgeom;
446  char *geojson;
447  text *result;
448  int has_bbox = 0;
449  int precision = DBL_DIG;
450  char *srs = NULL;
451 
452  /* Get the geometry */
453  if ( PG_ARGISNULL(0) )
454  PG_RETURN_NULL();
455 
456  geom = PG_GETARG_GSERIALIZED_P(0);
457 
458  /* Retrieve precision if any (default is max) */
459  if ( PG_NARGS() > 1 && !PG_ARGISNULL(1) )
460  {
461  precision = PG_GETARG_INT32(1);
462  if ( precision > DBL_DIG )
463  precision = DBL_DIG;
464  else if ( precision < 0 )
465  precision = 0;
466  }
467 
468  /* Retrieve output option
469  * 0 = without option (default)
470  * 1 = bbox
471  * 2 = short crs
472  * 4 = long crs
473  */
474  if ( PG_NARGS() > 2 && !PG_ARGISNULL(2) )
475  {
476  int option = PG_GETARG_INT32(2);
477 
478  if ( option & 2 || option & 4 )
479  {
480  int srid = gserialized_get_srid(geom);
481  if ( srid != SRID_UNKNOWN )
482  {
483  if ( option & 2 )
484  srs = getSRSbySRID(fcinfo, srid, true);
485 
486  if ( option & 4 )
487  srs = getSRSbySRID(fcinfo, srid, false);
488 
489  if ( !srs )
490  {
491  elog(ERROR,
492  "SRID %i unknown in spatial_ref_sys table",
493  srid);
494  PG_RETURN_NULL();
495  }
496  }
497  }
498 
499  if (option & 1)
500  has_bbox = 1;
501  }
502 
503  lwgeom = lwgeom_from_gserialized(geom);
504  geojson = lwgeom_to_geojson(lwgeom, srs, precision, has_bbox);
505  lwgeom_free(lwgeom);
506 
507  if (srs) pfree(srs);
508 
509  result = cstring_to_text(geojson);
510  lwfree(geojson);
511 
512  PG_FREE_IF_COPY(geom, 0);
513  PG_RETURN_TEXT_P(result);
514 }
static uint8_t precision
Definition: cu_in_twkb.c:25
int32_t gserialized_get_srid(const GSERIALIZED *s)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: g_serialized.c:100
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
void lwfree(void *mem)
Definition: lwutil.c:244
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:188
char * lwgeom_to_geojson(const LWGEOM *geo, char *srs, int precision, int has_bbox)
Takes a GEOMETRY and returns a GeoJson representation.
Definition: lwout_geojson.c:48
char * getSRSbySRID(FunctionCallInfo fcinfo, int srid, bool short_crs)
Definition: lwgeom_export.c:60

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

Referenced by LWGEOM_asGeoJson_old().

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