473 FuncCallContext *funcctx;
478 struct bandmetadata {
490 struct bandmetadata *bmd = NULL;
491 struct bandmetadata *bmd2 = NULL;
496 if (SRF_IS_FIRSTCALL()) {
497 MemoryContext oldcontext;
516 uint32_t *bandNums = NULL;
517 const char *chartmp = NULL;
524 funcctx = SRF_FIRSTCALL_INIT();
527 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
530 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
531 MemoryContextSwitchTo(oldcontext);
533 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
535 "function returning record called in context "
536 "that cannot accept type record"
541 BlessTupleDesc(tupdesc);
542 funcctx->tuple_desc = tupdesc;
545 if (PG_ARGISNULL(0)) {
546 bmd = (
struct bandmetadata *) palloc(
sizeof(
struct bandmetadata));
547 bmd->isnullband =
TRUE;
548 funcctx->user_fctx = bmd;
549 funcctx->max_calls = 1;
550 MemoryContextSwitchTo(oldcontext);
553 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
558 PG_FREE_IF_COPY(pgraster, 0);
559 MemoryContextSwitchTo(oldcontext);
560 elog(ERROR,
"RASTER_bandmetadata: Could not deserialize raster");
561 SRF_RETURN_DONE(funcctx);
567 elog(NOTICE,
"Raster provided has no bands");
569 PG_FREE_IF_COPY(pgraster, 0);
570 bmd = (
struct bandmetadata *) palloc(
sizeof(
struct bandmetadata));
571 bmd->isnullband =
TRUE;
572 funcctx->user_fctx = bmd;
573 funcctx->max_calls = 1;
574 MemoryContextSwitchTo(oldcontext);
579 array = PG_GETARG_ARRAYTYPE_P(1);
580 etype = ARR_ELEMTYPE(array);
581 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
589 PG_FREE_IF_COPY(pgraster, 0);
590 MemoryContextSwitchTo(oldcontext);
591 elog(ERROR,
"RASTER_bandmetadata: Invalid data type for band number(s)");
592 SRF_RETURN_DONE(funcctx);
596 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
599 bandNums = palloc(
sizeof(uint32_t) * n);
600 for (i = 0, j = 0; i < n; i++) {
601 if (nulls[i])
continue;
605 idx = (uint32_t) DatumGetInt16(e[i]);
608 idx = (uint32_t) DatumGetInt32(e[i]);
613 if (idx > numBands || idx < 1) {
614 elog(NOTICE,
"Invalid band index: %d. Indices must be 1-based. Returning NULL", idx);
617 PG_FREE_IF_COPY(pgraster, 0);
618 bmd = (
struct bandmetadata *) palloc(
sizeof(
struct bandmetadata));
619 bmd->isnullband =
TRUE;
620 funcctx->user_fctx = bmd;
621 funcctx->max_calls = 1;
622 MemoryContextSwitchTo(oldcontext);
633 bandNums = repalloc(bandNums,
sizeof(uint32_t) * j);
634 for (i = 0; i < j; i++)
638 bandNums = repalloc(bandNums,
sizeof(uint32_t) * j);
640 bmd = (
struct bandmetadata *) palloc0(
sizeof(
struct bandmetadata) * j);
642 for (i = 0; i < j; i++) {
645 elog(NOTICE,
"Could not get raster band at index %d", bandNums[i]);
647 PG_FREE_IF_COPY(pgraster, 0);
648 bmd[0].isnullband =
TRUE;
649 funcctx->user_fctx = bmd;
650 funcctx->max_calls = 1;
651 MemoryContextSwitchTo(oldcontext);
656 bmd[i].bandnum = bandNums[i];
660 charlen = strlen(chartmp) + 1;
661 bmd[i].pixeltype = palloc(
sizeof(
char) * charlen);
662 strncpy(bmd[i].pixeltype, chartmp, charlen);
666 bmd[i].hasnodata =
TRUE;
668 bmd[i].hasnodata =
FALSE;
671 if (bmd[i].hasnodata)
674 bmd[i].nodataval = 0;
679 charlen = strlen(chartmp) + 1;
680 bmd[i].bandpath = palloc(
sizeof(
char) * charlen);
681 strncpy(bmd[i].bandpath, chartmp, charlen);
684 bmd[i].bandpath = NULL;
687 bmd[i].isoutdb = bmd[i].bandpath ?
TRUE :
FALSE;
691 bmd[i].extbandnum = extbandnum + 1;
693 bmd[i].extbandnum = 0;
696 bmd[i].timestamp = 0;
699 if( VSIStatL(bmd[i].bandpath, &sStat) == 0 ) {
700 bmd[i].filesize = sStat.st_size;
701 bmd[i].timestamp = sStat.st_mtime;
709 PG_FREE_IF_COPY(pgraster, 0);
712 funcctx->user_fctx = bmd;
715 funcctx->max_calls = j;
717 MemoryContextSwitchTo(oldcontext);
723 funcctx = SRF_PERCALL_SETUP();
725 call_cntr = funcctx->call_cntr;
726 max_calls = funcctx->max_calls;
727 tupdesc = funcctx->tuple_desc;
728 bmd2 = funcctx->user_fctx;
731 if (call_cntr < max_calls) {
735 if (bmd2[0].isnullband) {
739 result = HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls));
740 SRF_RETURN_NEXT(funcctx,
result);
745 values[0] = UInt32GetDatum(bmd2[call_cntr].bandnum);
746 values[1] = CStringGetTextDatum(bmd2[call_cntr].pixeltype);
748 if (bmd2[call_cntr].hasnodata)
749 values[2] = Float8GetDatum(bmd2[call_cntr].nodataval);
753 values[3] = BoolGetDatum(bmd2[call_cntr].isoutdb);
754 if (bmd2[call_cntr].bandpath && strlen(bmd2[call_cntr].bandpath)) {
755 values[4] = CStringGetTextDatum(bmd2[call_cntr].bandpath);
756 values[5] = UInt32GetDatum(bmd2[call_cntr].extbandnum);
763 if (bmd2[call_cntr].filesize)
765 values[6] = UInt64GetDatum(bmd2[call_cntr].filesize);
766 values[7] = UInt64GetDatum(bmd2[call_cntr].timestamp);
775 tuple = heap_form_tuple(tupdesc, values, nulls);
778 result = HeapTupleGetDatum(tuple);
781 pfree(bmd2[call_cntr].pixeltype);
782 if (bmd2[call_cntr].bandpath) pfree(bmd2[call_cntr].bandpath);
784 SRF_RETURN_NEXT(funcctx,
result);
789 SRF_RETURN_DONE(funcctx);
char result[OUT_DOUBLE_BUFFER_SIZE]
const char * rt_band_get_ext_path(rt_band band)
Return band's external path (only valid when rt_band_is_offline returns non-zero).
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
void rt_band_destroy(rt_band band)
Destroy a raster band.
uint16_t rt_raster_get_num_bands(rt_raster raster)
rt_errorstate rt_band_get_ext_band_num(rt_band band, uint8_t *bandnum)
Return bands' external band number (only valid when rt_band_is_offline returns non-zero).
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
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.
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): ...
bool enable_outdb_rasters
#define POSTGIS_RT_DEBUG(level, msg)
#define POSTGIS_RT_DEBUGF(level, msg,...)