PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ geography_as_geojson()

Datum geography_as_geojson ( PG_FUNCTION_ARGS  )

Definition at line 476 of file geography_inout.c.

477 {
478  LWGEOM *lwgeom = NULL;
479  GSERIALIZED *g = NULL;
480  char *geojson;
481  text *result;
482  int version;
483  int option = 0;
484  int has_bbox = 0;
485  int precision = DBL_DIG;
486  char * srs = NULL;
487 
488  /* Get the version */
489  version = PG_GETARG_INT32(0);
490  if ( version != 1)
491  {
492  elog(ERROR, "Only GeoJSON 1 is supported");
493  PG_RETURN_NULL();
494  }
495 
496  /* Get the geography */
497  if (PG_ARGISNULL(1) ) PG_RETURN_NULL();
498  g = PG_GETARG_GSERIALIZED_P(1);
499 
500  /* Convert to lwgeom so we can run the old functions */
501  lwgeom = lwgeom_from_gserialized(g);
502 
503  /* Retrieve precision if any (default is max) */
504  if (PG_NARGS() >2 && !PG_ARGISNULL(2))
505  {
506  precision = PG_GETARG_INT32(2);
507  /* TODO: leave this to liblwgeom */
508  if ( precision > DBL_DIG )
509  precision = DBL_DIG;
510  else if ( precision < 0 ) precision = 0;
511  }
512 
513  /* Retrieve output option
514  * 0 = without option (default)
515  * 1 = bbox
516  * 2 = short crs
517  * 4 = long crs
518  */
519  if (PG_NARGS() >3 && !PG_ARGISNULL(3))
520  option = PG_GETARG_INT32(3);
521 
522  if (option & 2 || option & 4)
523  {
524  /* Geography only handle srid SRID_DEFAULT */
525  if (option & 2)
526  srs = getSRSbySRID(fcinfo, SRID_DEFAULT, true);
527  if (option & 4)
528  srs = getSRSbySRID(fcinfo, SRID_DEFAULT, false);
529 
530  if (!srs)
531  {
532  elog(ERROR, "SRID SRID_DEFAULT unknown in spatial_ref_sys table");
533  PG_RETURN_NULL();
534  }
535  }
536 
537  if (option & 1) has_bbox = 1;
538 
539  geojson = lwgeom_to_geojson(lwgeom, srs, precision, has_bbox);
540  lwgeom_free(lwgeom);
541  PG_FREE_IF_COPY(g, 1);
542  if (srs) pfree(srs);
543 
544  result = cstring_to_text(geojson);
545  lwfree(geojson);
546 
547  PG_RETURN_TEXT_P(result);
548 }
static uint8_t precision
Definition: cu_in_twkb.c:25
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
void lwfree(void *mem)
Definition: lwutil.c:244
#define SRID_DEFAULT
Definition: liblwgeom.h:198
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
char * getSRSbySRID(FunctionCallInfo fcinfo, int srid, bool short_crs)
Definition: lwgeom_export.c:60

References getSRSbySRID(), lwfree(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_to_geojson(), precision, and SRID_DEFAULT.

Here is the call graph for this function: