Compute summary statistics for a band.
- Parameters
-
band | : the band to query for summary stats |
exclude_nodata_value | : if non-zero, ignore nodata values |
sample | : percentage of pixels to sample |
inc_vals | : flag to include values in return struct |
cK | : number of pixels counted thus far in coverage |
cM | : M component of 1-pass stddev for coverage |
cQ | : Q component of 1-pass stddev for coverage |
- Returns
- the summary statistics for a band or NULL
Definition at line 110 of file rt_statistics.c.
References rt_bandstats_t::count, ES_NONE, FALSE, FLT_EQ, rt_band_t::height, rt_bandstats_t::max, rt_bandstats_t::mean, rt_bandstats_t::min, RASTER_DEBUG, RASTER_DEBUGF, rt_band_get_hasnodata_flag(), rt_band_get_isnodata_flag(), rt_band_get_nodata(), rt_band_get_pixel(), rtalloc(), rtdealloc(), rterror(), rtrealloc(), rtwarn(), rt_bandstats_t::sample, rt_bandstats_t::sorted, rt_bandstats_t::stddev, rt_bandstats_t::sum, genraster::value, rt_bandstats_t::values, rt_band_t::width, pixval::x, and pixval::y.
Referenced by RASTER_colorMap(), RASTER_histogram(), RASTER_histogramCoverage(), RASTER_quantile(), RASTER_summaryStats(), RASTER_summaryStats_transfn(), RASTER_summaryStatsCoverage(), and test_band_stats().
121 int hasnodata =
FALSE;
123 double *values = NULL;
139 #if POSTGIS_DEBUG_LEVEL > 0 145 #if POSTGIS_DEBUG_LEVEL > 0 149 assert(NULL != band);
155 rterror(
"rt_band_get_summary_stats: Could not allocate memory for stats");
159 rtwarn(
"Band is empty as width and/or height is 0");
165 stats->
min = stats->
max = 0;
174 if (hasnodata !=
FALSE)
177 exclude_nodata_value = 0;
181 RASTER_DEBUGF(3,
"exclude_nodata_value = %d", exclude_nodata_value);
187 rterror(
"rt_band_get_summary_stats: Could not allocate memory for stats");
195 if (exclude_nodata_value) {
196 rtwarn(
"All pixels of band have the NODATA value");
199 stats->
min = stats->
max = 0;
206 stats->
min = stats->
max = nodata;
208 stats->
mean = nodata;
217 (sample < 0 ||
FLT_EQ(sample, 0.0)) ||
218 (sample > 1 ||
FLT_EQ(sample, 1.0))
230 sample_per = band->
height;
238 sample_size = round((band->
width * band->
height) * sample);
239 sample_per = round(sample_size / band->
width);
242 sample_int = round(band->
height / sample_per);
246 RASTER_DEBUGF(3,
"sampling %d of %d available pixels w/ %d per set" 247 , sample_size, (band->
width * band->
height), sample_per);
250 values =
rtalloc(
sizeof(
double) * sample_size);
251 if (NULL == values) {
252 rtwarn(
"Could not allocate memory for values");
260 rterror(
"rt_band_get_summary_stats: Could not allocate memory for stats");
268 stats->
min = stats->
max = 0;
272 for (x = 0, j = 0, k = 0; x < band->
width; x++) {
276 for (i = 0, z = 0; i < sample_per; i++) {
280 offset = (rand() % sample_int) + 1;
282 diff = sample_int - offset;
285 if (y >= band->
height || z > sample_per)
break;
290 if (rtn ==
ES_NONE && (!exclude_nodata_value || (exclude_nodata_value && !isnodata))) {
293 if (inc_vals) values[k] =
value;
308 Q += (((k - 1) * pow(value - M, 2)) / k);
309 M += ((value - M ) / k);
320 *cQ += (((*cK - 1) * pow(value - *cM, 2)) / *cK);
321 *cM += ((value - *cM ) / *cK);
326 if (stats->
count < 1) {
331 if (value < stats->min)
333 if (value > stats->
max)
349 if (sample_size != k) {
350 values =
rtrealloc(values, k *
sizeof(
double));
357 stats->
mean = sum / k;
361 stats->
stddev = sqrt(Q / k);
367 stats->
stddev = sqrt(Q / (k - 1));
375 if (do_sample && k < 1)
376 rtwarn(
"All sampled pixels of band have the NODATA value");
378 #if POSTGIS_DEBUG_LEVEL > 0 380 elapsed = ((double) (stop - start)) / CLOCKS_PER_SEC;
381 RASTER_DEBUGF(3,
"(time, count, mean, stddev, min, max) = (%0.4f, %d, %f, %f, %f, %f)",
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
void * rtrealloc(void *mem, size_t size)
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
void rtwarn(const char *fmt,...)
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
#define RASTER_DEBUGF(level, msg,...)
struct rt_bandstats_t * rt_bandstats
void rtdealloc(void *mem)
#define RASTER_DEBUG(level, msg)
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.