PostGIS  2.5.0beta1dev-r@@SVN_REVISION@@
cu_band_stats.c
Go to the documentation of this file.
1 /*
2  * PostGIS Raster - Raster Types for PostGIS
3  * http://trac.osgeo.org/postgis/wiki/WKTRaster
4  *
5  * Copyright (C) 2012 Regents of the University of California
6  * <bkpark@ucdavis.edu>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  */
23 
24 #include "CUnit/Basic.h"
25 #include "cu_tester.h"
26 
27 static void test_band_stats() {
28  rt_bandstats stats = NULL;
29  rt_histogram histogram = NULL;
30  double bin_width[] = {100};
31  double quantiles[] = {0.1, 0.3, 0.5, 0.7, 0.9};
32  double quantiles2[] = {0.66666667};
33  rt_quantile quantile = NULL;
34  uint32_t count = 0;
35 
37  rt_band band;
38  uint32_t x;
39  uint32_t xmax = 100;
40  uint32_t y;
41  uint32_t ymax = 100;
42  uint32_t max_run;
43  double nodata;
44 
45  uint32_t values[] = {0, 91, 55, 86, 76, 41, 36, 97, 25, 63, 68, 2, 78, 15, 82, 47};
46  struct quantile_llist *qlls = NULL;
47  uint32_t qlls_count;
48 
49  raster = rt_raster_new(xmax, ymax);
50  CU_ASSERT(raster != NULL);
51  band = cu_add_band(raster, PT_32BUI, 1, 0);
52  CU_ASSERT(band != NULL);
53 
54  for (x = 0; x < xmax; x++) {
55  for (y = 0; y < ymax; y++) {
56  rt_band_set_pixel(band, x, y, x + y, NULL);
57  }
58  }
59 
60  rt_band_get_nodata(band, &nodata);
61  CU_ASSERT_DOUBLE_EQUAL(nodata, 0, DBL_EPSILON);
62 
63  stats = (rt_bandstats) rt_band_get_summary_stats(band, 1, 0, 1, NULL, NULL, NULL);
64  CU_ASSERT(stats != NULL);
65  CU_ASSERT_DOUBLE_EQUAL(stats->min, 1, DBL_EPSILON);
66  CU_ASSERT_DOUBLE_EQUAL(stats->max, 198, DBL_EPSILON);
67 
68  quantile = (rt_quantile) rt_band_get_quantiles(stats, NULL, 0, &count);
69  CU_ASSERT(quantile != NULL);
70  rtdealloc(quantile);
71 
72  histogram = (rt_histogram) rt_band_get_histogram(stats, 0, NULL, 0, 0, 0, 0, &count);
73  CU_ASSERT(histogram != NULL);
74  rtdealloc(histogram);
75 
76  histogram = (rt_histogram) rt_band_get_histogram(stats, 0, NULL, 0, 1, 0, 0, &count);
77  CU_ASSERT(histogram != NULL);
78  rtdealloc(histogram);
79 
80  histogram = (rt_histogram) rt_band_get_histogram(stats, 0, bin_width, 1, 0, 0, 0, &count);
81  CU_ASSERT(histogram != NULL);
82  rtdealloc(histogram);
83 
84  rtdealloc(stats->values);
85  rtdealloc(stats);
86 
87  stats = (rt_bandstats) rt_band_get_summary_stats(band, 1, 0.1, 1, NULL, NULL, NULL);
88  CU_ASSERT(stats != NULL);
89 
90  quantile = (rt_quantile) rt_band_get_quantiles(stats, NULL, 0, &count);
91  CU_ASSERT(quantile != NULL);
92  rtdealloc(quantile);
93 
94  quantile = (rt_quantile) rt_band_get_quantiles(stats, quantiles, 5, &count);
95  CU_ASSERT(quantile != NULL);
96  CU_ASSERT_EQUAL(count, 5);
97  rtdealloc(quantile);
98 
99  histogram = (rt_histogram) rt_band_get_histogram(stats, 0, NULL, 0, 0, 0, 0, &count);
100  CU_ASSERT(histogram != NULL);
101  rtdealloc(histogram);
102 
103  rtdealloc(stats->values);
104  rtdealloc(stats);
105 
106  stats = (rt_bandstats) rt_band_get_summary_stats(band, 1, 0.15, 0, NULL, NULL, NULL);
107  CU_ASSERT(stats != NULL);
108  rtdealloc(stats);
109 
110  stats = (rt_bandstats) rt_band_get_summary_stats(band, 1, 0.2, 0, NULL, NULL, NULL);
111  CU_ASSERT(stats != NULL);
112  rtdealloc(stats);
113 
114  stats = (rt_bandstats) rt_band_get_summary_stats(band, 1, 0.25, 0, NULL, NULL, NULL);
115  CU_ASSERT(stats != NULL);
116  rtdealloc(stats);
117 
118  stats = (rt_bandstats) rt_band_get_summary_stats(band, 0, 0, 1, NULL, NULL, NULL);
119  CU_ASSERT(stats != NULL);
120  CU_ASSERT_DOUBLE_EQUAL(stats->min, 0, DBL_EPSILON);
121  CU_ASSERT_DOUBLE_EQUAL(stats->max, 198, DBL_EPSILON);
122 
123  quantile = (rt_quantile) rt_band_get_quantiles(stats, NULL, 0, &count);
124  CU_ASSERT(quantile != NULL);
125  rtdealloc(quantile);
126 
127  rtdealloc(stats->values);
128  rtdealloc(stats);
129 
130  stats = (rt_bandstats) rt_band_get_summary_stats(band, 0, 0.1, 1, NULL, NULL, NULL);
131  CU_ASSERT(stats != NULL);
132 
133  quantile = (rt_quantile) rt_band_get_quantiles(stats, NULL, 0, &count);
134  CU_ASSERT(quantile != NULL);
135  rtdealloc(quantile);
136 
137  rtdealloc(stats->values);
138  rtdealloc(stats);
139 
140  cu_free_raster(raster);
141 
142  xmax = 4;
143  ymax = 4;
144  raster = rt_raster_new(4, 4);
145  CU_ASSERT(raster != NULL);
146  band = cu_add_band(raster, PT_8BUI, 0, 0);
147  CU_ASSERT(band != NULL);
148  rt_band_set_nodata(band, 0, NULL);
149 
150  for (x = 0; x < xmax; x++) {
151  for (y = 0; y < ymax; y++) {
152  rt_band_set_pixel(band, x, y, values[(x * ymax) + y], NULL);
153  }
154  }
155 
156  rt_band_get_nodata(band, &nodata);
157  CU_ASSERT_DOUBLE_EQUAL(nodata, 0, DBL_EPSILON);
158 
160  band, 1, 1, 15,
161  &qlls, &qlls_count,
162  quantiles2, 1,
163  &count);
164  CU_ASSERT(quantile != NULL);
165  CU_ASSERT_NOT_EQUAL(count, 0);
166  CU_ASSERT_NOT_EQUAL(qlls_count, 0);
167  CU_ASSERT_DOUBLE_EQUAL(quantile[0].value, 78, DBL_EPSILON);
168  rtdealloc(quantile);
169  quantile_llist_destroy(&qlls, qlls_count);
170  qlls = NULL;
171  qlls_count = 0;
172 
173  cu_free_raster(raster);
174 
175  xmax = 100;
176  ymax = 100;
177  raster = rt_raster_new(xmax, ymax);
178  CU_ASSERT(raster != NULL);
179  band = cu_add_band(raster, PT_64BF, 0, 0);
180  CU_ASSERT(band != NULL);
181  rt_band_set_nodata(band, 0, NULL);
182 
183  for (x = 0; x < xmax; x++) {
184  for (y = 0; y < ymax; y++) {
185  rt_band_set_pixel(band, x, y, (((double) x * y) + (x + y) + (x + y * x)) / (x + y + 1), NULL);
186  }
187  }
188 
189  rt_band_get_nodata(band, &nodata);
190  CU_ASSERT_DOUBLE_EQUAL(nodata, 0, DBL_EPSILON);
191 
192  max_run = 5;
193  for (x = 0; x < max_run; x++) {
195  band, 1, 1, xmax * ymax * max_run,
196  &qlls, &qlls_count,
197  quantiles2, 1,
198  &count);
199  CU_ASSERT(quantile != NULL);
200  CU_ASSERT_NOT_EQUAL(count, 0);
201  CU_ASSERT_NOT_EQUAL(qlls_count, 0);
202  rtdealloc(quantile);
203  }
204 
205  quantile_llist_destroy(&qlls, qlls_count);
206  qlls = NULL;
207  qlls_count = 0;
208 
209  cu_free_raster(raster);
210 }
211 
212 static void test_band_value_count() {
213  rt_valuecount vcnts = NULL;
214 
216  rt_band band;
217  uint32_t x;
218  uint32_t xmax = 100;
219  uint32_t y;
220  uint32_t ymax = 100;
221  uint32_t rtn = 0;
222 
223  double count[] = {3, 4, 5};
224 
225  raster = rt_raster_new(xmax, ymax);
226  CU_ASSERT(raster != NULL); /* or we're out of virtual memory */
227  band = cu_add_band(raster, PT_64BF, 0, 0);
228  CU_ASSERT(band != NULL);
229  rt_band_set_nodata(band, 0, NULL);
230 
231  for (x = 0; x < xmax; x++) {
232  for (y = 0; y < ymax; y++) {
233  rt_band_set_pixel(band, x, y, (((double) x * y) + (x + y) + (x + y * x)) / (x + y + 1), NULL);
234  }
235  }
236  vcnts = rt_band_get_value_count(band, 1, NULL, 0, 0, NULL, &rtn);
237  CU_ASSERT(vcnts != NULL);
238  CU_ASSERT_NOT_EQUAL(rtn, 0);
239  rtdealloc(vcnts);
240 
241  vcnts = rt_band_get_value_count(band, 1, NULL, 0, 0.01, NULL, &rtn);
242  CU_ASSERT(vcnts != NULL);
243  CU_ASSERT_NOT_EQUAL(rtn, 0);
244  rtdealloc(vcnts);
245 
246  vcnts = rt_band_get_value_count(band, 1, NULL, 0, 0.1, NULL, &rtn);
247  CU_ASSERT(vcnts != NULL);
248  CU_ASSERT_NOT_EQUAL(rtn, 0);
249  rtdealloc(vcnts);
250 
251  vcnts = rt_band_get_value_count(band, 1, NULL, 0, 1, NULL, &rtn);
252  CU_ASSERT(vcnts != NULL);
253  CU_ASSERT_NOT_EQUAL(rtn, 0);
254  rtdealloc(vcnts);
255 
256  vcnts = rt_band_get_value_count(band, 1, NULL, 0, 10, NULL, &rtn);
257  CU_ASSERT(vcnts != NULL);
258  CU_ASSERT_NOT_EQUAL(rtn, 0);
259  rtdealloc(vcnts);
260 
261  vcnts = rt_band_get_value_count(band, 1, count, 3, 1, NULL, &rtn);
262  CU_ASSERT(vcnts != NULL);
263  CU_ASSERT_NOT_EQUAL(rtn, 0);
264  rtdealloc(vcnts);
265 
266  cu_free_raster(raster);
267 }
268 
269 /* register tests */
270 void band_stats_suite_setup(void);
272 {
273  CU_pSuite suite = CU_add_suite("band_stats", NULL, NULL);
276 }
277 
int quantile_llist_destroy(struct quantile_llist **list, uint32_t list_count)
raster
Be careful!! Zeros function&#39;s input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
rt_quantile rt_band_get_quantiles_stream(rt_band band, int exclude_nodata_value, double sample, uint64_t cov_count, struct quantile_llist **qlls, uint32_t *qlls_count, double *quantiles, uint32_t quantiles_count, uint32_t *rtn_count)
Compute the default set of or requested quantiles for a coverage.
band
Definition: ovdump.py:57
rt_errorstate rt_band_set_nodata(rt_band band, double val, int *converted)
Set nodata value.
Definition: rt_band.c:733
static void test_band_value_count()
rt_histogram rt_band_get_histogram(rt_bandstats stats, uint32_t bin_count, double *bin_widths, uint32_t bin_widths_count, int right, double min, double max, uint32_t *rtn_count)
Count the distribution of data.
static void test_band_stats()
Definition: cu_band_stats.c:27
struct rt_quantile_t * rt_quantile
Definition: librtcore.h:152
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
Definition: rt_band.c:1730
unsigned int uint32_t
Definition: uthash.h:78
void cu_free_raster(rt_raster raster)
rt_band cu_add_band(rt_raster raster, rt_pixtype pixtype, int hasnodata, double nodataval)
rt_quantile rt_band_get_quantiles(rt_bandstats stats, double *quantiles, int quantiles_count, uint32_t *rtn_count)
Compute the default set of or requested quantiles for a set of data the quantile formula used is same...
#define PG_ADD_TEST(suite, testfunc)
int count
Definition: genraster.py:56
double * values
Definition: librtcore.h:2368
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.
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
Definition: rt_raster.c:48
void band_stats_suite_setup(void)
struct rt_histogram_t * rt_histogram
Definition: librtcore.h:151
struct rt_bandstats_t * rt_bandstats
Definition: librtcore.h:150
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.
void rtdealloc(void *mem)
Definition: rt_context.c:186
rt_errorstate rt_band_set_pixel(rt_band band, int x, int y, double val, int *converted)
Set single pixel&#39;s value.
Definition: rt_band.c:974
int value
Definition: genraster.py:61