PostGIS  2.1.10dev-r@@SVN_REVISION@@
Datum LWGEOM_asX3D ( PG_FUNCTION_ARGS  )

Definition at line 488 of file lwgeom_export.c.

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

489 {
490  GSERIALIZED *geom;
491  LWGEOM *lwgeom;
492  char *x3d;
493  text *result;
494  int version;
495  char *srs;
496  int srid;
497  int option = 0;
498  int precision = DBL_DIG;
499  static const char* default_defid = "x3d:"; /* default defid */
500  char *defidbuf;
501  const char* defid = default_defid;
502  text *defid_text;
503 
504  /* Get the version */
505  version = PG_GETARG_INT32(0);
506  if ( version != 3 )
507  {
508  elog(ERROR, "Only X3D version 3 are supported");
509  PG_RETURN_NULL();
510  }
511 
512  /* Get the geometry */
513  if ( PG_ARGISNULL(1) ) PG_RETURN_NULL();
514  geom = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
515 
516  /* Retrieve precision if any (default is max) */
517  if (PG_NARGS() >2 && !PG_ARGISNULL(2))
518  {
519  precision = PG_GETARG_INT32(2);
520  /* TODO: leave this to liblwgeom ? */
521  if ( precision > DBL_DIG )
522  precision = DBL_DIG;
523  else if ( precision < 0 ) precision = 0;
524  }
525 
526  /* retrieve option */
527  if (PG_NARGS() >3 && !PG_ARGISNULL(3))
528  option = PG_GETARG_INT32(3);
529 
530  /* retrieve defid */
531  if (PG_NARGS() >4 && !PG_ARGISNULL(4))
532  {
533  defid_text = PG_GETARG_TEXT_P(4);
534  if ( VARSIZE(defid_text)-VARHDRSZ == 0 )
535  {
536  defid = "";
537  }
538  else
539  {
540  /* +2 is one for the ':' and one for term null */
541  defidbuf = palloc(VARSIZE(defid_text)-VARHDRSZ+2);
542  memcpy(defidbuf, VARDATA(defid_text),
543  VARSIZE(defid_text)-VARHDRSZ);
544  /* add colon and null terminate */
545  defidbuf[VARSIZE(defid_text)-VARHDRSZ] = ':';
546  defidbuf[VARSIZE(defid_text)-VARHDRSZ+1] = '\0';
547  defid = defidbuf;
548  }
549  }
550 
551  srid = gserialized_get_srid(geom);
552  if (srid == SRID_UNKNOWN) srs = NULL;
553  else if (option & 1) srs = getSRSbySRID(srid, false);
554  else srs = getSRSbySRID(srid, true);
555 
556  lwgeom = lwgeom_from_gserialized(geom);
557 
558  x3d = lwgeom_to_x3d3(lwgeom, srs, precision,option, defid);
559 
560  lwgeom_free(lwgeom);
561  PG_FREE_IF_COPY(geom, 1);
562 
563  result = cstring2text(x3d);
564  lwfree(x3d);
565 
566  PG_RETURN_TEXT_P(result);
567 }
char * lwgeom_to_x3d3(const LWGEOM *geom, char *srs, int precision, int opts, const char *defid)
Definition: lwout_x3d.c:49
void lwfree(void *mem)
Definition: lwutil.c:190
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
char ** result
Definition: liblwgeom.h:218
LWGEOM * geom
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:154
char * getSRSbySRID(int srid, bool short_crs)
Definition: lwgeom_export.c:39
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:70

Here is the call graph for this function: