403{
404 FuncCallContext *funcctx;
405 TupleDesc tupdesc;
406 int call_cntr;
407 int max_calls;
408 int i = 0;
411 int z = 0;
412
413 int16 typlen;
414 bool typbyval;
415 char typalign;
416
419
420
421 if (SRF_IS_FIRSTCALL()) {
422 MemoryContext oldcontext;
426 int numbands = 0;
427 int j = 0;
428 bool exclude_nodata_value =
TRUE;
429
430 ArrayType *array;
431 Oid etype;
432 Datum *e;
433 bool *nulls;
434
435 double val = 0;
436 int isnodata = 0;
437
439
440
441 funcctx = SRF_FIRSTCALL_INIT();
442
443
444 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
445
446
447 if (PG_ARGISNULL(0)) {
448 MemoryContextSwitchTo(oldcontext);
449 SRF_RETURN_DONE(funcctx);
450 }
451 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
452
454 if (!raster) {
455 PG_FREE_IF_COPY(pgraster, 0);
456 ereport(ERROR, (
457 errcode(ERRCODE_OUT_OF_MEMORY),
458 errmsg("Could not deserialize raster")
459 ));
460 MemoryContextSwitchTo(oldcontext);
461 SRF_RETURN_DONE(funcctx);
462 }
463
464
465
466
467
468
469
470
471
472
473
474
475
477 if (!numbands) {
478 elog(NOTICE, "Raster provided has no bands");
480 PG_FREE_IF_COPY(pgraster, 0);
481 MemoryContextSwitchTo(oldcontext);
482 SRF_RETURN_DONE(funcctx);
483 }
484
485
487 if (arg1 == NULL) {
489 PG_FREE_IF_COPY(pgraster, 0);
490 MemoryContextSwitchTo(oldcontext);
491 elog(ERROR, "RASTER_dumpValues: Could not initialize argument structure");
492 SRF_RETURN_DONE(funcctx);
493 }
494
495
496 if (!PG_ARGISNULL(1)) {
497 array = PG_GETARG_ARRAYTYPE_P(1);
498 etype = ARR_ELEMTYPE(array);
499 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
500
501 switch (etype) {
502 case INT2OID:
503 case INT4OID:
504 break;
505 default:
508 PG_FREE_IF_COPY(pgraster, 0);
509 MemoryContextSwitchTo(oldcontext);
510 elog(ERROR, "RASTER_dumpValues: Invalid data type for band indexes");
511 SRF_RETURN_DONE(funcctx);
512 break;
513 }
514
515 deconstruct_array(array, etype, typlen, typbyval, typalign, &e, &nulls, &(arg1->
numbands));
516
518 if (arg1->
nbands == NULL) {
521 PG_FREE_IF_COPY(pgraster, 0);
522 MemoryContextSwitchTo(oldcontext);
523 elog(ERROR, "RASTER_dumpValues: Could not allocate memory for band indexes");
524 SRF_RETURN_DONE(funcctx);
525 }
526
527 for (i = 0, j = 0; i < arg1->
numbands; i++) {
528 if (nulls[i]) continue;
529
530 switch (etype) {
531 case INT2OID:
532 arg1->
nbands[j] = DatumGetInt16(e[i]) - 1;
533 break;
534 case INT4OID:
535 arg1->
nbands[j] = DatumGetInt32(e[i]) - 1;
536 break;
537 }
538
539 j++;
540 }
541
542 if (j < arg1->numbands) {
544 if (arg1->
nbands == NULL) {
547 PG_FREE_IF_COPY(pgraster, 0);
548 MemoryContextSwitchTo(oldcontext);
549 elog(ERROR, "RASTER_dumpValues: Could not reallocate memory for band indexes");
550 SRF_RETURN_DONE(funcctx);
551 }
552
554 }
555
556
557 for (i = 0; i < arg1->
numbands; i++) {
559 elog(NOTICE,
"Band at index %d not found in raster", arg1->
nbands[i] + 1);
562 PG_FREE_IF_COPY(pgraster, 0);
563 MemoryContextSwitchTo(oldcontext);
564 SRF_RETURN_DONE(funcctx);
565 }
566 }
567
568 }
569
570 else {
573
574 if (arg1->
nbands == NULL) {
577 PG_FREE_IF_COPY(pgraster, 0);
578 MemoryContextSwitchTo(oldcontext);
579 elog(ERROR, "RASTER_dumpValues: Could not allocate memory for band indexes");
580 SRF_RETURN_DONE(funcctx);
581 }
582
583 for (i = 0; i < arg1->
numbands; i++) {
586 }
587 }
588
591
592
593 if (!PG_ARGISNULL(2))
594 exclude_nodata_value = PG_GETARG_BOOL(2);
596
597
603 PG_FREE_IF_COPY(pgraster, 0);
604 MemoryContextSwitchTo(oldcontext);
605 elog(ERROR, "RASTER_dumpValues: Could not allocate memory for pixel values");
606 SRF_RETURN_DONE(funcctx);
607 }
610
611
612 for (z = 0; z < arg1->
numbands; z++) {
613
615 break;
616
618 if (!band) {
622 PG_FREE_IF_COPY(pgraster, 0);
623 MemoryContextSwitchTo(oldcontext);
624 elog(ERROR, "RASTER_dumpValues: Could not get band at index %d", nband);
625 SRF_RETURN_DONE(funcctx);
626 }
627
628
631 if (arg1->
values[z] == NULL || arg1->
nodata[z] == NULL) {
634 PG_FREE_IF_COPY(pgraster, 0);
635 MemoryContextSwitchTo(oldcontext);
636 elog(ERROR, "RASTER_dumpValues: Could not allocate memory for pixel values");
637 SRF_RETURN_DONE(funcctx);
638 }
641
642 i = 0;
643
644
646 for (i = (arg1->
rows * arg1->
columns) - 1; i >= 0; i--)
648 continue;
649 }
650
651 for (y = 0;
y < arg1->
rows;
y++) {
653
658 PG_FREE_IF_COPY(pgraster, 0);
659 MemoryContextSwitchTo(oldcontext);
660 elog(ERROR, "RASTER_dumpValues: Could not pixel (%d, %d) of band %d", x, y, nband);
661 SRF_RETURN_DONE(funcctx);
662 }
663
664 arg1->
values[z][i] = Float8GetDatum(val);
667
668 if (exclude_nodata_value && isnodata) {
671 }
672 else
674
675 i++;
676 }
677 }
678 }
679
680
682 PG_FREE_IF_COPY(pgraster, 0);
683
684
685 funcctx->user_fctx = arg1;
686
687
688 funcctx->max_calls = arg1->
numbands;
689
690
691 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
692 MemoryContextSwitchTo(oldcontext);
693 ereport(ERROR, (
694 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
695 errmsg(
696 "function returning record called in context "
697 "that cannot accept type record"
698 )
699 ));
700 }
701
702 BlessTupleDesc(tupdesc);
703 funcctx->tuple_desc = tupdesc;
704
705 MemoryContextSwitchTo(oldcontext);
706 }
707
708
709 funcctx = SRF_PERCALL_SETUP();
710
711 call_cntr = funcctx->call_cntr;
712 max_calls = funcctx->max_calls;
713 tupdesc = funcctx->tuple_desc;
714 arg2 = funcctx->user_fctx;
715
716
717 if (call_cntr < max_calls) {
720 HeapTuple tuple;
722 ArrayType *mdValues = NULL;
723 int ndim = 2;
725 int lbound[2] = {1, 1};
726
729
731
732 values[0] = Int32GetDatum(arg2->
nbands[call_cntr] + 1);
733
734
735 get_typlenbyvalalign(FLOAT8OID, &typlen, &typbyval, &typalign);
736
737
738 if (arg2->
values[call_cntr] == NULL)
739 ndim = 0;
740
741
742 mdValues = construct_md_array(
744 ndim, dim, lbound,
745 FLOAT8OID,
746 typlen, typbyval, typalign
747 );
748 values[1] = PointerGetDatum(mdValues);
749
750
751 tuple = heap_form_tuple(tupdesc, values, nulls);
752 result = HeapTupleGetDatum(tuple);
753
754 SRF_RETURN_NEXT(funcctx,
result);
755 }
756
757 else {
759 SRF_RETURN_DONE(funcctx);
760 }
761}
char result[OUT_DOUBLE_BUFFER_SIZE]
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,...)