PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ LWGEOM_asBinary()

Datum LWGEOM_asBinary ( PG_FUNCTION_ARGS  )

Definition at line 854 of file lwgeom_ogc.c.

855 {
856  GSERIALIZED *geom;
857  LWGEOM *lwgeom;
858  uint8_t *wkb;
859  size_t wkb_size;
860  bytea *result;
861  uint8_t variant = WKB_ISO;
862 
863  if (PG_ARGISNULL(0))
864  PG_RETURN_NULL();
865 
866  /* Get a 2D version of the geometry */
867  geom = PG_GETARG_GSERIALIZED_P(0);
868  lwgeom = lwgeom_from_gserialized(geom);
869 
870 
871  /* If user specified endianness, respect it */
872  if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )
873  {
874  text *wkb_endian = PG_GETARG_TEXT_P(1);
875 
876  if ( ! strncmp(VARDATA(wkb_endian), "xdr", 3) ||
877  ! strncmp(VARDATA(wkb_endian), "XDR", 3) )
878  {
879  variant = variant | WKB_XDR;
880  }
881  else
882  {
883  variant = variant | WKB_NDR;
884  }
885  }
886 
887  /* Write to WKB and free the geometry */
888  wkb = lwgeom_to_wkb(lwgeom, variant, &wkb_size);
889  lwgeom_free(lwgeom);
890 
891  /* Write to text and free the WKT */
892  result = palloc(wkb_size + VARHDRSZ);
893  memcpy(VARDATA(result), wkb, wkb_size);
894  SET_VARSIZE(result, wkb_size + VARHDRSZ);
895  lwfree(wkb);
896 
897  /* Return the text */
898  PG_FREE_IF_COPY(geom, 0);
899  PG_RETURN_BYTEA_P(result);
900 }
static uint8_t variant
Definition: cu_in_twkb.c:26
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
#define WKB_ISO
Definition: liblwgeom.h:2121
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
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:790
void lwfree(void *mem)
Definition: lwutil.c:242
#define WKB_NDR
Definition: liblwgeom.h:2124
#define WKB_XDR
Definition: liblwgeom.h:2125

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

Here is the call graph for this function: