PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches
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#include <math.h>
27
29 rt_raster rast;
30 rt_band band;
31 int x, y;
32 uint32_t rtn;
33 const int maxX = 10;
34 const int maxY = 10;
35 rt_pixel npixels = NULL;
36
37 double **value;
38 int **nodata;
39 int dimx;
40 int dimy;
41
42 rast = rt_raster_new(maxX, maxY);
43 CU_ASSERT(rast != NULL);
44
45 band = cu_add_band(rast, PT_32BUI, 1, 0);
46 CU_ASSERT(band != NULL);
47
48 for (x = 0; x < maxX; x++) {
49 for (y = 0; y < maxY; y++) {
50 rtn = rt_band_set_pixel(band, x, y, 1, NULL);
51 }
52 }
53
54 rt_band_set_pixel(band, 0, 0, 0, NULL);
55 rt_band_set_pixel(band, 3, 0, 0, NULL);
56 rt_band_set_pixel(band, 6, 0, 0, NULL);
57 rt_band_set_pixel(band, 9, 0, 0, NULL);
58 rt_band_set_pixel(band, 1, 2, 0, NULL);
59 rt_band_set_pixel(band, 4, 2, 0, NULL);
60 rt_band_set_pixel(band, 7, 2, 0, NULL);
61 rt_band_set_pixel(band, 2, 4, 0, NULL);
62 rt_band_set_pixel(band, 5, 4, 0, NULL);
63 rt_band_set_pixel(band, 8, 4, 0, NULL);
64 rt_band_set_pixel(band, 0, 6, 0, NULL);
65 rt_band_set_pixel(band, 3, 6, 0, NULL);
66 rt_band_set_pixel(band, 6, 6, 0, NULL);
67 rt_band_set_pixel(band, 9, 6, 0, NULL);
68 rt_band_set_pixel(band, 1, 8, 0, NULL);
69 rt_band_set_pixel(band, 4, 8, 0, NULL);
70 rt_band_set_pixel(band, 7, 8, 0, NULL);
71
72 /* 0,0 */
74 band,
75 0, 0,
76 0, 0,
77 1,
78 &npixels
79 );
80 CU_ASSERT_EQUAL(rtn, 3);
81 if (rtn)
82 rtdealloc(npixels);
83
84 /* 1,1 */
86 band,
87 1, 1,
88 0, 0,
89 1,
90 &npixels
91 );
92 CU_ASSERT_EQUAL(rtn, 6);
93 if (rtn)
94 rtdealloc(npixels);
95
96 /* 4,4 */
98 band,
99 4, 4,
100 0, 0,
101 1,
102 &npixels
103 );
104 CU_ASSERT_EQUAL(rtn, 7);
105 if (rtn)
106 rtdealloc(npixels);
107
108 /* 4,4 distance 2 */
110 band,
111 4, 4,
112 2, 2,
113 1,
114 &npixels
115 );
116 CU_ASSERT_EQUAL(rtn, 19);
117 if (rtn)
118 rtdealloc(npixels);
119
120 /* 10,10 */
122 band,
123 10, 10,
124 0, 0,
125 1,
126 &npixels
127 );
128 CU_ASSERT_EQUAL(rtn, 1);
129 if (rtn)
130 rtdealloc(npixels);
131
132 /* 11,11 distance 1 */
134 band,
135 11, 11,
136 1, 1,
137 1,
138 &npixels
139 );
140 CU_ASSERT_EQUAL(rtn, 0);
141 if (rtn)
142 rtdealloc(npixels);
143
144 /* -1,-1 */
146 band,
147 -1, -1,
148 0, 0,
149 1,
150 &npixels
151 );
152 CU_ASSERT_EQUAL(rtn, 3);
153 if (rtn)
154 rtdealloc(npixels);
155
156 /* -1,-1 distance 1 */
158 band,
159 -1, -1,
160 1, 1,
161 1,
162 &npixels
163 );
164 CU_ASSERT_EQUAL(rtn, 0);
165 if (rtn)
166 rtdealloc(npixels);
167
168 /* -1,1 distance 1 */
170 band,
171 -1, 1,
172 1, 1,
173 1,
174 &npixels
175 );
176 CU_ASSERT_EQUAL(rtn, 2);
177
179 npixels, rtn, NULL,
180 -1, 1,
181 1, 1,
182 &value,
183 &nodata,
184 &dimx, &dimy
185 );
186 rtdealloc(npixels);
187 CU_ASSERT_EQUAL(rtn, ES_NONE);
188 CU_ASSERT_EQUAL(dimx, 3);
189 CU_ASSERT_EQUAL(dimy, 3);
190
191 for (x = 0; x < dimx; x++) {
192 rtdealloc(nodata[x]);
193 rtdealloc(value[x]);
194 }
195
196 rtdealloc(nodata);
197 rtdealloc(value);
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 uint32_t maxX = 10;
296 uint32_t 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
371 rt_raster rast;
372 rt_band band;
373 rt_mask mask = NULL;
374 int x, y;
375 int rtn;
376 const int maxX = 10;
377 const int maxY = 10;
378 int maskX = 3;
379 int maskY = 3;
380 rt_pixel npixels = NULL;
381 int i;
382 double **value;
383 double val;
384 int nod;
385 int **nodata;
386 int dimx;
387 int dimy;
388
389 rast = rt_raster_new(maxX, maxY);
390 CU_ASSERT(rast != NULL);
391
392 band = cu_add_band(rast, PT_32BF, 1, 0);
393 CU_ASSERT(band != NULL);
394
395 for (x = 0; x < maxX; x++) {
396 for (y = 0; y < maxY; y++) {
397 rtn = rt_band_set_pixel(band, x, y, 1, NULL);
398 }
399 }
400
401
402 rtn = rt_band_get_pixel(band,4,4,&val,&nod);
403 CU_ASSERT_EQUAL(nod,0);
404 CU_ASSERT_DOUBLE_EQUAL(val,1,.01);
405
406 /* set up mask */
407
408 mask = (rt_mask) rtalloc(sizeof(struct rt_mask_t) );
409 CU_ASSERT(mask != NULL);
410 mask->values = rtalloc(sizeof(double*)*maskY);
411 mask->nodata = rtalloc(sizeof(int*)*maskY);
412
413 for( i = 0; i < maskY; i++) {
414 mask->values[i] = rtalloc(sizeof(double) *maskX);
415 mask->nodata[i] = rtalloc(sizeof(int) *maskX);
416 }
417
418 CU_ASSERT(mask->values != NULL);
419 CU_ASSERT(mask->nodata != NULL);
420
421 /* set mask to nodata */
422
423 for(y = 0; y < maskY; y++) {
424 for(x = 0; x < maskX; x++){
425 mask->values[y][x]= 0;
426 mask->nodata[y][x]= 1;
427 }
428 }
429
430 mask->dimx = maskX;
431 mask->dimy = maskY;
432 mask->weighted = 0;
433
435 band,
436 4,4,
437 1, 1,
438 1,
439 &npixels
440 );
441
442 CU_ASSERT_EQUAL(rtn,8);
443
445 npixels, rtn, mask,
446 4,4,
447 1, 1,
448 &value,
449 &nodata,
450 &dimx, &dimy
451 );
452
453
454 rtdealloc(npixels);
455 CU_ASSERT_EQUAL(rtn, ES_NONE);
456 CU_ASSERT_EQUAL(dimx, 3);
457 CU_ASSERT_EQUAL(dimy, 3);
458 CU_ASSERT_EQUAL(nodata[1][1],1);
459
460 for (x = 0; x < dimx; x++) {
461 rtdealloc(nodata[x]);
462 rtdealloc(value[x]);
463 }
464
465 rtdealloc(nodata);
466 rtdealloc(value);
467
468 /* set mask to 1 */
469
470 for(y = 0; y < maskY; y++) {
471 for(x = 0; x < maskX; x++){
472 mask->values[y][x]= 1;
473 mask->nodata[y][x]= 0;
474 }
475 }
476
477 mask->dimx = maskX;
478 mask->dimy = maskY;
479 mask->weighted = 0;
480
481
483 band,
484 4,4,
485 1, 1,
486 1,
487 &npixels
488 );
489
490 CU_ASSERT_EQUAL(rtn,8);
491
493 npixels, rtn, mask,
494 4,4,
495 1, 1,
496 &value,
497 &nodata,
498 &dimx, &dimy
499 );
500
501
502 rtdealloc(npixels);
503 CU_ASSERT_EQUAL(rtn, ES_NONE);
504 CU_ASSERT_EQUAL(dimx, 3);
505 CU_ASSERT_EQUAL(dimy, 3);
506 CU_ASSERT_NOT_EQUAL(nodata[0][0],1);
507 CU_ASSERT_DOUBLE_EQUAL(value[0][0],1,.01);
508
509 for (x = 0; x < dimx; x++) {
510 rtdealloc(nodata[x]);
511 rtdealloc(value[x]);
512 }
513
514 rtdealloc(nodata);
515 rtdealloc(value);
516
517 /* set mask to 0.5 */
518
519 for(y = 0; y < maskY; y++) {
520 for(x = 0; x < maskX; x++){
521 mask->values[y][x]= 0.5;
522 mask->nodata[y][x]= 0;
523 }
524 }
525
526 mask->dimx = maskX;
527 mask->dimy = maskY;
528 mask->weighted = 1;
529
531 band,
532 4,4,
533 1, 1,
534 1,
535 &npixels
536 );
537
538 CU_ASSERT_EQUAL(rtn,8);
539
541 npixels, rtn, mask,
542 4,4,
543 1, 1,
544 &value,
545 &nodata,
546 &dimx, &dimy
547 );
548
549
550 rtdealloc(npixels);
551 CU_ASSERT_EQUAL(rtn, ES_NONE);
552 CU_ASSERT_EQUAL(dimx, 3);
553 CU_ASSERT_EQUAL(dimy, 3);
554 CU_ASSERT_NOT_EQUAL(nodata[0][0],1);
555 CU_ASSERT_DOUBLE_EQUAL(value[0][0],0.5,0.1);
556
557 for (x = 0; x < dimx; x++) {
558 rtdealloc(nodata[x]);
559 rtdealloc(value[x]);
560 }
561
562 rtdealloc(nodata);
563 rtdealloc(value);
564
565 for( i = 0; i < maskY; i++) {
566 rtdealloc(mask->values[i]);
567 rtdealloc(mask->nodata[i]);
568 }
569
570 rtdealloc(mask->values);
571 rtdealloc(mask->nodata);
572 rtdealloc(mask);
573
574 if (rtn)
575 rtdealloc(npixels);
576
577 cu_free_raster(rast);
578
579}
580
581/* register tests */
582void band_misc_suite_setup(void);
584{
585 CU_pSuite suite = CU_add_suite("band_misc", NULL, NULL);
589}
590
static void test_band_get_pixel_of_value()
static void test_pixel_set_to_array()
void band_misc_suite_setup(void)
static void test_band_get_nearest_pixel()
#define TRUE
Definition dbfopen.c:73
#define FALSE
Definition dbfopen.c:72
#define PG_ADD_TEST(suite, testfunc)
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_band.c:1989
void * rtalloc(size_t size)
Wrappers used for managing memory.
Definition rt_context.c:191
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
Definition rt_band.c:1551
struct rt_mask_t * rt_mask
Definition librtcore.h:149
@ PT_32BUI
Definition librtcore.h:197
@ PT_32BF
Definition librtcore.h:199
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
Definition rt_raster.c:52
rt_errorstate rt_band_set_pixel(rt_band band, int x, int y, double val, int *converted)
Set single pixel's value.
Definition rt_band.c:1140
@ ES_NONE
Definition librtcore.h:182
rt_errorstate rt_pixel_set_to_array(rt_pixel npixel, uint32_t count, rt_mask mask, int x, int y, uint16_t distancex, uint16_t distancey, double ***value, int ***nodata, int *dimx, int *dimy)
Definition rt_pixel.c:301
uint32_t 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_band.c:1709
void rtdealloc(void *mem)
Definition rt_context.c:206
rt_band cu_add_band(rt_raster raster, rt_pixtype pixtype, int hasnodata, double nodataval)
void cu_free_raster(rt_raster raster)
double ** values
Definition librtcore.h:2548
uint16_t dimy
Definition librtcore.h:2547
uint16_t dimx
Definition librtcore.h:2546
int ** nodata
Definition librtcore.h:2549