PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ RASTER_valueCount()

Datum RASTER_valueCount ( PG_FUNCTION_ARGS  )

Definition at line 2404 of file rtpg_statistics.c.

2404 {
2405 FuncCallContext *funcctx;
2406 TupleDesc tupdesc;
2407
2408 int i;
2409 rt_valuecount vcnts;
2410 rt_valuecount vcnts2;
2411 int call_cntr;
2412 int max_calls;
2413
2414 /* first call of function */
2415 if (SRF_IS_FIRSTCALL()) {
2416 MemoryContext oldcontext;
2417
2418 rt_pgraster *pgraster = NULL;
2419 rt_raster raster = NULL;
2420 rt_band band = NULL;
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;
2427 uint32_t count;
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 /* create a function context for cross-call persistence */
2441 funcctx = SRF_FIRSTCALL_INIT();
2442
2443 /* switch to memory context appropriate for multiple function calls */
2444 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
2445
2446 /* pgraster is null, return nothing */
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
2453 raster = rt_raster_deserialize(pgraster, FALSE);
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 /* band index is 1-based */
2462 bandindex = PG_GETARG_INT32(1);
2463 num_bands = rt_raster_get_num_bands(raster);
2464 if (bandindex < 1 || bandindex > num_bands) {
2465 elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
2466 rt_raster_destroy(raster);
2467 PG_FREE_IF_COPY(pgraster, 0);
2468 MemoryContextSwitchTo(oldcontext);
2469 SRF_RETURN_DONE(funcctx);
2470 }
2471
2472 /* exclude_nodata_value flag */
2473 if (!PG_ARGISNULL(2))
2474 exclude_nodata_value = PG_GETARG_BOOL(2);
2475
2476 /* search values */
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:
2487 rt_raster_destroy(raster);
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
2511 POSTGIS_RT_DEBUGF(5, "search_values[%d] = %f", j, search_values[j]);
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 /* roundto */
2523 if (!PG_ARGISNULL(4)) {
2524 roundto = PG_GETARG_FLOAT8(4);
2525 if (roundto < 0.) roundto = 0;
2526 }
2527
2528 /* get band */
2529 band = rt_raster_get_band(raster, bandindex - 1);
2530 if (!band) {
2531 elog(NOTICE, "Cannot find band at index %d. Returning NULL", bandindex);
2532 rt_raster_destroy(raster);
2533 PG_FREE_IF_COPY(pgraster, 0);
2534 MemoryContextSwitchTo(oldcontext);
2535 SRF_RETURN_DONE(funcctx);
2536 }
2537
2538 /* get counts of values */
2539 vcnts = rt_band_get_value_count(band, (int) exclude_nodata_value, search_values, search_values_count, roundto, NULL, &count);
2540 rt_band_destroy(band);
2541 rt_raster_destroy(raster);
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
2549 POSTGIS_RT_DEBUGF(3, "%d value counts returned", count);
2550
2551 /* Store needed information */
2552 funcctx->user_fctx = vcnts;
2553
2554 /* total number of tuples to be returned */
2555 funcctx->max_calls = count;
2556
2557 /* Build a tuple descriptor for our result type */
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 /* stuff done on every call of the function */
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 /* do when there is more left to send */
2583 if (call_cntr < max_calls) {
2584 Datum values[VALUES_LENGTH];
2585 bool nulls[VALUES_LENGTH];
2586 HeapTuple tuple;
2587 Datum result;
2588
2589 POSTGIS_RT_DEBUGF(3, "Result %d", call_cntr);
2590
2591 memset(nulls, FALSE, sizeof(bool) * VALUES_LENGTH);
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 /* build a tuple */
2598 tuple = heap_form_tuple(tupdesc, values, nulls);
2599
2600 /* make the tuple into a datum */
2601 result = HeapTupleGetDatum(tuple);
2602
2603 SRF_RETURN_NEXT(funcctx, result);
2604 }
2605 /* do when there is no more left */
2606 else {
2607 pfree(vcnts2);
2608 SRF_RETURN_DONE(funcctx);
2609 }
2610}
#define TRUE
Definition dbfopen.c:169
#define FALSE
Definition dbfopen.c:168
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition rt_raster.c:82
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition rt_band.c:340
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition rt_raster.c:372
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.
Definition rt_raster.c:381
int count
Definition genraster.py:57
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition rtrowdump.py:121
#define VALUES_LENGTH
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition rtpostgis.h:65
Struct definitions.
Definition librtcore.h:2251

References quantile_llist::count, FALSE, POSTGIS_RT_DEBUGF, rt_band_destroy(), rt_band_get_value_count(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_band(), rt_raster_get_num_bands(), TRUE, and VALUES_LENGTH.

Here is the call graph for this function: