PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ LWGEOM_asGeoJson()

Datum LWGEOM_asGeoJson ( PG_FUNCTION_ARGS  )

Definition at line 408 of file lwgeom_export.c.

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

Referenced by LWGEOM_asGeoJson_old().

409 {
410  GSERIALIZED *geom;
411  LWGEOM *lwgeom;
412  char *geojson;
413  text *result;
414  int has_bbox = 0;
415  int precision = DBL_DIG;
416  char *srs = NULL;
417 
418  /* Get the geometry */
419  if ( PG_ARGISNULL(0) )
420  PG_RETURN_NULL();
421 
422  geom = PG_GETARG_GSERIALIZED_P(0);
423 
424  /* Retrieve precision if any (default is max) */
425  if ( PG_NARGS() > 1 && !PG_ARGISNULL(1) )
426  {
427  precision = PG_GETARG_INT32(1);
428  if ( precision > DBL_DIG )
429  precision = DBL_DIG;
430  else if ( precision < 0 )
431  precision = 0;
432  }
433 
434  /* Retrieve output option
435  * 0 = without option (default)
436  * 1 = bbox
437  * 2 = short crs
438  * 4 = long crs
439  */
440  if ( PG_NARGS() > 2 && !PG_ARGISNULL(2) )
441  {
442  int option = PG_GETARG_INT32(2);
443 
444  if ( option & 2 || option & 4 )
445  {
446  int srid = gserialized_get_srid(geom);
447  if ( srid != SRID_UNKNOWN )
448  {
449  if ( option & 2 )
450  srs = getSRSbySRID(srid, true);
451 
452  if ( option & 4 )
453  srs = getSRSbySRID(srid, false);
454 
455  if ( !srs )
456  {
457  elog(ERROR,
458  "SRID %i unknown in spatial_ref_sys table",
459  srid);
460  PG_RETURN_NULL();
461  }
462  }
463  }
464 
465  if (option & 1)
466  has_bbox = 1;
467  }
468 
469  lwgeom = lwgeom_from_gserialized(geom);
470  geojson = lwgeom_to_geojson(lwgeom, srs, precision, has_bbox);
471  lwgeom_free(lwgeom);
472 
473  if (srs) pfree(srs);
474 
475  result = cstring2text(geojson);
476  lwfree(geojson);
477 
478  PG_FREE_IF_COPY(geom, 0);
479  PG_RETURN_TEXT_P(result);
480 }
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:1099
LWGEOM * geom
#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
uint8_t precision
Definition: cu_in_twkb.c:25
char * getSRSbySRID(int srid, bool short_crs)
Definition: lwgeom_export.c:57
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
Here is the call graph for this function:
Here is the caller graph for this function: