71 uint32_t hasnodata,
double nodataval,
80 uint32_t src_hasnodata = 0;
81 double src_nodataval = 0.0;
95 assert(NULL != srcband);
96 assert(NULL != exprset && exprcount > 0);
108 numval = width * height;
110 mem = (
int *)
rtalloc(memsize);
112 rterror(
"rt_band_reclass: Could not allocate memory for band");
118 rterror(
"rt_band_reclass: Could not create new band");
127 for (
x = 0;
x < width;
x++) {
128 for (
y = 0;
y < height;
y++) {
136 RASTER_DEBUGF(4,
"(x, y, ov, isnodata) = (%d, %d, %f, %d)",
x,
y, ov, isnodata);
142 if (hasnodata && isnodata) {
147 for (i = 0; i < exprcount; i++) {
178 ov < expr->src.max ||
192 if (!do_nv)
continue;
202 if (hasnodata && isnodata) {
215 nv = (((ov - expr->
src.
min) * nr) / or) + expr->
dst.
min;
221 else if (nv < expr->dst.max)
226 if (nv < expr->dst.min)
228 else if (nv > expr->
dst.
max)
236 RASTER_DEBUGF(4,
"(%d, %d) ov: %f or: %f - %f nr: %f - %f nv: %f"
240 , (NULL != expr) ? expr->
src.
min : 0
241 , (NULL != expr) ? expr->
src.
max : 0
242 , (NULL != expr) ? expr->
dst.
min : 0
243 , (NULL != expr) ? expr->
dst.
max : 0
247 rterror(
"rt_band_reclass: Could not assign value to new band");
271 if (a->
src < b->
src)
return -1;
272 else if (a->
src > b->
src)
return 1;
290 uint32_t hasnodata,
double nodataval
298 uint32_t src_hasnodata = 0;
301 assert(NULL != srcband);
306 rterror(
"%s: reclassification map must contain at least one class pair", __func__);
310 if (!(src_hasnodata || hasnodata))
311 rterror(
"%s: source band missing nodata value and nodata value not supplied", __func__);
317 if (!hasnodata && src_hasnodata) {
319 hasnodata = src_hasnodata;
325 numval = width * height;
328 mem = (
int *)
rtalloc(memsize);
330 rterror(
"%s: Could not allocate memory for band", __func__);
334 rterror(
"%s: Could not add band to raster. Aborting", __func__);
352 for (uint32_t
x = 0;
x < width;
x++) {
353 for (uint32_t
y = 0;
y < height;
y++) {
355 double nv = nodataval;
381 rterror(
"%s: Could not assign value to new band", __func__);
437 if (_param == NULL) {
438 rterror(
"_rti_iterator_arg_init: Could not allocate memory for _rti_iterator_arg");
447 _param->
width = NULL;
474 if (_param->
raster != NULL)
478 if (_param->
width != NULL)
480 if (_param->
height != NULL)
494 if (_param->
offset != NULL) {
495 for (i = 0; i < _param->
count; i++) {
496 if (_param->
offset[i] == NULL)
520 if (_param->
arg != NULL) {
526 for (i = 0; i < _param->
count; i++) {
545 uint16_t distancex, uint16_t distancey,
546 int *allnull,
int *allempty
551 _param->
count = itrcount;
574 _param->
width == NULL ||
583 rterror(
"_rti_iterator_arg_populate: Could not allocate memory for children of _rti_iterator_arg");
595 for (i = 0; i < itrcount; i++) {
599 _param->
width[i] = 0;
611 if (itrset[i].
raster == NULL) {
630 if (!itrset[i].nbnodata) {
631 rterror(
"_rti_iterator_arg_populate: Band %d not found for raster %d", itrset[i].
nband, i);
635 RASTER_DEBUGF(4,
"Band %d not found for raster %d. Using NODATA", itrset[i].
nband, i);
643 rterror(
"_rti_iterator_arg_populate: Could not get band %d for raster %d", itrset[i].
nband, i);
671 if (_param->
offset[i] == NULL) {
672 rterror(
"_rti_iterator_arg_populate: Could not allocate memory for offsets");
688 rterror(
"_rti_iterator_arg_empty_init: Could not allocate memory for empty values and NODATA");
697 rterror(
"_rti_iterator_arg_empty_init: Could not allocate memory for elements of empty values and NODATA");
715 if (_param->
arg == NULL) {
716 rterror(
"_rti_iterator_arg_callback_init: Could not allocate memory for rt_iterator_arg");
729 rterror(
"_rti_iterator_arg_callback_init: Could not allocate memory for element of rt_iterator_arg");
732 memset(_param->
arg->
values, 0,
sizeof(
double **) * _param->
count);
736 for (i = 0; i < _param->
count; i++) {
740 rterror(
"_rti_iterator_arg_callback_init: Could not allocate memory for position elements of rt_iterator_arg");
761 for (i = 0; i < _param->
count; i++) {
827 uint8_t hasnodata,
double nodataval,
828 uint16_t distancex, uint16_t distancey,
851 double offset[4] = {0.};
872 assert(itrset != NULL && itrcount > 0);
873 assert(rtnraster != NULL);
879 if (callback == NULL) {
880 rterror(
"rt_raster_iterator: Callback function not provided");
886 rterror(
"rt_raster_iterator: Custom extent cannot be empty if extent type is ET_CUSTOM");
892 rterror(
"rt_raster_iterator: Pixel type cannot be PT_END");
898 rterror(
"rt_raster_iterator: Could not initialize internal variables");
904 rterror(
"rt_raster_iterator: Could not populate for internal variables");
910 if (allnull == itrcount) {
911 RASTER_DEBUG(3,
"all rasters are NULL, returning NULL");
918 else if (allempty == itrcount) {
919 RASTER_DEBUG(3,
"all rasters are empty, returning empty raster");
924 if (rtnrast == NULL) {
925 rterror(
"rt_raster_iterator: Could not create empty raster");
930 *rtnraster = rtnrast;
941 RASTER_DEBUG(4,
"using custom extent as reference raster");
946 for (i = 0; i < itrcount; i++) {
948 RASTER_DEBUGF(4,
"using raster at index %d as reference raster", i);
957 rterror(
"rt_raster_iterator: Could not find reference raster to use for alignment tests");
970 rterror(
"rt_raster_iterator: Could not test for alignment between reference raster and custom extent");
982 for (i = 0; i < itrcount; i++) {
988 rterror(
"rt_raster_iterator: Could not test for alignment between reference raster and raster %d", i);
994 RASTER_DEBUGF(5,
"raster at index %d alignment: %d", i, aligned);
1005 rterror(
"rt_raster_iterator: The set of rasters provided (custom extent included, if appropriate) do not have the same alignment");
1014 switch (extenttype) {
1019 if (rtnrast == NULL) {
1020 rterror(
"rt_raster_iterator: Could not allocate memory for output raster");
1027 for (i = 0; i < itrcount; i++) {
1034 rtnrast->
bands = NULL;
1038 for (i = i + 1; i < itrcount; i++) {
1046 rterror(
"rt_raster_iterator: Could not compute %s extent of rasters",
1047 extenttype ==
ET_UNION ?
"union" :
"intersection"
1055 rtinfo(
"rt_raster_iterator: Computed raster for %s extent is empty",
1056 extenttype ==
ET_UNION ?
"union" :
"intersection"
1086 if (i < 0) i = itrcount - 1;
1089 if (_param->
raster[i] == NULL) {
1090 RASTER_DEBUGF(3,
"returning NULL as %s raster is NULL and extent type is ET_%s",
1091 (i == 0 ?
"first" : (i == 1 ?
"second" :
"last")),
1092 (i == 0 ?
"FIRST" : (i == 1 ?
"SECOND" :
"LAST"))
1100 else if (_param->
isempty[i]) {
1101 RASTER_DEBUGF(3,
"returning empty raster as %s raster is empty and extent type is ET_%s",
1102 (i == 0 ?
"first" : (i == 1 ?
"second" :
"last")),
1103 (i == 0 ?
"FIRST" : (i == 1 ?
"SECOND" :
"LAST"))
1109 if (rtnrast == NULL) {
1110 rterror(
"rt_raster_iterator: Could not create empty raster");
1115 *rtnraster = rtnrast;
1122 if (rtnrast == NULL) {
1123 rterror(
"rt_raster_iterator: Could not allocate memory for output raster");
1130 switch (extenttype) {
1132 memcpy(rtnrast, customextent,
sizeof(
struct rt_raster_t));
1140 rtnrast->
bands = NULL;
1147 RASTER_DEBUGF(4,
"rtnrast (width, height, ulx, uly, scalex, scaley, skewx, skewy, srid) = (%d, %d, %f, %f, %f, %f, %f, %f, %d)",
1161 rterror(
"rt_raster_iterator: Could not initialize empty values and NODATA");
1174 hasnodata, nodataval,
1177 rterror(
"rt_raster_iterator: Could not add new band to output raster");
1187 if (rtnband == NULL) {
1188 rterror(
"rt_raster_iterator: Could not get new band from output raster");
1201 rterror(
"rt_raster_iterator: Could not initialize callback function argument");
1211 for (i = 0; i < itrcount; i++) {
1218 rterror(
"rt_raster_iterator: Could not compute raster offsets");
1227 _param->
offset[i][0] = offset[2];
1228 _param->
offset[i][1] = offset[3];
1229 RASTER_DEBUGF(4,
"rast %d offset: %f %f", i, offset[2], offset[3]);
1235 for (_y = 0; _y < _height; _y++) {
1236 for (_x = 0; _x < _width; _x++) {
1237 RASTER_DEBUGF(4,
"iterating output pixel (x, y) = (%d, %d)", _x, _y);
1242 for (i = 0; i < itrcount; i++) {
1255 RASTER_DEBUG(4,
"empty raster, band does not exist or band is NODATA. using empty values and NODATA");
1267 x = _x - (int) _param->
offset[i][0];
1268 y = _y - (
int) _param->
offset[i][1];
1277 if (distancex > 0 && distancey > 0) {
1283 distancex, distancey,
1288 rterror(
"rt_raster_iterator: Could not get pixel neighborhood");
1301 (
x >= 0 && x < _param->width[i]) &&
1302 (
y >= 0 && y < _param->height[i])
1311 rterror(
"rt_raster_iterator: Could not get the pixel value of band");
1323 RASTER_DEBUG(4,
"Outside band extent, setting value to NODATA");
1342 if (npixels == NULL) {
1343 rterror(
"rt_raster_iterator: Could not reallocate memory for neighborhood");
1352 npixels[status - 1].
x =
x;
1353 npixels[status - 1].
y =
y;
1354 npixels[status - 1].
nodata = 1;
1358 if ((!_param->
band.
hasnodata[i] && inextent) || !isnodata) {
1359 npixels[status - 1].
nodata = 0;
1365 npixels, status, mask,
1367 distancex, distancey,
1374 rterror(
"rt_raster_iterator: Could not create 2D array of neighborhood");
1388 status = callback(_param->
arg, userarg, &
value, &nodata);
1395 rterror(
"rt_raster_iterator: Callback function returned an error");
1410 else if (!hasnodata) {
1412 RASTER_DEBUGF(4,
"burning pixel (%d, %d) with minval: %f", _x, _y, minval);
1418 rterror(
"rt_raster_iterator: Could not set pixel value");
1432 *rtnraster = rtnrast;
1463 rterror(
"_rti_colormap_arg_init: Could not allocate memory for _rti_color_arg");
1477 if (arg->
raster == NULL) {
1478 rterror(
"_rti_colormap_arg_init: Could not create output raster");
1496 if (arg->
raster != NULL) {
1509 for (i = 0; i < arg->
nexpr; i++) {
1510 if (arg->
expr[i] != NULL)
1545 assert(colormap != NULL);
1552 if (colormap->
nentry < 1) {
1553 rterror(
"rt_raster_colormap: colormap must have at least one entry");
1559 rterror(
"rt_raster_colormap: raster has no band at index %d",
nband);
1565 rterror(
"rt_raster_colormap: Could not get band at index %d",
nband);
1572 rterror(
"rt_raster_colormap: Could not initialize internal variables");
1583 if (colormap->
ncolor < 1) {
1584 rterror(
"rt_raster_colormap: At least one color must be provided");
1588 else if (colormap->
ncolor > 4) {
1589 rtinfo(
"More than four colors indicated. Using only the first four colors");
1596 if (arg->
pos == NULL) {
1597 rterror(
"rt_raster_colormap: Could not allocate memory for valid entries");
1601 for (i = 0, j = 0; i < colormap->
nentry; i++) {
1608 rtwarn(
"More than one colormap entry found for NODATA value. Only using first NOTDATA entry");
1618 if (colormap->
method == CM_INTERPOLATE && arg->
npos < 2) {
1619 rtwarn(
"Method INTERPOLATE requires at least two non-NODATA colormap entries. Using NEAREST instead");
1620 colormap->
method = CM_NEAREST;
1625 rtinfo(
"Band at index %d has no NODATA value. Ignoring NODATA entry",
nband);
1633 if (colormap->
method == CM_INTERPOLATE)
1636 else if (colormap->
method == CM_EXACT)
1643 if (arg->
expr == NULL) {
1644 rterror(
"rt_raster_colormap: Could not allocate memory for reclass expressions");
1651 for (i = 0; i < arg->
nexpr; i++) {
1653 if (arg->
expr[i] == NULL) {
1654 rterror(
"rt_raster_colormap: Could not allocate memory for reclass expression");
1662 for (i = 0; i < colormap->
ncolor; i++) {
1682 RASTER_DEBUGF(4,
"NODATA expr[%d]->src (min, max, in, ix, en, ex) = (%f, %f, %d, %d, %d, %d)",
1691 RASTER_DEBUGF(4,
"NODATA expr[%d]->dst (min, max, in, ix, en, ex) = (%f, %f, %d, %d, %d, %d)",
1705 for (j = 0; j < arg->
npos; j++) {
1706 if (colormap->
method == CM_INTERPOLATE) {
1707 if (j == arg->
npos - 1)
1727 else if (colormap->
method == CM_NEAREST) {
1730 if (j != arg->
npos - 1) {
1763 else if (colormap->
method == CM_EXACT) {
1781 RASTER_DEBUGF(4,
"expr[%d]->src (min, max, in, ix, en, ex) = (%f, %f, %d, %d, %d, %d)",
1791 RASTER_DEBUGF(4,
"expr[%d]->dst (min, max, in, ix, en, ex) = (%f, %f, %d, %d, %d, %d)",
1805 if (colormap->
method == CM_EXACT) {
1822 RASTER_DEBUGF(4,
"expr[%d]->src (min, max, in, ix, en, ex) = (%f, %f, %d, %d, %d, %d)",
1832 RASTER_DEBUGF(4,
"expr[%d]->dst (min, max, in, ix, en, ex) = (%f, %f, %d, %d, %d, %d)",
1847 if (arg->
band == NULL) {
1848 rterror(
"rt_raster_colormap: Could not reclassify band");
1855 rterror(
"rt_raster_colormap: Could not add reclassified band to output raster");
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.
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.
void rt_band_set_ownsdata_flag(rt_band band, int flag)
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.
uint16_t rt_band_get_width(rt_band band)
Return width of this band.
#define RASTER_DEBUG(level, msg)
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
#define RASTER_DEBUGF(level, msg,...)
double rt_raster_get_x_skew(rt_raster raster)
Get skew about the X axis.
double rt_raster_get_x_offset(rt_raster raster)
Get raster x offset, in projection units.
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.
void void rtinfo(const char *fmt,...) __attribute__((format(printf
void void void rtwarn(const char *fmt,...) __attribute__((format(printf
void rt_raster_set_scale(rt_raster raster, double scaleX, double scaleY)
Set scale in projection units.
int rt_raster_add_band(rt_raster raster, rt_band band, int index)
Add band data to a raster.
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
int rt_raster_has_band(rt_raster raster, int nband)
Return TRUE if the raster has a band of this number.
struct rt_pixel_t * rt_pixel
double rt_raster_get_x_scale(rt_raster raster)
Get scale X in projection units.
double rt_band_get_min_value(rt_band band)
Returns the minimal possible value for the band according to the pixel type.
rt_errorstate rt_band_set_pixel(rt_band band, int x, int y, double val, int *converted)
Set single pixel's value.
rt_errorstate
Enum definitions.
void rt_band_destroy(rt_band band)
Destroy a raster band.
void * rtrealloc(void *mem, size_t size)
uint16_t rt_raster_get_num_bands(rt_raster raster)
rt_raster rt_raster_clone(rt_raster raster, uint8_t deep)
Clone an existing raster.
uint16_t rt_raster_get_height(rt_raster raster)
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
rt_errorstate rt_pixel_set_to_array(rt_pixel npixel, uint32_t count, rt_mask mask, int x, int y, uint16_t distancex, uint16_t distancey, double ***value, int ***nodata, int *dimx, int *dimy)
uint16_t rt_raster_get_width(rt_raster raster)
uint32_t rt_band_get_nearest_pixel(rt_band band, int x, int y, uint16_t distancex, uint16_t distancey, int exclude_nodata_value, rt_pixel *npixels)
Get nearest pixel(s) with value (not NODATA) to specified pixel.
void rtdealloc(void *mem)
rt_errorstate rt_raster_from_two_rasters(rt_raster rast1, rt_raster rast2, rt_extenttype extenttype, rt_raster *rtnraster, double *offset)
double rt_raster_get_y_scale(rt_raster raster)
Get scale Y in projection units.
rt_errorstate rt_raster_same_alignment(rt_raster rast1, rt_raster rast2, int *aligned, char **reason)
double rt_raster_get_y_skew(rt_raster raster)
Get skew about the Y axis.
uint16_t rt_band_get_height(rt_band band)
Return height of this band.
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
double rt_raster_get_y_offset(rt_raster raster)
Get raster y offset, in projection units.
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
This library is the generic raster handling section of PostGIS.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
rt_band rt_band_reclass(rt_band srcband, rt_pixtype pixtype, uint32_t hasnodata, double nodataval, rt_reclassexpr *exprset, int exprcount)
Returns new band with values reclassified.
static void _rti_colormap_arg_destroy(_rti_colormap_arg arg)
static int _rti_iterator_arg_callback_init(_rti_iterator_arg _param)
rt_raster rt_raster_colormap(rt_raster raster, int nband, rt_colormap colormap)
Returns a new raster with up to four 8BUI bands (RGBA) from applying a colormap to the user-specified...
static int _rti_iterator_arg_empty_init(_rti_iterator_arg _param)
struct _rti_colormap_arg_t * _rti_colormap_arg
static _rti_iterator_arg _rti_iterator_arg_init()
struct _rti_iterator_arg_t * _rti_iterator_arg
static int _rti_iterator_arg_populate(_rti_iterator_arg _param, rt_iterator itrset, uint16_t itrcount, uint16_t distancex, uint16_t distancey, int *allnull, int *allempty)
rt_errorstate rt_raster_iterator(rt_iterator itrset, uint16_t itrcount, rt_extenttype extenttype, rt_raster customextent, rt_pixtype pixtype, uint8_t hasnodata, double nodataval, uint16_t distancex, uint16_t distancey, rt_mask mask, void *userarg, int(*callback)(rt_iterator_arg arg, void *userarg, double *value, int *nodata), rt_raster *rtnraster)
n-raster iterator.
rt_band rt_band_reclass_exact(rt_band srcband, rt_reclassmap map, uint32_t hasnodata, double nodataval)
Returns new band with values reclassified.
static void _rti_iterator_arg_callback_clean(_rti_iterator_arg _param)
static int rt_classpair_cmp(const void *aptr, const void *bptr)
static void _rti_iterator_arg_destroy(_rti_iterator_arg _param)
static double rt_band_reclass_round_integer(rt_pixtype pixtype, double nv)
static _rti_colormap_arg _rti_colormap_arg_init(rt_raster raster)
rt_colormap_entry nodataentry
struct _rti_iterator_arg_t::@17 distance
struct _rti_iterator_arg_t::@16 band
struct _rti_iterator_arg_t::@18 dimension
struct _rti_iterator_arg_t::@19 empty
enum rt_colormap_t::@13 method
struct rt_reclassexpr_t::rt_reclassrange src
struct rt_reclassexpr_t::rt_reclassrange dst
struct rt_classpair_t * pairs