PostGIS  2.4.9dev-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 527 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().

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