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

Definition at line 71 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::stddev, rt_bandstats_t::sum, and TRUE.

72 {
73  rt_pgraster *pgraster = NULL;
74  rt_raster raster = NULL;
75  rt_band band = NULL;
76  int32_t bandindex = 1;
77  bool exclude_nodata_value = TRUE;
78  int num_bands = 0;
79  double sample = 0;
80  rt_bandstats stats = NULL;
81 
82  TupleDesc tupdesc;
83  int values_length = 6;
84  Datum values[values_length];
85  bool nulls[values_length];
86  HeapTuple tuple;
87  Datum result;
88 
89  /* pgraster is null, return null */
90  if (PG_ARGISNULL(0))
91  PG_RETURN_NULL();
92  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
93 
94  raster = rt_raster_deserialize(pgraster, FALSE);
95  if (!raster) {
96  PG_FREE_IF_COPY(pgraster, 0);
97  elog(ERROR, "RASTER_summaryStats: Cannot deserialize raster");
98  PG_RETURN_NULL();
99  }
100 
101  /* band index is 1-based */
102  if (!PG_ARGISNULL(1))
103  bandindex = PG_GETARG_INT32(1);
104  num_bands = rt_raster_get_num_bands(raster);
105  if (bandindex < 1 || bandindex > num_bands) {
106  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
107  rt_raster_destroy(raster);
108  PG_FREE_IF_COPY(pgraster, 0);
109  PG_RETURN_NULL();
110  }
111 
112  /* exclude_nodata_value flag */
113  if (!PG_ARGISNULL(2))
114  exclude_nodata_value = PG_GETARG_BOOL(2);
115 
116  /* sample % */
117  if (!PG_ARGISNULL(3)) {
118  sample = PG_GETARG_FLOAT8(3);
119  if (sample < 0 || sample > 1) {
120  elog(NOTICE, "Invalid sample percentage (must be between 0 and 1). Returning NULL");
121  rt_raster_destroy(raster);
122  PG_FREE_IF_COPY(pgraster, 0);
123  PG_RETURN_NULL();
124  }
125  else if (FLT_EQ(sample, 0.0))
126  sample = 1;
127  }
128  else
129  sample = 1;
130 
131  /* get band */
132  band = rt_raster_get_band(raster, bandindex - 1);
133  if (!band) {
134  elog(NOTICE, "Cannot find band at index %d. Returning NULL", bandindex);
135  rt_raster_destroy(raster);
136  PG_FREE_IF_COPY(pgraster, 0);
137  PG_RETURN_NULL();
138  }
139 
140  /* we don't need the raw values, hence the zero parameter */
141  stats = rt_band_get_summary_stats(band, (int) exclude_nodata_value, sample, 0, NULL, NULL, NULL);
142  rt_band_destroy(band);
143  rt_raster_destroy(raster);
144  PG_FREE_IF_COPY(pgraster, 0);
145  if (NULL == stats) {
146  elog(NOTICE, "Cannot compute summary statistics for band at index %d. Returning NULL", bandindex);
147  PG_RETURN_NULL();
148  }
149 
150  /* Build a tuple descriptor for our result type */
151  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
152  ereport(ERROR, (
153  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
154  errmsg(
155  "function returning record called in context "
156  "that cannot accept type record"
157  )
158  ));
159  }
160 
161  BlessTupleDesc(tupdesc);
162 
163  memset(nulls, FALSE, sizeof(bool) * values_length);
164 
165  values[0] = Int64GetDatum(stats->count);
166  if (stats->count > 0) {
167  values[1] = Float8GetDatum(stats->sum);
168  values[2] = Float8GetDatum(stats->mean);
169  values[3] = Float8GetDatum(stats->stddev);
170  values[4] = Float8GetDatum(stats->min);
171  values[5] = Float8GetDatum(stats->max);
172  }
173  else {
174  nulls[1] = TRUE;
175  nulls[2] = TRUE;
176  nulls[3] = TRUE;
177  nulls[4] = TRUE;
178  nulls[5] = TRUE;
179  }
180 
181  /* build a tuple */
182  tuple = heap_form_tuple(tupdesc, values, nulls);
183 
184  /* make the tuple into a datum */
185  result = HeapTupleGetDatum(tuple);
186 
187  /* clean up */
188  pfree(stats);
189 
190  PG_RETURN_DATUM(result);
191 }
int rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
uint32_t count
Definition: librtcore.h:2323
#define FLT_EQ(x, y)
Definition: librtcore.h:2197
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
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:381
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.
#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: