PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ RASTER_valueCount()

Datum RASTER_valueCount ( PG_FUNCTION_ARGS  )

Definition at line 2400 of file rtpg_statistics.c.

2400  {
2401  FuncCallContext *funcctx;
2402  TupleDesc tupdesc;
2403 
2404  int i;
2405  rt_valuecount vcnts;
2406  rt_valuecount vcnts2;
2407  int call_cntr;
2408  int max_calls;
2409 
2410  /* first call of function */
2411  if (SRF_IS_FIRSTCALL()) {
2412  MemoryContext oldcontext;
2413 
2414  rt_pgraster *pgraster = NULL;
2415  rt_raster raster = NULL;
2416  rt_band band = NULL;
2417  int32_t bandindex = 0;
2418  int num_bands = 0;
2419  bool exclude_nodata_value = TRUE;
2420  double *search_values = NULL;
2421  uint32_t search_values_count = 0;
2422  double roundto = 0;
2423  uint32_t count;
2424 
2425  int j;
2426  int n;
2427 
2428  ArrayType *array;
2429  Oid etype;
2430  Datum *e;
2431  bool *nulls;
2432  int16 typlen;
2433  bool typbyval;
2434  char typalign;
2435 
2436  /* create a function context for cross-call persistence */
2437  funcctx = SRF_FIRSTCALL_INIT();
2438 
2439  /* switch to memory context appropriate for multiple function calls */
2440  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
2441 
2442  /* pgraster is null, return nothing */
2443  if (PG_ARGISNULL(0)) {
2444  MemoryContextSwitchTo(oldcontext);
2445  SRF_RETURN_DONE(funcctx);
2446  }
2447  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
2448 
2449  raster = rt_raster_deserialize(pgraster, FALSE);
2450  if (!raster) {
2451  PG_FREE_IF_COPY(pgraster, 0);
2452  MemoryContextSwitchTo(oldcontext);
2453  elog(ERROR, "RASTER_valueCount: Cannot deserialize raster");
2454  SRF_RETURN_DONE(funcctx);
2455  }
2456 
2457  /* band index is 1-based */
2458  bandindex = PG_GETARG_INT32(1);
2459  num_bands = rt_raster_get_num_bands(raster);
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);
2466  }
2467 
2468  /* exclude_nodata_value flag */
2469  if (!PG_ARGISNULL(2))
2470  exclude_nodata_value = PG_GETARG_BOOL(2);
2471 
2472  /* search values */
2473  if (!PG_ARGISNULL(3)) {
2474  array = PG_GETARG_ARRAYTYPE_P(3);
2475  etype = ARR_ELEMTYPE(array);
2476  get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
2477 
2478  switch (etype) {
2479  case FLOAT4OID:
2480  case FLOAT8OID:
2481  break;
2482  default:
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);
2488  break;
2489  }
2490 
2491  deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
2492  &nulls, &n);
2493 
2494  search_values = palloc(sizeof(double) * n);
2495  for (i = 0, j = 0; i < n; i++) {
2496  if (nulls[i]) continue;
2497 
2498  switch (etype) {
2499  case FLOAT4OID:
2500  search_values[j] = (double) DatumGetFloat4(e[i]);
2501  break;
2502  case FLOAT8OID:
2503  search_values[j] = (double) DatumGetFloat8(e[i]);
2504  break;
2505  }
2506 
2507  POSTGIS_RT_DEBUGF(5, "search_values[%d] = %f", j, search_values[j]);
2508  j++;
2509  }
2510  search_values_count = j;
2511 
2512  if (j < 1) {
2513  pfree(search_values);
2514  search_values = NULL;
2515  }
2516  }
2517 
2518  /* roundto */
2519  if (!PG_ARGISNULL(4)) {
2520  roundto = PG_GETARG_FLOAT8(4);
2521  if (roundto < 0.) roundto = 0;
2522  }
2523 
2524  /* get band */
2525  band = rt_raster_get_band(raster, bandindex - 1);
2526  if (!band) {
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);
2532  }
2533 
2534  /* get counts of values */
2535  vcnts = rt_band_get_value_count(band, (int) exclude_nodata_value, search_values, search_values_count, roundto, NULL, &count);
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);
2543  }
2544 
2545  POSTGIS_RT_DEBUGF(3, "%d value counts returned", count);
2546 
2547  /* Store needed information */
2548  funcctx->user_fctx = vcnts;
2549 
2550  /* total number of tuples to be returned */
2551  funcctx->max_calls = count;
2552 
2553  /* Build a tuple descriptor for our result type */
2554  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
2555  ereport(ERROR, (
2556  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2557  errmsg(
2558  "function returning record called in context "
2559  "that cannot accept type record"
2560  )
2561  ));
2562  }
2563 
2564  BlessTupleDesc(tupdesc);
2565  funcctx->tuple_desc = tupdesc;
2566 
2567  MemoryContextSwitchTo(oldcontext);
2568  }
2569 
2570  /* stuff done on every call of the function */
2571  funcctx = SRF_PERCALL_SETUP();
2572 
2573  call_cntr = funcctx->call_cntr;
2574  max_calls = funcctx->max_calls;
2575  tupdesc = funcctx->tuple_desc;
2576  vcnts2 = funcctx->user_fctx;
2577 
2578  /* do when there is more left to send */
2579  if (call_cntr < max_calls) {
2580  Datum values[VALUES_LENGTH];
2581  bool nulls[VALUES_LENGTH];
2582  HeapTuple tuple;
2583  Datum result;
2584 
2585  POSTGIS_RT_DEBUGF(3, "Result %d", call_cntr);
2586 
2587  memset(nulls, FALSE, sizeof(bool) * VALUES_LENGTH);
2588 
2589  values[0] = Float8GetDatum(vcnts2[call_cntr].value);
2590  values[1] = UInt32GetDatum(vcnts2[call_cntr].count);
2591  values[2] = Float8GetDatum(vcnts2[call_cntr].percent);
2592 
2593  /* build a tuple */
2594  tuple = heap_form_tuple(tupdesc, values, nulls);
2595 
2596  /* make the tuple into a datum */
2597  result = HeapTupleGetDatum(tuple);
2598 
2599  SRF_RETURN_NEXT(funcctx, result);
2600  }
2601  /* do when there is no more left */
2602  else {
2603  pfree(vcnts2);
2604  SRF_RETURN_DONE(funcctx);
2605  }
2606 }
#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.
Definition: rt_serialize.c:725
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:381
int value
Definition: genraster.py:61
int count
Definition: genraster.py:56
band
Definition: ovdump.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:2250
unsigned int uint32_t
Definition: uthash.h:78

References ovdump::band, genraster::count, FALSE, POSTGIS_RT_DEBUGF, rtrowdump::raster, 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.

Here is the call graph for this function: