PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ TWKBFromLWGEOM()

Datum TWKBFromLWGEOM ( PG_FUNCTION_ARGS  )

Definition at line 471 of file lwgeom_inout.c.

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

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

Here is the call graph for this function: