PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ LWGEOM_asGeoJson()

Datum LWGEOM_asGeoJson ( PG_FUNCTION_ARGS  )

Definition at line 200 of file lwgeom_export.c.

201 {
202  GSERIALIZED *geom;
203  LWGEOM *lwgeom;
205  int output_bbox = LW_FALSE;
206  int output_long_crs = LW_FALSE;
207  int output_short_crs = LW_FALSE;
208  int output_guess_short_srid = LW_FALSE;
209  const char *srs = NULL;
210  int32_t srid;
211 
212  /* Get the geometry */
213  if (PG_ARGISNULL(0))
214  PG_RETURN_NULL();
215 
216  geom = PG_GETARG_GSERIALIZED_P(0);
217  srid = gserialized_get_srid(geom);
218 
219  /* Retrieve precision if any (default is max) */
220  if ( PG_NARGS() > 1 && !PG_ARGISNULL(1) )
221  {
222  precision = PG_GETARG_INT32(1);
223  }
224 
225  /* Retrieve output option
226  * 0 = without option
227  * 1 = bbox
228  * 2 = short crs
229  * 4 = long crs
230  * 8 = guess if CRS is needed (default)
231  */
232  if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
233  {
234  int option = PG_GETARG_INT32(2);
235  output_guess_short_srid = (option & 8) ? LW_TRUE : LW_FALSE;
236  output_short_crs = (option & 2) ? LW_TRUE : LW_FALSE;
237  output_long_crs = (option & 4) ? LW_TRUE : LW_FALSE;
238  output_bbox = (option & 1) ? LW_TRUE : LW_FALSE;
239  }
240  else
241  output_guess_short_srid = LW_TRUE;
242 
243  if (output_guess_short_srid && srid != WGS84_SRID && srid != SRID_UNKNOWN)
244  output_short_crs = LW_TRUE;
245 
246  if (srid != SRID_UNKNOWN && (output_short_crs || output_long_crs))
247  {
248  srs = GetSRSCacheBySRID(fcinfo, srid, !output_long_crs);
249 
250  if (!srs)
251  {
252  elog(ERROR, "SRID %i unknown in spatial_ref_sys table", srid);
253  PG_RETURN_NULL();
254  }
255  }
256 
257  lwgeom = lwgeom_from_gserialized(geom);
258  PG_RETURN_TEXT_P(lwgeom_to_geojson(lwgeom, srs, precision, output_bbox));
259 }
static uint8_t precision
Definition: cu_in_twkb.c:25
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: gserialized.c:126
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
#define WGS84_SRID
Definition: liblwgeom.h:149
#define LW_FALSE
Definition: liblwgeom.h:94
lwvarlena_t * lwgeom_to_geojson(const LWGEOM *geo, const char *srs, int precision, int has_bbox)
Takes a GEOMETRY and returns a GeoJson representation.
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:93
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:215
#define OUT_DEFAULT_DECIMAL_DIGITS

References gserialized_get_srid(), LW_FALSE, LW_TRUE, lwgeom_from_gserialized(), lwgeom_to_geojson(), OUT_DEFAULT_DECIMAL_DIGITS, precision, SRID_UNKNOWN, and WGS84_SRID.

Referenced by composite_to_geojson().

Here is the call graph for this function:
Here is the caller graph for this function: