PostGIS  2.1.10dev-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.

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

693 {
694  size_t buf_size;
695  uint8_t *buf = NULL;
696  uint8_t *wkb_out = NULL;
697 
698  /* Initialize output size */
699  if ( size_out ) *size_out = 0;
700 
701  if ( geom == NULL )
702  {
703  LWDEBUG(4,"Cannot convert NULL into WKB.");
704  lwerror("Cannot convert NULL into WKB.");
705  return NULL;
706  }
707 
708  /* Calculate the required size of the output buffer */
709  buf_size = lwgeom_to_wkb_size(geom, variant);
710  LWDEBUGF(4, "WKB output size: %d", buf_size);
711 
712  if ( buf_size == 0 )
713  {
714  LWDEBUG(4,"Error calculating output WKB buffer size.");
715  lwerror("Error calculating output WKB buffer size.");
716  return NULL;
717  }
718 
719  /* Hex string takes twice as much space as binary + a null character */
720  if ( variant & WKB_HEX )
721  {
722  buf_size = 2 * buf_size + 1;
723  LWDEBUGF(4, "Hex WKB output size: %d", buf_size);
724  }
725 
726  /* If neither or both variants are specified, choose the native order */
727  if ( ! (variant & WKB_NDR || variant & WKB_XDR) ||
728  (variant & WKB_NDR && variant & WKB_XDR) )
729  {
730  if ( getMachineEndian() == NDR )
731  variant = variant | WKB_NDR;
732  else
733  variant = variant | WKB_XDR;
734  }
735 
736  /* Allocate the buffer */
737  buf = lwalloc(buf_size);
738 
739  if ( buf == NULL )
740  {
741  LWDEBUGF(4,"Unable to allocate %d bytes for WKB output buffer.", buf_size);
742  lwerror("Unable to allocate %d bytes for WKB output buffer.", buf_size);
743  return NULL;
744  }
745 
746  /* Retain a pointer to the front of the buffer for later */
747  wkb_out = buf;
748 
749  /* Write the WKB into the output buffer */
750  buf = lwgeom_to_wkb_buf(geom, buf, variant);
751 
752  /* Null the last byte if this is a hex output */
753  if ( variant & WKB_HEX )
754  {
755  *buf = '\0';
756  buf++;
757  }
758 
759  LWDEBUGF(4,"buf (%p) - wkb_out (%p) = %d", buf, wkb_out, buf - wkb_out);
760 
761  /* The buffer pointer should now land at the end of the allocated buffer space. Let's check. */
762  if ( buf_size != (buf - wkb_out) )
763  {
764  LWDEBUG(4,"Output WKB is not the same size as the allocated buffer.");
765  lwerror("Output WKB is not the same size as the allocated buffer.");
766  lwfree(wkb_out);
767  return NULL;
768  }
769 
770  /* Report output size */
771  if ( size_out ) *size_out = buf_size;
772 
773  return wkb_out;
774 }
#define WKB_NDR
Definition: liblwgeom.h:1770
void lwfree(void *mem)
Definition: lwutil.c:190
#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:577
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:67
static uint8_t * lwgeom_to_wkb_buf(const LWGEOM *geom, uint8_t *buf, uint8_t variant)
Definition: lwout_wkb.c:636
char getMachineEndian(void)
Definition: lwutil.c:306
#define WKB_XDR
Definition: liblwgeom.h:1771
void * lwalloc(size_t size)
Definition: lwutil.c:175
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55
#define WKB_HEX
Definition: liblwgeom.h:1772

Here is the call graph for this function:

Here is the caller graph for this function: