PostGIS  2.1.10dev-r@@SVN_REVISION@@
uint8_t* rt_raster_to_wkb ( rt_raster  raster,
int  outasin,
uint32_t *  wkbsize 
)

Return this raster in WKB form.

Parameters
raster: the raster
outasin: if TRUE, out-db bands are treated as in-db
wkbsize: will be set to the size of returned wkb form
Returns
WKB of raster or NULL on error

Definition at line 7887 of file rt_api.c.

References ovdump::band, rt_raster_t::bands, BANDTYPE_FLAG_HASNODATA, BANDTYPE_FLAG_ISNODATA, BANDTYPE_FLAG_OFFDB, rt_band_t::data, rt_band_t::hasnodata, rt_raster_t::height, isMachineLittleEndian(), rt_band_t::isnodata, rt_band_t::nodataval, rt_raster_t::numBands, rt_band_t::offline, rt_band_t::pixtype, PT_16BSI, PT_16BUI, PT_1BB, PT_2BUI, PT_32BF, PT_32BSI, PT_32BUI, PT_4BUI, PT_64BF, PT_8BSI, PT_8BUI, RASTER_DEBUG, RASTER_DEBUGF, rt_band_get_data(), rt_pixtype_size(), rt_raster_wkb_size(), rtalloc(), rtdealloc(), rterror(), rt_raster_t::width, and write_uint16().

Referenced by RASTER_to_binary(), RASTER_to_bytea(), and rt_raster_to_hexwkb().

