2401 FuncCallContext *funcctx;
2411 if (SRF_IS_FIRSTCALL()) {
2412 MemoryContext oldcontext;
2417 int32_t bandindex = 0;
2419 bool exclude_nodata_value =
TRUE;
2420 double *search_values = NULL;
2437 funcctx = SRF_FIRSTCALL_INIT();
2440 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
2443 if (PG_ARGISNULL(0)) {
2444 MemoryContextSwitchTo(oldcontext);
2445 SRF_RETURN_DONE(funcctx);
2447 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
2451 PG_FREE_IF_COPY(pgraster, 0);
2452 MemoryContextSwitchTo(oldcontext);
2453 elog(ERROR,
"RASTER_valueCount: Cannot deserialize raster");
2454 SRF_RETURN_DONE(funcctx);
2458 bandindex = PG_GETARG_INT32(1);
2460 if (bandindex < 1 || bandindex > num_bands) {
2461 elog(NOTICE,
"Invalid band index (must use 1-based). Returning NULL");
2463 PG_FREE_IF_COPY(pgraster, 0);
2464 MemoryContextSwitchTo(oldcontext);
2465 SRF_RETURN_DONE(funcctx);
2469 if (!PG_ARGISNULL(2))
2470 exclude_nodata_value = PG_GETARG_BOOL(2);
2473 if (!PG_ARGISNULL(3)) {
2474 array = PG_GETARG_ARRAYTYPE_P(3);
2475 etype = ARR_ELEMTYPE(array);
2476 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
2484 PG_FREE_IF_COPY(pgraster, 0);
2485 MemoryContextSwitchTo(oldcontext);
2486 elog(ERROR,
"RASTER_valueCount: Invalid data type for values");
2487 SRF_RETURN_DONE(funcctx);
2491 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
2494 search_values = palloc(
sizeof(
double) * n);
2495 for (i = 0, j = 0; i < n; i++) {
2496 if (nulls[i])
continue;
2500 search_values[j] = (double) DatumGetFloat4(e[i]);
2503 search_values[j] = (double) DatumGetFloat8(e[i]);
2510 search_values_count = j;
2513 pfree(search_values);
2514 search_values = NULL;
2519 if (!PG_ARGISNULL(4)) {
2520 roundto = PG_GETARG_FLOAT8(4);
2521 if (roundto < 0.) roundto = 0;
2527 elog(NOTICE,
"Cannot find band at index %d. Returning NULL", bandindex);
2529 PG_FREE_IF_COPY(pgraster, 0);
2530 MemoryContextSwitchTo(oldcontext);
2531 SRF_RETURN_DONE(funcctx);
2538 PG_FREE_IF_COPY(pgraster, 0);
2539 if (NULL == vcnts || !
count) {
2540 elog(NOTICE,
"Cannot count the values for band at index %d", bandindex);
2541 MemoryContextSwitchTo(oldcontext);
2542 SRF_RETURN_DONE(funcctx);
2548 funcctx->user_fctx = vcnts;
2551 funcctx->max_calls =
count;
2554 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
2556 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2558 "function returning record called in context "
2559 "that cannot accept type record"
2564 BlessTupleDesc(tupdesc);
2565 funcctx->tuple_desc = tupdesc;
2567 MemoryContextSwitchTo(oldcontext);
2571 funcctx = SRF_PERCALL_SETUP();
2573 call_cntr = funcctx->call_cntr;
2574 max_calls = funcctx->max_calls;
2575 tupdesc = funcctx->tuple_desc;
2576 vcnts2 = funcctx->user_fctx;
2579 if (call_cntr < max_calls) {
2589 values[0] = Float8GetDatum(vcnts2[call_cntr].
value);
2590 values[1] = UInt32GetDatum(vcnts2[call_cntr].
count);
2591 values[2] = Float8GetDatum(vcnts2[call_cntr].percent);
2594 tuple = heap_form_tuple(tupdesc, values, nulls);
2597 result = HeapTupleGetDatum(tuple);
2599 SRF_RETURN_NEXT(funcctx, result);
2604 SRF_RETURN_DONE(funcctx);
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_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.
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): ...
#define POSTGIS_RT_DEBUGF(level, msg,...)