PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ LWGEOM_asGeoJson()

Datum LWGEOM_asGeoJson ( PG_FUNCTION_ARGS  )

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

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