PostGIS  2.1.10dev-r@@SVN_REVISION@@
cu_mapalgebra.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  uint16_t rasters;
30  uint32_t rows;
31  uint32_t columns;
32 };
33 
34 /* callback for 1 raster, 0 distance, FIRST or SECOND or LAST or UNION or INTERSECTION */
35 static int testRasterIterator1_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata) {
36  _callback_userargs _userarg = (_callback_userargs) userarg;
37 
38  /* check that we're getting what we expect from userarg */
39  CU_ASSERT_EQUAL(arg->rasters, _userarg->rasters);
40  CU_ASSERT_EQUAL(arg->rows, _userarg->rows);
41  CU_ASSERT_EQUAL(arg->columns, _userarg->columns);
42 
43  /* 0,0 */
44  if (
45  arg->dst_pixel[0] == 0 &&
46  arg->dst_pixel[1] == 0
47  ) {
48  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 0, DBL_EPSILON);
49  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
50  }
51  /* 4,4 */
52  else if (
53  arg->dst_pixel[0] == 4 &&
54  arg->dst_pixel[1] == 4
55  ) {
56  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 24, DBL_EPSILON);
57  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
58  }
59  /* 1,1 */
60  else if (
61  arg->dst_pixel[0] == 1 &&
62  arg->dst_pixel[1] == 1
63  ) {
64  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 1);
65  }
66  /* 2,2 */
67  else if (
68  arg->dst_pixel[0] == 2 &&
69  arg->dst_pixel[1] == 2
70  ) {
71  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 12, DBL_EPSILON);
72  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
73  }
74  /* 3,1 */
75  else if (
76  arg->dst_pixel[0] == 3 &&
77  arg->dst_pixel[1] == 1
78  ) {
79  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 8, DBL_EPSILON);
80  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
81  }
82  /* 1,0 */
83  else if (
84  arg->dst_pixel[0] == 1 &&
85  arg->dst_pixel[1] == 0
86  ) {
87  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 1, DBL_EPSILON);
88  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
89  }
90 
91  return 1;
92 }
93 
94 /* callback for 2 raster, 0 distance, UNION */
95 static int testRasterIterator2_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata) {
96  _callback_userargs _userarg = (_callback_userargs) userarg;
97 
98  /* check that we're getting what we expect from userarg */
99  CU_ASSERT_EQUAL(arg->rasters, _userarg->rasters);
100  CU_ASSERT_EQUAL(arg->rows, _userarg->rows);
101  CU_ASSERT_EQUAL(arg->columns, _userarg->columns);
102 
103  /* 0,0 */
104  if (
105  arg->dst_pixel[0] == 0 &&
106  arg->dst_pixel[1] == 0
107  ) {
108  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 0, DBL_EPSILON);
109  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
110 
111  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 1);
112  }
113  /* 4,4 */
114  else if (
115  arg->dst_pixel[0] == 4 &&
116  arg->dst_pixel[1] == 4
117  ) {
118  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 24, DBL_EPSILON);
119  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
120 
121  CU_ASSERT_DOUBLE_EQUAL(arg->values[1][0][0], 118, DBL_EPSILON);
122  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 0);
123  }
124  /* 1,1 */
125  else if (
126  arg->dst_pixel[0] == 1 &&
127  arg->dst_pixel[1] == 1
128  ) {
129  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 1);
130 
131  CU_ASSERT_DOUBLE_EQUAL(arg->values[1][0][0], 100, DBL_EPSILON);
132  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 0);
133  }
134  /* 2,2 */
135  else if (
136  arg->dst_pixel[0] == 2 &&
137  arg->dst_pixel[1] == 2
138  ) {
139  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 12, DBL_EPSILON);
140  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
141 
142  CU_ASSERT_DOUBLE_EQUAL(arg->values[1][0][0], 106, DBL_EPSILON);
143  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 0);
144  }
145  /* 3,1 */
146  else if (
147  arg->dst_pixel[0] == 3 &&
148  arg->dst_pixel[1] == 1
149  ) {
150  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 8, DBL_EPSILON);
151  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
152 
153  CU_ASSERT_DOUBLE_EQUAL(arg->values[1][0][0], 102, DBL_EPSILON);
154  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 0);
155  }
156  /* 1,0 */
157  else if (
158  arg->dst_pixel[0] == 1 &&
159  arg->dst_pixel[1] == 0
160  ) {
161  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 1, DBL_EPSILON);
162  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
163 
164  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 1);
165  }
166  /* 1,3 */
167  else if (
168  arg->dst_pixel[0] == 1 &&
169  arg->dst_pixel[1] == 3
170  ) {
171  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 16, DBL_EPSILON);
172  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
173 
174  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 1);
175  }
176  /* 5,0 */
177  else if (
178  arg->dst_pixel[0] == 5 &&
179  arg->dst_pixel[1] == 0
180  ) {
181  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 1);
182 
183  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 1);
184  }
185 
186  return 1;
187 }
188 
189 /* callback for 2 raster, 0 distance, INTERSECTION */
190 static int testRasterIterator3_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata) {
191  _callback_userargs _userarg = (_callback_userargs) userarg;
192 
193  /* check that we're getting what we expect from userarg */
194  CU_ASSERT_EQUAL(arg->rasters, _userarg->rasters);
195  CU_ASSERT_EQUAL(arg->rows, _userarg->rows);
196  CU_ASSERT_EQUAL(arg->columns, _userarg->columns);
197 
198  /* 0,0 */
199  if (
200  arg->dst_pixel[0] == 0 &&
201  arg->dst_pixel[1] == 0
202  ) {
203  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 1);
204 
205  CU_ASSERT_DOUBLE_EQUAL(arg->values[1][0][0], 100, DBL_EPSILON);
206  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 0);
207  }
208  /* 0,3 */
209  else if (
210  arg->dst_pixel[0] == 0 &&
211  arg->dst_pixel[1] == 3
212  ) {
213  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 21, DBL_EPSILON);
214  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
215 
216  CU_ASSERT_DOUBLE_EQUAL(arg->values[1][0][0], 115, DBL_EPSILON);
217  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 0);
218  }
219  /* 3,0 */
220  else if (
221  arg->dst_pixel[0] == 3 &&
222  arg->dst_pixel[1] == 0
223  ) {
224  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 9, DBL_EPSILON);
225  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
226 
227  CU_ASSERT_DOUBLE_EQUAL(arg->values[1][0][0], 103, DBL_EPSILON);
228  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 0);
229  }
230  /* 3,3 */
231  else if (
232  arg->dst_pixel[0] == 3 &&
233  arg->dst_pixel[1] == 3
234  ) {
235  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 24, DBL_EPSILON);
236  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
237 
238  CU_ASSERT_DOUBLE_EQUAL(arg->values[1][0][0], 118, DBL_EPSILON);
239  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 0);
240  }
241  /* 0,2 */
242  else if (
243  arg->dst_pixel[0] == 3 &&
244  arg->dst_pixel[1] == 3
245  ) {
246  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 16, DBL_EPSILON);
247  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
248 
249  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 1);
250  }
251 
252  return 1;
253 }
254 
255 /* callback for 2 raster, 0 distance, FIRST */
256 static int testRasterIterator4_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata) {
257  _callback_userargs _userarg = (_callback_userargs) userarg;
258 
259  /* check that we're getting what we expect from userarg */
260  CU_ASSERT_EQUAL(arg->rasters, _userarg->rasters);
261  CU_ASSERT_EQUAL(arg->rows, _userarg->rows);
262  CU_ASSERT_EQUAL(arg->columns, _userarg->columns);
263 
264  /* 0,0 */
265  if (
266  arg->dst_pixel[0] == 0 &&
267  arg->dst_pixel[1] == 0
268  ) {
269  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 0, DBL_EPSILON);
270  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
271 
272  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 1);
273  }
274  /* 4,4 */
275  else if (
276  arg->dst_pixel[0] == 4 &&
277  arg->dst_pixel[1] == 4
278  ) {
279  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 24, DBL_EPSILON);
280  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
281 
282  CU_ASSERT_DOUBLE_EQUAL(arg->values[1][0][0], 118, DBL_EPSILON);
283  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 0);
284  }
285  /* 4,1 */
286  else if (
287  arg->dst_pixel[0] == 4 &&
288  arg->dst_pixel[1] == 1
289  ) {
290  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 9, DBL_EPSILON);
291  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
292 
293  CU_ASSERT_DOUBLE_EQUAL(arg->values[1][0][0], 103, DBL_EPSILON);
294  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 0);
295  }
296  /* 4,0 */
297  else if (
298  arg->dst_pixel[0] == 4 &&
299  arg->dst_pixel[1] == 0
300  ) {
301  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 4, DBL_EPSILON);
302  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
303 
304  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 1);
305  }
306 
307  return 1;
308 }
309 
310 /* callback for 2 raster, 0 distance, SECOND or LAST */
311 static int testRasterIterator5_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata) {
312  _callback_userargs _userarg = (_callback_userargs) userarg;
313 
314  /* check that we're getting what we expect from userarg */
315  CU_ASSERT_EQUAL(arg->rasters, _userarg->rasters);
316  CU_ASSERT_EQUAL(arg->rows, _userarg->rows);
317  CU_ASSERT_EQUAL(arg->columns, _userarg->columns);
318 
319  /* 0,0 */
320  if (
321  arg->dst_pixel[0] == 0 &&
322  arg->dst_pixel[1] == 0
323  ) {
324  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 1);
325 
326  CU_ASSERT_DOUBLE_EQUAL(arg->values[1][0][0], 100, DBL_EPSILON);
327  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 0);
328  }
329  /* 4,4 */
330  else if (
331  arg->dst_pixel[0] == 4 &&
332  arg->dst_pixel[1] == 4
333  ) {
334  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 1);
335 
336  CU_ASSERT_DOUBLE_EQUAL(arg->values[1][0][0], 124, DBL_EPSILON);
337  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 0);
338  }
339  /* 4,1 */
340  else if (
341  arg->dst_pixel[0] == 4 &&
342  arg->dst_pixel[1] == 1
343  ) {
344  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 1);
345 
346  CU_ASSERT_DOUBLE_EQUAL(arg->values[1][0][0], 109, DBL_EPSILON);
347  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 0);
348  }
349  /* 0,2 */
350  else if (
351  arg->dst_pixel[0] == 0 &&
352  arg->dst_pixel[1] == 2
353  ) {
354  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 16, DBL_EPSILON);
355  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
356 
357  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 1);
358  }
359 
360  return 1;
361 }
362 
363 /* callback for 2 raster, 0 distance, CUSTOM */
364 static int testRasterIterator6_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata) {
365  _callback_userargs _userarg = (_callback_userargs) userarg;
366 
367  /* check that we're getting what we expect from userarg */
368  CU_ASSERT_EQUAL(arg->rasters, _userarg->rasters);
369  CU_ASSERT_EQUAL(arg->rows, _userarg->rows);
370  CU_ASSERT_EQUAL(arg->columns, _userarg->columns);
371 
372  /* 0,0 */
373  if (
374  arg->dst_pixel[0] == 0 &&
375  arg->dst_pixel[1] == 0
376  ) {
377  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 16, DBL_EPSILON);
378  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
379 
380  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 1);
381  }
382  /* 1,0 */
383  else if (
384  arg->dst_pixel[0] == 1 &&
385  arg->dst_pixel[1] == 0
386  ) {
387  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 17, DBL_EPSILON);
388  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
389 
390  CU_ASSERT_DOUBLE_EQUAL(arg->values[1][0][0], 111, DBL_EPSILON);
391  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 0);
392  }
393  /* 0,1 */
394  else if (
395  arg->dst_pixel[0] == 0 &&
396  arg->dst_pixel[1] == 1
397  ) {
398  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 21, DBL_EPSILON);
399  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
400 
401  CU_ASSERT_DOUBLE_EQUAL(arg->values[1][0][0], 115, DBL_EPSILON);
402  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 0);
403  }
404  /* 1,1 */
405  else if (
406  arg->dst_pixel[0] == 1 &&
407  arg->dst_pixel[1] == 1
408  ) {
409  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 22, DBL_EPSILON);
410  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
411 
412  CU_ASSERT_DOUBLE_EQUAL(arg->values[1][0][0], 116, DBL_EPSILON);
413  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 0);
414  }
415 
416  return 1;
417 }
418 
419 /* callback for 2 raster, 1 distance, CUSTOM */
420 static int testRasterIterator7_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata) {
421  _callback_userargs _userarg = (_callback_userargs) userarg;
422 
423  /* check that we're getting what we expect from userarg */
424  CU_ASSERT_EQUAL(arg->rasters, _userarg->rasters);
425  CU_ASSERT_EQUAL(arg->rows, _userarg->rows);
426  CU_ASSERT_EQUAL(arg->columns, _userarg->columns);
427 
428  /* 0,0 */
429  if (
430  arg->dst_pixel[0] == 0 &&
431  arg->dst_pixel[1] == 0
432  ) {
433  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][1][1], 16, DBL_EPSILON);
434  CU_ASSERT_EQUAL(arg->nodata[0][1][1], 0);
435 
436  CU_ASSERT_EQUAL(arg->nodata[1][1][1], 1);
437 
438  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 10, DBL_EPSILON);
439  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
440 
441  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 1);
442  }
443  /* 1,0 */
444  else if (
445  arg->dst_pixel[0] == 1 &&
446  arg->dst_pixel[1] == 0
447  ) {
448  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][1][1], 17, DBL_EPSILON);
449  CU_ASSERT_EQUAL(arg->nodata[0][1][1], 0);
450 
451  CU_ASSERT_DOUBLE_EQUAL(arg->values[1][1][1], 111, DBL_EPSILON);
452  CU_ASSERT_EQUAL(arg->nodata[1][1][1], 0);
453 
454  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][2][2], 23, DBL_EPSILON);
455  CU_ASSERT_EQUAL(arg->nodata[0][2][2], 0);
456 
457  CU_ASSERT_DOUBLE_EQUAL(arg->values[1][2][2], 117, DBL_EPSILON);
458  CU_ASSERT_EQUAL(arg->nodata[1][2][2], 0);
459  }
460  /* 0,1 */
461  else if (
462  arg->dst_pixel[0] == 0 &&
463  arg->dst_pixel[1] == 1
464  ) {
465  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][1][1], 21, DBL_EPSILON);
466  CU_ASSERT_EQUAL(arg->nodata[0][1][1], 0);
467 
468  CU_ASSERT_DOUBLE_EQUAL(arg->values[1][1][1], 115, DBL_EPSILON);
469  CU_ASSERT_EQUAL(arg->nodata[1][1][1], 0);
470 
471  CU_ASSERT_EQUAL(arg->nodata[0][2][0], 1);
472 
473  CU_ASSERT_EQUAL(arg->nodata[1][2][0], 1);
474  }
475  /* 1,1 */
476  else if (
477  arg->dst_pixel[0] == 1 &&
478  arg->dst_pixel[1] == 1
479  ) {
480  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][1][1], 22, DBL_EPSILON);
481  CU_ASSERT_EQUAL(arg->nodata[0][1][1], 0);
482 
483  CU_ASSERT_DOUBLE_EQUAL(arg->values[1][1][1], 116, DBL_EPSILON);
484  CU_ASSERT_EQUAL(arg->nodata[1][1][1], 0);
485 
486  CU_ASSERT_DOUBLE_EQUAL(arg->values[0][0][0], 16, DBL_EPSILON);
487  CU_ASSERT_EQUAL(arg->nodata[0][0][0], 0);
488 
489  CU_ASSERT_EQUAL(arg->nodata[1][0][0], 1);
490  }
491 
492  return 1;
493 }
494 
495 static void test_raster_iterator() {
496  rt_raster rast1;
497  rt_raster rast2;
498  rt_raster rast3;
499 
500  int num = 2;
501 
502  rt_raster rtn = NULL;
503  rt_band band;
504  int maxX = 5;
505  int maxY = 5;
506  rt_iterator itrset;
507  _callback_userargs userargs;
508  int noerr = 0;
509  int x = 0;
510  int y = 0;
511 
512  rast1 = rt_raster_new(maxX, maxY);
513  CU_ASSERT(rast1 != NULL);
514 
515  rt_raster_set_offsets(rast1, 0, 0);
516  rt_raster_set_scale(rast1, 1, -1);
517 
518  band = cu_add_band(rast1, PT_32BUI, 1, 6);
519  CU_ASSERT(band != NULL);
520 
521  for (y = 0; y < maxY; y++) {
522  for (x = 0; x < maxX; x++) {
523  rt_band_set_pixel(band, x, y, x + (y * maxX), NULL);
524  }
525  }
526 
527  rast2 = rt_raster_new(maxX, maxY);
528  CU_ASSERT(rast2 != NULL);
529 
530  rt_raster_set_offsets(rast2, 1, -1);
531  rt_raster_set_scale(rast2, 1, -1);
532 
533  band = cu_add_band(rast2, PT_32BUI, 1, 110);
534  CU_ASSERT(band != NULL);
535 
536  for (y = 0; y < maxY; y++) {
537  for (x = 0; x < maxX; x++) {
538  rt_band_set_pixel(band, x, y, (x + (y * maxX)) + 100, NULL);
539  }
540  }
541 
542  rast3 = rt_raster_new(2, 2);
543  CU_ASSERT(rast3 != NULL);
544 
545  rt_raster_set_offsets(rast3, 1, -3);
546  rt_raster_set_scale(rast3, 1, -1);
547 
548  /* allocate user args */
549  userargs = rtalloc(sizeof(struct _callback_userargs_t));
550  CU_ASSERT(userargs != NULL);
551 
552  /* allocate itrset */
553  itrset = rtalloc(sizeof(struct rt_iterator_t) * num);
554  CU_ASSERT(itrset != NULL);
555  itrset[0].raster = rast1;
556  itrset[0].nband = 0;
557  itrset[0].nbnodata = 1;
558  itrset[1].raster = rast2;
559  itrset[1].nband = 0;
560  itrset[1].nbnodata = 1;
561 
562  /* 1 raster, 0 distance, FIRST or SECOND or LAST or UNION or INTERSECTION */
563  userargs->rasters = 1;
564  userargs->rows = 1;
565  userargs->columns = 1;
566 
567  noerr = rt_raster_iterator(
568  itrset, 1,
569  ET_INTERSECTION, NULL,
570  PT_32BUI,
571  1, 0,
572  0, 0,
573  userargs,
575  &rtn
576  );
577  CU_ASSERT_EQUAL(noerr, ES_NONE);
578  CU_ASSERT_EQUAL(rt_raster_get_width(rtn), 5);
579  CU_ASSERT_EQUAL(rt_raster_get_height(rtn), 5);
580  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(rtn), 0, DBL_EPSILON);
581  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(rtn), 0, DBL_EPSILON);
582  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(rtn), 1, DBL_EPSILON);
583  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(rtn), -1, DBL_EPSILON);
584  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(rtn), 0, DBL_EPSILON);
585  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(rtn), 0, DBL_EPSILON);
586  CU_ASSERT_EQUAL(rt_raster_get_srid(rtn), 0);
587 
588  if (rtn != NULL) cu_free_raster(rtn);
589  rtn = NULL;
590 
591  /* 1 raster, 0 distance, FIRST or SECOND or LAST or UNION or INTERSECTION */
592  userargs->rasters = 1;
593  userargs->rows = 1;
594  userargs->columns = 1;
595 
596  noerr = rt_raster_iterator(
597  itrset, 1,
598  ET_UNION, NULL,
599  PT_32BUI,
600  1, 0,
601  0, 0,
602  userargs,
604  &rtn
605  );
606  CU_ASSERT_EQUAL(noerr, ES_NONE);
607  CU_ASSERT_EQUAL(rt_raster_get_width(rtn), 5);
608  CU_ASSERT_EQUAL(rt_raster_get_height(rtn), 5);
609  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(rtn), 0, DBL_EPSILON);
610  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(rtn), 0, DBL_EPSILON);
611  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(rtn), 1, DBL_EPSILON);
612  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(rtn), -1, DBL_EPSILON);
613  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(rtn), 0, DBL_EPSILON);
614  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(rtn), 0, DBL_EPSILON);
615  CU_ASSERT_EQUAL(rt_raster_get_srid(rtn), 0);
616 
617  if (rtn != NULL) cu_free_raster(rtn);
618  rtn = NULL;
619 
620  /* 2 raster, 0 distance, UNION */
621  userargs->rasters = 2;
622  userargs->rows = 1;
623  userargs->columns = 1;
624 
625  noerr = rt_raster_iterator(
626  itrset, 2,
627  ET_UNION, NULL,
628  PT_32BUI,
629  1, 0,
630  0, 0,
631  userargs,
633  &rtn
634  );
635  CU_ASSERT_EQUAL(noerr, ES_NONE);
636  CU_ASSERT_EQUAL(rt_raster_get_width(rtn), 6);
637  CU_ASSERT_EQUAL(rt_raster_get_height(rtn), 6);
638  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(rtn), 0, DBL_EPSILON);
639  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(rtn), 0, DBL_EPSILON);
640  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(rtn), 1, DBL_EPSILON);
641  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(rtn), -1, DBL_EPSILON);
642  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(rtn), 0, DBL_EPSILON);
643  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(rtn), 0, DBL_EPSILON);
644  CU_ASSERT_EQUAL(rt_raster_get_srid(rtn), 0);
645 
646  if (rtn != NULL) cu_free_raster(rtn);
647  rtn = NULL;
648 
649  /* 2 raster, 0 distance, INTERSECTION */
650  noerr = rt_raster_iterator(
651  itrset, 2,
652  ET_INTERSECTION, NULL,
653  PT_32BUI,
654  1, 0,
655  0, 0,
656  userargs,
658  &rtn
659  );
660  CU_ASSERT_EQUAL(noerr, ES_NONE);
661  CU_ASSERT_EQUAL(rt_raster_get_width(rtn), 4);
662  CU_ASSERT_EQUAL(rt_raster_get_height(rtn), 4);
663  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(rtn), 1, DBL_EPSILON);
664  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(rtn), -1, DBL_EPSILON);
665  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(rtn), 1, DBL_EPSILON);
666  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(rtn), -1, DBL_EPSILON);
667  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(rtn), 0, DBL_EPSILON);
668  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(rtn), 0, DBL_EPSILON);
669  CU_ASSERT_EQUAL(rt_raster_get_srid(rtn), 0);
670 
671  if (rtn != NULL) cu_free_raster(rtn);
672  rtn = NULL;
673 
674  /* 2 raster, 0 distance, FIRST */
675  noerr = rt_raster_iterator(
676  itrset, 2,
677  ET_FIRST, NULL,
678  PT_32BUI,
679  1, 0,
680  0, 0,
681  userargs,
683  &rtn
684  );
685  CU_ASSERT_EQUAL(noerr, ES_NONE);
686  CU_ASSERT_EQUAL(rt_raster_get_width(rtn), 5);
687  CU_ASSERT_EQUAL(rt_raster_get_height(rtn), 5);
688  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(rtn), 0, DBL_EPSILON);
689  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(rtn), 0, DBL_EPSILON);
690  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(rtn), 1, DBL_EPSILON);
691  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(rtn), -1, DBL_EPSILON);
692  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(rtn), 0, DBL_EPSILON);
693  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(rtn), 0, DBL_EPSILON);
694  CU_ASSERT_EQUAL(rt_raster_get_srid(rtn), 0);
695 
696  if (rtn != NULL) cu_free_raster(rtn);
697  rtn = NULL;
698 
699  /* 2 raster, 0 distance, LAST or SECOND */
700  noerr = rt_raster_iterator(
701  itrset, 2,
702  ET_LAST, NULL,
703  PT_32BUI,
704  1, 0,
705  0, 0,
706  userargs,
708  &rtn
709  );
710  CU_ASSERT_EQUAL(noerr, ES_NONE);
711  CU_ASSERT_EQUAL(rt_raster_get_width(rtn), 5);
712  CU_ASSERT_EQUAL(rt_raster_get_height(rtn), 5);
713  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(rtn), 1, DBL_EPSILON);
714  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(rtn), -1, DBL_EPSILON);
715  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(rtn), 1, DBL_EPSILON);
716  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(rtn), -1, DBL_EPSILON);
717  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(rtn), 0, DBL_EPSILON);
718  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(rtn), 0, DBL_EPSILON);
719  CU_ASSERT_EQUAL(rt_raster_get_srid(rtn), 0);
720 
721  if (rtn != NULL) cu_free_raster(rtn);
722  rtn = NULL;
723 
724  /* 2 raster, 0 distance, CUSTOM */
725  noerr = rt_raster_iterator(
726  itrset, 2,
727  ET_CUSTOM, rast3,
728  PT_32BUI,
729  1, 0,
730  0, 0,
731  userargs,
733  &rtn
734  );
735  CU_ASSERT_EQUAL(noerr, ES_NONE);
736  CU_ASSERT_EQUAL(rt_raster_get_width(rtn), 2);
737  CU_ASSERT_EQUAL(rt_raster_get_height(rtn), 2);
738  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(rtn), 1, DBL_EPSILON);
739  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(rtn), -3, DBL_EPSILON);
740  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(rtn), 1, DBL_EPSILON);
741  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(rtn), -1, DBL_EPSILON);
742  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(rtn), 0, DBL_EPSILON);
743  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(rtn), 0, DBL_EPSILON);
744  CU_ASSERT_EQUAL(rt_raster_get_srid(rtn), 0);
745 
746  if (rtn != NULL) cu_free_raster(rtn);
747  rtn = NULL;
748 
749  /* 2 raster, 1 distance, CUSTOM */
750  userargs->rasters = 2;
751  userargs->rows = 3;
752  userargs->columns = 3;
753 
754  noerr = rt_raster_iterator(
755  itrset, 2,
756  ET_CUSTOM, rast3,
757  PT_32BUI,
758  1, 0,
759  1, 1,
760  userargs,
762  &rtn
763  );
764  CU_ASSERT_EQUAL(noerr, ES_NONE);
765  CU_ASSERT_EQUAL(rt_raster_get_width(rtn), 2);
766  CU_ASSERT_EQUAL(rt_raster_get_height(rtn), 2);
767  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(rtn), 1, DBL_EPSILON);
768  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(rtn), -3, DBL_EPSILON);
769  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(rtn), 1, DBL_EPSILON);
770  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(rtn), -1, DBL_EPSILON);
771  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(rtn), 0, DBL_EPSILON);
772  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(rtn), 0, DBL_EPSILON);
773  CU_ASSERT_EQUAL(rt_raster_get_srid(rtn), 0);
774 
775  if (rtn != NULL) cu_free_raster(rtn);
776  rtn = NULL;
777 
778  rtdealloc(userargs);
779  rtdealloc(itrset);
780 
781  cu_free_raster(rast1);
782  cu_free_raster(rast2);
783  cu_free_raster(rast3);
784 
785  if (rtn != NULL) cu_free_raster(rtn);
786 }
787 
788 static void test_band_reclass() {
789  rt_reclassexpr *exprset;
790 
792  rt_band band;
793  uint16_t x;
794  uint16_t y;
795  double nodata;
796  int cnt = 2;
797  int i = 0;
798  int rtn;
799  rt_band newband;
800  double val;
801 
802  raster = rt_raster_new(100, 10);
803  CU_ASSERT(raster != NULL); /* or we're out of virtual memory */
804  band = cu_add_band(raster, PT_16BUI, 0, 0);
805  CU_ASSERT(band != NULL);
806  rt_band_set_nodata(band, 0, NULL);
807 
808  for (x = 0; x < 100; x++) {
809  for (y = 0; y < 10; y++) {
810  rtn = rt_band_set_pixel(band, x, y, x * y + (x + y), NULL);
811  }
812  }
813 
814  rt_band_get_nodata(band, &nodata);
815  CU_ASSERT_DOUBLE_EQUAL(nodata, 0, DBL_EPSILON);
816 
817  exprset = rtalloc(cnt * sizeof(rt_reclassexpr));
818  CU_ASSERT(exprset != NULL);
819 
820  for (i = 0; i < cnt; i++) {
821  exprset[i] = rtalloc(sizeof(struct rt_reclassexpr_t));
822  CU_ASSERT(exprset[i] != NULL);
823 
824  if (i == 0) {
825  /* nodata */
826  exprset[i]->src.min = 0;
827  exprset[i]->src.inc_min = 0;
828  exprset[i]->src.exc_min = 0;
829 
830  exprset[i]->src.max = 0;
831  exprset[i]->src.inc_max = 0;
832  exprset[i]->src.exc_max = 0;
833 
834  exprset[i]->dst.min = 0;
835  exprset[i]->dst.max = 0;
836  }
837  else {
838  /* range */
839  exprset[i]->src.min = 0;
840  exprset[i]->src.inc_min = 0;
841  exprset[i]->src.exc_min = 0;
842 
843  exprset[i]->src.max = 1000;
844  exprset[i]->src.inc_max = 1;
845  exprset[i]->src.exc_max = 0;
846 
847  exprset[i]->dst.min = 1;
848  exprset[i]->dst.max = 255;
849  }
850  }
851 
852  newband = rt_band_reclass(band, PT_8BUI, 0, 0, exprset, cnt);
853  CU_ASSERT(newband != NULL);
854 
855  rtn = rt_band_get_pixel(newband, 0, 0, &val, NULL);
856  CU_ASSERT_EQUAL(rtn, ES_NONE);
857  CU_ASSERT_DOUBLE_EQUAL(val, 0, DBL_EPSILON);
858 
859  rtn = rt_band_get_pixel(newband, 49, 5, &val, NULL);
860  CU_ASSERT_EQUAL(rtn, ES_NONE);
861  CU_ASSERT_DOUBLE_EQUAL(val, 77, DBL_EPSILON);
862 
863  rtn = rt_band_get_pixel(newband, 99, 9, &val, NULL);
864  CU_ASSERT_EQUAL(rtn, ES_NONE);
865  CU_ASSERT_DOUBLE_EQUAL(val, 255, DBL_EPSILON);
866 
867  for (i = cnt - 1; i >= 0; i--) rtdealloc(exprset[i]);
868  rtdealloc(exprset);
869  cu_free_raster(raster);
870 
871  rt_band_destroy(newband);
872 }
873 
874 static void test_raster_colormap() {
876  rt_raster rtn;
877  rt_band band;
878  int x;
879  int y;
880  rt_colormap colormap = NULL;
881  double value;
882  int nodata;
883 
884  raster = rt_raster_new(9, 9);
885  CU_ASSERT(raster != NULL); /* or we're out of virtual memory */
886  band = cu_add_band(raster, PT_8BUI, 0, 0);
887  CU_ASSERT(band != NULL);
888  rt_band_set_nodata(band, 0, NULL);
889 
890  for (y = 0; y < 9; y++) {
891  for (x = 0; x < 9; x++) {
892  rt_band_set_pixel(band, x, y, x, NULL);
893  }
894  }
895 
896  colormap = (rt_colormap) rtalloc(sizeof(struct rt_colormap_t));
897  CU_ASSERT(colormap != NULL);
898  colormap->nentry = 3;
899  colormap->entry = (rt_colormap_entry) rtalloc(sizeof(struct rt_colormap_entry_t) * colormap->nentry);
900  CU_ASSERT(colormap->entry != NULL);
901 
902  colormap->entry[0].isnodata = 0;
903  colormap->entry[0].value = 8;
904  colormap->entry[0].color[0] = 255;
905  colormap->entry[0].color[1] = 255;
906  colormap->entry[0].color[2] = 255;
907  colormap->entry[0].color[3] = 255;
908 
909  colormap->entry[1].isnodata = 0;
910  colormap->entry[1].value = 3;
911  colormap->entry[1].color[0] = 127;
912  colormap->entry[1].color[1] = 127;
913  colormap->entry[1].color[2] = 127;
914  colormap->entry[1].color[3] = 255;
915 
916  colormap->entry[2].isnodata = 0;
917  colormap->entry[2].value = 0;
918  colormap->entry[2].color[0] = 0;
919  colormap->entry[2].color[1] = 0;
920  colormap->entry[2].color[2] = 0;
921  colormap->entry[2].color[3] = 255;
922 
923  /* 2 colors, 3 entries, INTERPOLATE */
924  colormap->ncolor = 2;
925  colormap->method = CM_INTERPOLATE;
926 
927  rtn = rt_raster_colormap(
928  raster, 0,
929  colormap
930  );
931  CU_ASSERT(rtn != NULL);
932  CU_ASSERT_EQUAL(rt_raster_get_num_bands(rtn), colormap->ncolor);
933 
934  band = rt_raster_get_band(rtn, 0);
935  CU_ASSERT(band != NULL);
936  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 0, 0, &value, &nodata), ES_NONE);
937  CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
938  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 3, 0, &value, &nodata), ES_NONE);
939  CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
940  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 8, 0, &value, &nodata), ES_NONE);
941  CU_ASSERT_DOUBLE_EQUAL(value, 255, DBL_EPSILON);
942 
943  cu_free_raster(rtn);
944 
945  /* 4 colors, 3 entries, INTERPOLATE */
946  colormap->ncolor = 4;
947 
948  rtn = rt_raster_colormap(
949  raster, 0,
950  colormap
951  );
952  CU_ASSERT(rtn != NULL);
953  CU_ASSERT_EQUAL(rt_raster_get_num_bands(rtn), colormap->ncolor);
954  cu_free_raster(rtn);
955 
956  /* 4 colors, 3 entries, EXACT */
957  colormap->method = CM_EXACT;
958 
959  rtn = rt_raster_colormap(
960  raster, 0,
961  colormap
962  );
963  CU_ASSERT(rtn != NULL);
964  CU_ASSERT_EQUAL(rt_raster_get_num_bands(rtn), colormap->ncolor);
965 
966  band = rt_raster_get_band(rtn, 0);
967  CU_ASSERT(band != NULL);
968  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 0, 0, &value, &nodata), ES_NONE);
969  CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
970  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 3, 0, &value, &nodata), ES_NONE);
971  CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
972  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 8, 0, &value, &nodata), ES_NONE);
973  CU_ASSERT_DOUBLE_EQUAL(value, 255, DBL_EPSILON);
974  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 1, 0, &value, &nodata), ES_NONE);
975  CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
976  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 7, 0, &value, &nodata), ES_NONE);
977  CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
978 
979  cu_free_raster(rtn);
980 
981  /* 4 colors, 3 entries, NEAREST */
982  colormap->method = CM_NEAREST;
983 
984  rtn = rt_raster_colormap(
985  raster, 0,
986  colormap
987  );
988  CU_ASSERT(rtn != NULL);
989  CU_ASSERT_EQUAL(rt_raster_get_num_bands(rtn), colormap->ncolor);
990 
991  band = rt_raster_get_band(rtn, 0);
992  CU_ASSERT(band != NULL);
993  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 0, 0, &value, &nodata), ES_NONE);
994  CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
995  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 3, 0, &value, &nodata), ES_NONE);
996  CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
997  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 8, 0, &value, &nodata), ES_NONE);
998  CU_ASSERT_DOUBLE_EQUAL(value, 255, DBL_EPSILON);
999  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 1, 0, &value, &nodata), ES_NONE);
1000  CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
1001  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 2, 0, &value, &nodata), ES_NONE);
1002  CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1003  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 4, 0, &value, &nodata), ES_NONE);
1004  CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1005  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 7, 0, &value, &nodata), ES_NONE);
1006  CU_ASSERT_DOUBLE_EQUAL(value, 255, DBL_EPSILON);
1007 
1008  cu_free_raster(rtn);
1009 
1010  /* 4 colors, 2 entries, NEAREST */
1011  colormap->nentry = 2;
1012  colormap->method = CM_NEAREST;
1013 
1014  rtn = rt_raster_colormap(
1015  raster, 0,
1016  colormap
1017  );
1018  CU_ASSERT(rtn != NULL);
1019  CU_ASSERT_EQUAL(rt_raster_get_num_bands(rtn), colormap->ncolor);
1020 
1021  band = rt_raster_get_band(rtn, 0);
1022  CU_ASSERT(band != NULL);
1023  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 0, 0, &value, &nodata), ES_NONE);
1024  CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1025  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 3, 0, &value, &nodata), ES_NONE);
1026  CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1027  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 8, 0, &value, &nodata), ES_NONE);
1028  CU_ASSERT_DOUBLE_EQUAL(value, 255, DBL_EPSILON);
1029  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 1, 0, &value, &nodata), ES_NONE);
1030  CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1031  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 2, 0, &value, &nodata), ES_NONE);
1032  CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1033  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 4, 0, &value, &nodata), ES_NONE);
1034  CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1035  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 7, 0, &value, &nodata), ES_NONE);
1036  CU_ASSERT_DOUBLE_EQUAL(value, 255, DBL_EPSILON);
1037 
1038  cu_free_raster(rtn);
1039 
1040  rtdealloc(colormap->entry);
1041  rtdealloc(colormap);
1042 
1043  cu_free_raster(raster);
1044 
1045  /* new set of tests */
1046  raster = rt_raster_new(10, 10);
1047  CU_ASSERT(raster != NULL); /* or we're out of virtual memory */
1048  band = cu_add_band(raster, PT_8BUI, 0, 0);
1049  CU_ASSERT(band != NULL);
1050  rt_band_set_nodata(band, 0, NULL);
1051 
1052  for (y = 0; y < 10; y++) {
1053  for (x = 0; x < 10; x++) {
1054  rt_band_set_pixel(band, x, y, (x * y) + x, NULL);
1055  }
1056  }
1057 
1058  colormap = (rt_colormap) rtalloc(sizeof(struct rt_colormap_t));
1059  CU_ASSERT(colormap != NULL);
1060  colormap->nentry = 10;
1061  colormap->entry = (rt_colormap_entry) rtalloc(sizeof(struct rt_colormap_entry_t) * colormap->nentry);
1062  CU_ASSERT(colormap->entry != NULL);
1063 
1064  colormap->entry[0].isnodata = 0;
1065  colormap->entry[0].value = 90;
1066  colormap->entry[0].color[0] = 255;
1067  colormap->entry[0].color[1] = 255;
1068  colormap->entry[0].color[2] = 255;
1069  colormap->entry[0].color[3] = 255;
1070 
1071  colormap->entry[1].isnodata = 0;
1072  colormap->entry[1].value = 80;
1073  colormap->entry[1].color[0] = 255;
1074  colormap->entry[1].color[1] = 227;
1075  colormap->entry[1].color[2] = 227;
1076  colormap->entry[1].color[3] = 255;
1077 
1078  colormap->entry[2].isnodata = 0;
1079  colormap->entry[2].value = 70;
1080  colormap->entry[2].color[0] = 255;
1081  colormap->entry[2].color[1] = 198;
1082  colormap->entry[2].color[2] = 198;
1083  colormap->entry[2].color[3] = 255;
1084 
1085  colormap->entry[3].isnodata = 0;
1086  colormap->entry[3].value = 60;
1087  colormap->entry[3].color[0] = 255;
1088  colormap->entry[3].color[1] = 170;
1089  colormap->entry[3].color[2] = 170;
1090  colormap->entry[3].color[3] = 255;
1091 
1092  colormap->entry[4].isnodata = 0;
1093  colormap->entry[4].value = 50;
1094  colormap->entry[4].color[0] = 255;
1095  colormap->entry[4].color[1] = 142;
1096  colormap->entry[4].color[2] = 142;
1097  colormap->entry[4].color[3] = 255;
1098 
1099  colormap->entry[5].isnodata = 0;
1100  colormap->entry[5].value = 40;
1101  colormap->entry[5].color[0] = 255;
1102  colormap->entry[5].color[1] = 113;
1103  colormap->entry[5].color[2] = 113;
1104  colormap->entry[5].color[3] = 255;
1105 
1106  colormap->entry[6].isnodata = 0;
1107  colormap->entry[6].value = 30;
1108  colormap->entry[6].color[0] = 255;
1109  colormap->entry[6].color[1] = 85;
1110  colormap->entry[6].color[2] = 85;
1111  colormap->entry[6].color[3] = 255;
1112 
1113  colormap->entry[7].isnodata = 0;
1114  colormap->entry[7].value = 20;
1115  colormap->entry[7].color[0] = 255;
1116  colormap->entry[7].color[1] = 57;
1117  colormap->entry[7].color[2] = 57;
1118  colormap->entry[7].color[3] = 255;
1119 
1120  colormap->entry[8].isnodata = 0;
1121  colormap->entry[8].value = 10;
1122  colormap->entry[8].color[0] = 255;
1123  colormap->entry[8].color[1] = 28;
1124  colormap->entry[8].color[2] = 28;
1125  colormap->entry[8].color[3] = 255;
1126 
1127  colormap->entry[9].isnodata = 0;
1128  colormap->entry[9].value = 0;
1129  colormap->entry[9].color[0] = 255;
1130  colormap->entry[9].color[1] = 0;
1131  colormap->entry[9].color[2] = 0;
1132  colormap->entry[9].color[3] = 255;
1133 
1134  /* 2 colors, 3 entries, INTERPOLATE */
1135  colormap->ncolor = 4;
1136  colormap->method = CM_INTERPOLATE;
1137 
1138  rtn = rt_raster_colormap(
1139  raster, 0,
1140  colormap
1141  );
1142  CU_ASSERT(rtn != NULL);
1143  CU_ASSERT_EQUAL(rt_raster_get_num_bands(rtn), colormap->ncolor);
1144 
1145  band = rt_raster_get_band(rtn, 2);
1146  CU_ASSERT(band != NULL);
1147  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 0, 0, &value, &nodata), ES_NONE);
1148  CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
1149  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 5, 0, &value, &nodata), ES_NONE);
1150  CU_ASSERT_DOUBLE_EQUAL(value, 14, DBL_EPSILON);
1151  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 6, 0, &value, &nodata), ES_NONE);
1152  CU_ASSERT_DOUBLE_EQUAL(value, 17, DBL_EPSILON);
1153  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 9, 0, &value, &nodata), ES_NONE);
1154  CU_ASSERT_DOUBLE_EQUAL(value, 25, DBL_EPSILON);
1155 
1156  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 2, 4, &value, &nodata), ES_NONE);
1157  CU_ASSERT_DOUBLE_EQUAL(value, 28, DBL_EPSILON);
1158  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 3, 4, &value, &nodata), ES_NONE);
1159  CU_ASSERT_DOUBLE_EQUAL(value, 43, DBL_EPSILON);
1160  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 4, 4, &value, &nodata), ES_NONE);
1161  CU_ASSERT_DOUBLE_EQUAL(value, 57, DBL_EPSILON);
1162 
1163  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 6, 9, &value, &nodata), ES_NONE);
1164  CU_ASSERT_DOUBLE_EQUAL(value, 170, DBL_EPSILON);
1165  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 7, 9, &value, &nodata), ES_NONE);
1166  CU_ASSERT_DOUBLE_EQUAL(value, 198, DBL_EPSILON);
1167  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 8, 9, &value, &nodata), ES_NONE);
1168  CU_ASSERT_DOUBLE_EQUAL(value, 227, DBL_EPSILON);
1169 
1170  cu_free_raster(rtn);
1171 
1172  rtdealloc(colormap->entry);
1173  rtdealloc(colormap);
1174 
1175  cu_free_raster(raster);
1176 }
1177 
1178 /* register tests */
1179 void mapalgebra_suite_setup(void);
1181 {
1182  CU_pSuite suite = CU_add_suite("mapalgebra", NULL, NULL);
1186 }
1187 
int rt_raster_get_num_bands(rt_raster raster)
Definition: rt_api.c:5677
void mapalgebra_suite_setup(void)
rt_raster rt_raster_colormap(rt_raster raster, int nband, rt_colormap colormap)
Returns a new raster with up to four 8BUI bands (RGBA) from applying a colormap to the user-specified...
Definition: rt_api.c:14937
void rtdealloc(void *mem)
Definition: rt_api.c:882
double rt_raster_get_y_offset(rt_raster raster)
Get raster y offset, in projection units.
Definition: rt_api.c:5527
uint16_t nentry
Definition: rt_api.h:2410
static int testRasterIterator7_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
static int testRasterIterator3_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
static int testRasterIterator1_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
Definition: cu_mapalgebra.c:35
rt_errorstate rt_band_set_nodata(rt_band band, double val, int *converted)
Set nodata value.
Definition: rt_api.c:2061
struct rt_reclassexpr_t::rt_reclassrange dst
static int testRasterIterator4_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
double rt_raster_get_x_skew(rt_raster raster)
Get skew about the X axis.
Definition: rt_api.c:5486
uint8_t color[4]
Definition: rt_api.h:2399
tuple band
Definition: ovdump.py:57
double rt_raster_get_y_skew(rt_raster raster)
Get skew about the Y axis.
Definition: rt_api.c:5495
uint32_t rows
Definition: rt_api.h:2370
double rt_raster_get_x_offset(rt_raster raster)
Get raster x offset, in projection units.
Definition: rt_api.c:5518
tuple raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:123
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
Definition: rt_api.c:5661
static int testRasterIterator6_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
void cu_free_raster(rt_raster raster)
struct rt_colormap_t * rt_colormap
Definition: rt_api.h:148
rt_band cu_add_band(rt_raster raster, rt_pixtype pixtype, int hasnodata, double nodataval)
void rt_raster_set_scale(rt_raster raster, double scaleX, double scaleY)
Set scale in projection units.
Definition: rt_api.c:5442
struct rt_reclassexpr_t::rt_reclassrange src
uint32_t columns
Definition: rt_api.h:2372
enum rt_colormap_t::@15 method
uint16_t rasters
Definition: rt_api.h:2368
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
Definition: rt_api.c:3058
#define PG_ADD_TEST(suite, testfunc)
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
Definition: rt_api.c:2549
rt_colormap_entry entry
Definition: rt_api.h:2411
double rt_raster_get_x_scale(rt_raster raster)
Get scale X in projection units.
Definition: rt_api.c:5455
rt_raster raster
Definition: rt_api.h:2360
struct _callback_userargs_t * _callback_userargs
Definition: cu_mapalgebra.c:27
tuple x
Definition: pixval.py:53
rt_band rt_band_reclass(rt_band srcband, rt_pixtype pixtype, uint32_t hasnodata, double nodataval, rt_reclassexpr *exprset, int exprcount)
Returns new band with values reclassified.
Definition: rt_api.c:5009
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition: rt_api.c:1650
uint16_t nband
Definition: rt_api.h:2361
static void test_raster_colormap()
uint16_t rt_raster_get_height(rt_raster raster)
Definition: rt_api.c:5434
void * rtalloc(size_t size)
Raster core memory management functions.
Definition: rt_api.c:867
double *** values
Definition: rt_api.h:2376
rt_band rt_raster_get_band(rt_raster raster, int n)
Return Nth band, or NULL if unavailable.
Definition: rt_api.c:5686
Definition: rt_api.h:2396
uint8_t nbnodata
Definition: rt_api.h:2362
rt_errorstate rt_raster_iterator(rt_iterator itrset, uint16_t itrcount, rt_extenttype extenttype, rt_raster customextent, rt_pixtype pixtype, uint8_t hasnodata, double nodataval, uint16_t distancex, uint16_t distancey, void *userarg, int(*callback)(rt_iterator_arg arg, void *userarg, double *value, int *nodata), rt_raster *rtnraster)
n-raster iterator.
Definition: rt_api.c:13927
double rt_raster_get_y_scale(rt_raster raster)
Get scale Y in projection units.
Definition: rt_api.c:5464
static void test_band_reclass()
static int testRasterIterator2_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
Definition: cu_mapalgebra.c:95
static int testRasterIterator5_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
Definition: rt_api.c:5353
int *** nodata
Definition: rt_api.h:2378
static void test_raster_iterator()
int isnodata
Definition: rt_api.h:2397
struct rt_colormap_entry_t * rt_colormap_entry
Definition: rt_api.h:147
int dst_pixel[2]
Definition: rt_api.h:2384
void rt_raster_set_offsets(rt_raster raster, double x, double y)
Set insertion points in projection units.
Definition: rt_api.c:5504
double value
Definition: rt_api.h:2398
tuple y
Definition: pixval.py:54
uint16_t rt_raster_get_width(rt_raster raster)
Definition: rt_api.c:5426
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