PostGIS  2.5.0beta1dev-r@@SVN_REVISION@@

◆ LWGEOM_asBinary()

Datum LWGEOM_asBinary ( PG_FUNCTION_ARGS  )

Definition at line 893 of file lwgeom_ogc.c.

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

Referenced by LWGEOM_asText().

894 {
895  GSERIALIZED *geom;
896  LWGEOM *lwgeom;
897  uint8_t *wkb;
898  size_t wkb_size;
899  bytea *result;
901 
902  /* Get a 2D version of the geometry */
903  geom = PG_GETARG_GSERIALIZED_P(0);
904  lwgeom = lwgeom_from_gserialized(geom);
905 
906  /* If user specified endianness, respect it */
907  if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )
908  {
909  text *wkb_endian = PG_GETARG_TEXT_P(1);
910 
911  if ( ! strncmp(VARDATA(wkb_endian), "xdr", 3) ||
912  ! strncmp(VARDATA(wkb_endian), "XDR", 3) )
913  {
914  variant = variant | WKB_XDR;
915  }
916  else
917  {
918  variant = variant | WKB_NDR;
919  }
920  }
921 
922  /* Write to WKB and free the geometry */
923  wkb = lwgeom_to_wkb(lwgeom, variant, &wkb_size);
924  lwgeom_free(lwgeom);
925 
926  /* Write to text and free the WKT */
927  result = palloc(wkb_size + VARHDRSZ);
928  memcpy(VARDATA(result), wkb, wkb_size);
929  SET_VARSIZE(result, wkb_size + VARHDRSZ);
930  pfree(wkb);
931 
932  /* Return the text */
933  PG_FREE_IF_COPY(geom, 0);
934  PG_RETURN_BYTEA_P(result);
935 }
uint8_t variant
Definition: cu_in_twkb.c:26
#define WKB_NDR
Definition: liblwgeom.h:2068
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
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:2065
#define WKB_XDR
Definition: liblwgeom.h:2069
unsigned char uint8_t
Definition: uthash.h:79
Here is the call graph for this function:
Here is the caller graph for this function: