PostGIS  2.2.7dev-r@@SVN_REVISION@@
uint8_t* lwgeom_to_wkb ( const LWGEOM geom,
uint8_t  variant,
size_t *  size_out 
)

Convert LWGEOM to a char* in WKB format.

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 750 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().

751 {
752  size_t buf_size;
753  uint8_t *buf = NULL;
754  uint8_t *wkb_out = NULL;
755 
756  /* Initialize output size */
757  if ( size_out ) *size_out = 0;
758 
759  if ( geom == NULL )
760  {
761  LWDEBUG(4,"Cannot convert NULL into WKB.");
762  lwerror("Cannot convert NULL into WKB.");
763  return NULL;
764  }
765 
766  /* Calculate the required size of the output buffer */
767  buf_size = lwgeom_to_wkb_size(geom, variant);
768  LWDEBUGF(4, "WKB output size: %d", buf_size);
769 
770  if ( buf_size == 0 )
771  {
772  LWDEBUG(4,"Error calculating output WKB buffer size.");
773  lwerror("Error calculating output WKB buffer size.");
774  return NULL;
775  }
776 
777  /* Hex string takes twice as much space as binary + a null character */
778  if ( variant & WKB_HEX )
779  {
780  buf_size = 2 * buf_size + 1;
781  LWDEBUGF(4, "Hex WKB output size: %d", buf_size);
782  }
783 
784  /* If neither or both variants are specified, choose the native order */
785  if ( ! (variant & WKB_NDR || variant & WKB_XDR) ||
786  (variant & WKB_NDR && variant & WKB_XDR) )
787  {
788  if ( getMachineEndian() == NDR )
789  variant = variant | WKB_NDR;
790  else
791  variant = variant | WKB_XDR;
792  }
793 
794  /* Allocate the buffer */
795  buf = lwalloc(buf_size);
796 
797  if ( buf == NULL )
798  {
799  LWDEBUGF(4,"Unable to allocate %d bytes for WKB output buffer.", buf_size);
800  lwerror("Unable to allocate %d bytes for WKB output buffer.", buf_size);
801  return NULL;
802  }
803 
804  /* Retain a pointer to the front of the buffer for later */
805  wkb_out = buf;
806 
807  /* Write the WKB into the output buffer */
808  buf = lwgeom_to_wkb_buf(geom, buf, variant);
809 
810  /* Null the last byte if this is a hex output */
811  if ( variant & WKB_HEX )
812  {
813  *buf = '\0';
814  buf++;
815  }
816 
817  LWDEBUGF(4,"buf (%p) - wkb_out (%p) = %d", buf, wkb_out, buf - wkb_out);
818 
819  /* The buffer pointer should now land at the end of the allocated buffer space. Let's check. */
820  if ( buf_size != (buf - wkb_out) )
821  {
822  LWDEBUG(4,"Output WKB is not the same size as the allocated buffer.");
823  lwerror("Output WKB is not the same size as the allocated buffer.");
824  lwfree(wkb_out);
825  return NULL;
826  }
827 
828  /* Report output size */
829  if ( size_out ) *size_out = buf_size;
830 
831  return wkb_out;
832 }
uint8_t variant
Definition: cu_in_twkb.c:26
#define WKB_NDR
Definition: liblwgeom.h:1933
void lwfree(void *mem)
Definition: lwutil.c:214
#define NDR
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:50
static size_t lwgeom_to_wkb_size(const LWGEOM *geom, uint8_t variant)
Definition: lwout_wkb.c:634
static uint8_t * lwgeom_to_wkb_buf(const LWGEOM *geom, uint8_t *buf, uint8_t variant)
Definition: lwout_wkb.c:693
char getMachineEndian(void)
Definition: lwutil.c:330
#define WKB_XDR
Definition: liblwgeom.h:1934
void * lwalloc(size_t size)
Definition: lwutil.c:199
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55
#define WKB_HEX
Definition: liblwgeom.h:1935
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:74

Here is the call graph for this function:

Here is the caller graph for this function: