PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ TWKBFromLWGEOM()

Datum TWKBFromLWGEOM ( PG_FUNCTION_ARGS  )

Definition at line 465 of file lwgeom_inout.c.

References gserialized_get_srid(), lwgeom_from_gserialized(), lwgeom_to_twkb(), PG_FUNCTION_INFO_V1(), TWKB_BBOX, TWKB_DEFAULT_PRECISION, TWKB_ID, TWKB_SIZE, TWKBFromLWGEOMArray(), and variant.

Referenced by WKBFromLWGEOM().

466 {
467  GSERIALIZED *geom;
468  LWGEOM *lwgeom;
469  uint8_t *twkb;
470  size_t twkb_size;
471  uint8_t variant = 0;
472  bytea *result;
473  srs_precision sp;
474 
475  /*check for null input since we cannot have the sql-function as strict.
476  That is because we use null as default for optional ID*/
477  if ( PG_ARGISNULL(0) ) PG_RETURN_NULL();
478 
479  geom = PG_GETARG_GSERIALIZED_P(0);
480 
481  /* Read sensible precision defaults (about one meter) given the srs */
482  sp = srid_axis_precision(fcinfo, gserialized_get_srid(geom), TWKB_DEFAULT_PRECISION);
483 
484  /* If user specified XY precision, use it */
485  if ( PG_NARGS() > 1 && ! PG_ARGISNULL(1) )
486  sp.precision_xy = PG_GETARG_INT32(1);
487 
488  /* If user specified Z precision, use it */
489  if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
490  sp.precision_z = PG_GETARG_INT32(2);
491 
492  /* If user specified M precision, use it */
493  if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
494  sp.precision_m = PG_GETARG_INT32(3);
495 
496  /* We don't permit ids for single geoemtries */
497  variant = variant & ~TWKB_ID;
498 
499  /* If user wants registered twkb sizes */
500  if ( PG_NARGS() > 4 && ! PG_ARGISNULL(4) && PG_GETARG_BOOL(4) )
501  variant |= TWKB_SIZE;
502 
503  /* If user wants bounding boxes */
504  if ( PG_NARGS() > 5 && ! PG_ARGISNULL(5) && PG_GETARG_BOOL(5) )
505  variant |= TWKB_BBOX;
506 
507  /* Create TWKB binary string */
508  lwgeom = lwgeom_from_gserialized(geom);
509  twkb = lwgeom_to_twkb(lwgeom, variant, sp.precision_xy, sp.precision_z, sp.precision_m, &twkb_size);
510 
511  /* Prepare the PgSQL text return type */
512  result = palloc(twkb_size + VARHDRSZ);
513  memcpy(VARDATA(result), twkb, twkb_size);
514  SET_VARSIZE(result, twkb_size + VARHDRSZ);
515 
516  PG_RETURN_BYTEA_P(result);
517 }
uint8_t variant
Definition: cu_in_twkb.c:26
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
#define TWKB_ID
Definition: liblwgeom.h:2093
#define TWKB_DEFAULT_PRECISION
Definition: liblwgeom.h:2096
#define TWKB_BBOX
Definition: liblwgeom.h:2091
#define TWKB_SIZE
Definition: liblwgeom.h:2092
uint8_t * lwgeom_to_twkb(const LWGEOM *geom, uint8_t variant, int8_t precision_xy, int8_t precision_z, int8_t precision_m, size_t *twkb_size)
Definition: lwout_twkb.c:615
unsigned char uint8_t
Definition: uthash.h:79
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: