PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ LWGEOM_asBinary()

Datum LWGEOM_asBinary ( PG_FUNCTION_ARGS  )

Definition at line 860 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().

861 {
862  GSERIALIZED *geom;
863  LWGEOM *lwgeom;
864  uint8_t *wkb;
865  size_t wkb_size;
866  bytea *result;
867  uint8_t variant = WKB_ISO;
868 
869  /* Get a 2D version of the geometry */
870  geom = PG_GETARG_GSERIALIZED_P(0);
871  lwgeom = lwgeom_from_gserialized(geom);
872 
873  /* If user specified endianness, respect it */
874  if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )
875  {
876  text *wkb_endian = PG_GETARG_TEXT_P(1);
877 
878  if ( ! strncmp(VARDATA(wkb_endian), "xdr", 3) ||
879  ! strncmp(VARDATA(wkb_endian), "XDR", 3) )
880  {
881  variant = variant | WKB_XDR;
882  }
883  else
884  {
885  variant = variant | WKB_NDR;
886  }
887  }
888 
889  /* Write to WKB and free the geometry */
890  wkb = lwgeom_to_wkb(lwgeom, variant, &wkb_size);
891  lwgeom_free(lwgeom);
892 
893  /* Write to text and free the WKT */
894  result = palloc(wkb_size + VARHDRSZ);
895  memcpy(VARDATA(result), wkb, wkb_size);
896  SET_VARSIZE(result, wkb_size + VARHDRSZ);
897  pfree(wkb);
898 
899  /* Return the text */
900  PG_FREE_IF_COPY(geom, 0);
901  PG_RETURN_BYTEA_P(result);
902 }
uint8_t variant
Definition: cu_in_twkb.c:26
#define WKB_NDR
Definition: liblwgeom.h:1933
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
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:750
#define WKB_ISO
Definition: liblwgeom.h:1930
#define WKB_XDR
Definition: liblwgeom.h:1934
Here is the call graph for this function:
Here is the caller graph for this function: