PostGIS  2.2.7dev-r@@SVN_REVISION@@
Datum RASTER_valueCount ( PG_FUNCTION_ARGS  )

Definition at line 2406 of file rtpg_statistics.c.

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, and genraster::value.

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