PostGIS  2.1.10dev-r@@SVN_REVISION@@
cu_band_misc.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 
29  rt_band band;
30  uint32_t x, y;
31  int rtn;
32  const int maxX = 10;
33  const int maxY = 10;
34  rt_pixel npixels = NULL;
35 
36  double **value;
37  int **nodata;
38  int dimx;
39  int dimy;
40 
41  rast = rt_raster_new(maxX, maxY);
42  CU_ASSERT(rast != NULL);
43 
44  band = cu_add_band(rast, PT_32BUI, 1, 0);
45  CU_ASSERT(band != NULL);
46 
47  for (x = 0; x < maxX; x++) {
48  for (y = 0; y < maxY; y++) {
49  rtn = rt_band_set_pixel(band, x, y, 1, NULL);
50  }
51  }
52 
53  rt_band_set_pixel(band, 0, 0, 0, NULL);
54  rt_band_set_pixel(band, 3, 0, 0, NULL);
55  rt_band_set_pixel(band, 6, 0, 0, NULL);
56  rt_band_set_pixel(band, 9, 0, 0, NULL);
57  rt_band_set_pixel(band, 1, 2, 0, NULL);
58  rt_band_set_pixel(band, 4, 2, 0, NULL);
59  rt_band_set_pixel(band, 7, 2, 0, NULL);
60  rt_band_set_pixel(band, 2, 4, 0, NULL);
61  rt_band_set_pixel(band, 5, 4, 0, NULL);
62  rt_band_set_pixel(band, 8, 4, 0, NULL);
63  rt_band_set_pixel(band, 0, 6, 0, NULL);
64  rt_band_set_pixel(band, 3, 6, 0, NULL);
65  rt_band_set_pixel(band, 6, 6, 0, NULL);
66  rt_band_set_pixel(band, 9, 6, 0, NULL);
67  rt_band_set_pixel(band, 1, 8, 0, NULL);
68  rt_band_set_pixel(band, 4, 8, 0, NULL);
69  rt_band_set_pixel(band, 7, 8, 0, NULL);
70 
71  /* 0,0 */
73  band,
74  0, 0,
75  0, 0,
76  1,
77  &npixels
78  );
79  CU_ASSERT_EQUAL(rtn, 3);
80  if (rtn)
81  rtdealloc(npixels);
82 
83  /* 1,1 */
85  band,
86  1, 1,
87  0, 0,
88  1,
89  &npixels
90  );
91  CU_ASSERT_EQUAL(rtn, 6);
92  if (rtn)
93  rtdealloc(npixels);
94 
95  /* 4,4 */
97  band,
98  4, 4,
99  0, 0,
100  1,
101  &npixels
102  );
103  CU_ASSERT_EQUAL(rtn, 7);
104  if (rtn)
105  rtdealloc(npixels);
106 
107  /* 4,4 distance 2 */
109  band,
110  4, 4,
111  2, 2,
112  1,
113  &npixels
114  );
115  CU_ASSERT_EQUAL(rtn, 19);
116  if (rtn)
117  rtdealloc(npixels);
118 
119  /* 10,10 */
121  band,
122  10, 10,
123  0, 0,
124  1,
125  &npixels
126  );
127  CU_ASSERT_EQUAL(rtn, 1);
128  if (rtn)
129  rtdealloc(npixels);
130 
131  /* 11,11 distance 1 */
133  band,
134  11, 11,
135  1, 1,
136  1,
137  &npixels
138  );
139  CU_ASSERT_EQUAL(rtn, 0);
140  if (rtn)
141  rtdealloc(npixels);
142 
143  /* -1,-1 */
145  band,
146  -1, -1,
147  0, 0,
148  1,
149  &npixels
150  );
151  CU_ASSERT_EQUAL(rtn, 3);
152  if (rtn)
153  rtdealloc(npixels);
154 
155  /* -1,-1 distance 1 */
157  band,
158  -1, -1,
159  1, 1,
160  1,
161  &npixels
162  );
163  CU_ASSERT_EQUAL(rtn, 0);
164  if (rtn)
165  rtdealloc(npixels);
166 
167  /* -1,1 distance 1 */
169  band,
170  -1, 1,
171  1, 1,
172  1,
173  &npixels
174  );
175  CU_ASSERT_EQUAL(rtn, 2);
176 
177  rtn = rt_pixel_set_to_array(
178  npixels, rtn,
179  -1, 1,
180  1, 1,
181  &value,
182  &nodata,
183  &dimx, &dimy
184  );
185  rtdealloc(npixels);
186  CU_ASSERT_EQUAL(rtn, ES_NONE);
187  CU_ASSERT_EQUAL(dimx, 3);
188  CU_ASSERT_EQUAL(dimy, 3);
189 
190  for (x = 0; x < dimx; x++) {
191  rtdealloc(nodata[x]);
192  rtdealloc(value[x]);
193  }
194 
195  rtdealloc(nodata);
196  rtdealloc(value);
197 
198 
199  /* -2,2 distance 1 */
201  band,
202  -2, 2,
203  1, 1,
204  1,
205  &npixels
206  );
207  CU_ASSERT_EQUAL(rtn, 0);
208  if (rtn)
209  rtdealloc(npixels);
210 
211  /* -10,2 distance 3 */
213  band,
214  -10, 2,
215  3, 3,
216  1,
217  &npixels
218  );
219  CU_ASSERT_EQUAL(rtn, 0);
220  if (rtn)
221  rtdealloc(npixels);
222 
223  /* -10,2 distance 3 include NODATA */
225  band,
226  -10, 2,
227  3, 3,
228  0,
229  &npixels
230  );
231  CU_ASSERT_EQUAL(rtn, 48);
232  if (rtn)
233  rtdealloc(npixels);
234 
235  /* 4,4 distance 3,2 */
237  band,
238  4, 4,
239  3, 2,
240  1,
241  &npixels
242  );
243  CU_ASSERT_EQUAL(rtn, 27);
244  if (rtn)
245  rtdealloc(npixels);
246 
247  /* 2,7 distance 3,1 */
249  band,
250  2, 7,
251  3, 1,
252  1,
253  &npixels
254  );
255  CU_ASSERT_EQUAL(rtn, 13);
256  if (rtn)
257  rtdealloc(npixels);
258 
259  /* 10,10 distance 1,3 */
261  band,
262  10,10,
263  1, 3,
264  1,
265  &npixels
266  );
267  CU_ASSERT_EQUAL(rtn, 3);
268  if (rtn)
269  rtdealloc(npixels);
270 
271  /* band with no NODATA */
272  band = cu_add_band(rast, PT_32BUI, 0, 0);
273  CU_ASSERT(band != NULL);
274 
275  /* 0,0 */
277  band,
278  0, 0,
279  0, 0,
280  1,
281  &npixels
282  );
283  CU_ASSERT_EQUAL(rtn, 8);
284  if (rtn)
285  rtdealloc(npixels);
286 
287  cu_free_raster(rast);
288 }
289 
291  rt_raster rast;
292  rt_band band;
293  uint32_t x, y;
294  int rtn;
295  const int maxX = 10;
296  const int maxY = 10;
297  rt_pixel pixels = NULL;
298 
299  double search0[1] = {0};
300  double search1[1] = {1};
301  double search2[2] = {3, 5};
302 
303  rast = rt_raster_new(maxX, maxY);
304  CU_ASSERT(rast != NULL);
305 
306  band = cu_add_band(rast, PT_32BUI, 1, 0);
307  CU_ASSERT(band != NULL);
308 
309  for (x = 0; x < maxX; x++) {
310  for (y = 0; y < maxY; y++) {
311  rtn = rt_band_set_pixel(band, x, y, 1, NULL);
312  }
313  }
314 
315  rt_band_set_pixel(band, 0, 0, 0, NULL);
316  rt_band_set_pixel(band, 3, 0, 0, NULL);
317  rt_band_set_pixel(band, 6, 0, 0, NULL);
318  rt_band_set_pixel(band, 9, 0, 0, NULL);
319  rt_band_set_pixel(band, 1, 2, 0, NULL);
320  rt_band_set_pixel(band, 4, 2, 0, NULL);
321  rt_band_set_pixel(band, 7, 2, 0, NULL);
322  rt_band_set_pixel(band, 2, 4, 0, NULL);
323  rt_band_set_pixel(band, 5, 4, 0, NULL);
324  rt_band_set_pixel(band, 8, 4, 0, NULL);
325  rt_band_set_pixel(band, 0, 6, 0, NULL);
326  rt_band_set_pixel(band, 3, 6, 0, NULL);
327  rt_band_set_pixel(band, 6, 6, 0, NULL);
328  rt_band_set_pixel(band, 9, 6, 0, NULL);
329  rt_band_set_pixel(band, 1, 8, 0, NULL);
330  rt_band_set_pixel(band, 4, 8, 0, NULL);
331  rt_band_set_pixel(band, 7, 8, 0, NULL);
332 
333  pixels = NULL;
335  band, TRUE,
336  search1, 1,
337  &pixels
338  );
339  CU_ASSERT_EQUAL(rtn, 83);
340  if (rtn)
341  rtdealloc(pixels);
342 
343  pixels = NULL;
345  band, FALSE,
346  search0, 1,
347  &pixels
348  );
349  CU_ASSERT_EQUAL(rtn, 17);
350  if (rtn)
351  rtdealloc(pixels);
352 
353  rt_band_set_pixel(band, 4, 2, 3, NULL);
354  rt_band_set_pixel(band, 7, 2, 5, NULL);
355  rt_band_set_pixel(band, 1, 8, 3, NULL);
356 
357  pixels = NULL;
359  band, TRUE,
360  search2, 2,
361  &pixels
362  );
363  CU_ASSERT_EQUAL(rtn, 3);
364  if (rtn)
365  rtdealloc(pixels);
366 
367  cu_free_raster(rast);
368 }
369 
370 /* register tests */
371 void band_misc_suite_setup(void);
373 {
374  CU_pSuite suite = CU_add_suite("band_misc", NULL, NULL);
377 }
rt_errorstate rt_pixel_set_to_array(rt_pixel npixel, int count, int x, int y, uint16_t distancex, uint16_t distancey, double ***value, int ***nodata, int *dimx, int *dimy)
Definition: rt_api.c:1341
static void test_band_get_nearest_pixel()
Definition: cu_band_misc.c:27
void rtdealloc(void *mem)
Definition: rt_api.c:882
int rt_band_get_nearest_pixel(rt_band band, int x, int y, uint16_t distancex, uint16_t distancey, int exclude_nodata_value, rt_pixel *npixels)
Get nearest pixel(s) with value (not NODATA) to specified pixel.
Definition: rt_api.c:2702
tuple band
Definition: ovdump.py:57
tuple rast
Definition: rtpixdump.py:62
void band_misc_suite_setup(void)
Definition: cu_band_misc.c:372
void cu_free_raster(rt_raster raster)
rt_band cu_add_band(rt_raster raster, rt_pixtype pixtype, int hasnodata, double nodataval)
int rt_band_get_pixel_of_value(rt_band band, int exclude_nodata_value, double *searchset, int searchcount, rt_pixel *pixels)
Search band for pixel(s) with search values.
Definition: rt_api.c:2980
#define PG_ADD_TEST(suite, testfunc)
static void test_band_get_pixel_of_value()
Definition: cu_band_misc.c:290
tuple x
Definition: pixval.py:53
#define FALSE
Definition: dbfopen.c:169
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
Definition: rt_api.c:5353
#define TRUE
Definition: dbfopen.c:170
tuple y
Definition: pixval.py:54
rt_errorstate rt_band_set_pixel(rt_band band, int x, int y, double val, int *converted)
Set single pixel's value.
Definition: rt_api.c:2302