PostGIS  3.7.0dev-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 497 of file rt_wkb.c.

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

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: