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

Definition at line 853 of file lwgeom_ogc.c.

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

854 {
855  GSERIALIZED *geom;
856  LWGEOM *lwgeom;
857  uint8_t *wkb;
858  size_t wkb_size;
859  bytea *result;
860  uint8_t variant = WKB_ISO;
861 
862  /* Get a 2D version of the geometry */
863  geom = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
864  lwgeom = lwgeom_from_gserialized(geom);
865 
866  /* If user specified endianness, respect it */
867  if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )
868  {
869  text *wkb_endian = PG_GETARG_TEXT_P(1);
870 
871  if ( ! strncmp(VARDATA(wkb_endian), "xdr", 3) ||
872  ! strncmp(VARDATA(wkb_endian), "XDR", 3) )
873  {
874  variant = variant | WKB_XDR;
875  }
876  else
877  {
878  variant = variant | WKB_NDR;
879  }
880  }
881 
882  /* Write to WKB and free the geometry */
883  wkb = lwgeom_to_wkb(lwgeom, variant, &wkb_size);
884  lwgeom_free(lwgeom);
885 
886  /* Write to text and free the WKT */
887  result = palloc(wkb_size + VARHDRSZ);
888  memcpy(VARDATA(result), wkb, wkb_size);
889  SET_VARSIZE(result, wkb_size + VARHDRSZ);
890  pfree(wkb);
891 
892  /* Return the text */
893  PG_FREE_IF_COPY(geom, 0);
894  PG_RETURN_BYTEA_P(result);
895 }
#define WKB_NDR
Definition: liblwgeom.h:1770
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
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:692
#define WKB_ISO
Definition: liblwgeom.h:1767
#define WKB_XDR
Definition: liblwgeom.h:1771

Here is the call graph for this function: