127 int maxbandindex = 0;
129 int lastnumbands = 0;
131 text *text_pixtype = NULL;
132 char *char_pixtype = NULL;
142 struct addbandarg *arg = NULL;
162 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
167 PG_FREE_IF_COPY(pgraster, 0);
168 elog(ERROR,
"RASTER_addBand: Could not deserialize raster");
174 array = PG_GETARG_ARRAYTYPE_P(1);
175 etype = ARR_ELEMTYPE(array);
176 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
178 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
182 PG_FREE_IF_COPY(pgraster, 0);
183 elog(ERROR,
"RASTER_addBand: Invalid argument for addbandargset");
188 arg = (
struct addbandarg *) palloc(
sizeof(
struct addbandarg) * n);
191 PG_FREE_IF_COPY(pgraster, 0);
192 elog(ERROR,
"RASTER_addBand: Could not allocate memory for addbandarg");
202 for (i = 0; i < n; i++) {
203 if (nulls[i])
continue;
208 tup = (HeapTupleHeader) DatumGetPointer(e[i]);
212 PG_FREE_IF_COPY(pgraster, 0);
213 elog(ERROR,
"RASTER_addBand: Invalid argument for addbandargset");
219 arg[i].append =
TRUE;
220 tupv = GetAttributeByName(tup,
"index", &isnull);
222 arg[i].index = DatumGetInt32(tupv);
223 arg[i].append =
FALSE;
227 if (!arg[i].append && arg[i].index < 1) {
230 PG_FREE_IF_COPY(pgraster, 0);
231 elog(ERROR,
"RASTER_addBand: Invalid argument for addbandargset. Invalid band index (must be 1-based) for addbandarg of index %d", i);
237 tupv = GetAttributeByName(tup,
"pixeltype", &isnull);
241 PG_FREE_IF_COPY(pgraster, 0);
242 elog(ERROR,
"RASTER_addBand: Invalid argument for addbandargset. Pixel type cannot be NULL for addbandarg of index %d", i);
245 text_pixtype = (text *) DatumGetPointer(tupv);
246 if (text_pixtype == NULL) {
249 PG_FREE_IF_COPY(pgraster, 0);
250 elog(ERROR,
"RASTER_addBand: Invalid argument for addbandargset. Pixel type cannot be NULL for addbandarg of index %d", i);
257 if (arg[i].pixtype ==
PT_END) {
260 PG_FREE_IF_COPY(pgraster, 0);
261 elog(ERROR,
"RASTER_addBand: Invalid argument for addbandargset. Invalid pixel type for addbandarg of index %d", i);
266 arg[i].initialvalue = 0;
267 tupv = GetAttributeByName(tup,
"initialvalue", &isnull);
269 arg[i].initialvalue = DatumGetFloat8(tupv);
272 arg[i].hasnodata =
FALSE;
273 arg[i].nodatavalue = 0;
274 tupv = GetAttributeByName(tup,
"nodataval", &isnull);
276 arg[i].hasnodata =
TRUE;
277 arg[i].nodatavalue = DatumGetFloat8(tupv);
283 for (i = 0; i < n; i++) {
284 if (nulls[i])
continue;
287 maxbandindex = lastnumbands + 1;
290 if (!arg[i].append) {
291 if (arg[i].index > maxbandindex) {
292 elog(NOTICE,
"Band index for addbandarg of index %d exceeds possible value. Adding band at index %d", i, maxbandindex);
293 arg[i].index = maxbandindex;
298 arg[i].index = maxbandindex;
300 POSTGIS_RT_DEBUGF(4,
"new band (index, pixtype, initialvalue, hasnodata, nodatavalue) = (%d, %s, %f, %s, %f)",
304 arg[i].hasnodata ?
"TRUE" :
"FALSE",
310 arg[i].pixtype, arg[i].initialvalue,
311 arg[i].hasnodata, arg[i].nodatavalue,
316 if (numbands == lastnumbands || bandindex == -1) {
319 PG_FREE_IF_COPY(pgraster, 0);
320 elog(ERROR,
"RASTER_addBand: Could not add band defined by addbandarg of index %d to raster", i);
324 lastnumbands = numbands;
332 PG_FREE_IF_COPY(pgraster, 0);
336 SET_VARSIZE(pgrtn, pgrtn->
size);
337 PG_RETURN_POINTER(pgrtn);
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_pixtype rt_pixtype_index_from_name(const char *pixname)
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
uint16_t rt_raster_get_num_bands(rt_raster raster)
const char * rt_pixtype_name(rt_pixtype pixtype)
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
char * text_to_cstring(const text *textptr)
#define POSTGIS_RT_DEBUG(level, msg)
#define POSTGIS_RT_DEBUGF(level, msg,...)