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

Definition at line 886 of file lwgeom_ogc.c.

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

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

Here is the call graph for this function: