PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ lwgeom_to_wkb()

uint8_t* lwgeom_to_wkb ( const LWGEOM geom,
uint8_t  variant,
size_t *  size_out 
)

Convert LWGEOM to a char* in WKB format.

Parameters
lwgeomgeometry to convert to WKT
variantoutput format to use (WKB_ISO, WKB_SFSQL, WKB_EXTENDED, WKB_NDR, WKB_XDR)

Caller is responsible for freeing the returned array.

Parameters
variant.Unsigned bitmask value. Accepts one of: WKB_ISO, WKB_EXTENDED, WKB_SFSQL. Accepts any of: WKB_NDR, WKB_HEX. For example: Variant = ( WKB_ISO | WKB_NDR ) would return the little-endian ISO form of WKB. For Example: Variant = ( WKB_EXTENDED | WKB_HEX ) would return the big-endian extended form of WKB, as hex-encoded ASCII (the "canonical form").
size_outIf supplied, will return the size of the returned memory segment, including the null terminator in the case of ASCII.

Definition at line 790 of file lwout_wkb.c.

791 {
792  size_t buf_size;
793  uint8_t *buf = NULL;
794  uint8_t *wkb_out = NULL;
795 
796  /* Initialize output size */
797  if ( size_out ) *size_out = 0;
798 
799  if ( geom == NULL )
800  {
801  LWDEBUG(4,"Cannot convert NULL into WKB.");
802  lwerror("Cannot convert NULL into WKB.");
803  return NULL;
804  }
805 
806  /* Calculate the required size of the output buffer */
807  buf_size = lwgeom_to_wkb_size(geom, variant);
808  LWDEBUGF(4, "WKB output size: %d", buf_size);
809 
810  if ( buf_size == 0 )
811  {
812  LWDEBUG(4,"Error calculating output WKB buffer size.");
813  lwerror("Error calculating output WKB buffer size.");
814  return NULL;
815  }
816 
817  /* Hex string takes twice as much space as binary + a null character */
818  if ( variant & WKB_HEX )
819  {
820  buf_size = 2 * buf_size + 1;
821  LWDEBUGF(4, "Hex WKB output size: %d", buf_size);
822  }
823 
824  /* If neither or both variants are specified, choose the native order */
825  if ( ! (variant & WKB_NDR || variant & WKB_XDR) ||
826  (variant & WKB_NDR && variant & WKB_XDR) )
827  {
828  if (IS_BIG_ENDIAN)
829  variant = variant | WKB_XDR;
830  else
831  variant = variant | WKB_NDR;
832  }
833 
834  /* Allocate the buffer */
835  buf = lwalloc(buf_size);
836 
837  if ( buf == NULL )
838  {
839  LWDEBUGF(4,"Unable to allocate %d bytes for WKB output buffer.", buf_size);
840  lwerror("Unable to allocate %d bytes for WKB output buffer.", buf_size);
841  return NULL;
842  }
843 
844  /* Retain a pointer to the front of the buffer for later */
845  wkb_out = buf;
846 
847  /* Write the WKB into the output buffer */
848  buf = lwgeom_to_wkb_buf(geom, buf, variant);
849 
850  /* Null the last byte if this is a hex output */
851  if ( variant & WKB_HEX )
852  {
853  *buf = '\0';
854  buf++;
855  }
856 
857  LWDEBUGF(4,"buf (%p) - wkb_out (%p) = %d", buf, wkb_out, buf - wkb_out);
858 
859  /* The buffer pointer should now land at the end of the allocated buffer space. Let's check. */
860  if ( buf_size != (size_t) (buf - wkb_out) )
861  {
862  LWDEBUG(4,"Output WKB is not the same size as the allocated buffer.");
863  lwerror("Output WKB is not the same size as the allocated buffer.");
864  lwfree(wkb_out);
865  return NULL;
866  }
867 
868  /* Report output size */
869  if ( size_out ) *size_out = buf_size;
870 
871  return wkb_out;
872 }
static uint8_t variant
Definition: cu_in_twkb.c:26
#define WKB_HEX
Definition: liblwgeom.h:2126
void lwfree(void *mem)
Definition: lwutil.c:242
#define WKB_NDR
Definition: liblwgeom.h:2124
void * lwalloc(size_t size)
Definition: lwutil.c:227
#define WKB_XDR
Definition: liblwgeom.h:2125
#define IS_BIG_ENDIAN
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
static size_t lwgeom_to_wkb_size(const LWGEOM *geom, uint8_t variant)
Definition: lwout_wkb.c:674
static uint8_t * lwgeom_to_wkb_buf(const LWGEOM *geom, uint8_t *buf, uint8_t variant)
Definition: lwout_wkb.c:733

References IS_BIG_ENDIAN, lwalloc(), LWDEBUG, LWDEBUGF, lwerror(), lwfree(), lwgeom_to_wkb_buf(), lwgeom_to_wkb_size(), variant, WKB_HEX, WKB_NDR, and WKB_XDR.

Referenced by cu_wkb(), cu_wkb_from_hexwkb(), cu_wkb_in(), geography_send(), LWGEOM_asBinary(), lwgeom_to_hexwkb(), polyhedralsurface_parse(), RASTER_asRaster(), RASTER_clip(), RASTER_setPixelValuesGeomval(), rt_raster_gdal_polygonize(), and WKBFromLWGEOM().

Here is the call graph for this function:
Here is the caller graph for this function: