PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ RASTER_summaryStatsCoverage()

Datum RASTER_summaryStatsCoverage ( PG_FUNCTION_ARGS  )

Definition at line 198 of file rtpg_statistics.c.

References ovdump::band, rt_bandstats_t::count, FALSE, FLT_EQ, rt_bandstats_t::max, rt_bandstats_t::mean, rt_bandstats_t::min, rtrowdump::raster, rt_band_destroy(), rt_band_get_summary_stats(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_band(), rt_raster_get_num_bands(), rt_bandstats_t::sample, rt_bandstats_t::sorted, rtgdalraster::sql, rt_bandstats_t::stddev, rt_bandstats_t::sum, TRUE, rt_bandstats_t::values, and VALUES_LENGTH.

Referenced by RASTER_summaryStats().

199 {
200  text *tablenametext = NULL;
201  char *tablename = NULL;
202  text *colnametext = NULL;
203  char *colname = NULL;
204  int32_t bandindex = 1;
205  bool exclude_nodata_value = TRUE;
206  double sample = 0;
207 
208  int len = 0;
209  char *sql = NULL;
210  int spi_result;
211  Portal portal;
212  TupleDesc tupdesc;
213  SPITupleTable *tuptable = NULL;
214  HeapTuple tuple;
215  Datum datum;
216  bool isNull = FALSE;
217 
218  rt_pgraster *pgraster = NULL;
219  rt_raster raster = NULL;
220  rt_band band = NULL;
221  int num_bands = 0;
222  uint64_t cK = 0;
223  double cM = 0;
224  double cQ = 0;
225  rt_bandstats stats = NULL;
226  rt_bandstats rtn = NULL;
227 
228  Datum values[VALUES_LENGTH];
229  bool nulls[VALUES_LENGTH];
230  Datum result;
231 
232  /* tablename is null, return null */
233  if (PG_ARGISNULL(0)) {
234  elog(NOTICE, "Table name must be provided");
235  PG_RETURN_NULL();
236  }
237  tablenametext = PG_GETARG_TEXT_P(0);
238  tablename = text_to_cstring(tablenametext);
239  if (!strlen(tablename)) {
240  elog(NOTICE, "Table name must be provided");
241  PG_RETURN_NULL();
242  }
243 
244  /* column name is null, return null */
245  if (PG_ARGISNULL(1)) {
246  elog(NOTICE, "Column name must be provided");
247  PG_RETURN_NULL();
248  }
249  colnametext = PG_GETARG_TEXT_P(1);
250  colname = text_to_cstring(colnametext);
251  if (!strlen(colname)) {
252  elog(NOTICE, "Column name must be provided");
253  PG_RETURN_NULL();
254  }
255 
256  /* band index is 1-based */
257  if (!PG_ARGISNULL(2))
258  bandindex = PG_GETARG_INT32(2);
259 
260  /* exclude_nodata_value flag */
261  if (!PG_ARGISNULL(3))
262  exclude_nodata_value = PG_GETARG_BOOL(3);
263 
264  /* sample % */
265  if (!PG_ARGISNULL(4)) {
266  sample = PG_GETARG_FLOAT8(4);
267  if (sample < 0 || sample > 1) {
268  elog(NOTICE, "Invalid sample percentage (must be between 0 and 1). Returning NULL");
269  rt_raster_destroy(raster);
270  PG_RETURN_NULL();
271  }
272  else if (FLT_EQ(sample, 0.0))
273  sample = 1;
274  }
275  else
276  sample = 1;
277 
278  /* iterate through rasters of coverage */
279  /* connect to database */
280  spi_result = SPI_connect();
281  if (spi_result != SPI_OK_CONNECT) {
282  pfree(sql);
283  elog(ERROR, "RASTER_summaryStatsCoverage: Cannot connect to database using SPI");
284  PG_RETURN_NULL();
285  }
286 
287  /* create sql */
288  len = sizeof(char) * (strlen("SELECT \"\" FROM \"\" WHERE \"\" IS NOT NULL") + (strlen(colname) * 2) + strlen(tablename) + 1);
289  sql = (char *) palloc(len);
290  if (NULL == sql) {
291  if (SPI_tuptable) SPI_freetuptable(tuptable);
292  SPI_finish();
293  elog(ERROR, "RASTER_summaryStatsCoverage: Cannot allocate memory for sql");
294  PG_RETURN_NULL();
295  }
296 
297  /* get cursor */
298  snprintf(sql, len, "SELECT \"%s\" FROM \"%s\" WHERE \"%s\" IS NOT NULL", colname, tablename, colname);
299  portal = SPI_cursor_open_with_args(
300  "coverage",
301  sql,
302  0, NULL,
303  NULL, NULL,
304  TRUE, 0
305  );
306  pfree(sql);
307 
308  /* process resultset */
309  SPI_cursor_fetch(portal, TRUE, 1);
310  while (SPI_processed == 1 && SPI_tuptable != NULL) {
311  tupdesc = SPI_tuptable->tupdesc;
312  tuptable = SPI_tuptable;
313  tuple = tuptable->vals[0];
314 
315  datum = SPI_getbinval(tuple, tupdesc, 1, &isNull);
316  if (SPI_result == SPI_ERROR_NOATTRIBUTE) {
317 
318  if (SPI_tuptable) SPI_freetuptable(tuptable);
319  SPI_cursor_close(portal);
320  SPI_finish();
321 
322  if (NULL != rtn) pfree(rtn);
323  elog(ERROR, "RASTER_summaryStatsCoverage: Cannot get raster of coverage");
324  PG_RETURN_NULL();
325  }
326  else if (isNull) {
327  SPI_cursor_fetch(portal, TRUE, 1);
328  continue;
329  }
330 
331  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(datum);
332 
333  raster = rt_raster_deserialize(pgraster, FALSE);
334  if (!raster) {
335 
336  if (SPI_tuptable) SPI_freetuptable(tuptable);
337  SPI_cursor_close(portal);
338  SPI_finish();
339 
340  if (NULL != rtn) pfree(rtn);
341  elog(ERROR, "RASTER_summaryStatsCoverage: Cannot deserialize raster");
342  PG_RETURN_NULL();
343  }
344 
345  /* inspect number of bands */
346  num_bands = rt_raster_get_num_bands(raster);
347  if (bandindex < 1 || bandindex > num_bands) {
348  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
349 
350  rt_raster_destroy(raster);
351 
352  if (SPI_tuptable) SPI_freetuptable(tuptable);
353  SPI_cursor_close(portal);
354  SPI_finish();
355 
356  if (NULL != rtn) pfree(rtn);
357  PG_RETURN_NULL();
358  }
359 
360  /* get band */
361  band = rt_raster_get_band(raster, bandindex - 1);
362  if (!band) {
363  elog(NOTICE, "Cannot find band at index %d. Returning NULL", bandindex);
364 
365  rt_raster_destroy(raster);
366 
367  if (SPI_tuptable) SPI_freetuptable(tuptable);
368  SPI_cursor_close(portal);
369  SPI_finish();
370 
371  if (NULL != rtn) pfree(rtn);
372  PG_RETURN_NULL();
373  }
374 
375  /* we don't need the raw values, hence the zero parameter */
376  stats = rt_band_get_summary_stats(band, (int) exclude_nodata_value, sample, 0, &cK, &cM, &cQ);
377 
378  rt_band_destroy(band);
379  rt_raster_destroy(raster);
380 
381  if (NULL == stats) {
382  elog(NOTICE, "Cannot compute summary statistics for band at index %d. Returning NULL", bandindex);
383 
384  if (SPI_tuptable) SPI_freetuptable(tuptable);
385  SPI_cursor_close(portal);
386  SPI_finish();
387 
388  if (NULL != rtn) pfree(rtn);
389  PG_RETURN_NULL();
390  }
391 
392  /* initialize rtn */
393  if (stats->count > 0) {
394  if (NULL == rtn) {
395  rtn = (rt_bandstats) SPI_palloc(sizeof(struct rt_bandstats_t));
396  if (NULL == rtn) {
397 
398  if (SPI_tuptable) SPI_freetuptable(tuptable);
399  SPI_cursor_close(portal);
400  SPI_finish();
401 
402  elog(ERROR, "RASTER_summaryStatsCoverage: Cannot allocate memory for summary stats of coverage");
403  PG_RETURN_NULL();
404  }
405 
406  rtn->sample = stats->sample;
407  rtn->count = stats->count;
408  rtn->min = stats->min;
409  rtn->max = stats->max;
410  rtn->sum = stats->sum;
411  rtn->mean = stats->mean;
412  rtn->stddev = -1;
413 
414  rtn->values = NULL;
415  rtn->sorted = 0;
416  }
417  else {
418  rtn->count += stats->count;
419  rtn->sum += stats->sum;
420 
421  if (stats->min < rtn->min)
422  rtn->min = stats->min;
423  if (stats->max > rtn->max)
424  rtn->max = stats->max;
425  }
426  }
427 
428  pfree(stats);
429 
430  /* next record */
431  SPI_cursor_fetch(portal, TRUE, 1);
432  }
433 
434  if (SPI_tuptable) SPI_freetuptable(tuptable);
435  SPI_cursor_close(portal);
436  SPI_finish();
437 
438  if (NULL == rtn) {
439  elog(ERROR, "RASTER_summaryStatsCoverage: Cannot compute coverage summary stats");
440  PG_RETURN_NULL();
441  }
442 
443  /* coverage mean and deviation */
444  rtn->mean = rtn->sum / rtn->count;
445  /* sample deviation */
446  if (rtn->sample > 0 && rtn->sample < 1)
447  rtn->stddev = sqrt(cQ / (rtn->count - 1));
448  /* standard deviation */
449  else
450  rtn->stddev = sqrt(cQ / rtn->count);
451 
452  /* Build a tuple descriptor for our result type */
453  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
454  ereport(ERROR, (
455  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
456  errmsg(
457  "function returning record called in context "
458  "that cannot accept type record"
459  )
460  ));
461  }
462 
463  BlessTupleDesc(tupdesc);
464 
465  memset(nulls, FALSE, sizeof(bool) * VALUES_LENGTH);
466 
467  values[0] = Int64GetDatum(rtn->count);
468  if (rtn->count > 0) {
469  values[1] = Float8GetDatum(rtn->sum);
470  values[2] = Float8GetDatum(rtn->mean);
471  values[3] = Float8GetDatum(rtn->stddev);
472  values[4] = Float8GetDatum(rtn->min);
473  values[5] = Float8GetDatum(rtn->max);
474  }
475  else {
476  nulls[1] = TRUE;
477  nulls[2] = TRUE;
478  nulls[3] = TRUE;
479  nulls[4] = TRUE;
480  nulls[5] = TRUE;
481  }
482 
483  /* build a tuple */
484  tuple = heap_form_tuple(tupdesc, values, nulls);
485 
486  /* make the tuple into a datum */
487  result = HeapTupleGetDatum(tuple);
488 
489  /* clean up */
490  pfree(rtn);
491 
492  PG_RETURN_DATUM(result);
493 }
#define VALUES_LENGTH
int rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
uint32_t count
Definition: librtcore.h:2311
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
#define FLT_EQ(x, y)
Definition: librtcore.h:2185
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition: rt_band.c:242
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:381
double * values
Definition: librtcore.h:2319
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
rt_bandstats rt_band_get_summary_stats(rt_band band, int exclude_nodata_value, double sample, int inc_vals, uint64_t *cK, double *cM, double *cQ)
Compute summary statistics for a band.
struct rt_bandstats_t * rt_bandstats
Definition: librtcore.h:150
#define FALSE
Definition: dbfopen.c:168
Struct definitions.
Definition: librtcore.h:2201
#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: