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

Definition at line 390 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().

391 {
392  GSERIALIZED *geom;
393  LWGEOM *lwgeom;
394  char *geojson;
395  text *result;
396  int has_bbox = 0;
397  int precision = DBL_DIG;
398  char *srs = NULL;
399 
400  /* Get the geometry */
401  if ( PG_ARGISNULL(0) )
402  PG_RETURN_NULL();
403 
404  geom = PG_GETARG_GSERIALIZED_P(0);
405 
406  /* Retrieve precision if any (default is max) */
407  if ( PG_NARGS() > 1 && !PG_ARGISNULL(1) )
408  {
409  precision = PG_GETARG_INT32(1);
410  if ( precision > DBL_DIG )
411  precision = DBL_DIG;
412  else if ( precision < 0 )
413  precision = 0;
414  }
415 
416  /* Retrieve output option
417  * 0 = without option (default)
418  * 1 = bbox
419  * 2 = short crs
420  * 4 = long crs
421  */
422  if ( PG_NARGS() > 2 && !PG_ARGISNULL(2) )
423  {
424  int option = PG_GETARG_INT32(2);
425 
426  if ( option & 2 || option & 4 )
427  {
428  int srid = gserialized_get_srid(geom);
429  if ( srid != SRID_UNKNOWN )
430  {
431  if ( option & 2 )
432  srs = getSRSbySRID(srid, true);
433 
434  if ( option & 4 )
435  srs = getSRSbySRID(srid, false);
436 
437  if ( !srs )
438  {
439  elog(ERROR,
440  "SRID %i unknown in spatial_ref_sys table",
441  srid);
442  PG_RETURN_NULL();
443  }
444  }
445  }
446 
447  if (option & 1)
448  has_bbox = 1;
449  }
450 
451  lwgeom = lwgeom_from_gserialized(geom);
452  geojson = lwgeom_to_geojson(lwgeom, srs, precision, has_bbox);
453  lwgeom_free(lwgeom);
454 
455  if (srs) pfree(srs);
456 
457  result = cstring2text(geojson);
458  lwfree(geojson);
459 
460  PG_FREE_IF_COPY(geom, 0);
461  PG_RETURN_TEXT_P(result);
462 }
void lwfree(void *mem)
Definition: lwutil.c:214
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
LWGEOM * geom
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:172
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:34
uint8_t precision
Definition: cu_in_twkb.c:25
char * getSRSbySRID(int srid, bool short_crs)
Definition: lwgeom_export.c:40
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:69

Here is the call graph for this function:

Here is the caller graph for this function: