PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ geography_as_geojson()

Datum geography_as_geojson ( PG_FUNCTION_ARGS  )

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

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