PostGIS  2.5.0dev-r@@SVN_REVISION@@
Datum LWGEOM_asGeoJson ( PG_FUNCTION_ARGS  )

Definition at line 416 of file lwgeom_export.c.

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

Referenced by LWGEOM_asGeoJson_old().

417 {
418  GSERIALIZED *geom;
419  LWGEOM *lwgeom;
420  char *geojson;
421  text *result;
422  int has_bbox = 0;
423  int precision = DBL_DIG;
424  char *srs = NULL;
425 
426  /* Get the geometry */
427  if ( PG_ARGISNULL(0) )
428  PG_RETURN_NULL();
429 
430  geom = PG_GETARG_GSERIALIZED_P(0);
431 
432  /* Retrieve precision if any (default is max) */
433  if ( PG_NARGS() > 1 && !PG_ARGISNULL(1) )
434  {
435  precision = PG_GETARG_INT32(1);
436  if ( precision > DBL_DIG )
437  precision = DBL_DIG;
438  else if ( precision < 0 )
439  precision = 0;
440  }
441 
442  /* Retrieve output option
443  * 0 = without option (default)
444  * 1 = bbox
445  * 2 = short crs
446  * 4 = long crs
447  */
448  if ( PG_NARGS() > 2 && !PG_ARGISNULL(2) )
449  {
450  int option = PG_GETARG_INT32(2);
451 
452  if ( option & 2 || option & 4 )
453  {
454  int srid = gserialized_get_srid(geom);
455  if ( srid != SRID_UNKNOWN )
456  {
457  if ( option & 2 )
458  srs = getSRSbySRID(srid, true);
459 
460  if ( option & 4 )
461  srs = getSRSbySRID(srid, false);
462 
463  if ( !srs )
464  {
465  elog(ERROR,
466  "SRID %i unknown in spatial_ref_sys table",
467  srid);
468  PG_RETURN_NULL();
469  }
470  }
471  }
472 
473  if (option & 1)
474  has_bbox = 1;
475  }
476 
477  lwgeom = lwgeom_from_gserialized(geom);
478  geojson = lwgeom_to_geojson(lwgeom, srs, precision, has_bbox);
479  lwgeom_free(lwgeom);
480 
481  if (srs) pfree(srs);
482 
483  result = cstring2text(geojson);
484  lwfree(geojson);
485 
486  PG_FREE_IF_COPY(geom, 0);
487  PG_RETURN_TEXT_P(result);
488 }
void lwfree(void *mem)
Definition: lwutil.c:244
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1137
LWGEOM * geom
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:187
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
uint8_t precision
Definition: cu_in_twkb.c:25
char * getSRSbySRID(int srid, bool short_crs)
Definition: lwgeom_export.c:55
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:99

Here is the call graph for this function:

Here is the caller graph for this function: