#include "gdal_vrt.h"
/******************************************************************************
- * Some rules for *.(c|h) files in rt_core
- *
- * All functions in rt_core that receive a band index parameter
- * must be 0-based
- *****************************************************************************/
+* Some rules for *.(c|h) files in rt_core
+*
+* All functions in rt_core that receive a band index parameter
+* must be 0-based
+*
+* Variables and functions that are interal to *.c should be prefixed
+* with _rti_, e.g. _rti_iterator_arg.
+******************************************************************************/
/*--- Utilities -------------------------------------------------*/
return NULL;
}
-typedef struct _rti_param_t *_rti_param;
-struct _rti_param_t {
+/******************************************************************************
+* rt_raster_iterator()
+******************************************************************************/
+
+typedef struct _rti_iterator_arg_t *_rti_iterator_arg;
+struct _rti_iterator_arg_t {
int count;
rt_raster *raster;
rt_iterator_arg arg;
};
-static _rti_param
-_rti_param_init() {
- _rti_param _param;
+static _rti_iterator_arg
+_rti_iterator_arg_init() {
+ _rti_iterator_arg _param;
- _param = rtalloc(sizeof(struct _rti_param_t));
+ _param = rtalloc(sizeof(struct _rti_iterator_arg_t));
if (_param == NULL) {
- rterror("_rti_param_init: Unable to allocate memory for _rti_param");
+ rterror("_rti_iterator_arg_init: Unable to allocate memory for _rti_iterator_arg");
return NULL;
}
}
static void
-_rti_param_destroy(_rti_param _param) {
+_rti_iterator_arg_destroy(_rti_iterator_arg _param) {
int i = 0;
if (_param->raster != NULL)
}
static int
-_rti_param_populate(
- _rti_param _param,
+_rti_iterator_arg_populate(
+ _rti_iterator_arg _param,
rt_iterator itrset, uint16_t itrcount,
uint16_t distancex, uint16_t distancey,
int *allnull, int *allempty
_param->band.nodataval == NULL ||
_param->band.minval == NULL
) {
- rterror("_rti_param_populate: Unable to allocate memory for children of _rti_param");
+ rterror("_rti_iterator_arg_populate: Unable to allocate memory for children of _rti_iterator_arg");
return 0;
}
hasband = rt_raster_has_band(itrset[i].raster, itrset[i].nband);
if (!hasband) {
if (!itrset[i].nbnodata) {
- rterror("_rti_param_populate: Band %d not found for raster %d", itrset[i].nband, i);
+ rterror("_rti_iterator_arg_populate: Band %d not found for raster %d", itrset[i].nband, i);
return 0;
}
else {
if (hasband) {
_param->band.rtband[i] = rt_raster_get_band(itrset[i].raster, itrset[i].nband);
if (_param->band.rtband[i] == NULL) {
- rterror("_rti_param_populate: Unable to get band %d for raster %d", itrset[i].nband, i);
+ rterror("_rti_iterator_arg_populate: Unable to get band %d for raster %d", itrset[i].nband, i);
return 0;
}
/* init offset */
_param->offset[i] = rtalloc(sizeof(double) * 2);
if (_param->offset[i] == NULL) {
- rterror("_rti_param_populate: Unable to allocate memory for offsets");
+ rterror("_rti_iterator_arg_populate: Unable to allocate memory for offsets");
return 0;
}
}
}
static int
-_rti_param_empty_init(_rti_param _param) {
+_rti_iterator_arg_empty_init(_rti_iterator_arg _param) {
int x = 0;
int y = 0;
_param->empty.values = rtalloc(sizeof(double *) * _param->dimension.rows);
_param->empty.nodata = rtalloc(sizeof(int *) * _param->dimension.rows);
if (_param->empty.values == NULL || _param->empty.nodata == NULL) {
- rterror("_rti_param_empty_init: Unable to allocate memory for empty values and NODATA");
+ rterror("_rti_iterator_arg_empty_init: Unable to allocate memory for empty values and NODATA");
return 0;
}
_param->empty.nodata[y] = rtalloc(sizeof(int) * _param->dimension.columns);
if (_param->empty.values[y] == NULL || _param->empty.nodata[y] == NULL) {
- rterror("_rti_param_empty_init: Unable to allocate memory for elements of empty values and NODATA");
+ rterror("_rti_iterator_arg_empty_init: Unable to allocate memory for elements of empty values and NODATA");
return 0;
}
}
static int
-_rti_param_arg_init(_rti_param _param) {
+_rti_iterator_arg_callback_init(_rti_iterator_arg _param) {
int i = 0;
_param->arg = rtalloc(sizeof(struct rt_iterator_arg_t));
if (_param->arg == NULL) {
- rterror("_rti_param_arg_init: Unable to allocate memory for rt_iterator_arg");
+ rterror("_rti_iterator_arg_callback_init: Unable to allocate memory for rt_iterator_arg");
return 0;
}
_param->arg->nodata = rtalloc(sizeof(int **) * _param->count);
_param->arg->src_pixel = rtalloc(sizeof(int *) * _param->count);
if (_param->arg->values == NULL || _param->arg->nodata == NULL || _param->arg->src_pixel == NULL) {
- rterror("_rti_param_arg_init: Unable to allocate memory for element of rt_iterator_arg");
+ rterror("_rti_iterator_arg_callback_init: Unable to allocate memory for element of rt_iterator_arg");
return 0;
}
memset(_param->arg->values, 0, sizeof(double **) * _param->count);
_param->arg->src_pixel[i] = rtalloc(sizeof(int) * 2);
if (_param->arg->src_pixel[i] == NULL) {
- rterror("_rti_param_arg_init: Unable to allocate memory for position elements of rt_iterator_arg");
+ rterror("_rti_iterator_arg_callback_init: Unable to allocate memory for position elements of rt_iterator_arg");
return 0;
}
memset(_param->arg->src_pixel[i], 0, sizeof(int) * 2);
}
static void
-_rti_param_arg_clean(_rti_param _param) {
+_rti_iterator_arg_callback_clean(_rti_iterator_arg _param) {
int i = 0;
int y = 0;
/* working raster */
rt_raster rast = NULL;
- _rti_param _param = NULL;
+ _rti_iterator_arg _param = NULL;
int allnull = 0;
int allempty = 0;
int aligned = 0;
return NULL;
}
- /* initialize _rti_param */
- if ((_param = _rti_param_init()) == NULL) {
+ /* initialize _param */
+ if ((_param = _rti_iterator_arg_init()) == NULL) {
rterror("rt_raster_iterator: Unable to initialize internal variables");
return NULL;
}
/* fill _param */
- if (!_rti_param_populate(_param, itrset, itrcount, distancex, distancey, &allnull, &allempty)) {
+ if (!_rti_iterator_arg_populate(_param, itrset, itrcount, distancex, distancey, &allnull, &allempty)) {
rterror("rt_raster_iterator: Unable to populate for internal variables");
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
return NULL;
}
if (allnull == itrcount) {
RASTER_DEBUG(3, "all rasters are NULL, returning NULL");
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
*noerr = 1;
return NULL;
else if (allempty == itrcount) {
RASTER_DEBUG(3, "all rasters are empty, returning empty raster");
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
rtnrast = rt_raster_new(0, 0);
if (rtnrast == NULL) {
if (rast == NULL) {
rterror("rt_raster_iterator: Could not find reference raster to use for alignment tests");
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
return NULL;
}
if (!rt_raster_same_alignment(rast, customextent, &aligned)) {
rterror("rt_raster_iterator: Unable to test for alignment between reference raster and custom extent");
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
return NULL;
}
if (!rt_raster_same_alignment(rast, _param->raster[i], &aligned)) {
rterror("rt_raster_iterator: Unable to test for alignment between reference raster and raster %d", i);
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
return NULL;
}
if (!aligned) {
rterror("rt_raster_iterator: The set of rasters provided (custom extent included, if appropriate) do not have the same alignment");
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
return NULL;
}
if (rtnrast == NULL) {
rterror("rt_raster_iterator: Unable to allocate memory for output raster");
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
return NULL;
}
extenttype == ET_UNION ? "union" : "intersection"
);
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
return NULL;
}
extenttype == ET_UNION ? "union" : "intersection"
);
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
*noerr = 1;
return rast;
(i == 0 ? "FIRST" : (i == 1 ? "SECOND" : "LAST"))
);
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
*noerr = 1;
return NULL;
(i == 0 ? "FIRST" : (i == 1 ? "SECOND" : "LAST"))
);
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
rtnrast = rt_raster_new(0, 0);
if (rtnrast == NULL) {
if (rtnrast == NULL) {
rterror("rt_raster_iterator: Unable to allocate memory for output raster");
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
return NULL;
}
);
/* init values and NODATA for use with empty rasters */
- if (!_rti_param_empty_init(_param)) {
+ if (!_rti_iterator_arg_empty_init(_param)) {
rterror("rt_raster_iterator: Unable to initialize empty values and NODATA");
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
rt_raster_destroy(rtnrast);
return NULL;
) < 0) {
rterror("rt_raster_iterator: Unable to add new band to output raster");
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
rt_raster_destroy(rtnrast);
return NULL;
if (rtnband == NULL) {
rterror("rt_raster_iterator: Unable to get new band from output raster");
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
rt_raster_destroy(rtnrast);
return NULL;
minval = rt_band_get_min_value(rtnband);
/* initialize argument for callback function */
- if (!_rti_param_arg_init(_param)) {
+ if (!_rti_iterator_arg_callback_init(_param)) {
rterror("rt_raster_iterator: Unable to initialize callback function argument");
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
rt_band_destroy(rtnband);
rt_raster_destroy(rtnrast);
if (!status) {
rterror("rt_raster_iterator: Unable to compute raster offsets");
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
rt_band_destroy(rtnband);
rt_raster_destroy(rtnrast);
if (status < 0) {
rterror("rt_raster_iterator: Unable to get pixel neighborhood");
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
rt_band_destroy(rtnband);
rt_raster_destroy(rtnrast);
) < 0) {
rterror("rt_raster_iterator: Unable to get the pixel value of band");
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
rt_band_destroy(rtnband);
rt_raster_destroy(rtnrast);
if (npixels == NULL) {
rterror("rt_raster_iterator: Unable to reallocate memory for neighborhood");
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
rt_band_destroy(rtnband);
rt_raster_destroy(rtnrast);
if (!status) {
rterror("rt_raster_iterator: Unable to create 2D array of neighborhood");
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
rt_band_destroy(rtnband);
rt_raster_destroy(rtnrast);
nodata = 0;
status = callback(_param->arg, userarg, &value, &nodata);
- /* free memory */
- _rti_param_arg_clean(_param);
+ /* free memory from callback */
+ _rti_iterator_arg_callback_clean(_param);
/* handle callback status */
if (status == 0) {
rterror("rt_raster_iterator: Callback function returned an error");
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
rt_band_destroy(rtnband);
rt_raster_destroy(rtnrast);
if (status < 0) {
rterror("rt_raster_iterator: Unable to set pixel value");
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
rt_band_destroy(rtnband);
rt_raster_destroy(rtnrast);
}
/* lots of cleanup */
- _rti_param_destroy(_param);
+ _rti_iterator_arg_destroy(_param);
*noerr = 1;
return rtnrast;