PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ RASTER_valueCount()

Datum RASTER_valueCount ( PG_FUNCTION_ARGS  )

Definition at line 2410 of file rtpg_statistics.c.

References ovdump::band, genraster::count, rtpg_summarystats_arg_t::exclude_nodata_value, FALSE, PG_FUNCTION_INFO_V1(), POSTGIS_RT_DEBUGF, rtrowdump::raster, RASTER_valueCountCoverage(), rt_band_destroy(), rt_band_get_value_count(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_band(), rt_raster_get_num_bands(), TRUE, genraster::value, and VALUES_LENGTH.

2410  {
2411  FuncCallContext *funcctx;
2412  TupleDesc tupdesc;
2413 
2414  int i;
2415  rt_valuecount vcnts;
2416  rt_valuecount vcnts2;
2417  int call_cntr;
2418  int max_calls;
2419 
2420  /* first call of function */
2421  if (SRF_IS_FIRSTCALL()) {
2422  MemoryContext oldcontext;
2423 
2424  rt_pgraster *pgraster = NULL;
2425  rt_raster raster = NULL;
2426  rt_band band = NULL;
2427  int32_t bandindex = 0;
2428  int num_bands = 0;
2429  bool exclude_nodata_value = TRUE;
2430  double *search_values = NULL;
2431  uint32_t search_values_count = 0;
2432  double roundto = 0;
2433  uint32_t count;
2434 
2435  int j;
2436  int n;
2437 
2438  ArrayType *array;
2439  Oid etype;
2440  Datum *e;
2441  bool *nulls;
2442  int16 typlen;
2443  bool typbyval;
2444  char typalign;
2445 
2446  /* create a function context for cross-call persistence */
2447  funcctx = SRF_FIRSTCALL_INIT();
2448 
2449  /* switch to memory context appropriate for multiple function calls */
2450  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
2451 
2452  /* pgraster is null, return nothing */
2453  if (PG_ARGISNULL(0)) {
2454  MemoryContextSwitchTo(oldcontext);
2455  SRF_RETURN_DONE(funcctx);
2456  }
2457  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
2458 
2459  raster = rt_raster_deserialize(pgraster, FALSE);
2460  if (!raster) {
2461  PG_FREE_IF_COPY(pgraster, 0);
2462  MemoryContextSwitchTo(oldcontext);
2463  elog(ERROR, "RASTER_valueCount: Cannot deserialize raster");
2464  SRF_RETURN_DONE(funcctx);
2465  }
2466 
2467  /* band index is 1-based */
2468  bandindex = PG_GETARG_INT32(1);
2469  num_bands = rt_raster_get_num_bands(raster);
2470  if (bandindex < 1 || bandindex > num_bands) {
2471  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
2472  rt_raster_destroy(raster);
2473  PG_FREE_IF_COPY(pgraster, 0);
2474  MemoryContextSwitchTo(oldcontext);
2475  SRF_RETURN_DONE(funcctx);
2476  }
2477 
2478  /* exclude_nodata_value flag */
2479  if (!PG_ARGISNULL(2))
2480  exclude_nodata_value = PG_GETARG_BOOL(2);
2481 
2482  /* search values */
2483  if (!PG_ARGISNULL(3)) {
2484  array = PG_GETARG_ARRAYTYPE_P(3);
2485  etype = ARR_ELEMTYPE(array);
2486  get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
2487 
2488  switch (etype) {
2489  case FLOAT4OID:
2490  case FLOAT8OID:
2491  break;
2492  default:
2493  rt_raster_destroy(raster);
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);
2498  break;
2499  }
2500 
2501  deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
2502  &nulls, &n);
2503 
2504  search_values = palloc(sizeof(double) * n);
2505  for (i = 0, j = 0; i < n; i++) {
2506  if (nulls[i]) continue;
2507 
2508  switch (etype) {
2509  case FLOAT4OID:
2510  search_values[j] = (double) DatumGetFloat4(e[i]);
2511  break;
2512  case FLOAT8OID:
2513  search_values[j] = (double) DatumGetFloat8(e[i]);
2514  break;
2515  }
2516 
2517  POSTGIS_RT_DEBUGF(5, "search_values[%d] = %f", j, search_values[j]);
2518  j++;
2519  }
2520  search_values_count = j;
2521 
2522  if (j < 1) {
2523  pfree(search_values);
2524  search_values = NULL;
2525  }
2526  }
2527 
2528  /* roundto */
2529  if (!PG_ARGISNULL(4)) {
2530  roundto = PG_GETARG_FLOAT8(4);
2531  if (roundto < 0.) roundto = 0;
2532  }
2533 
2534  /* get band */
2535  band = rt_raster_get_band(raster, bandindex - 1);
2536  if (!band) {
2537  elog(NOTICE, "Cannot find band at index %d. Returning NULL", bandindex);
2538  rt_raster_destroy(raster);
2539  PG_FREE_IF_COPY(pgraster, 0);
2540  MemoryContextSwitchTo(oldcontext);
2541  SRF_RETURN_DONE(funcctx);
2542  }
2543 
2544  /* get counts of values */
2545  vcnts = rt_band_get_value_count(band, (int) exclude_nodata_value, search_values, search_values_count, roundto, NULL, &count);
2546  rt_band_destroy(band);
2547  rt_raster_destroy(raster);
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);
2553  }
2554 
2555  POSTGIS_RT_DEBUGF(3, "%d value counts returned", count);
2556 
2557  /* Store needed information */
2558  funcctx->user_fctx = vcnts;
2559 
2560  /* total number of tuples to be returned */
2561  funcctx->max_calls = count;
2562 
2563  /* Build a tuple descriptor for our result type */
2564  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
2565  ereport(ERROR, (
2566  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2567  errmsg(
2568  "function returning record called in context "
2569  "that cannot accept type record"
2570  )
2571  ));
2572  }
2573 
2574  BlessTupleDesc(tupdesc);
2575  funcctx->tuple_desc = tupdesc;
2576 
2577  MemoryContextSwitchTo(oldcontext);
2578  }
2579 
2580  /* stuff done on every call of the function */
2581  funcctx = SRF_PERCALL_SETUP();
2582 
2583  call_cntr = funcctx->call_cntr;
2584  max_calls = funcctx->max_calls;
2585  tupdesc = funcctx->tuple_desc;
2586  vcnts2 = funcctx->user_fctx;
2587 
2588  /* do when there is more left to send */
2589  if (call_cntr < max_calls) {
2590  Datum values[VALUES_LENGTH];
2591  bool nulls[VALUES_LENGTH];
2592  HeapTuple tuple;
2593  Datum result;
2594 
2595  POSTGIS_RT_DEBUGF(3, "Result %d", call_cntr);
2596 
2597  memset(nulls, FALSE, sizeof(bool) * VALUES_LENGTH);
2598 
2599  values[0] = Float8GetDatum(vcnts2[call_cntr].value);
2600  values[1] = UInt32GetDatum(vcnts2[call_cntr].count);
2601  values[2] = Float8GetDatum(vcnts2[call_cntr].percent);
2602 
2603  /* build a tuple */
2604  tuple = heap_form_tuple(tupdesc, values, nulls);
2605 
2606  /* make the tuple into a datum */
2607  result = HeapTupleGetDatum(tuple);
2608 
2609  SRF_RETURN_NEXT(funcctx, result);
2610  }
2611  /* do when there is no more left */
2612  else {
2613  pfree(vcnts2);
2614  SRF_RETURN_DONE(funcctx);
2615  }
2616 }
#define VALUES_LENGTH
int rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
raster
Be careful!! Zeros function&#39;s input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
band
Definition: ovdump.py:57
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition: rt_band.c:242
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:65
unsigned int uint32_t
Definition: uthash.h:78
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:56
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
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.
#define FALSE
Definition: dbfopen.c:168
Struct definitions.
Definition: librtcore.h:2201
int value
Definition: genraster.py:61
#define TRUE
Definition: dbfopen.c:169
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:717
Here is the call graph for this function: