PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ LWGEOM_asX3D()

Datum LWGEOM_asX3D ( PG_FUNCTION_ARGS  )

TODO: we need to support UTM and other coordinate systems supported by X3D eventually http://www.web3d.org/documents/specifications/19775-1/V3.2/Part01/components/geodata.html#t-earthgeoids

Definition at line 509 of file lwgeom_export.c.

References dumpnode::geom, getSRSbySRID(), gserialized_get_srid(), LW_X3D_USE_GEOCOORDS, lwfree(), LWGEOM_asEncodedPolyline(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_to_x3d3(), PG_FUNCTION_INFO_V1(), precision, and SRID_UNKNOWN.

Referenced by LWGEOM_asSVG().

510 {
511  GSERIALIZED *geom;
512  LWGEOM *lwgeom;
513  char *x3d;
514  text *result;
515  int version;
516  char *srs;
517  int srid;
518  int option = 0;
519  int precision = DBL_DIG;
520  static const char* default_defid = "x3d:"; /* default defid */
521  char *defidbuf;
522  const char* defid = default_defid;
523  text *defid_text;
524 
525  /* Get the version */
526  version = PG_GETARG_INT32(0);
527  if ( version != 3 )
528  {
529  elog(ERROR, "Only X3D version 3 are supported");
530  PG_RETURN_NULL();
531  }
532 
533  /* Get the geometry */
534  if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
535  geom = PG_GETARG_GSERIALIZED_P(1);
536 
537  /* Retrieve precision if any (default is max) */
538  if (PG_NARGS() >2 && !PG_ARGISNULL(2))
539  {
540  precision = PG_GETARG_INT32(2);
541  /* TODO: leave this to liblwgeom ? */
542  if ( precision > DBL_DIG )
543  precision = DBL_DIG;
544  else if ( precision < 0 ) precision = 0;
545  }
546 
547  /* retrieve option */
548  if (PG_NARGS() >3 && !PG_ARGISNULL(3))
549  option = PG_GETARG_INT32(3);
550 
551 
552 
553  /* retrieve defid */
554  if (PG_NARGS() >4 && !PG_ARGISNULL(4))
555  {
556  defid_text = PG_GETARG_TEXT_P(4);
557  if ( VARSIZE(defid_text)-VARHDRSZ == 0 )
558  {
559  defid = "";
560  }
561  else
562  {
563  /* +2 is one for the ':' and one for term null */
564  defidbuf = palloc(VARSIZE(defid_text)-VARHDRSZ+2);
565  memcpy(defidbuf, VARDATA(defid_text),
566  VARSIZE(defid_text)-VARHDRSZ);
567  /* add colon and null terminate */
568  defidbuf[VARSIZE(defid_text)-VARHDRSZ] = ':';
569  defidbuf[VARSIZE(defid_text)-VARHDRSZ+1] = '\0';
570  defid = defidbuf;
571  }
572  }
573 
574  lwgeom = lwgeom_from_gserialized(geom);
575  srid = gserialized_get_srid(geom);
576  if (srid == SRID_UNKNOWN) srs = NULL;
577  else if (option & 1) srs = getSRSbySRID(srid, false);
578  else srs = getSRSbySRID(srid, true);
579 
580  if (option & LW_X3D_USE_GEOCOORDS) {
581  if (srid != 4326) {
582  PG_FREE_IF_COPY(geom, 0);
585  elog(ERROR, "Only SRID 4326 is supported for geocoordinates.");
586  PG_RETURN_NULL();
587  }
588  }
589 
590 
591  x3d = lwgeom_to_x3d3(lwgeom, srs, precision,option, defid);
592 
593  lwgeom_free(lwgeom);
594  PG_FREE_IF_COPY(geom, 1);
595 
596  result = cstring2text(x3d);
597  lwfree(x3d);
598 
599  PG_RETURN_TEXT_P(result);
600 }
char * lwgeom_to_x3d3(const LWGEOM *geom, char *srs, int precision, int opts, const char *defid)
Definition: lwout_x3d.c:48
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 LW_X3D_USE_GEOCOORDS
Definition: liblwgeom.h:1488
LWGEOM * geom
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:172
uint8_t precision
Definition: cu_in_twkb.c:25
char * getSRSbySRID(int srid, bool short_crs)
Definition: lwgeom_export.c:40
int32_t gserialized_get_srid(const GSERIALIZED *s)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: g_serialized.c:69
Here is the call graph for this function:
Here is the caller graph for this function: