PostGIS  2.1.10dev-r@@SVN_REVISION@@
Datum RASTER_summaryStats ( PG_FUNCTION_ARGS  )

Definition at line 7975 of file rt_pg.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, result, 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::stddev, rt_bandstats_t::sum, and TRUE.

7976 {
7977  rt_pgraster *pgraster = NULL;
7978  rt_raster raster = NULL;
7979  rt_band band = NULL;
7980  int32_t bandindex = 1;
7981  bool exclude_nodata_value = TRUE;
7982  int num_bands = 0;
7983  double sample = 0;
7984  rt_bandstats stats = NULL;
7985 
7986  TupleDesc tupdesc;
7987  int values_length = 6;
7988  Datum values[values_length];
7989  bool nulls[values_length];
7990  HeapTuple tuple;
7991  Datum result;
7992 
7993  /* pgraster is null, return null */
7994  if (PG_ARGISNULL(0))
7995  PG_RETURN_NULL();
7996  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
7997 
7998  raster = rt_raster_deserialize(pgraster, FALSE);
7999  if (!raster) {
8000  PG_FREE_IF_COPY(pgraster, 0);
8001  elog(ERROR, "RASTER_summaryStats: Could not deserialize raster");
8002  PG_RETURN_NULL();
8003  }
8004 
8005  /* band index is 1-based */
8006  if (!PG_ARGISNULL(1))
8007  bandindex = PG_GETARG_INT32(1);
8008  num_bands = rt_raster_get_num_bands(raster);
8009  if (bandindex < 1 || bandindex > num_bands) {
8010  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
8011  rt_raster_destroy(raster);
8012  PG_FREE_IF_COPY(pgraster, 0);
8013  PG_RETURN_NULL();
8014  }
8015 
8016  /* exclude_nodata_value flag */
8017  if (!PG_ARGISNULL(2))
8018  exclude_nodata_value = PG_GETARG_BOOL(2);
8019 
8020  /* sample % */
8021  if (!PG_ARGISNULL(3)) {
8022  sample = PG_GETARG_FLOAT8(3);
8023  if (sample < 0 || sample > 1) {
8024  elog(NOTICE, "Invalid sample percentage (must be between 0 and 1). Returning NULL");
8025  rt_raster_destroy(raster);
8026  PG_FREE_IF_COPY(pgraster, 0);
8027  PG_RETURN_NULL();
8028  }
8029  else if (FLT_EQ(sample, 0.0))
8030  sample = 1;
8031  }
8032  else
8033  sample = 1;
8034 
8035  /* get band */
8036  band = rt_raster_get_band(raster, bandindex - 1);
8037  if (!band) {
8038  elog(NOTICE, "Could not find band at index %d. Returning NULL", bandindex);
8039  rt_raster_destroy(raster);
8040  PG_FREE_IF_COPY(pgraster, 0);
8041  PG_RETURN_NULL();
8042  }
8043 
8044  /* we don't need the raw values, hence the zero parameter */
8045  stats = rt_band_get_summary_stats(band, (int) exclude_nodata_value, sample, 0, NULL, NULL, NULL);
8046  rt_band_destroy(band);
8047  rt_raster_destroy(raster);
8048  PG_FREE_IF_COPY(pgraster, 0);
8049  if (NULL == stats) {
8050  elog(NOTICE, "Could not compute summary statistics for band at index %d. Returning NULL", bandindex);
8051  PG_RETURN_NULL();
8052  }
8053 
8054  /* Build a tuple descriptor for our result type */
8055  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
8056  ereport(ERROR, (
8057  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
8058  errmsg(
8059  "function returning record called in context "
8060  "that cannot accept type record"
8061  )
8062  ));
8063  }
8064 
8065  BlessTupleDesc(tupdesc);
8066 
8067  memset(nulls, FALSE, sizeof(bool) * values_length);
8068 
8069  values[0] = Int64GetDatum(stats->count);
8070  if (stats->count > 0) {
8071  values[1] = Float8GetDatum(stats->sum);
8072  values[2] = Float8GetDatum(stats->mean);
8073  values[3] = Float8GetDatum(stats->stddev);
8074  values[4] = Float8GetDatum(stats->min);
8075  values[5] = Float8GetDatum(stats->max);
8076  }
8077  else {
8078  nulls[1] = TRUE;
8079  nulls[2] = TRUE;
8080  nulls[3] = TRUE;
8081  nulls[4] = TRUE;
8082  nulls[5] = TRUE;
8083  }
8084 
8085  /* build a tuple */
8086  tuple = heap_form_tuple(tupdesc, values, nulls);
8087 
8088  /* make the tuple into a datum */
8089  result = HeapTupleGetDatum(tuple);
8090 
8091  /* clean up */
8092  pfree(stats);
8093 
8094  PG_RETURN_DATUM(result);
8095 }
int rt_raster_get_num_bands(rt_raster raster)
Definition: rt_api.c:5677
double mean
Definition: rt_api.h:2282
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_api.c:5387
uint32_t count
Definition: rt_api.h:2277
double sum
Definition: rt_api.h:2281
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:123
char ** result
Definition: liblwgeom.h:218
double min
Definition: rt_api.h:2279
#define FLT_EQ(x, y)
Definition: rt_api.h:2159
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition: rt_api.c:1650
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.
Definition: rt_api.c:3269
rt_band rt_raster_get_band(rt_raster raster, int n)
Return Nth band, or NULL if unavailable.
Definition: rt_api.c:5686
#define FALSE
Definition: dbfopen.c:169
Struct definitions.
Definition: rt_api.h:2175
double max
Definition: rt_api.h:2280
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_api.c:8350
double stddev
Definition: rt_api.h:2283
#define TRUE
Definition: dbfopen.c:170

Here is the call graph for this function: