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

◆ rt_raster_generate_new_band()

int rt_raster_generate_new_band ( rt_raster  raster,
rt_pixtype  pixtype,
double  initialvalue,
uint32_t  hasnodata,
double  nodatavalue,
int  index 
)

Generate a new inline band and add it to a raster.

Parameters
raster: the raster to add a band to
pixtype: the pixel type for the new band
initialvalue: initial value for pixels
hasnodata: indicates if the band has a nodata value
nodatavalue: nodata value for the new band
index: position to add the new band in the raster
Returns
identifier (position) for the just-added raster, or -1 on error

Memory is allocated in this function for band data.

Parameters
raster: the raster to add a band to
pixtype: the pixel type for the new band
initialvalue: initial value for pixels
hasnodata: indicates if the band has a nodata value
nodatavalue: nodata value for the new band
index: position to add the new band in the raster
Returns
identifier (position) for the just-added raster, or -1 on error

Definition at line 489 of file rt_raster.c.

493 {
494 rt_band band = NULL;
495 int width = 0;
496 int height = 0;
497 int numval = 0;
498 int datasize = 0;
499 int oldnumbands = 0;
500 int numbands = 0;
501 void * mem = NULL;
502
503 assert(NULL != raster);
504
505 /* Make sure index is in a valid range */
506 oldnumbands = rt_raster_get_num_bands(raster);
507 if (index < 0)
508 index = 0;
509 else if (index > oldnumbands + 1)
510 index = oldnumbands + 1;
511
512 /* Determine size of memory block to allocate and allocate it */
513 width = rt_raster_get_width(raster);
514 height = rt_raster_get_height(raster);
515 numval = width * height;
516 datasize = rt_pixtype_size(pixtype) * numval;
517
518 mem = (int *)rtalloc(datasize);
519 if (!mem) {
520 rterror("rt_raster_generate_new_band: Could not allocate memory for band");
521 return -1;
522 }
523
524 band = rt_band_new_inline(width, height, pixtype, hasnodata, nodatavalue, mem);
525 if (! band) {
526 rterror("rt_raster_generate_new_band: Could not add band to raster. Aborting");
527 rtdealloc(mem);
528 return -1;
529 }
530
531 rt_band_init_value(band, initialvalue);
532
533 rt_band_set_ownsdata_flag(band, 1); /* we DO own this data!!! */
534 index = rt_raster_add_band(raster, band, index);
535 numbands = rt_raster_get_num_bands(raster);
536 if (numbands == oldnumbands || index == -1) {
537 rterror("rt_raster_generate_new_band: Could not add band to raster. Aborting");
538 rt_band_destroy(band);
539 return -1;
540 }
541
542 /* set isnodata if hasnodata = TRUE and initial value = nodatavalue */
543 if (hasnodata && FLT_EQ(initialvalue, nodatavalue))
545
546 return index;
547}
void rt_band_init_value(rt_band band, double initval)
Fill in the cells of a band with a starting value frequently used to init with nodata value.
Definition rt_band.c:113
rt_band rt_band_new_inline(uint16_t width, uint16_t height, rt_pixtype pixtype, uint32_t hasnodata, double nodataval, uint8_t *data)
Create an in-db rt_band with no data.
Definition rt_band.c:64
void rt_band_set_ownsdata_flag(rt_band band, int flag)
Definition rt_band.c:826
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
rt_errorstate rt_band_set_isnodata_flag(rt_band band, int flag)
Set isnodata flag value.
Definition rt_band.c:854
#define FLT_EQ(x, y)
Definition librtcore.h:2436
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition rt_band.c:499
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
int rt_raster_add_band(rt_raster raster, rt_band band, int index)
Add band data to a raster.
Definition rt_raster.c:409
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition rt_raster.c:376
uint16_t rt_raster_get_height(rt_raster raster)
Definition rt_raster.c:133
uint16_t rt_raster_get_width(rt_raster raster)
Definition rt_raster.c:125

References FLT_EQ, rt_band_destroy(), rt_band_init_value(), rt_band_new_inline(), rt_band_set_isnodata_flag(), rt_band_set_ownsdata_flag(), rt_pixtype_size(), rt_raster_add_band(), rt_raster_get_height(), rt_raster_get_num_bands(), rt_raster_get_width(), rtalloc(), rtdealloc(), and rterror().

Referenced by RASTER_addBand(), RASTER_clip(), RASTER_mapAlgebra2(), RASTER_mapAlgebraExpr(), RASTER_mapAlgebraFct(), RASTER_mapAlgebraFctNgb(), RASTER_tile(), RASTER_union_transfn(), rt_raster_from_gdal_dataset(), rt_raster_iterator(), and test_raster_get_pixel_bilinear().

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