PostGIS  3.0.0dev-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 536 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().

537 {
538  GSERIALIZED *geom;
539  LWGEOM *lwgeom;
540  char *x3d;
541  text *result;
542  int version;
543  char *srs;
544  int srid;
545  int option = 0;
546  int precision = DBL_DIG;
547  static const char* default_defid = "x3d:"; /* default defid */
548  char *defidbuf;
549  const char* defid = default_defid;
550  text *defid_text;
551 
552  /* Get the version */
553  version = PG_GETARG_INT32(0);
554  if ( version != 3 )
555  {
556  elog(ERROR, "Only X3D version 3 are supported");
557  PG_RETURN_NULL();
558  }
559 
560  /* Get the geometry */
561  if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
562  geom = PG_GETARG_GSERIALIZED_P(1);
563 
564  /* Retrieve precision if any (default is max) */
565  if (PG_NARGS() >2 && !PG_ARGISNULL(2))
566  {
567  precision = PG_GETARG_INT32(2);
568  /* TODO: leave this to liblwgeom ? */
569  if ( precision > DBL_DIG )
570  precision = DBL_DIG;
571  else if ( precision < 0 ) precision = 0;
572  }
573 
574  /* retrieve option */
575  if (PG_NARGS() >3 && !PG_ARGISNULL(3))
576  option = PG_GETARG_INT32(3);
577 
578 
579 
580  /* retrieve defid */
581  if (PG_NARGS() >4 && !PG_ARGISNULL(4))
582  {
583  defid_text = PG_GETARG_TEXT_P(4);
584  if ( VARSIZE(defid_text)-VARHDRSZ == 0 )
585  {
586  defid = "";
587  }
588  else
589  {
590  /* +2 is one for the ':' and one for term null */
591  defidbuf = palloc(VARSIZE(defid_text)-VARHDRSZ+2);
592  memcpy(defidbuf, VARDATA(defid_text),
593  VARSIZE(defid_text)-VARHDRSZ);
594  /* add colon and null terminate */
595  defidbuf[VARSIZE(defid_text)-VARHDRSZ] = ':';
596  defidbuf[VARSIZE(defid_text)-VARHDRSZ+1] = '\0';
597  defid = defidbuf;
598  }
599  }
600 
601  lwgeom = lwgeom_from_gserialized(geom);
602  srid = gserialized_get_srid(geom);
603  if (srid == SRID_UNKNOWN) srs = NULL;
604  else if (option & 1) srs = getSRSbySRID(srid, false);
605  else srs = getSRSbySRID(srid, true);
606 
607  if (option & LW_X3D_USE_GEOCOORDS) {
608  if (srid != 4326) {
609  PG_FREE_IF_COPY(geom, 0);
612  elog(ERROR, "Only SRID 4326 is supported for geocoordinates.");
613  PG_RETURN_NULL();
614  }
615  }
616 
617 
618  x3d = lwgeom_to_x3d3(lwgeom, srs, precision,option, defid);
619 
620  lwgeom_free(lwgeom);
621  PG_FREE_IF_COPY(geom, 1);
622 
623  result = cstring_to_text(x3d);
624  lwfree(x3d);
625 
626  PG_RETURN_TEXT_P(result);
627 }
char * lwgeom_to_x3d3(const LWGEOM *geom, char *srs, int precision, int opts, const char *defid)
Definition: lwout_x3d.c:36
void lwfree(void *mem)
Definition: lwutil.c:242
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1128
#define LW_X3D_USE_GEOCOORDS
Definition: liblwgeom.h:1522
LWGEOM * geom
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:187
uint8_t precision
Definition: cu_in_twkb.c:25
char * getSRSbySRID(int srid, bool short_crs)
Definition: lwgeom_export.c:56
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:99
Here is the call graph for this function:
Here is the caller graph for this function: