2112 MemoryContext aggcontext;
2113 MemoryContext oldcontext;
2123 int isempty[2] = {0};
2124 int hasband[2] = {0};
2126 double _offset[4] = {0.};
2134 char *utypename = NULL;
2138 double nodataval = 0;
2144 uint16_t _dim[2] = {0};
2151 if (!AggCheckCallContext(fcinfo, &aggcontext)) {
2152 elog(ERROR,
"RASTER_union_transfn: Cannot be called in a non-aggregate context");
2157 oldcontext = MemoryContextSwitchTo(aggcontext);
2159 if (PG_ARGISNULL(0)) {
2164 MemoryContextSwitchTo(oldcontext);
2165 elog(ERROR,
"RASTER_union_transfn: Could not allocate memory for state variable");
2181 if (!PG_ARGISNULL(1)) {
2183 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
2187 if (raster == NULL) {
2190 PG_FREE_IF_COPY(pgraster, 1);
2192 MemoryContextSwitchTo(oldcontext);
2193 elog(ERROR,
"RASTER_union_transfn: Could not deserialize raster");
2206 if (!PG_ARGISNULL(2)) {
2207 Oid calltype = get_fn_expr_argtype(fcinfo->flinfo, 2);
2218 utypename = text_to_cstring(PG_GETARG_TEXT_P(2));
2242 if (numband > idx) {
2258 if (raster != NULL) {
2260 PG_FREE_IF_COPY(pgraster, 1);
2263 MemoryContextSwitchTo(oldcontext);
2264 elog(ERROR,
"RASTER_union_transfn: Could not allocate memory for band information");
2269 for (i = idx; i < iwr->
numband; i++) {
2293 nband = PG_GETARG_INT32(2);
2297 if (raster != NULL) {
2299 PG_FREE_IF_COPY(pgraster, 1);
2302 MemoryContextSwitchTo(oldcontext);
2303 elog(ERROR,
"RASTER_union_transfn: Band number must be greater than zero (1-based)");
2312 if (raster != NULL) {
2314 PG_FREE_IF_COPY(pgraster, 1);
2317 MemoryContextSwitchTo(oldcontext);
2318 elog(ERROR,
"RASTER_union_transfn: Could not allocate memory for band information");
2337 if (raster != NULL) {
2339 PG_FREE_IF_COPY(pgraster, 1);
2342 MemoryContextSwitchTo(oldcontext);
2343 elog(ERROR,
"RASTER_union_transfn: Could not process unionarg");
2352 if (nargs > 3 && !PG_ARGISNULL(3)) {
2353 utypename = text_to_cstring(PG_GETARG_TEXT_P(3));
2367 for (i = 0; i < iwr->
numband; i++) {
2380 if (raster != NULL) {
2382 PG_FREE_IF_COPY(pgraster, 1);
2385 MemoryContextSwitchTo(oldcontext);
2386 elog(ERROR,
"RASTER_union_transfn: Could not allocate memory for working raster(s)");
2399 if (raster != NULL) {
2401 PG_FREE_IF_COPY(pgraster, 1);
2404 MemoryContextSwitchTo(oldcontext);
2405 elog(ERROR,
"RASTER_union_transfn: Could not create working raster");
2420 if (raster != NULL) {
2422 PG_FREE_IF_COPY(pgraster, 1);
2425 MemoryContextSwitchTo(oldcontext);
2426 elog(ERROR,
"RASTER_union_transfn: Could not check and balance number of bands");
2433 if (itrset == NULL) {
2436 if (raster != NULL) {
2438 PG_FREE_IF_COPY(pgraster, 1);
2441 MemoryContextSwitchTo(oldcontext);
2442 elog(ERROR,
"RASTER_union_transfn: Could not allocate memory for iterator arguments");
2447 for (i = 0; i < iwr->
numband; i++) {
2467 if (!isempty[0] && hasband[0])
2469 else if (!isempty[1] && hasband[1])
2476 if (_band != NULL) {
2514 itrset[0].
nband = 0;
2530 if (!isempty[0] && !isempty[1] && hasband[0] && hasband[1]) {
2542 if (raster != NULL) {
2544 PG_FREE_IF_COPY(pgraster, 1);
2547 MemoryContextSwitchTo(oldcontext);
2548 elog(ERROR,
"RASTER_union_transfn: Could not create internal raster");
2552 _offset[0], _offset[1], _offset[2], _offset[3]);
2559 double igt[6] = {0};
2575 hasnodata, nodataval,
2582 if (raster != NULL) {
2584 PG_FREE_IF_COPY(pgraster, 1);
2587 MemoryContextSwitchTo(oldcontext);
2588 elog(ERROR,
"RASTER_union_transfn: Could not add new band to internal raster");
2596 for (y = 0; y < _dim[1]; y++) {
2597 POSTGIS_RT_DEBUGF(4,
"Getting pixel line of working raster at (x, y, length) = (0, %d, %d)", y, _dim[0]);
2609 if (raster != NULL) {
2611 PG_FREE_IF_COPY(pgraster, 1);
2614 MemoryContextSwitchTo(oldcontext);
2615 elog(ERROR,
"RASTER_union_transfn: Could not get pixel line from band of working raster");
2619 POSTGIS_RT_DEBUGF(4,
"Setting pixel line at (x, y, length) = (%d, %d, %d)", (
int) _offset[0], (
int) _offset[1] + y, nvals);
2622 (
int) _offset[0], (
int) _offset[1] + y,
2630 if (raster != NULL) {
2632 PG_FREE_IF_COPY(pgraster, 1);
2635 MemoryContextSwitchTo(oldcontext);
2636 elog(ERROR,
"RASTER_union_transfn: Could not set pixel line to band of internal raster");
2652 hasnodata, nodataval,
2667 if (raster != NULL) {
2669 PG_FREE_IF_COPY(pgraster, 1);
2672 MemoryContextSwitchTo(oldcontext);
2673 elog(ERROR,
"RASTER_union_transfn: Could not run raster iterator function");
2681 for (y = 0; y < _dim[1]; y++) {
2682 POSTGIS_RT_DEBUGF(4,
"Getting pixel line of iterator raster at (x, y, length) = (0, %d, %d)", y, _dim[0]);
2696 if (raster != NULL) {
2698 PG_FREE_IF_COPY(pgraster, 1);
2701 MemoryContextSwitchTo(oldcontext);
2702 elog(ERROR,
"RASTER_union_transfn: Could not get pixel line from band of working raster");
2706 POSTGIS_RT_DEBUGF(4,
"Setting pixel line at (x, y, length) = (%d, %d, %d)", (
int) _offset[2], (
int) _offset[3] + y, nvals);
2709 (
int) _offset[2], (
int) _offset[3] + y,
2719 if (raster != NULL) {
2721 PG_FREE_IF_COPY(pgraster, 1);
2724 MemoryContextSwitchTo(oldcontext);
2725 elog(ERROR,
"RASTER_union_transfn: Could not set pixel line to band of internal raster");
2745 hasnodata, nodataval,
2757 if (raster != NULL) {
2759 PG_FREE_IF_COPY(pgraster, 1);
2762 MemoryContextSwitchTo(oldcontext);
2763 elog(ERROR,
"RASTER_union_transfn: Could not run raster iterator function");
2780 if (raster != NULL) {
2782 PG_FREE_IF_COPY(pgraster, 1);
2786 MemoryContextSwitchTo(oldcontext);
2790 PG_RETURN_POINTER(iwr);
int rt_raster_get_num_bands(rt_raster raster)
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
rtpg_union_band_arg bandarg
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.
static int rtpg_union_unionarg_process(rtpg_union_arg arg, ArrayType *array)
rt_errorstate rt_raster_from_two_rasters(rt_raster rast1, rt_raster rast2, rt_extenttype extenttype, rt_raster *rtnraster, double *offset)
int rt_util_same_geotransform_matrix(double *gt1, double *gt2)
void rt_band_destroy(rt_band band)
Destroy a raster band.
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
#define POSTGIS_RT_DEBUGF(level, msg,...)
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
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.
rt_errorstate rt_band_get_pixel_line(rt_band band, int x, int y, uint16_t len, void **vals, uint16_t *nvals)
Get values of multiple pixels.
rt_raster rt_raster_clone(rt_raster raster, uint8_t deep)
Clone an existing raster.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
static int rtpg_union_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
int rt_raster_has_band(rt_raster raster, int nband)
Return TRUE if the raster has a band of this number.
double rt_pixtype_get_min_value(rt_pixtype pixtype)
Return minimum value possible for pixel type.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
static int rtpg_union_noarg(rtpg_union_arg arg, rt_raster raster)
static void rtpg_union_arg_destroy(rtpg_union_arg arg)
static rtpg_union_type rtpg_uniontype_index_from_name(const char *cutype)
uint16_t rt_raster_get_width(rt_raster raster)
double rt_band_get_min_value(rt_band band)
Returns the minimal possible value for the band according to the pixel type.
const char * rt_pixtype_name(rt_pixtype pixtype)
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_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
rt_errorstate rt_band_set_pixel_line(rt_band band, int x, int y, void *vals, uint32_t len)
Set values of multiple pixels.
struct rtpg_union_arg_t * rtpg_union_arg
rtpg_union_type uniontype
uint16_t rt_raster_get_height(rt_raster raster)
#define POSTGIS_RT_DEBUG(level, msg)
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
char * rtpg_strtoupper(char *str)