PostGIS  2.5.0beta2dev-r@@SVN_REVISION@@

◆ geography_as_geojson()

Datum geography_as_geojson ( PG_FUNCTION_ARGS  )

Definition at line 474 of file geography_inout.c.

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

Referenced by geography_as_svg().

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