204{
205 FuncCallContext *funcctx;
206 TupleDesc tupdesc;
207 int call_cntr;
208 int max_calls;
209 int i = 0;
212 int z = 0;
213
214 int16 typlen;
215 bool typbyval;
216 char typalign;
217
220
221
222 if (SRF_IS_FIRSTCALL()) {
223 MemoryContext oldcontext;
227 int numbands = 0;
228 int j = 0;
229 bool exclude_nodata_value =
TRUE;
230
231 ArrayType *array;
232 Oid etype;
233 Datum *e;
234 bool *nulls;
235
236 double val = 0;
237 int isnodata = 0;
238
240
241
242 funcctx = SRF_FIRSTCALL_INIT();
243
244
245 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
246
247
248 if (PG_ARGISNULL(0)) {
249 MemoryContextSwitchTo(oldcontext);
250 SRF_RETURN_DONE(funcctx);
251 }
252 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
253
255 if (!raster) {
256 PG_FREE_IF_COPY(pgraster, 0);
257 ereport(ERROR, (
258 errcode(ERRCODE_OUT_OF_MEMORY),
259 errmsg("Could not deserialize raster")
260 ));
261 MemoryContextSwitchTo(oldcontext);
262 SRF_RETURN_DONE(funcctx);
263 }
264
265
266
267
268
269
270
271
272
273
274
275
276
278 if (!numbands) {
279 elog(NOTICE, "Raster provided has no bands");
281 PG_FREE_IF_COPY(pgraster, 0);
282 MemoryContextSwitchTo(oldcontext);
283 SRF_RETURN_DONE(funcctx);
284 }
285
286
288 if (arg1 == NULL) {
290 PG_FREE_IF_COPY(pgraster, 0);
291 MemoryContextSwitchTo(oldcontext);
292 elog(ERROR, "RASTER_dumpValues: Could not initialize argument structure");
293 SRF_RETURN_DONE(funcctx);
294 }
295
296
297 if (!PG_ARGISNULL(1)) {
298 array = PG_GETARG_ARRAYTYPE_P(1);
299 etype = ARR_ELEMTYPE(array);
300 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
301
302 switch (etype) {
303 case INT2OID:
304 case INT4OID:
305 break;
306 default:
309 PG_FREE_IF_COPY(pgraster, 0);
310 MemoryContextSwitchTo(oldcontext);
311 elog(ERROR, "RASTER_dumpValues: Invalid data type for band indexes");
312 SRF_RETURN_DONE(funcctx);
313 break;
314 }
315
316 deconstruct_array(array, etype, typlen, typbyval, typalign, &e, &nulls, &(arg1->
numbands));
317
319 if (arg1->
nbands == NULL) {
322 PG_FREE_IF_COPY(pgraster, 0);
323 MemoryContextSwitchTo(oldcontext);
324 elog(ERROR, "RASTER_dumpValues: Could not allocate memory for band indexes");
325 SRF_RETURN_DONE(funcctx);
326 }
327
328 for (i = 0, j = 0; i < arg1->
numbands; i++) {
329 if (nulls[i]) continue;
330
331 switch (etype) {
332 case INT2OID:
333 arg1->
nbands[j] = DatumGetInt16(e[i]) - 1;
334 break;
335 case INT4OID:
336 arg1->
nbands[j] = DatumGetInt32(e[i]) - 1;
337 break;
338 }
339
340 j++;
341 }
342
343 if (j < arg1->numbands) {
345 if (arg1->
nbands == NULL) {
348 PG_FREE_IF_COPY(pgraster, 0);
349 MemoryContextSwitchTo(oldcontext);
350 elog(ERROR, "RASTER_dumpValues: Could not reallocate memory for band indexes");
351 SRF_RETURN_DONE(funcctx);
352 }
353
355 }
356
357
358 for (i = 0; i < arg1->
numbands; i++) {
360 elog(NOTICE,
"Band at index %d not found in raster", arg1->
nbands[i] + 1);
363 PG_FREE_IF_COPY(pgraster, 0);
364 MemoryContextSwitchTo(oldcontext);
365 SRF_RETURN_DONE(funcctx);
366 }
367 }
368
369 }
370
371 else {
374
375 if (arg1->
nbands == NULL) {
378 PG_FREE_IF_COPY(pgraster, 0);
379 MemoryContextSwitchTo(oldcontext);
380 elog(ERROR, "RASTER_dumpValues: Could not allocate memory for band indexes");
381 SRF_RETURN_DONE(funcctx);
382 }
383
384 for (i = 0; i < arg1->
numbands; i++) {
387 }
388 }
389
392
393
394 if (!PG_ARGISNULL(2))
395 exclude_nodata_value = PG_GETARG_BOOL(2);
397
398
404 PG_FREE_IF_COPY(pgraster, 0);
405 MemoryContextSwitchTo(oldcontext);
406 elog(ERROR, "RASTER_dumpValues: Could not allocate memory for pixel values");
407 SRF_RETURN_DONE(funcctx);
408 }
411
412
413 for (z = 0; z < arg1->
numbands; z++) {
414
416 break;
417
419 if (!band) {
423 PG_FREE_IF_COPY(pgraster, 0);
424 MemoryContextSwitchTo(oldcontext);
425 elog(ERROR, "RASTER_dumpValues: Could not get band at index %d", nband);
426 SRF_RETURN_DONE(funcctx);
427 }
428
429
432 if (arg1->
values[z] == NULL || arg1->
nodata[z] == NULL) {
435 PG_FREE_IF_COPY(pgraster, 0);
436 MemoryContextSwitchTo(oldcontext);
437 elog(ERROR, "RASTER_dumpValues: Could not allocate memory for pixel values");
438 SRF_RETURN_DONE(funcctx);
439 }
442
443 i = 0;
444
445
447 for (i = (arg1->
rows * arg1->
columns) - 1; i >= 0; i--)
449 continue;
450 }
451
452 for (y = 0;
y < arg1->
rows;
y++) {
454
459 PG_FREE_IF_COPY(pgraster, 0);
460 MemoryContextSwitchTo(oldcontext);
461 elog(ERROR, "RASTER_dumpValues: Could not pixel (%d, %d) of band %d", x, y, nband);
462 SRF_RETURN_DONE(funcctx);
463 }
464
465 arg1->
values[z][i] = Float8GetDatum(val);
468
469 if (exclude_nodata_value && isnodata) {
472 }
473 else
475
476 i++;
477 }
478 }
479 }
480
481
483 PG_FREE_IF_COPY(pgraster, 0);
484
485
486 funcctx->user_fctx = arg1;
487
488
489 funcctx->max_calls = arg1->
numbands;
490
491
492 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
493 MemoryContextSwitchTo(oldcontext);
494 ereport(ERROR, (
495 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
496 errmsg(
497 "function returning record called in context "
498 "that cannot accept type record"
499 )
500 ));
501 }
502
503 BlessTupleDesc(tupdesc);
504 funcctx->tuple_desc = tupdesc;
505
506 MemoryContextSwitchTo(oldcontext);
507 }
508
509
510 funcctx = SRF_PERCALL_SETUP();
511
512 call_cntr = funcctx->call_cntr;
513 max_calls = funcctx->max_calls;
514 tupdesc = funcctx->tuple_desc;
515 arg2 = funcctx->user_fctx;
516
517
518 if (call_cntr < max_calls) {
521 HeapTuple tuple;
522 Datum result;
523 ArrayType *mdValues = NULL;
524 int ndim = 2;
526 int lbound[2] = {1, 1};
527
530
532
533 values[0] = Int32GetDatum(arg2->
nbands[call_cntr] + 1);
534
535
536 get_typlenbyvalalign(FLOAT8OID, &typlen, &typbyval, &typalign);
537
538
539 if (arg2->
values[call_cntr] == NULL)
540 ndim = 0;
541
542
543 mdValues = construct_md_array(
545 ndim, dim, lbound,
546 FLOAT8OID,
547 typlen, typbyval, typalign
548 );
549 values[1] = PointerGetDatum(mdValues);
550
551
552 tuple = heap_form_tuple(tupdesc, values, nulls);
553 result = HeapTupleGetDatum(tuple);
554
555 SRF_RETURN_NEXT(funcctx, result);
556 }
557
558 else {
560 SRF_RETURN_DONE(funcctx);
561 }
562}
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.
int rt_raster_has_band(rt_raster raster, int nband)
Return TRUE if the raster has a band of this number.
uint16_t rt_raster_get_num_bands(rt_raster raster)
uint16_t rt_raster_get_height(rt_raster raster)
uint16_t rt_raster_get_width(rt_raster raster)
int rt_band_clamped_value_is_nodata(rt_band band, double val)
Compare clamped value to band's clamped NODATA value.
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
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.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
static void rtpg_dumpvalues_arg_destroy(rtpg_dumpvalues_arg arg)
static rtpg_dumpvalues_arg rtpg_dumpvalues_arg_init()
#define POSTGIS_RT_DEBUG(level, msg)
#define POSTGIS_RT_DEBUGF(level, msg,...)