PostGIS  2.2.7dev-r@@SVN_REVISION@@
Datum TWKBFromLWGEOM ( PG_FUNCTION_ARGS  )

Definition at line 441 of file lwgeom_inout.c.

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

442 {
443  GSERIALIZED *geom;
444  LWGEOM *lwgeom;
445  uint8_t *twkb;
446  size_t twkb_size;
447  uint8_t variant = 0;
448  bytea *result;
449  srs_precision sp;
450 
451  /*check for null input since we cannot have the sql-function as strict.
452  That is because we use null as default for optional ID*/
453  if ( PG_ARGISNULL(0) ) PG_RETURN_NULL();
454 
455  geom = PG_GETARG_GSERIALIZED_P(0);
456 
457  /* Read sensible precision defaults (about one meter) given the srs */
458  sp = srid_axis_precision(fcinfo, gserialized_get_srid(geom), TWKB_DEFAULT_PRECISION);
459 
460  /* If user specified XY precision, use it */
461  if ( PG_NARGS() > 1 && ! PG_ARGISNULL(1) )
462  sp.precision_xy = PG_GETARG_INT32(1);
463 
464  /* If user specified Z precision, use it */
465  if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
466  sp.precision_z = PG_GETARG_INT32(2);
467 
468  /* If user specified M precision, use it */
469  if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
470  sp.precision_m = PG_GETARG_INT32(3);
471 
472  /* We don't permit ids for single geoemtries */
473  variant = variant & ~TWKB_ID;
474 
475  /* If user wants registered twkb sizes */
476  if ( PG_NARGS() > 4 && ! PG_ARGISNULL(4) && PG_GETARG_BOOL(4) )
477  variant |= TWKB_SIZE;
478 
479  /* If user wants bounding boxes */
480  if ( PG_NARGS() > 5 && ! PG_ARGISNULL(5) && PG_GETARG_BOOL(5) )
481  variant |= TWKB_BBOX;
482 
483  /* Create TWKB binary string */
484  lwgeom = lwgeom_from_gserialized(geom);
485  twkb = lwgeom_to_twkb(lwgeom, variant, sp.precision_xy, sp.precision_z, sp.precision_m, &twkb_size);
486  lwgeom_free(lwgeom);
487 
488  /* Prepare the PgSQL text return type */
489  result = palloc(twkb_size + VARHDRSZ);
490  memcpy(VARDATA(result), twkb, twkb_size);
491  SET_VARSIZE(result, twkb_size + VARHDRSZ);
492 
493  pfree(twkb);
494  PG_FREE_IF_COPY(geom, 0);
495  PG_RETURN_BYTEA_P(result);
496 }
uint8_t variant
Definition: cu_in_twkb.c:26
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
#define TWKB_ID
Definition: liblwgeom.h:1949
#define TWKB_DEFAULT_PRECISION
Definition: liblwgeom.h:1952
#define TWKB_BBOX
Definition: liblwgeom.h:1947
#define TWKB_SIZE
Definition: liblwgeom.h:1948
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:594
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:69

Here is the call graph for this function: