PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

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

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

References BANDTYPE_FLAG_HASNODATA, BANDTYPE_FLAG_ISNODATA, BANDTYPE_FLAG_OFFDB, isMachineLittleEndian(), PT_16BF, 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(), rt_util_float_to_float16(), 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: