PostGIS  2.4.9dev-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 764 of file lwout_wkb.c.

References getMachineEndian(), lwalloc(), LWDEBUG, LWDEBUGF, lwerror(), lwfree(), lwgeom_to_wkb_buf(), lwgeom_to_wkb_size(), NDR, 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().

765 {
766  size_t buf_size;
767  uint8_t *buf = NULL;
768  uint8_t *wkb_out = NULL;
769 
770  /* Initialize output size */
771  if ( size_out ) *size_out = 0;
772 
773  if ( geom == NULL )
774  {
775  LWDEBUG(4,"Cannot convert NULL into WKB.");
776  lwerror("Cannot convert NULL into WKB.");
777  return NULL;
778  }
779 
780  /* Calculate the required size of the output buffer */
781  buf_size = lwgeom_to_wkb_size(geom, variant);
782  LWDEBUGF(4, "WKB output size: %d", buf_size);
783 
784  if ( buf_size == 0 )
785  {
786  LWDEBUG(4,"Error calculating output WKB buffer size.");
787  lwerror("Error calculating output WKB buffer size.");
788  return NULL;
789  }
790 
791  /* Hex string takes twice as much space as binary + a null character */
792  if ( variant & WKB_HEX )
793  {
794  buf_size = 2 * buf_size + 1;
795  LWDEBUGF(4, "Hex WKB output size: %d", buf_size);
796  }
797 
798  /* If neither or both variants are specified, choose the native order */
799  if ( ! (variant & WKB_NDR || variant & WKB_XDR) ||
800  (variant & WKB_NDR && variant & WKB_XDR) )
801  {
802  if ( getMachineEndian() == NDR )
803  variant = variant | WKB_NDR;
804  else
805  variant = variant | WKB_XDR;
806  }
807 
808  /* Allocate the buffer */
809  buf = lwalloc(buf_size);
810 
811  if ( buf == NULL )
812  {
813  LWDEBUGF(4,"Unable to allocate %d bytes for WKB output buffer.", buf_size);
814  lwerror("Unable to allocate %d bytes for WKB output buffer.", buf_size);
815  return NULL;
816  }
817 
818  /* Retain a pointer to the front of the buffer for later */
819  wkb_out = buf;
820 
821  /* Write the WKB into the output buffer */
822  buf = lwgeom_to_wkb_buf(geom, buf, variant);
823 
824  /* Null the last byte if this is a hex output */
825  if ( variant & WKB_HEX )
826  {
827  *buf = '\0';
828  buf++;
829  }
830 
831  LWDEBUGF(4,"buf (%p) - wkb_out (%p) = %d", buf, wkb_out, buf - wkb_out);
832 
833  /* The buffer pointer should now land at the end of the allocated buffer space. Let's check. */
834  if ( buf_size != (buf - wkb_out) )
835  {
836  LWDEBUG(4,"Output WKB is not the same size as the allocated buffer.");
837  lwerror("Output WKB is not the same size as the allocated buffer.");
838  lwfree(wkb_out);
839  return NULL;
840  }
841 
842  /* Report output size */
843  if ( size_out ) *size_out = buf_size;
844 
845  return wkb_out;
846 }
uint8_t variant
Definition: cu_in_twkb.c:26
#define WKB_NDR
Definition: liblwgeom.h:2077
void lwfree(void *mem)
Definition: lwutil.c:244
#define NDR
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
static size_t lwgeom_to_wkb_size(const LWGEOM *geom, uint8_t variant)
Definition: lwout_wkb.c:648
static uint8_t * lwgeom_to_wkb_buf(const LWGEOM *geom, uint8_t *buf, uint8_t variant)
Definition: lwout_wkb.c:707
char getMachineEndian(void)
Definition: lwutil.c:360
#define WKB_XDR
Definition: liblwgeom.h:2078
void * lwalloc(size_t size)
Definition: lwutil.c:229
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
unsigned char uint8_t
Definition: uthash.h:79
#define WKB_HEX
Definition: liblwgeom.h:2079
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
Here is the call graph for this function:
Here is the caller graph for this function: