PostGIS  2.5.0beta2dev-r@@SVN_REVISION@@

◆ LWGEOM_asGeoJson()

Datum LWGEOM_asGeoJson ( PG_FUNCTION_ARGS  )

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

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