351 FuncCallContext *funcctx;
356 struct bandmetadata {
364 struct bandmetadata *bmd = NULL;
365 struct bandmetadata *bmd2 = NULL;
370 if (SRF_IS_FIRSTCALL()) {
371 MemoryContext oldcontext;
391 const char *tmp = NULL;
396 funcctx = SRF_FIRSTCALL_INIT();
399 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
402 if (PG_ARGISNULL(0)) {
403 MemoryContextSwitchTo(oldcontext);
404 SRF_RETURN_DONE(funcctx);
406 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
411 PG_FREE_IF_COPY(pgraster, 0);
412 MemoryContextSwitchTo(oldcontext);
413 elog(ERROR,
"RASTER_bandmetadata: Could not deserialize raster");
414 SRF_RETURN_DONE(funcctx);
420 elog(NOTICE,
"Raster provided has no bands");
422 PG_FREE_IF_COPY(pgraster, 0);
423 MemoryContextSwitchTo(oldcontext);
424 SRF_RETURN_DONE(funcctx);
428 array = PG_GETARG_ARRAYTYPE_P(1);
429 etype = ARR_ELEMTYPE(array);
430 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
438 PG_FREE_IF_COPY(pgraster, 0);
439 MemoryContextSwitchTo(oldcontext);
440 elog(ERROR,
"RASTER_bandmetadata: Invalid data type for band number(s)");
441 SRF_RETURN_DONE(funcctx);
445 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
448 bandNums = palloc(
sizeof(
uint32_t) * n);
449 for (i = 0, j = 0; i < n; i++) {
450 if (nulls[i])
continue;
454 idx = (
uint32_t) DatumGetInt16(e[i]);
457 idx = (
uint32_t) DatumGetInt32(e[i]);
462 if (idx > numBands || idx < 1) {
463 elog(NOTICE,
"Invalid band index: %d. Indices must be 1-based. Returning NULL", idx);
466 PG_FREE_IF_COPY(pgraster, 0);
467 MemoryContextSwitchTo(oldcontext);
468 SRF_RETURN_DONE(funcctx);
478 bandNums = repalloc(bandNums,
sizeof(
uint32_t) * j);
479 for (i = 0; i < j; i++)
483 bandNums = repalloc(bandNums,
sizeof(
uint32_t) * j);
485 bmd = (
struct bandmetadata *) palloc(
sizeof(
struct bandmetadata) * j);
487 for (i = 0; i < j; i++) {
490 elog(NOTICE,
"Could not get raster band at index %d", bandNums[i]);
492 PG_FREE_IF_COPY(pgraster, 0);
493 MemoryContextSwitchTo(oldcontext);
494 SRF_RETURN_DONE(funcctx);
498 bmd[i].bandnum = bandNums[i];
502 bmd[i].pixeltype = palloc(
sizeof(
char) * (strlen(tmp) + 1));
503 strncpy(bmd[i].pixeltype, tmp, strlen(tmp) + 1);
507 bmd[i].hasnodata =
TRUE;
509 bmd[i].hasnodata =
FALSE;
512 if (bmd[i].hasnodata)
515 bmd[i].nodataval = 0;
520 bmd[i].bandpath = palloc(
sizeof(
char) * (strlen(tmp) + 1));
521 strncpy(bmd[i].bandpath, tmp, strlen(tmp) + 1);
524 bmd[i].bandpath = NULL;
527 bmd[i].isoutdb = bmd[i].bandpath ?
TRUE :
FALSE;
533 PG_FREE_IF_COPY(pgraster, 0);
536 funcctx->user_fctx = bmd;
539 funcctx->max_calls = j;
542 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
543 MemoryContextSwitchTo(oldcontext);
545 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
547 "function returning record called in context " 548 "that cannot accept type record" 553 BlessTupleDesc(tupdesc);
554 funcctx->tuple_desc = tupdesc;
556 MemoryContextSwitchTo(oldcontext);
560 funcctx = SRF_PERCALL_SETUP();
562 call_cntr = funcctx->call_cntr;
563 max_calls = funcctx->max_calls;
564 tupdesc = funcctx->tuple_desc;
565 bmd2 = funcctx->user_fctx;
568 if (call_cntr < max_calls) {
574 values[0] = UInt32GetDatum(bmd2[call_cntr].bandnum);
575 values[1] = CStringGetTextDatum(bmd2[call_cntr].pixeltype);
577 if (bmd2[call_cntr].hasnodata)
578 values[2] = Float8GetDatum(bmd2[call_cntr].nodataval);
582 values[3] = BoolGetDatum(bmd2[call_cntr].isoutdb);
583 if (bmd2[call_cntr].bandpath && strlen(bmd2[call_cntr].bandpath))
584 values[4] = CStringGetTextDatum(bmd2[call_cntr].bandpath);
589 tuple = heap_form_tuple(tupdesc, values, nulls);
592 result = HeapTupleGetDatum(tuple);
595 pfree(bmd2[call_cntr].pixeltype);
596 if (bmd2[call_cntr].bandpath) pfree(bmd2[call_cntr].bandpath);
598 SRF_RETURN_NEXT(funcctx, result);
603 SRF_RETURN_DONE(funcctx);
int rt_raster_get_num_bands(rt_raster raster)
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
void rt_band_destroy(rt_band band)
Destroy a raster band.
#define POSTGIS_RT_DEBUGF(level, msg,...)
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
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).
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
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.
const char * rt_pixtype_name(rt_pixtype pixtype)
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
#define POSTGIS_RT_DEBUG(level, msg)
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.