PostGIS  3.0.6dev-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 522 of file lwgeom_export.c.

523 {
524  GSERIALIZED *geom;
525  LWGEOM *lwgeom;
526  char *x3d;
527  text *result;
528  int version;
529  char *srs;
530  int32_t srid;
531  int option = 0;
532  int precision = DBL_DIG;
533  static const char* default_defid = "x3d:"; /* default defid */
534  char *defidbuf;
535  const char* defid = default_defid;
536  text *defid_text;
537 
538  /* Get the version */
539  version = PG_GETARG_INT32(0);
540  if ( version != 3 )
541  {
542  elog(ERROR, "Only X3D version 3 are supported");
543  PG_RETURN_NULL();
544  }
545 
546  /* Get the geometry */
547  if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
548  geom = PG_GETARG_GSERIALIZED_P(1);
549 
550  /* Retrieve precision if any (default is max) */
551  if (PG_NARGS() >2 && !PG_ARGISNULL(2))
552  {
553  precision = PG_GETARG_INT32(2);
554  /* TODO: leave this to liblwgeom ? */
555  if ( precision > DBL_DIG )
556  precision = DBL_DIG;
557  else if ( precision < 0 ) precision = 0;
558  }
559 
560  /* retrieve option */
561  if (PG_NARGS() >3 && !PG_ARGISNULL(3))
562  option = PG_GETARG_INT32(3);
563 
564 
565 
566  /* retrieve defid */
567  if (PG_NARGS() >4 && !PG_ARGISNULL(4))
568  {
569  defid_text = PG_GETARG_TEXT_P(4);
570  if ( VARSIZE_ANY_EXHDR(defid_text) == 0 )
571  {
572  defid = "";
573  }
574  else
575  {
576  /* +2 is one for the ':' and one for term null */
577  defidbuf = palloc(VARSIZE_ANY_EXHDR(defid_text)+2);
578  memcpy(defidbuf, VARDATA(defid_text),
579  VARSIZE_ANY_EXHDR(defid_text));
580  /* add colon and null terminate */
581  defidbuf[VARSIZE_ANY_EXHDR(defid_text)] = ':';
582  defidbuf[VARSIZE_ANY_EXHDR(defid_text)+1] = '\0';
583  defid = defidbuf;
584  }
585  }
586 
587  lwgeom = lwgeom_from_gserialized(geom);
588  srid = gserialized_get_srid(geom);
589  if (srid == SRID_UNKNOWN) srs = NULL;
590  else if (option & 1)
591  srs = getSRSbySRID(fcinfo, srid, false);
592  else
593  srs = getSRSbySRID(fcinfo, 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 = cstring_to_text(x3d);
612  lwfree(x3d);
613 
614  PG_RETURN_TEXT_P(result);
615 }
static uint8_t precision
Definition: cu_in_twkb.c:25
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: gserialized.c:126
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
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
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:229
#define LW_X3D_USE_GEOCOORDS
Definition: liblwgeom.h:1667
char * getSRSbySRID(FunctionCallInfo fcinfo, int32_t srid, bool short_crs)
Definition: lwgeom_export.c:66

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: