2404 {
2405 FuncCallContext *funcctx;
2406 TupleDesc tupdesc;
2407
2408 int i;
2411 int call_cntr;
2412 int max_calls;
2413
2414
2415 if (SRF_IS_FIRSTCALL()) {
2416 MemoryContext oldcontext;
2417
2421 int32_t bandindex = 0;
2422 int num_bands = 0;
2423 bool exclude_nodata_value =
TRUE;
2424 double *search_values = NULL;
2425 uint32_t search_values_count = 0;
2426 double roundto = 0;
2428
2429 int j;
2430 int n;
2431
2432 ArrayType *array;
2433 Oid etype;
2434 Datum *e;
2435 bool *nulls;
2436 int16 typlen;
2437 bool typbyval;
2438 char typalign;
2439
2440
2441 funcctx = SRF_FIRSTCALL_INIT();
2442
2443
2444 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
2445
2446
2447 if (PG_ARGISNULL(0)) {
2448 MemoryContextSwitchTo(oldcontext);
2449 SRF_RETURN_DONE(funcctx);
2450 }
2451 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
2452
2454 if (!raster) {
2455 PG_FREE_IF_COPY(pgraster, 0);
2456 MemoryContextSwitchTo(oldcontext);
2457 elog(ERROR, "RASTER_valueCount: Cannot deserialize raster");
2458 SRF_RETURN_DONE(funcctx);
2459 }
2460
2461
2462 bandindex = PG_GETARG_INT32(1);
2464 if (bandindex < 1 || bandindex > num_bands) {
2465 elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
2467 PG_FREE_IF_COPY(pgraster, 0);
2468 MemoryContextSwitchTo(oldcontext);
2469 SRF_RETURN_DONE(funcctx);
2470 }
2471
2472
2473 if (!PG_ARGISNULL(2))
2474 exclude_nodata_value = PG_GETARG_BOOL(2);
2475
2476
2477 if (!PG_ARGISNULL(3)) {
2478 array = PG_GETARG_ARRAYTYPE_P(3);
2479 etype = ARR_ELEMTYPE(array);
2480 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
2481
2482 switch (etype) {
2483 case FLOAT4OID:
2484 case FLOAT8OID:
2485 break;
2486 default:
2488 PG_FREE_IF_COPY(pgraster, 0);
2489 MemoryContextSwitchTo(oldcontext);
2490 elog(ERROR, "RASTER_valueCount: Invalid data type for values");
2491 SRF_RETURN_DONE(funcctx);
2492 break;
2493 }
2494
2495 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
2496 &nulls, &n);
2497
2498 search_values = palloc(sizeof(double) * n);
2499 for (i = 0, j = 0; i < n; i++) {
2500 if (nulls[i]) continue;
2501
2502 switch (etype) {
2503 case FLOAT4OID:
2504 search_values[j] = (double) DatumGetFloat4(e[i]);
2505 break;
2506 case FLOAT8OID:
2507 search_values[j] = (double) DatumGetFloat8(e[i]);
2508 break;
2509 }
2510
2512 j++;
2513 }
2514 search_values_count = j;
2515
2516 if (j < 1) {
2517 pfree(search_values);
2518 search_values = NULL;
2519 }
2520 }
2521
2522
2523 if (!PG_ARGISNULL(4)) {
2524 roundto = PG_GETARG_FLOAT8(4);
2525 if (roundto < 0.) roundto = 0;
2526 }
2527
2528
2530 if (!band) {
2531 elog(NOTICE, "Cannot find band at index %d. Returning NULL", bandindex);
2533 PG_FREE_IF_COPY(pgraster, 0);
2534 MemoryContextSwitchTo(oldcontext);
2535 SRF_RETURN_DONE(funcctx);
2536 }
2537
2538
2539 vcnts =
rt_band_get_value_count(band, (
int) exclude_nodata_value, search_values, search_values_count, roundto, NULL, &count);
2542 PG_FREE_IF_COPY(pgraster, 0);
2543 if (NULL == vcnts || !count) {
2544 elog(NOTICE, "Cannot count the values for band at index %d", bandindex);
2545 MemoryContextSwitchTo(oldcontext);
2546 SRF_RETURN_DONE(funcctx);
2547 }
2548
2550
2551
2552 funcctx->user_fctx = vcnts;
2553
2554
2555 funcctx->max_calls =
count;
2556
2557
2558 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
2559 ereport(ERROR, (
2560 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2561 errmsg(
2562 "function returning record called in context "
2563 "that cannot accept type record"
2564 )
2565 ));
2566 }
2567
2568 BlessTupleDesc(tupdesc);
2569 funcctx->tuple_desc = tupdesc;
2570
2571 MemoryContextSwitchTo(oldcontext);
2572 }
2573
2574
2575 funcctx = SRF_PERCALL_SETUP();
2576
2577 call_cntr = funcctx->call_cntr;
2578 max_calls = funcctx->max_calls;
2579 tupdesc = funcctx->tuple_desc;
2580 vcnts2 = funcctx->user_fctx;
2581
2582
2583 if (call_cntr < max_calls) {
2586 HeapTuple tuple;
2587 Datum result;
2588
2590
2592
2593 values[0] = Float8GetDatum(vcnts2[call_cntr].value);
2594 values[1] = UInt32GetDatum(vcnts2[call_cntr].count);
2595 values[2] = Float8GetDatum(vcnts2[call_cntr].percent);
2596
2597
2598 tuple = heap_form_tuple(tupdesc, values, nulls);
2599
2600
2601 result = HeapTupleGetDatum(tuple);
2602
2603 SRF_RETURN_NEXT(funcctx, result);
2604 }
2605
2606 else {
2607 pfree(vcnts2);
2608 SRF_RETURN_DONE(funcctx);
2609 }
2610}
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,...)