7887  {
7888 
7889 #if POSTGIS_DEBUG_LEVEL > 0
7890  const uint8_t *wkbend = NULL;
7891 #endif
7892 
7893  uint8_t *wkb = NULL;
7894  uint8_t *ptr = NULL;
7895  uint16_t i = 0;
7896  uint8_t littleEndian = isMachineLittleEndian();
7897 
7898  assert(NULL != raster);
7899  assert(NULL != wkbsize);
7900 
7901  RASTER_DEBUG(2, "rt_raster_to_wkb: about to call rt_raster_wkb_size");
7902 
7903  *wkbsize = rt_raster_wkb_size(raster, outasin);
7904  RASTER_DEBUGF(3, "rt_raster_to_wkb: found size: %d", *wkbsize);
7905 
7906  wkb = (uint8_t*) rtalloc(*wkbsize);
7907  if (!wkb) {
7908  rterror("rt_raster_to_wkb: Out of memory allocating WKB for raster");
7909  return NULL;
7910  }
7911 
7912  ptr = wkb;
7913 
7914 #if POSTGIS_DEBUG_LEVEL > 2
7915  wkbend = ptr + (*wkbsize);
7916 #endif
7917  RASTER_DEBUGF(3, "Writing raster header to wkb on position %d (expected 0)",
7918  d_binptr_to_pos(ptr, wkbend, *wkbsize));
7919 
7920  /* Write endianness */
7921  *ptr = littleEndian;
7922  ptr += 1;
7923 
7924  /* Write version(size - (end - ptr)) */
7925  write_uint16(&ptr, littleEndian, 0);
7926 
7927  /* Copy header (from numBands up) */
7928  memcpy(ptr, &(raster->numBands), sizeof (struct rt_raster_serialized_t) - 6);
7929  ptr += sizeof (struct rt_raster_serialized_t) - 6;
7930 
7931  RASTER_DEBUGF(3, "Writing bands header to wkb position %d (expected 61)",
7932  d_binptr_to_pos(ptr, wkbend, *wkbsize));
7933 
7934  /* Serialize bands now */
7935  for (i = 0; i < raster->numBands; ++i) {
7936  rt_band band = raster->bands[i];
7937  rt_pixtype pixtype = band->pixtype;
7938  int pixbytes = rt_pixtype_size(pixtype);
7939 
7940  RASTER_DEBUGF(3, "Writing WKB for band %d", i);
7941  RASTER_DEBUGF(3, "Writing band pixel type to wkb position %d",
7942  d_binptr_to_pos(ptr, wkbend, *wkbsize));
7943 
7944  if (pixbytes < 1) {
7945  rterror("rt_raster_to_wkb: Corrupted band: unknown pixtype");
7946  rtdealloc(wkb);
7947  return NULL;
7948  }
7949 
7950  /* Add band type */
7951  *ptr = band->pixtype;
7952  if (!outasin && band->offline) *ptr |= BANDTYPE_FLAG_OFFDB;
7953  if (band->hasnodata) *ptr |= BANDTYPE_FLAG_HASNODATA;
7954  if (band->isnodata) *ptr |= BANDTYPE_FLAG_ISNODATA;
7955  ptr += 1;
7956 
7957 #if 0
7958  /* no padding required for WKB */
7959  /* Add padding (if needed) */
7960  if (pixbytes > 1) {
7961  memset(ptr, '\0', pixbytes - 1);
7962  ptr += pixbytes - 1;
7963  }
7964  /* Consistency checking (ptr is pixbytes-aligned) */
7965  assert(!(((uint64_t) ptr) % pixbytes));
7966 #endif
7967 
7968  RASTER_DEBUGF(3, "Writing band nodata to wkb position %d",
7969  d_binptr_to_pos(ptr, wkbend, *wkbsize));
7970 
7971  /* Add nodata value */
7972  switch (pixtype) {
7973  case PT_1BB:
7974  case PT_2BUI:
7975  case PT_4BUI:
7976  case PT_8BUI: {
7977  uint8_t v = band->nodataval;
7978  *ptr = v;
7979  ptr += 1;
7980  break;
7981  }
7982  case PT_8BSI: {
7983  int8_t v = band->nodataval;
7984  *ptr = v;
7985  ptr += 1;
7986  break;
7987  }
7988  case PT_16BSI:
7989  case PT_16BUI: {
7990  uint16_t v = band->nodataval;
7991  memcpy(ptr, &v, 2);
7992  ptr += 2;
7993  break;
7994  }
7995  case PT_32BSI:
7996  case PT_32BUI: {
7997  uint32_t v = band->nodataval;
7998  memcpy(ptr, &v, 4);
7999  ptr += 4;
8000  break;
8001  }
8002  case PT_32BF: {
8003  float v = band->nodataval;
8004  memcpy(ptr, &v, 4);
8005  ptr += 4;
8006  break;
8007  }
8008  case PT_64BF: {
8009  memcpy(ptr, &band->nodataval, 8);
8010  ptr += 8;
8011  break;
8012  }
8013  default:
8014  rterror("rt_raster_to_wkb: Fatal error caused by unknown pixel type. Aborting.");
8015  rtdealloc(wkb);
8016  abort(); /* shoudn't happen */
8017  return 0;
8018  }
8019 
8020 #if 0
8021  /* no padding for WKB */
8022  /* Consistency checking (ptr is pixbytes-aligned) */
8023  assert(!((uint64_t) ptr % pixbytes));
8024 #endif
8025 
8026  if (!outasin && band->offline) {
8027  /* Write band number */
8028  *ptr = band->data.offline.bandNum;
8029  ptr += 1;
8030 
8031  /* Write path */
8032  strcpy((char*) ptr, band->data.offline.path);
8033  ptr += strlen(band->data.offline.path) + 1;
8034  }
8035  else {
8036  /* Write data */
8037  uint32_t datasize = raster->width * raster->height * pixbytes;
8038  RASTER_DEBUGF(4, "rt_raster_to_wkb: Copying %d bytes", datasize);
8039 
8040  memcpy(ptr, rt_band_get_data(band), datasize);
8041 
8042  ptr += datasize;
8043  }
8044 
8045 #if 0
8046  /* no padding for WKB */
8047  /* Pad up to 8-bytes boundary */
8048  while ((uint64_t) ptr % 8) {
8049  *ptr = 0;
8050  ++ptr;
8051  }
8052 
8053  /* Consistency checking (ptr is pixbytes-aligned) */
8054  assert(!((uint64_t) ptr % pixbytes));
8055 #endif
8056  }
8057 
8058  return wkb;
8059 }
void rtdealloc(void *mem)
Definition: rt_api.c:882
void * rt_band_get_data(rt_band band)
Get pointer to raster band data.
Definition: rt_api.c:1710
uint16_t numBands
Definition: rt_api.h:2215
rt_pixtype pixtype
Definition: rt_api.h:2239
tuple band
Definition: ovdump.py:57
Definition: rt_api.h:173
uint16_t height
Definition: rt_api.h:2227
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
Definition: rt_api.c:1097
#define RASTER_DEBUG(level, msg)
Definition: rt_api.h:281
static void write_uint16(uint8_t **to, uint8_t littleEndian, uint16_t v)
Definition: rt_api.c:7196
rt_pixtype
Definition: rt_api.h:172
#define RASTER_DEBUGF(level, msg,...)
Definition: rt_api.h:285
uint16_t width
Definition: rt_api.h:2226
double nodataval
Definition: rt_api.h:2246
static uint32_t rt_raster_wkb_size(rt_raster raster, int outasin)
Definition: rt_api.c:7834
union rt_band_t::@14 data
#define BANDTYPE_FLAG_ISNODATA
Definition: rt_api.c:7419
void * rtalloc(size_t size)
Raster core memory management functions.
Definition: rt_api.c:867
void rterror(const char *fmt,...)
Raster core error and info handlers.
Definition: rt_api.c:895
int32_t offline
Definition: rt_api.h:2240
int32_t isnodata
Definition: rt_api.h:2244
#define BANDTYPE_FLAG_HASNODATA
Definition: rt_api.c:7418
Struct definitions.
Definition: rt_api.h:2175
rt_band * bands
Definition: rt_api.h:2228
int32_t hasnodata
Definition: rt_api.h:2243
static uint8_t isMachineLittleEndian(void)
Definition: rt_api.c:7137
#define BANDTYPE_FLAG_OFFDB
Definition: rt_api.c:7417

Here is the call graph for this function:

Here is the caller graph for this function: