PostGIS  2.5.0beta2dev-r@@SVN_REVISION@@

◆ RASTER_valueCount()

Datum RASTER_valueCount ( PG_FUNCTION_ARGS  )

Definition at line 2396 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, and genraster::value.

Referenced by RASTER_quantileCoverage().

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