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

Definition at line 444 of file geography_inout.c.

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

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

Here is the call graph for this function: