475 FuncCallContext *funcctx;
480 struct bandmetadata {
491 struct bandmetadata *bmd = NULL;
492 struct bandmetadata *bmd2 = NULL;
497 if (SRF_IS_FIRSTCALL()) {
498 MemoryContext oldcontext;
518 const char *chartmp = NULL;
525 funcctx = SRF_FIRSTCALL_INIT();
528 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
531 if (PG_ARGISNULL(0)) {
532 MemoryContextSwitchTo(oldcontext);
533 SRF_RETURN_DONE(funcctx);
535 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
540 PG_FREE_IF_COPY(pgraster, 0);
541 MemoryContextSwitchTo(oldcontext);
542 elog(ERROR,
"RASTER_bandmetadata: Could not deserialize raster");
543 SRF_RETURN_DONE(funcctx);
549 elog(NOTICE,
"Raster provided has no bands");
551 PG_FREE_IF_COPY(pgraster, 0);
552 MemoryContextSwitchTo(oldcontext);
553 SRF_RETURN_DONE(funcctx);
557 array = PG_GETARG_ARRAYTYPE_P(1);
558 etype = ARR_ELEMTYPE(array);
559 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
567 PG_FREE_IF_COPY(pgraster, 0);
568 MemoryContextSwitchTo(oldcontext);
569 elog(ERROR,
"RASTER_bandmetadata: Invalid data type for band number(s)");
570 SRF_RETURN_DONE(funcctx);
574 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
577 bandNums = palloc(
sizeof(
uint32_t) * n);
578 for (i = 0, j = 0; i < n; i++) {
579 if (nulls[i])
continue;
583 idx = (
uint32_t) DatumGetInt16(e[i]);
586 idx = (
uint32_t) DatumGetInt32(e[i]);
591 if (idx > numBands || idx < 1) {
592 elog(NOTICE,
"Invalid band index: %d. Indices must be 1-based. Returning NULL", idx);
595 PG_FREE_IF_COPY(pgraster, 0);
596 MemoryContextSwitchTo(oldcontext);
597 SRF_RETURN_DONE(funcctx);
607 bandNums = repalloc(bandNums,
sizeof(
uint32_t) * j);
608 for (i = 0; i < j; i++)
612 bandNums = repalloc(bandNums,
sizeof(
uint32_t) * j);
614 bmd = (
struct bandmetadata *) palloc(
sizeof(
struct bandmetadata) * j);
616 for (i = 0; i < j; i++) {
619 elog(NOTICE,
"Could not get raster band at index %d", bandNums[i]);
621 PG_FREE_IF_COPY(pgraster, 0);
622 MemoryContextSwitchTo(oldcontext);
623 SRF_RETURN_DONE(funcctx);
627 bmd[i].bandnum = bandNums[i];
631 charlen = strlen(chartmp) + 1;
632 bmd[i].pixeltype = palloc(
sizeof(
char) * charlen);
633 strncpy(bmd[i].pixeltype, chartmp, charlen);
637 bmd[i].hasnodata =
TRUE;
639 bmd[i].hasnodata =
FALSE;
642 if (bmd[i].hasnodata)
645 bmd[i].nodataval = 0;
650 charlen = strlen(chartmp) + 1;
651 bmd[i].bandpath = palloc(
sizeof(
char) * charlen);
652 strncpy(bmd[i].bandpath, chartmp, charlen);
655 bmd[i].bandpath = NULL;
658 bmd[i].isoutdb = bmd[i].bandpath ?
TRUE :
FALSE;
662 bmd[i].extbandnum = extbandnum + 1;
664 bmd[i].extbandnum = 0;
667 bmd[i].timestamp = 0;
670 if( VSIStatL(bmd[i].bandpath, &sStat) == 0 ) {
671 bmd[i].filesize = sStat.st_size;
672 bmd[i].timestamp = sStat.st_mtime;
680 PG_FREE_IF_COPY(pgraster, 0);
683 funcctx->user_fctx = bmd;
686 funcctx->max_calls = j;
689 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
690 MemoryContextSwitchTo(oldcontext);
692 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
694 "function returning record called in context "
695 "that cannot accept type record"
700 BlessTupleDesc(tupdesc);
701 funcctx->tuple_desc = tupdesc;
703 MemoryContextSwitchTo(oldcontext);
707 funcctx = SRF_PERCALL_SETUP();
709 call_cntr = funcctx->call_cntr;
710 max_calls = funcctx->max_calls;
711 tupdesc = funcctx->tuple_desc;
712 bmd2 = funcctx->user_fctx;
715 if (call_cntr < max_calls) {
721 values[0] = UInt32GetDatum(bmd2[call_cntr].bandnum);
722 values[1] = CStringGetTextDatum(bmd2[call_cntr].pixeltype);
724 if (bmd2[call_cntr].hasnodata)
725 values[2] = Float8GetDatum(bmd2[call_cntr].nodataval);
729 values[3] = BoolGetDatum(bmd2[call_cntr].isoutdb);
730 if (bmd2[call_cntr].bandpath && strlen(bmd2[call_cntr].bandpath)) {
731 values[4] = CStringGetTextDatum(bmd2[call_cntr].bandpath);
732 values[5] = UInt32GetDatum(bmd2[call_cntr].extbandnum);
739 if (bmd2[call_cntr].filesize) {
740 #if POSTGIS_PGSQL_VERSION > 95
741 values[6] = UInt64GetDatum(bmd2[call_cntr].filesize);
742 values[7] = UInt64GetDatum(bmd2[call_cntr].timestamp);
744 values[6] = Int64GetDatum(bmd2[call_cntr].filesize);
745 values[7] = Int64GetDatum(bmd2[call_cntr].timestamp);
754 tuple = heap_form_tuple(tupdesc, values, nulls);
757 result = HeapTupleGetDatum(tuple);
760 pfree(bmd2[call_cntr].pixeltype);
761 if (bmd2[call_cntr].bandpath) pfree(bmd2[call_cntr].bandpath);
763 SRF_RETURN_NEXT(funcctx, result);
768 SRF_RETURN_DONE(funcctx);
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,...)