PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ rt_raster_to_wkb()

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 494 of file rt_wkb.c.

494  {
495 
496 #if POSTGIS_DEBUG_LEVEL > 0
497  const uint8_t *wkbend = NULL;
498 #endif
499 
500  uint8_t *wkb = NULL;
501  uint8_t *ptr = NULL;
502  uint16_t i = 0;
503  uint8_t littleEndian = isMachineLittleEndian();
504 
505  assert(NULL != raster);
506  assert(NULL != wkbsize);
507 
508  RASTER_DEBUG(2, "rt_raster_to_wkb: about to call rt_raster_wkb_size");
509 
510  *wkbsize = rt_raster_wkb_size(raster, outasin);
511  RASTER_DEBUGF(3, "rt_raster_to_wkb: found size: %d", *wkbsize);
512 
513  wkb = (uint8_t*) rtalloc(*wkbsize);
514  if (!wkb) {
515  rterror("rt_raster_to_wkb: Out of memory allocating WKB for raster");
516  return NULL;
517  }
518 
519  ptr = wkb;
520 
521 #if POSTGIS_DEBUG_LEVEL > 2
522  wkbend = ptr + (*wkbsize);
523 #endif
524  RASTER_DEBUGF(3, "Writing raster header to wkb on position %d (expected 0)",
525  d_binptr_to_pos(ptr, wkbend, *wkbsize));
526 
527  /* Write endianness */
528  *ptr = littleEndian;
529  ptr += 1;
530 
531  /* Write version(size - (end - ptr)) */
532  write_uint16(&ptr, littleEndian, 0);
533 
534  /* Copy header (from numBands up) */
535  memcpy(ptr, &(raster->numBands), sizeof (struct rt_raster_serialized_t) - 6);
536  ptr += sizeof (struct rt_raster_serialized_t) - 6;
537 
538  RASTER_DEBUGF(3, "Writing bands header to wkb position %d (expected 61)",
539  d_binptr_to_pos(ptr, wkbend, *wkbsize));
540 
541  /* Serialize bands now */
542  for (i = 0; i < raster->numBands; ++i) {
543  rt_band band = raster->bands[i];
544  rt_pixtype pixtype = band->pixtype;
545  int pixbytes = rt_pixtype_size(pixtype);
546 
547  RASTER_DEBUGF(3, "Writing WKB for band %d", i);
548  RASTER_DEBUGF(3, "Writing band pixel type to wkb position %d",
549  d_binptr_to_pos(ptr, wkbend, *wkbsize));
550 
551  if (pixbytes < 1) {
552  rterror("rt_raster_to_wkb: Corrupted band: unknown pixtype");
553  rtdealloc(wkb);
554  return NULL;
555  }
556 
557  /* Add band type */
558  *ptr = band->pixtype;
559  if (!outasin && band->offline) *ptr |= BANDTYPE_FLAG_OFFDB;
560  if (band->hasnodata) *ptr |= BANDTYPE_FLAG_HASNODATA;
561  if (band->isnodata) *ptr |= BANDTYPE_FLAG_ISNODATA;
562  ptr += 1;
563 
564 #if 0
565  /* no padding required for WKB */
566  /* Add padding (if needed) */
567  if (pixbytes > 1) {
568  memset(ptr, '\0', pixbytes - 1);
569  ptr += pixbytes - 1;
570  }
571  /* Consistency checking (ptr is pixbytes-aligned) */
572  assert(!(((uint64_t) ptr) % pixbytes));
573 #endif
574 
575  RASTER_DEBUGF(3, "Writing band nodata to wkb position %d",
576  d_binptr_to_pos(ptr, wkbend, *wkbsize));
577 
578  /* Add nodata value */
579  switch (pixtype) {
580  case PT_1BB:
581  case PT_2BUI:
582  case PT_4BUI:
583  case PT_8BUI: {
584  uint8_t v = band->nodataval;
585  *ptr = v;
586  ptr += 1;
587  break;
588  }
589  case PT_8BSI: {
590  int8_t v = band->nodataval;
591  *ptr = v;
592  ptr += 1;
593  break;
594  }
595  case PT_16BSI: {
596  int16_t v = band->nodataval;
597  memcpy(ptr, &v, 2);
598  ptr += 2;
599  break;
600  }
601  case PT_16BUI: {
602  uint16_t v = band->nodataval;
603  memcpy(ptr, &v, 2);
604  ptr += 2;
605  break;
606  }
607  case PT_32BSI: {
608  int32_t v = band->nodataval;
609  memcpy(ptr, &v, 4);
610  ptr += 4;
611  break;
612  }
613  case PT_32BUI: {
614  uint32_t v = band->nodataval;
615  memcpy(ptr, &v, 4);
616  ptr += 4;
617  break;
618  }
619  case PT_32BF: {
620  float v = band->nodataval;
621  memcpy(ptr, &v, 4);
622  ptr += 4;
623  break;
624  }
625  case PT_64BF: {
626  memcpy(ptr, &band->nodataval, 8);
627  ptr += 8;
628  break;
629  }
630  default:
631  rterror("rt_raster_to_wkb: Fatal error caused by unknown pixel type. Aborting.");
632  rtdealloc(wkb);
633  abort(); /* shoudn't happen */
634  return 0;
635  }
636 
637 #if 0
638  /* no padding for WKB */
639  /* Consistency checking (ptr is pixbytes-aligned) */
640  assert(!((uint64_t) ptr % pixbytes));
641 #endif
642 
643  if (!outasin && band->offline) {
644  /* Write band number */
645  *ptr = band->data.offline.bandNum;
646  ptr += 1;
647 
648  /* Write path */
649  strcpy((char*) ptr, band->data.offline.path);
650  ptr += strlen(band->data.offline.path) + 1;
651  }
652  else {
653  /* Write data */
654  uint32_t datasize = raster->width * raster->height * pixbytes;
655  RASTER_DEBUGF(4, "rt_raster_to_wkb: Copying %d bytes", datasize);
656 
657  memcpy(ptr, rt_band_get_data(band), datasize);
658 
659  ptr += datasize;
660  }
661 
662 #if 0
663  /* no padding for WKB */
664  /* Pad up to 8-bytes boundary */
665  while ((uint64_t) ptr % 8) {
666  *ptr = 0;
667  ++ptr;
668  }
669 
670  /* Consistency checking (ptr is pixbytes-aligned) */
671  assert(!((uint64_t) ptr % pixbytes));
672 #endif
673  }
674 
675  return wkb;
676 }
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
Definition: rt_context.c:199
void * rtalloc(size_t size)
Wrappers used for managing memory.
Definition: rt_context.c:171
#define RASTER_DEBUG(level, msg)
Definition: librtcore.h:295
#define RASTER_DEBUGF(level, msg,...)
Definition: librtcore.h:299
rt_pixtype
Definition: librtcore.h:185
@ PT_32BUI
Definition: librtcore.h:194
@ PT_2BUI
Definition: librtcore.h:187
@ PT_32BSI
Definition: librtcore.h:193
@ PT_4BUI
Definition: librtcore.h:188
@ PT_32BF
Definition: librtcore.h:195
@ PT_1BB
Definition: librtcore.h:186
@ PT_16BUI
Definition: librtcore.h:192
@ PT_8BSI
Definition: librtcore.h:189
@ PT_16BSI
Definition: librtcore.h:191
@ PT_64BF
Definition: librtcore.h:196
@ PT_8BUI
Definition: librtcore.h:190
void rtdealloc(void *mem)
Definition: rt_context.c:186
void * rt_band_get_data(rt_band band)
Get pointer to raster band data.
Definition: rt_band.c:400
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
Definition: rt_pixel.c:39
band
Definition: ovdump.py:57
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
uint8_t isMachineLittleEndian(void)
Definition: rt_serialize.c:190
void write_uint16(uint8_t **to, uint8_t littleEndian, uint16_t v)
Definition: rt_serialize.c:247
#define BANDTYPE_FLAG_HASNODATA
Definition: rt_serialize.h:38
#define BANDTYPE_FLAG_OFFDB
Definition: rt_serialize.h:37
#define BANDTYPE_FLAG_ISNODATA
Definition: rt_serialize.h:39
static uint32_t rt_raster_wkb_size(rt_raster raster, int outasin)
Definition: rt_wkb.c:441
Struct definitions.
Definition: librtcore.h:2250
unsigned int uint32_t
Definition: uthash.h:78
unsigned char uint8_t
Definition: uthash.h:79

References ovdump::band, BANDTYPE_FLAG_HASNODATA, BANDTYPE_FLAG_ISNODATA, BANDTYPE_FLAG_OFFDB, isMachineLittleEndian(), PT_16BSI, PT_16BUI, PT_1BB, PT_2BUI, PT_32BF, PT_32BSI, PT_32BUI, PT_4BUI, PT_64BF, PT_8BSI, PT_8BUI, rtrowdump::raster, RASTER_DEBUG, RASTER_DEBUGF, rt_band_get_data(), rt_pixtype_size(), rt_raster_wkb_size(), rtalloc(), rtdealloc(), rterror(), and write_uint16().

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

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