PostGIS  2.5.0beta2dev-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;
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] == 0 &&
244  arg->dst_pixel[1] == 2
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  NULL,
574  userargs,
576  &rtn
577  );
578  CU_ASSERT_EQUAL(noerr, ES_NONE);
579  CU_ASSERT_EQUAL(rt_raster_get_width(rtn), 5);
580  CU_ASSERT_EQUAL(rt_raster_get_height(rtn), 5);
581  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(rtn), 0, DBL_EPSILON);
582  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(rtn), 0, DBL_EPSILON);
583  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(rtn), 1, DBL_EPSILON);
584  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(rtn), -1, DBL_EPSILON);
585  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(rtn), 0, DBL_EPSILON);
586  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(rtn), 0, DBL_EPSILON);
587  CU_ASSERT_EQUAL(rt_raster_get_srid(rtn), 0);
588 
589  if (rtn != NULL) cu_free_raster(rtn);
590  rtn = NULL;
591 
592  /* 1 raster, 0 distance, FIRST or SECOND or LAST or UNION or INTERSECTION */
593  userargs->rasters = 1;
594  userargs->rows = 1;
595  userargs->columns = 1;
596 
597  noerr = rt_raster_iterator(
598  itrset, 1,
599  ET_UNION, NULL,
600  PT_32BUI,
601  1, 0,
602  0, 0,
603  NULL,
604  userargs,
606  &rtn
607  );
608  CU_ASSERT_EQUAL(noerr, ES_NONE);
609  CU_ASSERT_EQUAL(rt_raster_get_width(rtn), 5);
610  CU_ASSERT_EQUAL(rt_raster_get_height(rtn), 5);
611  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(rtn), 0, DBL_EPSILON);
612  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(rtn), 0, DBL_EPSILON);
613  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(rtn), 1, DBL_EPSILON);
614  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(rtn), -1, DBL_EPSILON);
615  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(rtn), 0, DBL_EPSILON);
616  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(rtn), 0, DBL_EPSILON);
617  CU_ASSERT_EQUAL(rt_raster_get_srid(rtn), 0);
618 
619  if (rtn != NULL) cu_free_raster(rtn);
620  rtn = NULL;
621 
622  /* 2 raster, 0 distance, UNION */
623  userargs->rasters = 2;
624  userargs->rows = 1;
625  userargs->columns = 1;
626 
627  noerr = rt_raster_iterator(
628  itrset, 2,
629  ET_UNION, NULL,
630  PT_32BUI,
631  1, 0,
632  0, 0,
633  NULL,
634  userargs,
636  &rtn
637  );
638  CU_ASSERT_EQUAL(noerr, ES_NONE);
639  CU_ASSERT_EQUAL(rt_raster_get_width(rtn), 6);
640  CU_ASSERT_EQUAL(rt_raster_get_height(rtn), 6);
641  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(rtn), 0, DBL_EPSILON);
642  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(rtn), 0, DBL_EPSILON);
643  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(rtn), 1, DBL_EPSILON);
644  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(rtn), -1, DBL_EPSILON);
645  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(rtn), 0, DBL_EPSILON);
646  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(rtn), 0, DBL_EPSILON);
647  CU_ASSERT_EQUAL(rt_raster_get_srid(rtn), 0);
648 
649  if (rtn != NULL) cu_free_raster(rtn);
650  rtn = NULL;
651 
652  /* 2 raster, 0 distance, INTERSECTION */
653  noerr = rt_raster_iterator(
654  itrset, 2,
655  ET_INTERSECTION, NULL,
656  PT_32BUI,
657  1, 0,
658  0, 0,
659  NULL,
660  userargs,
662  &rtn
663  );
664  CU_ASSERT_EQUAL(noerr, ES_NONE);
665  CU_ASSERT_EQUAL(rt_raster_get_width(rtn), 4);
666  CU_ASSERT_EQUAL(rt_raster_get_height(rtn), 4);
667  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(rtn), 1, DBL_EPSILON);
668  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(rtn), -1, DBL_EPSILON);
669  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(rtn), 1, DBL_EPSILON);
670  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(rtn), -1, DBL_EPSILON);
671  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(rtn), 0, DBL_EPSILON);
672  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(rtn), 0, DBL_EPSILON);
673  CU_ASSERT_EQUAL(rt_raster_get_srid(rtn), 0);
674 
675  if (rtn != NULL) cu_free_raster(rtn);
676  rtn = NULL;
677 
678  /* 2 raster, 0 distance, FIRST */
679  noerr = rt_raster_iterator(
680  itrset, 2,
681  ET_FIRST, NULL,
682  PT_32BUI,
683  1, 0,
684  0, 0,
685  NULL,
686  userargs,
688  &rtn
689  );
690  CU_ASSERT_EQUAL(noerr, ES_NONE);
691  CU_ASSERT_EQUAL(rt_raster_get_width(rtn), 5);
692  CU_ASSERT_EQUAL(rt_raster_get_height(rtn), 5);
693  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(rtn), 0, DBL_EPSILON);
694  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(rtn), 0, DBL_EPSILON);
695  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(rtn), 1, DBL_EPSILON);
696  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(rtn), -1, DBL_EPSILON);
697  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(rtn), 0, DBL_EPSILON);
698  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(rtn), 0, DBL_EPSILON);
699  CU_ASSERT_EQUAL(rt_raster_get_srid(rtn), 0);
700 
701  if (rtn != NULL) cu_free_raster(rtn);
702  rtn = NULL;
703 
704  /* 2 raster, 0 distance, LAST or SECOND */
705  noerr = rt_raster_iterator(
706  itrset, 2,
707  ET_LAST, NULL,
708  PT_32BUI,
709  1, 0,
710  0, 0,
711  NULL,
712  userargs,
714  &rtn
715  );
716  CU_ASSERT_EQUAL(noerr, ES_NONE);
717  CU_ASSERT_EQUAL(rt_raster_get_width(rtn), 5);
718  CU_ASSERT_EQUAL(rt_raster_get_height(rtn), 5);
719  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(rtn), 1, DBL_EPSILON);
720  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(rtn), -1, DBL_EPSILON);
721  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(rtn), 1, DBL_EPSILON);
722  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(rtn), -1, DBL_EPSILON);
723  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(rtn), 0, DBL_EPSILON);
724  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(rtn), 0, DBL_EPSILON);
725  CU_ASSERT_EQUAL(rt_raster_get_srid(rtn), 0);
726 
727  if (rtn != NULL) cu_free_raster(rtn);
728  rtn = NULL;
729 
730  /* 2 raster, 0 distance, CUSTOM */
731  noerr = rt_raster_iterator(
732  itrset, 2,
733  ET_CUSTOM, rast3,
734  PT_32BUI,
735  1, 0,
736  0, 0,
737  NULL,
738  userargs,
740  &rtn
741  );
742  CU_ASSERT_EQUAL(noerr, ES_NONE);
743  CU_ASSERT_EQUAL(rt_raster_get_width(rtn), 2);
744  CU_ASSERT_EQUAL(rt_raster_get_height(rtn), 2);
745  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(rtn), 1, DBL_EPSILON);
746  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(rtn), -3, DBL_EPSILON);
747  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(rtn), 1, DBL_EPSILON);
748  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(rtn), -1, DBL_EPSILON);
749  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(rtn), 0, DBL_EPSILON);
750  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(rtn), 0, DBL_EPSILON);
751  CU_ASSERT_EQUAL(rt_raster_get_srid(rtn), 0);
752 
753  if (rtn != NULL) cu_free_raster(rtn);
754  rtn = NULL;
755 
756  /* 2 raster, 1 distance, CUSTOM */
757  userargs->rasters = 2;
758  userargs->rows = 3;
759  userargs->columns = 3;
760 
761  noerr = rt_raster_iterator(
762  itrset, 2,
763  ET_CUSTOM, rast3,
764  PT_32BUI,
765  1, 0,
766  1, 1,
767  NULL,
768  userargs,
770  &rtn
771  );
772  CU_ASSERT_EQUAL(noerr, ES_NONE);
773  CU_ASSERT_EQUAL(rt_raster_get_width(rtn), 2);
774  CU_ASSERT_EQUAL(rt_raster_get_height(rtn), 2);
775  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(rtn), 1, DBL_EPSILON);
776  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(rtn), -3, DBL_EPSILON);
777  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(rtn), 1, DBL_EPSILON);
778  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(rtn), -1, DBL_EPSILON);
779  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(rtn), 0, DBL_EPSILON);
780  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(rtn), 0, DBL_EPSILON);
781  CU_ASSERT_EQUAL(rt_raster_get_srid(rtn), 0);
782 
783  if (rtn != NULL) cu_free_raster(rtn);
784  rtn = NULL;
785 
786  rtdealloc(userargs);
787  rtdealloc(itrset);
788 
789  cu_free_raster(rast1);
790  cu_free_raster(rast2);
791  cu_free_raster(rast3);
792 
793  if (rtn != NULL) cu_free_raster(rtn);
794 }
795 
796 static void test_band_reclass() {
797  rt_reclassexpr *exprset;
798 
800  rt_band band;
801  uint16_t x;
802  uint16_t y;
803  double nodata;
804  int cnt = 2;
805  int i = 0;
806  int rtn;
807  rt_band newband;
808  double val;
809 
810  raster = rt_raster_new(100, 10);
811  CU_ASSERT(raster != NULL); /* or we're out of virtual memory */
812  band = cu_add_band(raster, PT_16BUI, 0, 0);
813  CU_ASSERT(band != NULL);
814  rt_band_set_nodata(band, 0, NULL);
815 
816  for (x = 0; x < 100; x++) {
817  for (y = 0; y < 10; y++) {
818  rtn = rt_band_set_pixel(band, x, y, x * y + (x + y), NULL);
819  }
820  }
821 
822  rt_band_get_nodata(band, &nodata);
823  CU_ASSERT_DOUBLE_EQUAL(nodata, 0, DBL_EPSILON);
824 
825  exprset = rtalloc(cnt * sizeof(rt_reclassexpr));
826  CU_ASSERT(exprset != NULL);
827 
828  for (i = 0; i < cnt; i++) {
829  exprset[i] = rtalloc(sizeof(struct rt_reclassexpr_t));
830  CU_ASSERT(exprset[i] != NULL);
831 
832  if (i == 0) {
833  /* nodata */
834  exprset[i]->src.min = 0;
835  exprset[i]->src.inc_min = 0;
836  exprset[i]->src.exc_min = 0;
837 
838  exprset[i]->src.max = 0;
839  exprset[i]->src.inc_max = 0;
840  exprset[i]->src.exc_max = 0;
841 
842  exprset[i]->dst.min = 0;
843  exprset[i]->dst.max = 0;
844  }
845  else {
846  /* range */
847  exprset[i]->src.min = 0;
848  exprset[i]->src.inc_min = 0;
849  exprset[i]->src.exc_min = 0;
850 
851  exprset[i]->src.max = 1000;
852  exprset[i]->src.inc_max = 1;
853  exprset[i]->src.exc_max = 0;
854 
855  exprset[i]->dst.min = 1;
856  exprset[i]->dst.max = 255;
857  }
858  }
859 
860  newband = rt_band_reclass(band, PT_8BUI, 0, 0, exprset, cnt);
861  CU_ASSERT(newband != NULL);
862 
863  rtn = rt_band_get_pixel(newband, 0, 0, &val, NULL);
864  CU_ASSERT_EQUAL(rtn, ES_NONE);
865  CU_ASSERT_DOUBLE_EQUAL(val, 0, DBL_EPSILON);
866 
867  rtn = rt_band_get_pixel(newband, 49, 5, &val, NULL);
868  CU_ASSERT_EQUAL(rtn, ES_NONE);
869  CU_ASSERT_DOUBLE_EQUAL(val, 77, DBL_EPSILON);
870 
871  rtn = rt_band_get_pixel(newband, 99, 9, &val, NULL);
872  CU_ASSERT_EQUAL(rtn, ES_NONE);
873  CU_ASSERT_DOUBLE_EQUAL(val, 255, DBL_EPSILON);
874 
875  for (i = cnt - 1; i >= 0; i--) rtdealloc(exprset[i]);
876  rtdealloc(exprset);
877  cu_free_raster(raster);
878 
879  rt_band_destroy(newband);
880 }
881 
882 static void test_raster_colormap() {
884  rt_raster rtn;
885  rt_band band;
886  int x;
887  int y;
888  rt_colormap colormap = NULL;
889  double value;
890  int nodata;
891 
892  raster = rt_raster_new(9, 9);
893  CU_ASSERT(raster != NULL); /* or we're out of virtual memory */
894  band = cu_add_band(raster, PT_8BUI, 0, 0);
895  CU_ASSERT(band != NULL);
896  rt_band_set_nodata(band, 0, NULL);
897 
898  for (y = 0; y < 9; y++) {
899  for (x = 0; x < 9; x++) {
900  rt_band_set_pixel(band, x, y, x, NULL);
901  }
902  }
903 
904  colormap = (rt_colormap) rtalloc(sizeof(struct rt_colormap_t));
905  CU_ASSERT(colormap != NULL);
906  colormap->nentry = 3;
907  colormap->entry = (rt_colormap_entry) rtalloc(sizeof(struct rt_colormap_entry_t) * colormap->nentry);
908  CU_ASSERT(colormap->entry != NULL);
909 
910  colormap->entry[0].isnodata = 0;
911  colormap->entry[0].value = 8;
912  colormap->entry[0].color[0] = 255;
913  colormap->entry[0].color[1] = 255;
914  colormap->entry[0].color[2] = 255;
915  colormap->entry[0].color[3] = 255;
916 
917  colormap->entry[1].isnodata = 0;
918  colormap->entry[1].value = 3;
919  colormap->entry[1].color[0] = 127;
920  colormap->entry[1].color[1] = 127;
921  colormap->entry[1].color[2] = 127;
922  colormap->entry[1].color[3] = 255;
923 
924  colormap->entry[2].isnodata = 0;
925  colormap->entry[2].value = 0;
926  colormap->entry[2].color[0] = 0;
927  colormap->entry[2].color[1] = 0;
928  colormap->entry[2].color[2] = 0;
929  colormap->entry[2].color[3] = 255;
930 
931  /* 2 colors, 3 entries, INTERPOLATE */
932  colormap->ncolor = 2;
933  colormap->method = CM_INTERPOLATE;
934 
935  rtn = rt_raster_colormap(
936  raster, 0,
937  colormap
938  );
939  CU_ASSERT(rtn != NULL);
940  CU_ASSERT_EQUAL(rt_raster_get_num_bands(rtn), colormap->ncolor);
941 
942  band = rt_raster_get_band(rtn, 0);
943  CU_ASSERT(band != NULL);
944  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 0, 0, &value, &nodata), ES_NONE);
945  CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
946  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 3, 0, &value, &nodata), ES_NONE);
947  CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
948  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 8, 0, &value, &nodata), ES_NONE);
949  CU_ASSERT_DOUBLE_EQUAL(value, 255, DBL_EPSILON);
950 
951  cu_free_raster(rtn);
952 
953  /* 4 colors, 3 entries, INTERPOLATE */
954  colormap->ncolor = 4;
955 
956  rtn = rt_raster_colormap(
957  raster, 0,
958  colormap
959  );
960  CU_ASSERT(rtn != NULL);
961  CU_ASSERT_EQUAL(rt_raster_get_num_bands(rtn), colormap->ncolor);
962  cu_free_raster(rtn);
963 
964  /* 4 colors, 3 entries, EXACT */
965  colormap->method = CM_EXACT;
966 
967  rtn = rt_raster_colormap(
968  raster, 0,
969  colormap
970  );
971  CU_ASSERT(rtn != NULL);
972  CU_ASSERT_EQUAL(rt_raster_get_num_bands(rtn), colormap->ncolor);
973 
974  band = rt_raster_get_band(rtn, 0);
975  CU_ASSERT(band != NULL);
976  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 0, 0, &value, &nodata), ES_NONE);
977  CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
978  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 3, 0, &value, &nodata), ES_NONE);
979  CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
980  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 8, 0, &value, &nodata), ES_NONE);
981  CU_ASSERT_DOUBLE_EQUAL(value, 255, DBL_EPSILON);
982  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 1, 0, &value, &nodata), ES_NONE);
983  CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
984  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 7, 0, &value, &nodata), ES_NONE);
985  CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
986 
987  cu_free_raster(rtn);
988 
989  /* 4 colors, 3 entries, NEAREST */
990  colormap->method = CM_NEAREST;
991 
992  rtn = rt_raster_colormap(
993  raster, 0,
994  colormap
995  );
996  CU_ASSERT(rtn != NULL);
997  CU_ASSERT_EQUAL(rt_raster_get_num_bands(rtn), colormap->ncolor);
998 
999  band = rt_raster_get_band(rtn, 0);
1000  CU_ASSERT(band != NULL);
1001  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 0, 0, &value, &nodata), ES_NONE);
1002  CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
1003  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 3, 0, &value, &nodata), ES_NONE);
1004  CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1005  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 8, 0, &value, &nodata), ES_NONE);
1006  CU_ASSERT_DOUBLE_EQUAL(value, 255, DBL_EPSILON);
1007  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 1, 0, &value, &nodata), ES_NONE);
1008  CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
1009  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 2, 0, &value, &nodata), ES_NONE);
1010  CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1011  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 4, 0, &value, &nodata), ES_NONE);
1012  CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1013  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 7, 0, &value, &nodata), ES_NONE);
1014  CU_ASSERT_DOUBLE_EQUAL(value, 255, DBL_EPSILON);
1015 
1016  cu_free_raster(rtn);
1017 
1018  /* 4 colors, 2 entries, NEAREST */
1019  colormap->nentry = 2;
1020  colormap->method = CM_NEAREST;
1021 
1022  rtn = rt_raster_colormap(
1023  raster, 0,
1024  colormap
1025  );
1026  CU_ASSERT(rtn != NULL);
1027  CU_ASSERT_EQUAL(rt_raster_get_num_bands(rtn), colormap->ncolor);
1028 
1029  band = rt_raster_get_band(rtn, 0);
1030  CU_ASSERT(band != NULL);
1031  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 0, 0, &value, &nodata), ES_NONE);
1032  CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1033  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 3, 0, &value, &nodata), ES_NONE);
1034  CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1035  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 8, 0, &value, &nodata), ES_NONE);
1036  CU_ASSERT_DOUBLE_EQUAL(value, 255, DBL_EPSILON);
1037  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 1, 0, &value, &nodata), ES_NONE);
1038  CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1039  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 2, 0, &value, &nodata), ES_NONE);
1040  CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1041  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 4, 0, &value, &nodata), ES_NONE);
1042  CU_ASSERT_DOUBLE_EQUAL(value, 127, DBL_EPSILON);
1043  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 7, 0, &value, &nodata), ES_NONE);
1044  CU_ASSERT_DOUBLE_EQUAL(value, 255, DBL_EPSILON);
1045 
1046  cu_free_raster(rtn);
1047 
1048  rtdealloc(colormap->entry);
1049  rtdealloc(colormap);
1050 
1051  cu_free_raster(raster);
1052 
1053  /* new set of tests */
1054  raster = rt_raster_new(10, 10);
1055  CU_ASSERT(raster != NULL); /* or we're out of virtual memory */
1056  band = cu_add_band(raster, PT_8BUI, 0, 0);
1057  CU_ASSERT(band != NULL);
1058  rt_band_set_nodata(band, 0, NULL);
1059 
1060  for (y = 0; y < 10; y++) {
1061  for (x = 0; x < 10; x++) {
1062  rt_band_set_pixel(band, x, y, (x * y) + x, NULL);
1063  }
1064  }
1065 
1066  colormap = (rt_colormap) rtalloc(sizeof(struct rt_colormap_t));
1067  CU_ASSERT(colormap != NULL);
1068  colormap->nentry = 10;
1069  colormap->entry = (rt_colormap_entry) rtalloc(sizeof(struct rt_colormap_entry_t) * colormap->nentry);
1070  CU_ASSERT(colormap->entry != NULL);
1071 
1072  colormap->entry[0].isnodata = 0;
1073  colormap->entry[0].value = 90;
1074  colormap->entry[0].color[0] = 255;
1075  colormap->entry[0].color[1] = 255;
1076  colormap->entry[0].color[2] = 255;
1077  colormap->entry[0].color[3] = 255;
1078 
1079  colormap->entry[1].isnodata = 0;
1080  colormap->entry[1].value = 80;
1081  colormap->entry[1].color[0] = 255;
1082  colormap->entry[1].color[1] = 227;
1083  colormap->entry[1].color[2] = 227;
1084  colormap->entry[1].color[3] = 255;
1085 
1086  colormap->entry[2].isnodata = 0;
1087  colormap->entry[2].value = 70;
1088  colormap->entry[2].color[0] = 255;
1089  colormap->entry[2].color[1] = 198;
1090  colormap->entry[2].color[2] = 198;
1091  colormap->entry[2].color[3] = 255;
1092 
1093  colormap->entry[3].isnodata = 0;
1094  colormap->entry[3].value = 60;
1095  colormap->entry[3].color[0] = 255;
1096  colormap->entry[3].color[1] = 170;
1097  colormap->entry[3].color[2] = 170;
1098  colormap->entry[3].color[3] = 255;
1099 
1100  colormap->entry[4].isnodata = 0;
1101  colormap->entry[4].value = 50;
1102  colormap->entry[4].color[0] = 255;
1103  colormap->entry[4].color[1] = 142;
1104  colormap->entry[4].color[2] = 142;
1105  colormap->entry[4].color[3] = 255;
1106 
1107  colormap->entry[5].isnodata = 0;
1108  colormap->entry[5].value = 40;
1109  colormap->entry[5].color[0] = 255;
1110  colormap->entry[5].color[1] = 113;
1111  colormap->entry[5].color[2] = 113;
1112  colormap->entry[5].color[3] = 255;
1113 
1114  colormap->entry[6].isnodata = 0;
1115  colormap->entry[6].value = 30;
1116  colormap->entry[6].color[0] = 255;
1117  colormap->entry[6].color[1] = 85;
1118  colormap->entry[6].color[2] = 85;
1119  colormap->entry[6].color[3] = 255;
1120 
1121  colormap->entry[7].isnodata = 0;
1122  colormap->entry[7].value = 20;
1123  colormap->entry[7].color[0] = 255;
1124  colormap->entry[7].color[1] = 57;
1125  colormap->entry[7].color[2] = 57;
1126  colormap->entry[7].color[3] = 255;
1127 
1128  colormap->entry[8].isnodata = 0;
1129  colormap->entry[8].value = 10;
1130  colormap->entry[8].color[0] = 255;
1131  colormap->entry[8].color[1] = 28;
1132  colormap->entry[8].color[2] = 28;
1133  colormap->entry[8].color[3] = 255;
1134 
1135  colormap->entry[9].isnodata = 0;
1136  colormap->entry[9].value = 0;
1137  colormap->entry[9].color[0] = 255;
1138  colormap->entry[9].color[1] = 0;
1139  colormap->entry[9].color[2] = 0;
1140  colormap->entry[9].color[3] = 255;
1141 
1142  /* 2 colors, 3 entries, INTERPOLATE */
1143  colormap->ncolor = 4;
1144  colormap->method = CM_INTERPOLATE;
1145 
1146  rtn = rt_raster_colormap(
1147  raster, 0,
1148  colormap
1149  );
1150  CU_ASSERT(rtn != NULL);
1151  CU_ASSERT_EQUAL(rt_raster_get_num_bands(rtn), colormap->ncolor);
1152 
1153  band = rt_raster_get_band(rtn, 2);
1154  CU_ASSERT(band != NULL);
1155  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 0, 0, &value, &nodata), ES_NONE);
1156  CU_ASSERT_DOUBLE_EQUAL(value, 0, DBL_EPSILON);
1157  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 5, 0, &value, &nodata), ES_NONE);
1158  CU_ASSERT_DOUBLE_EQUAL(value, 14, DBL_EPSILON);
1159  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 6, 0, &value, &nodata), ES_NONE);
1160  CU_ASSERT_DOUBLE_EQUAL(value, 17, DBL_EPSILON);
1161  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 9, 0, &value, &nodata), ES_NONE);
1162  CU_ASSERT_DOUBLE_EQUAL(value, 25, DBL_EPSILON);
1163 
1164  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 2, 4, &value, &nodata), ES_NONE);
1165  CU_ASSERT_DOUBLE_EQUAL(value, 28, DBL_EPSILON);
1166  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 3, 4, &value, &nodata), ES_NONE);
1167  CU_ASSERT_DOUBLE_EQUAL(value, 43, DBL_EPSILON);
1168  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 4, 4, &value, &nodata), ES_NONE);
1169  CU_ASSERT_DOUBLE_EQUAL(value, 57, DBL_EPSILON);
1170 
1171  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 6, 9, &value, &nodata), ES_NONE);
1172  CU_ASSERT_DOUBLE_EQUAL(value, 170, DBL_EPSILON);
1173  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 7, 9, &value, &nodata), ES_NONE);
1174  CU_ASSERT_DOUBLE_EQUAL(value, 198, DBL_EPSILON);
1175  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 8, 9, &value, &nodata), ES_NONE);
1176  CU_ASSERT_DOUBLE_EQUAL(value, 227, DBL_EPSILON);
1177 
1178  cu_free_raster(rtn);
1179 
1180  rtdealloc(colormap->entry);
1181  rtdealloc(colormap);
1182 
1183  cu_free_raster(raster);
1184 }
1185 
1186 /* register tests */
1187 void mapalgebra_suite_setup(void);
1189 {
1190  CU_pSuite suite = CU_add_suite("mapalgebra", NULL, NULL);
1194 }
1195 
void mapalgebra_suite_setup(void)
uint16_t nentry
Definition: librtcore.h:2495
static int testRasterIterator7_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
double rt_raster_get_x_offset(rt_raster raster)
Get raster x offset, in projection units.
Definition: rt_raster.c:213
double rt_raster_get_y_skew(rt_raster raster)
Get skew about the Y axis.
Definition: rt_raster.c:190
static int testRasterIterator3_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
static int testRasterIterator1_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
Definition: cu_mapalgebra.c:35
raster
Be careful!! Zeros function&#39;s input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
struct rt_reclassexpr_t::rt_reclassrange dst
static int testRasterIterator4_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
band
Definition: ovdump.py:57
uint8_t color[4]
Definition: librtcore.h:2484
void * rtalloc(size_t size)
Wrappers used for managing memory.
Definition: rt_context.c:171
rt_errorstate rt_band_set_nodata(rt_band band, double val, int *converted)
Set nodata value.
Definition: rt_band.c:733
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition: rt_band.c:340
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
Definition: rt_band.c:1730
static int testRasterIterator6_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
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)
struct rt_reclassexpr_t::rt_reclassrange src
uint32_t columns
Definition: librtcore.h:2455
uint16_t rasters
Definition: librtcore.h:2451
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
Definition: rt_band.c:1221
void rt_raster_set_scale(rt_raster raster, double scaleX, double scaleY)
Set scale in projection units.
Definition: rt_raster.c:137
void rt_raster_set_offsets(rt_raster raster, double x, double y)
Set insertion points in projection units.
Definition: rt_raster.c:199
#define PG_ADD_TEST(suite, testfunc)
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:381
struct rt_colormap_entry_t * rt_colormap_entry
Definition: librtcore.h:160
double rt_raster_get_x_scale(rt_raster raster)
Get scale X in projection units.
Definition: rt_raster.c:150
rt_colormap_entry entry
Definition: librtcore.h:2496
int32_t rt_raster_get_srid(rt_raster raster)
Get raster&#39;s SRID.
Definition: rt_raster.c:356
rt_raster raster
Definition: librtcore.h:2443
double rt_raster_get_y_scale(rt_raster raster)
Get scale Y in projection units.
Definition: rt_raster.c:159
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
Definition: rt_raster.c:48
double rt_raster_get_x_skew(rt_raster raster)
Get skew about the X axis.
Definition: rt_raster.c:181
struct _callback_userargs_t * _callback_userargs
Definition: cu_mapalgebra.c:27
uint16_t nband
Definition: librtcore.h:2444
static void test_raster_colormap()
uint16_t rt_raster_get_width(rt_raster raster)
Definition: rt_raster.c:121
double *** values
Definition: librtcore.h:2459
void rtdealloc(void *mem)
Definition: rt_context.c:186
Definition: librtcore.h:2481
uint8_t nbnodata
Definition: librtcore.h:2445
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, rt_mask mask, void *userarg, int(*callback)(rt_iterator_arg arg, void *userarg, double *value, int *nodata), rt_raster *rtnraster)
n-raster iterator.
static void test_band_reclass()
struct rt_colormap_t * rt_colormap
Definition: librtcore.h:161
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
static int testRasterIterator2_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
Definition: cu_mapalgebra.c:95
int value
Definition: genraster.py:61
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_mapalgebra.c:50
uint16_t rt_raster_get_height(rt_raster raster)
Definition: rt_raster.c:129
static int testRasterIterator5_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
static void test_raster_iterator()
int isnodata
Definition: librtcore.h:2482
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...
double rt_raster_get_y_offset(rt_raster raster)
Get raster y offset, in projection units.
Definition: rt_raster.c:222
double value
Definition: librtcore.h:2483
enum rt_colormap_t::@9 method