PostGIS  2.5.0dev-r@@SVN_REVISION@@
Datum LWGEOM_asBinary ( PG_FUNCTION_ARGS  )

Definition at line 889 of file lwgeom_ogc.c.

References lwgeom_free(), lwgeom_from_gserialized(), lwgeom_to_wkb(), variant, WKB_ISO, WKB_NDR, and WKB_XDR.

890 {
891  GSERIALIZED *geom;
892  LWGEOM *lwgeom;
893  uint8_t *wkb;
894  size_t wkb_size;
895  bytea *result;
897 
898  /* Get a 2D version of the geometry */
899  geom = PG_GETARG_GSERIALIZED_P(0);
900  lwgeom = lwgeom_from_gserialized(geom);
901 
902  /* If user specified endianness, respect it */
903  if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )
904  {
905  text *wkb_endian = PG_GETARG_TEXT_P(1);
906 
907  if ( ! strncmp(VARDATA(wkb_endian), "xdr", 3) ||
908  ! strncmp(VARDATA(wkb_endian), "XDR", 3) )
909  {
910  variant = variant | WKB_XDR;
911  }
912  else
913  {
914  variant = variant | WKB_NDR;
915  }
916  }
917 
918  /* Write to WKB and free the geometry */
919  wkb = lwgeom_to_wkb(lwgeom, variant, &wkb_size);
920  lwgeom_free(lwgeom);
921 
922  /* Write to text and free the WKT */
923  result = palloc(wkb_size + VARHDRSZ);
924  memcpy(VARDATA(result), wkb, wkb_size);
925  SET_VARSIZE(result, wkb_size + VARHDRSZ);
926  pfree(wkb);
927 
928  /* Return the text */
929  PG_FREE_IF_COPY(geom, 0);
930  PG_RETURN_BYTEA_P(result);
931 }
uint8_t variant
Definition: cu_in_twkb.c:26
#define WKB_NDR
Definition: liblwgeom.h:2062
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1137
uint8_t * lwgeom_to_wkb(const LWGEOM *geom, uint8_t variant, size_t *size_out)
Convert LWGEOM to a char* in WKB format.
Definition: lwout_wkb.c:764
#define WKB_ISO
Definition: liblwgeom.h:2059
#define WKB_XDR
Definition: liblwgeom.h:2063
unsigned char uint8_t
Definition: uthash.h:79

Here is the call graph for this function: