PostGIS  2.2.7dev-r@@SVN_REVISION@@
Datum geography_as_geojson ( PG_FUNCTION_ARGS  )

Definition at line 443 of file geography_inout.c.

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

444 {
445  LWGEOM *lwgeom = NULL;
446  GSERIALIZED *g = NULL;
447  char *geojson;
448  text *result;
449  int version;
450  int option = 0;
451  int has_bbox = 0;
452  int precision = DBL_DIG;
453  char * srs = NULL;
454 
455  /* Get the version */
456  version = PG_GETARG_INT32(0);
457  if ( version != 1)
458  {
459  elog(ERROR, "Only GeoJSON 1 is supported");
460  PG_RETURN_NULL();
461  }
462 
463  /* Get the geography */
464  if (PG_ARGISNULL(1) ) PG_RETURN_NULL();
465  g = PG_GETARG_GSERIALIZED_P(1);
466 
467  /* Convert to lwgeom so we can run the old functions */
468  lwgeom = lwgeom_from_gserialized(g);
469 
470  /* Retrieve precision if any (default is max) */
471  if (PG_NARGS() >2 && !PG_ARGISNULL(2))
472  {
473  precision = PG_GETARG_INT32(2);
474  /* TODO: leave this to liblwgeom */
475  if ( precision > DBL_DIG )
476  precision = DBL_DIG;
477  else if ( precision < 0 ) precision = 0;
478  }
479 
480  /* Retrieve output option
481  * 0 = without option (default)
482  * 1 = bbox
483  * 2 = short crs
484  * 4 = long crs
485  */
486  if (PG_NARGS() >3 && !PG_ARGISNULL(3))
487  option = PG_GETARG_INT32(3);
488 
489  if (option & 2 || option & 4)
490  {
491  /* Geography only handle srid SRID_DEFAULT */
492  if (option & 2) srs = getSRSbySRID(SRID_DEFAULT, true);
493  if (option & 4) srs = getSRSbySRID(SRID_DEFAULT, false);
494 
495  if (!srs)
496  {
497  elog(ERROR, "SRID SRID_DEFAULT unknown in spatial_ref_sys table");
498  PG_RETURN_NULL();
499  }
500  }
501 
502  if (option & 1) has_bbox = 1;
503 
504  geojson = lwgeom_to_geojson(lwgeom, srs, precision, has_bbox);
505  lwgeom_free(lwgeom);
506  PG_FREE_IF_COPY(g, 1);
507  if (srs) pfree(srs);
508 
509  result = cstring2text(geojson);
510  lwfree(geojson);
511 
512  PG_RETURN_TEXT_P(result);
513 }
void lwfree(void *mem)
Definition: lwutil.c:214
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
#define SRID_DEFAULT
Definition: liblwgeom.h:179
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:34
uint8_t precision
Definition: cu_in_twkb.c:25
char * getSRSbySRID(int srid, bool short_crs)
Definition: lwgeom_export.c:40

Here is the call graph for this function: