PostGIS  3.0.0dev-r@@SVN_REVISION@@

◆ LWGEOM_asBinary()

Datum LWGEOM_asBinary ( PG_FUNCTION_ARGS  )

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

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