PostGIS  2.5.7dev-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 561 of file lwgeom_export.c.

562 {
563  GSERIALIZED *geom;
564  LWGEOM *lwgeom;
565  char *x3d;
566  text *result;
567  int version;
568  char *srs;
569  int srid;
570  int option = 0;
571  int precision = DBL_DIG;
572  static const char* default_defid = "x3d:"; /* default defid */
573  char *defidbuf;
574  const char* defid = default_defid;
575  text *defid_text;
576 
577  /* Get the version */
578  version = PG_GETARG_INT32(0);
579  if ( version != 3 )
580  {
581  elog(ERROR, "Only X3D version 3 are supported");
582  PG_RETURN_NULL();
583  }
584 
585  /* Get the geometry */
586  if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
587  geom = PG_GETARG_GSERIALIZED_P(1);
588 
589  /* Retrieve precision if any (default is max) */
590  if (PG_NARGS() >2 && !PG_ARGISNULL(2))
591  {
592  precision = PG_GETARG_INT32(2);
593  /* TODO: leave this to liblwgeom ? */
594  if ( precision > DBL_DIG )
595  precision = DBL_DIG;
596  else if ( precision < 0 ) precision = 0;
597  }
598 
599  /* retrieve option */
600  if (PG_NARGS() >3 && !PG_ARGISNULL(3))
601  option = PG_GETARG_INT32(3);
602 
603 
604 
605  /* retrieve defid */
606  if (PG_NARGS() >4 && !PG_ARGISNULL(4))
607  {
608  defid_text = PG_GETARG_TEXT_P(4);
609  if ( VARSIZE(defid_text)-VARHDRSZ == 0 )
610  {
611  defid = "";
612  }
613  else
614  {
615  /* +2 is one for the ':' and one for term null */
616  defidbuf = palloc(VARSIZE(defid_text)-VARHDRSZ+2);
617  memcpy(defidbuf, VARDATA(defid_text),
618  VARSIZE(defid_text)-VARHDRSZ);
619  /* add colon and null terminate */
620  defidbuf[VARSIZE(defid_text)-VARHDRSZ] = ':';
621  defidbuf[VARSIZE(defid_text)-VARHDRSZ+1] = '\0';
622  defid = defidbuf;
623  }
624  }
625 
626  lwgeom = lwgeom_from_gserialized(geom);
627  srid = gserialized_get_srid(geom);
628  if (srid == SRID_UNKNOWN) srs = NULL;
629  else if (option & 1)
630  srs = getSRSbySRID(fcinfo, srid, false);
631  else
632  srs = getSRSbySRID(fcinfo, srid, true);
633 
634  if (option & LW_X3D_USE_GEOCOORDS) {
635  if (srid != 4326) {
636  PG_FREE_IF_COPY(geom, 0);
639  elog(ERROR, "Only SRID 4326 is supported for geocoordinates.");
640  PG_RETURN_NULL();
641  }
642  }
643 
644 
645  x3d = lwgeom_to_x3d3(lwgeom, srs, precision,option, defid);
646 
647  lwgeom_free(lwgeom);
648  PG_FREE_IF_COPY(geom, 1);
649 
650  result = cstring_to_text(x3d);
651  lwfree(x3d);
652 
653  PG_RETURN_TEXT_P(result);
654 }
static uint8_t precision
Definition: cu_in_twkb.c:25
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:100
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
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
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:188
#define LW_X3D_USE_GEOCOORDS
Definition: liblwgeom.h:1561
char * getSRSbySRID(FunctionCallInfo fcinfo, int srid, bool short_crs)
Definition: lwgeom_export.c:60

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

Here is the call graph for this function: