2411 FuncCallContext *funcctx;
2421 if (SRF_IS_FIRSTCALL()) {
2422 MemoryContext oldcontext;
2427 int32_t bandindex = 0;
2429 bool exclude_nodata_value =
TRUE;
2430 double *search_values = NULL;
2447 funcctx = SRF_FIRSTCALL_INIT();
2450 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
2453 if (PG_ARGISNULL(0)) {
2454 MemoryContextSwitchTo(oldcontext);
2455 SRF_RETURN_DONE(funcctx);
2457 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
2461 PG_FREE_IF_COPY(pgraster, 0);
2462 MemoryContextSwitchTo(oldcontext);
2463 elog(ERROR,
"RASTER_valueCount: Cannot deserialize raster");
2464 SRF_RETURN_DONE(funcctx);
2468 bandindex = PG_GETARG_INT32(1);
2470 if (bandindex < 1 || bandindex > num_bands) {
2471 elog(NOTICE,
"Invalid band index (must use 1-based). Returning NULL");
2473 PG_FREE_IF_COPY(pgraster, 0);
2474 MemoryContextSwitchTo(oldcontext);
2475 SRF_RETURN_DONE(funcctx);
2479 if (!PG_ARGISNULL(2))
2480 exclude_nodata_value = PG_GETARG_BOOL(2);
2483 if (!PG_ARGISNULL(3)) {
2484 array = PG_GETARG_ARRAYTYPE_P(3);
2485 etype = ARR_ELEMTYPE(array);
2486 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
2494 PG_FREE_IF_COPY(pgraster, 0);
2495 MemoryContextSwitchTo(oldcontext);
2496 elog(ERROR,
"RASTER_valueCount: Invalid data type for values");
2497 SRF_RETURN_DONE(funcctx);
2501 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
2504 search_values = palloc(
sizeof(
double) * n);
2505 for (i = 0, j = 0; i < n; i++) {
2506 if (nulls[i])
continue;
2510 search_values[j] = (double) DatumGetFloat4(e[i]);
2513 search_values[j] = (double) DatumGetFloat8(e[i]);
2520 search_values_count = j;
2523 pfree(search_values);
2524 search_values = NULL;
2529 if (!PG_ARGISNULL(4)) {
2530 roundto = PG_GETARG_FLOAT8(4);
2531 if (roundto < 0.) roundto = 0;
2537 elog(NOTICE,
"Cannot find band at index %d. Returning NULL", bandindex);
2539 PG_FREE_IF_COPY(pgraster, 0);
2540 MemoryContextSwitchTo(oldcontext);
2541 SRF_RETURN_DONE(funcctx);
2545 vcnts =
rt_band_get_value_count(band, (
int) exclude_nodata_value, search_values, search_values_count, roundto, NULL, &count);
2548 PG_FREE_IF_COPY(pgraster, 0);
2549 if (NULL == vcnts || !count) {
2550 elog(NOTICE,
"Cannot count the values for band at index %d", bandindex);
2551 MemoryContextSwitchTo(oldcontext);
2552 SRF_RETURN_DONE(funcctx);
2558 funcctx->user_fctx = vcnts;
2561 funcctx->max_calls =
count;
2564 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
2566 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2568 "function returning record called in context " 2569 "that cannot accept type record" 2574 BlessTupleDesc(tupdesc);
2575 funcctx->tuple_desc = tupdesc;
2577 MemoryContextSwitchTo(oldcontext);
2581 funcctx = SRF_PERCALL_SETUP();
2583 call_cntr = funcctx->call_cntr;
2584 max_calls = funcctx->max_calls;
2585 tupdesc = funcctx->tuple_desc;
2586 vcnts2 = funcctx->user_fctx;
2589 if (call_cntr < max_calls) {
2599 values[0] = Float8GetDatum(vcnts2[call_cntr].
value);
2600 values[1] = UInt32GetDatum(vcnts2[call_cntr].count);
2601 values[2] = Float8GetDatum(vcnts2[call_cntr].percent);
2604 tuple = heap_form_tuple(tupdesc, values, nulls);
2607 result = HeapTupleGetDatum(tuple);
2609 SRF_RETURN_NEXT(funcctx, result);
2614 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_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
rt_valuecount rt_band_get_value_count(rt_band band, int exclude_nodata_value, double *search_values, uint32_t search_values_count, double roundto, uint32_t *rtn_total, uint32_t *rtn_count)
Count the number of times provided value(s) occur in the band.
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.