PostGIS  2.5.0dev-r@@SVN_REVISION@@
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 535 of file lwgeom_export.c.

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

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