PostGIS  2.3.7dev-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 524 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.

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

Here is the call graph for this function: