PostGIS  2.5.7dev-r@@SVN_REVISION@@
cu_raster_geometry.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-2013 Regents of the University of California
6  * <bkpark@ucdavis.edu>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  */
23 
24 #include "CUnit/Basic.h"
25 #include "cu_tester.h"
26 
27 static void test_raster_envelope() {
28  rt_raster raster = NULL;
29  rt_envelope rtenv;
30 
31  /* width = 0, height = 0 */
32  raster = rt_raster_new(0, 0);
33  CU_ASSERT(raster != NULL);
34 
35  rt_raster_set_offsets(raster, 0.5, 0.5);
37 
38  CU_ASSERT_EQUAL(rt_raster_get_envelope(raster, &rtenv), ES_NONE);
39  CU_ASSERT_DOUBLE_EQUAL(rtenv.MinX, 0.5, DBL_EPSILON);
40  CU_ASSERT_DOUBLE_EQUAL(rtenv.MaxX, 0.5, DBL_EPSILON);
41  CU_ASSERT_DOUBLE_EQUAL(rtenv.MinY, 0.5, DBL_EPSILON);
42  CU_ASSERT_DOUBLE_EQUAL(rtenv.MaxY, 0.5, DBL_EPSILON);
44 
45  /* width = 0 */
46  raster = rt_raster_new(0, 5);
47  CU_ASSERT(raster != NULL);
48 
49  rt_raster_set_offsets(raster, 0.5, 0.5);
51 
52  CU_ASSERT_EQUAL(rt_raster_get_envelope(raster, &rtenv), ES_NONE);
53  CU_ASSERT_DOUBLE_EQUAL(rtenv.MinX, 0.5, DBL_EPSILON);
54  CU_ASSERT_DOUBLE_EQUAL(rtenv.MaxX, 0.5, DBL_EPSILON);
55  CU_ASSERT_DOUBLE_EQUAL(rtenv.MinY, -4.5, DBL_EPSILON);
56  CU_ASSERT_DOUBLE_EQUAL(rtenv.MaxY, 0.5, DBL_EPSILON);
58 
59  /* height = 0 */
60  raster = rt_raster_new(5, 0);
61  CU_ASSERT(raster != NULL);
62 
63  rt_raster_set_offsets(raster, 0.5, 0.5);
65 
66  CU_ASSERT_EQUAL(rt_raster_get_envelope(raster, &rtenv), ES_NONE);
67  CU_ASSERT_DOUBLE_EQUAL(rtenv.MinX, 0.5, DBL_EPSILON);
68  CU_ASSERT_DOUBLE_EQUAL(rtenv.MaxX, 5.5, DBL_EPSILON);
69  CU_ASSERT_DOUBLE_EQUAL(rtenv.MinY, 0.5, DBL_EPSILON);
70  CU_ASSERT_DOUBLE_EQUAL(rtenv.MaxY, 0.5, DBL_EPSILON);
72 
73  /* normal raster */
74  raster = rt_raster_new(5, 5);
75  CU_ASSERT(raster != NULL);
76 
77  rt_raster_set_offsets(raster, 0.5, 0.5);
79 
80  CU_ASSERT_EQUAL(rt_raster_get_envelope(raster, &rtenv), ES_NONE);
81  CU_ASSERT_DOUBLE_EQUAL(rtenv.MinX, 0.5, DBL_EPSILON);
82  CU_ASSERT_DOUBLE_EQUAL(rtenv.MaxX, 5.5, DBL_EPSILON);
83  CU_ASSERT_DOUBLE_EQUAL(rtenv.MinY, -4.5, DBL_EPSILON);
84  CU_ASSERT_DOUBLE_EQUAL(rtenv.MaxY, 0.5, DBL_EPSILON);
86 }
87 
89  rt_raster raster = NULL;
90  LWGEOM *env = NULL;
91  LWPOLY *poly = NULL;
92  POINTARRAY *ring = NULL;
93  POINT4D pt;
94 
95  /* NULL raster */
96  CU_ASSERT_EQUAL(rt_raster_get_envelope_geom(NULL, &env), ES_NONE);
97  CU_ASSERT(env == NULL);
98 
99  /* width = 0, height = 0 */
100  raster = rt_raster_new(0, 0);
101  CU_ASSERT(raster != NULL);
102 
103  CU_ASSERT_EQUAL(rt_raster_get_envelope_geom(raster, &env), ES_NONE);
104  CU_ASSERT_EQUAL(env->type, POINTTYPE);
105  lwgeom_free(env);
107 
108  /* width = 0 */
109  raster = rt_raster_new(0, 256);
110  CU_ASSERT(raster != NULL);
111 
112  CU_ASSERT_EQUAL(rt_raster_get_envelope_geom(raster, &env), ES_NONE);
113  CU_ASSERT_EQUAL(env->type, LINETYPE);
114  lwgeom_free(env);
116 
117  /* height = 0 */
118  raster = rt_raster_new(256, 0);
119  CU_ASSERT(raster != NULL);
120 
121  CU_ASSERT_EQUAL(rt_raster_get_envelope_geom(raster, &env), ES_NONE);
122  CU_ASSERT_EQUAL(env->type, LINETYPE);
123  lwgeom_free(env);
125 
126  /* normal raster */
127  raster = rt_raster_new(5, 5);
128  CU_ASSERT(raster != NULL);
129 
130  rt_raster_set_offsets(raster, 0.5, 0.5);
131  rt_raster_set_scale(raster, 1, -1);
132 
133  CU_ASSERT_EQUAL(rt_raster_get_envelope_geom(raster, &env), ES_NONE);
134  poly = lwgeom_as_lwpoly(env);
135  CU_ASSERT_EQUAL(poly->srid, rt_raster_get_srid(raster));
136  CU_ASSERT_EQUAL(poly->nrings, 1);
137 
138  ring = poly->rings[0];
139  CU_ASSERT(ring != NULL);
140  CU_ASSERT_EQUAL(ring->npoints, 5);
141 
142  getPoint4d_p(ring, 0, &pt);
143  CU_ASSERT_DOUBLE_EQUAL(pt.x, 0.5, DBL_EPSILON);
144  CU_ASSERT_DOUBLE_EQUAL(pt.y, 0.5, DBL_EPSILON);
145 
146  getPoint4d_p(ring, 1, &pt);
147  CU_ASSERT_DOUBLE_EQUAL(pt.x, 5.5, DBL_EPSILON);
148  CU_ASSERT_DOUBLE_EQUAL(pt.y, 0.5, DBL_EPSILON);
149 
150  getPoint4d_p(ring, 2, &pt);
151  CU_ASSERT_DOUBLE_EQUAL(pt.x, 5.5, DBL_EPSILON);
152  CU_ASSERT_DOUBLE_EQUAL(pt.y, -4.5, DBL_EPSILON);
153 
154  getPoint4d_p(ring, 3, &pt);
155  CU_ASSERT_DOUBLE_EQUAL(pt.x, 0.5, DBL_EPSILON);
156  CU_ASSERT_DOUBLE_EQUAL(pt.y, -4.5, DBL_EPSILON);
157 
158  getPoint4d_p(ring, 4, &pt);
159  CU_ASSERT_DOUBLE_EQUAL(pt.x, 0.5, DBL_EPSILON);
160  CU_ASSERT_DOUBLE_EQUAL(pt.y, 0.5, DBL_EPSILON);
161 
162  lwgeom_free(env);
164 }
165 
166 static void test_raster_convex_hull() {
167  rt_raster raster = NULL;
168  LWGEOM *hull = NULL;
169  LWPOLY *poly = NULL;
170  POINTARRAY *ring = NULL;
171  POINT4D pt;
172 
173  /* NULL raster */
174  CU_ASSERT_EQUAL(rt_raster_get_convex_hull(NULL, &hull), ES_NONE);
175  CU_ASSERT(hull == NULL);
176 
177  /* width = 0, height = 0 */
178  raster = rt_raster_new(0, 0);
179  CU_ASSERT(raster != NULL);
180 
181  CU_ASSERT_EQUAL(rt_raster_get_convex_hull(raster, &hull), ES_NONE);
182  CU_ASSERT_EQUAL(hull->type, POINTTYPE);
183  lwgeom_free(hull);
185 
186  /* width = 0 */
187  raster = rt_raster_new(0, 256);
188  CU_ASSERT(raster != NULL);
189 
190  CU_ASSERT_EQUAL(rt_raster_get_convex_hull(raster, &hull), ES_NONE);
191  CU_ASSERT_EQUAL(hull->type, LINETYPE);
192  lwgeom_free(hull);
194 
195  /* height = 0 */
196  raster = rt_raster_new(256, 0);
197  CU_ASSERT(raster != NULL);
198 
199  CU_ASSERT_EQUAL(rt_raster_get_convex_hull(raster, &hull), ES_NONE);
200  CU_ASSERT_EQUAL(hull->type, LINETYPE);
201  lwgeom_free(hull);
203 
204  /* normal raster */
205  raster = rt_raster_new(256, 256);
206  CU_ASSERT(raster != NULL);
207 
208  rt_raster_set_offsets(raster, 0.5, 0.5);
211 
212  CU_ASSERT_EQUAL(rt_raster_get_convex_hull(raster, &hull), ES_NONE);
213  poly = lwgeom_as_lwpoly(hull);
214  CU_ASSERT_EQUAL(poly->srid, rt_raster_get_srid(raster));
215  CU_ASSERT_EQUAL(poly->nrings, 1);
216 
217  ring = poly->rings[0];
218  CU_ASSERT(ring != NULL);
219  CU_ASSERT_EQUAL(ring->npoints, 5);
220 
221  getPoint4d_p(ring, 0, &pt);
222  CU_ASSERT_DOUBLE_EQUAL(pt.x, 0.5, DBL_EPSILON);
223  CU_ASSERT_DOUBLE_EQUAL(pt.y, 0.5, DBL_EPSILON);
224 
225  getPoint4d_p(ring, 1, &pt);
226  CU_ASSERT_DOUBLE_EQUAL(pt.x, 256.5, DBL_EPSILON);
227  CU_ASSERT_DOUBLE_EQUAL(pt.y, 1280.5, DBL_EPSILON);
228 
229  getPoint4d_p(ring, 2, &pt);
230  CU_ASSERT_DOUBLE_EQUAL(pt.x, 1280.5, DBL_EPSILON);
231  CU_ASSERT_DOUBLE_EQUAL(pt.y, 1536.5, DBL_EPSILON);
232 
233  getPoint4d_p(ring, 3, &pt);
234  CU_ASSERT_DOUBLE_EQUAL(pt.x, 1024.5, DBL_EPSILON);
235  CU_ASSERT_DOUBLE_EQUAL(pt.y, 256.5, DBL_EPSILON);
236 
237  getPoint4d_p(ring, 4, &pt);
238  CU_ASSERT_DOUBLE_EQUAL(pt.x, 0.5, DBL_EPSILON);
239  CU_ASSERT_DOUBLE_EQUAL(pt.y, 0.5, DBL_EPSILON);
240 
241  lwgeom_free(hull);
243 }
244 
245 static char *
246 lwgeom_to_text(const LWGEOM *lwgeom) {
247  char *wkt;
248  size_t wkt_size;
249 
250  wkt = lwgeom_to_wkt(lwgeom, WKT_ISO, DBL_DIG, &wkt_size);
251 
252  return wkt;
253 }
254 
255 static void test_raster_surface() {
256  rt_raster rast;
257  rt_band band;
258  const int maxX = 5;
259  const int maxY = 5;
260  int x, y;
261  LWMPOLY *mpoly = NULL;
262  int err;
263  LWGEOM *gexpected, *gobserved;
264 
265  rast = rt_raster_new(maxX, maxY);
266  CU_ASSERT(rast != NULL);
267 
269  rt_raster_set_scale(rast, 1, -1);
270 
271  band = cu_add_band(rast, PT_32BUI, 1, 0);
272  CU_ASSERT(band != NULL);
273 
274  for (y = 0; y < maxY; y++) {
275  for (x = 0; x < maxX; x++) {
276  rt_band_set_pixel(band, x, y, 1, NULL);
277  }
278  }
279 
280  err = rt_raster_surface(rast, 0, &mpoly);
281  CU_ASSERT_EQUAL(err, ES_NONE);
282  CU_ASSERT(mpoly != NULL);
283  //wkt = lwgeom_to_text(lwmpoly_as_lwgeom(mpoly));
284  gobserved = (LWGEOM *)lwmpoly_as_lwgeom(mpoly);
285  gexpected = lwgeom_from_wkt("MULTIPOLYGON(((0 0,0 -5,5 -5,5 0,0 0)))", LW_PARSER_CHECK_NONE);
286  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
287  lwmpoly_free(mpoly);
288  mpoly = NULL;
289 
290  /* 0,0 NODATA */
291  rt_band_set_pixel(band, 0, 0, 0, NULL);
292 
293  err = rt_raster_surface(rast, 0, &mpoly);
294  CU_ASSERT_EQUAL(err, ES_NONE);
295  CU_ASSERT(mpoly != NULL);
296  gobserved = (LWGEOM *)lwmpoly_as_lwgeom(mpoly);
297  gexpected = lwgeom_from_wkt("MULTIPOLYGON(((1 0,1 -1,0 -1,0 -5,4 -5,5 -5,5 0,1 0)))", LW_PARSER_CHECK_NONE);
298  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
299  lwmpoly_free(mpoly);
300  mpoly = NULL;
301 
302  /* plus 1,1 NODATA */
303  rt_band_set_pixel(band, 1, 1, 0, NULL);
304 
305  err = rt_raster_surface(rast, 0, &mpoly);
306  CU_ASSERT_EQUAL(err, ES_NONE);
307  CU_ASSERT(mpoly != NULL);
308  gobserved = (LWGEOM *)lwmpoly_as_lwgeom(mpoly);
309  gexpected = lwgeom_from_wkt("MULTIPOLYGON(((1 0,1 -1,0 -1,0 -5,4 -5,5 -5,5 0,1 0),(1 -1,1 -2,2 -2,2 -1,1 -1)))",
311  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
312  lwmpoly_free(mpoly);
313  mpoly = NULL;
314 
315  /* plus 2,2 NODATA */
316  rt_band_set_pixel(band, 2, 2, 0, NULL);
317 
318  err = rt_raster_surface(rast, 0, &mpoly);
319  CU_ASSERT_EQUAL(err, ES_NONE);
320  CU_ASSERT(mpoly != NULL);
321  gobserved = (LWGEOM *)lwmpoly_as_lwgeom(mpoly);
322  gexpected = lwgeom_from_wkt(
323  "MULTIPOLYGON(((1 -1,1 0,5 0,5 -5,4 -5,0 -5,0 -1,1 -1),(1 -1,1 -2,2 -2,2 -1,1 -1),(2 -2,2 -3,3 -3,3 -2,2 -2)))",
325  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
326  lwmpoly_free(mpoly);
327  mpoly = NULL;
328 
329  /* plus 3,3 NODATA */
330  rt_band_set_pixel(band, 3, 3, 0, NULL);
331 
332  err = rt_raster_surface(rast, 0, &mpoly);
333  CU_ASSERT_EQUAL(err, ES_NONE);
334  CU_ASSERT(mpoly != NULL);
335  gobserved = (LWGEOM *)lwmpoly_as_lwgeom(mpoly);
336  gexpected = lwgeom_from_wkt(
337  "MULTIPOLYGON(((1 -1,1 0,5 0,5 -5,4 -5,0 -5,0 -1,1 -1),(1 -1,1 -2,2 -2,2 -1,1 -1),(2 -2,2 -3,3 -3,3 -2,2 -2),(3 -3,3 -4,4 -4,4 -3,3 -3)))",
339  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
340  lwmpoly_free(mpoly);
341  mpoly = NULL;
342 
343  /* plus 4,4 NODATA */
344  rt_band_set_pixel(band, 4, 4, 0, NULL);
345 
346  err = rt_raster_surface(rast, 0, &mpoly);
347  CU_ASSERT_EQUAL(err, ES_NONE);
348  CU_ASSERT(mpoly != NULL);
349  gobserved = (LWGEOM *)lwmpoly_as_lwgeom(mpoly);
350  gexpected = lwgeom_from_wkt(
351  "MULTIPOLYGON(((4 -4,4 -5,0 -5,0 -1,1 -1,1 -2,2 -2,2 -3,3 -3,3 -4,4 -4)),((1 -1,1 0,5 0,5 -4,4 -4,4 -3,3 -3,3 -2,2 -2,2 -1,1 -1)))",
353  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
354  lwmpoly_free(mpoly);
355  mpoly = NULL;
356 
357  /* a whole lot more NODATA */
358  rt_band_set_pixel(band, 4, 0, 0, NULL);
359  rt_band_set_pixel(band, 3, 1, 0, NULL);
360  rt_band_set_pixel(band, 1, 3, 0, NULL);
361  rt_band_set_pixel(band, 0, 4, 0, NULL);
362 
363  err = rt_raster_surface(rast, 0, &mpoly);
364  CU_ASSERT_EQUAL(err, ES_NONE);
365  CU_ASSERT(mpoly != NULL);
366  gobserved = (LWGEOM *)lwmpoly_as_lwgeom(mpoly);
367  gexpected = lwgeom_from_wkt(
368  "MULTIPOLYGON(((1 -4,2 -4,2 -3,3 -3,3 -4,4 -4,4 -5,3 -5,1 -5,1 -4)),((1 -4,0 -4,0 -1,1 -1,1 -2,2 -2,2 -3,1 -3,1 -4)),((3 -2,4 -2,4 -1,5 -1,5 -4,4 -4,4 -3,3 -3,3 -2)),((3 -2,2 -2,2 -1,1 -1,1 0,4 0,4 -1,3 -1,3 -2)))",
370  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
371  lwmpoly_free(mpoly);
372  mpoly = NULL;
373 
375 }
376 
377 static void test_raster_perimeter() {
378  rt_raster rast;
379  rt_band band;
380  const int maxX = 5;
381  const int maxY = 5;
382  int x, y;
383  char *wkt = NULL;
384  LWGEOM *geom = NULL;
385  int err;
386 
387  rast = rt_raster_new(maxX, maxY);
388  CU_ASSERT(rast != NULL);
389 
391  rt_raster_set_scale(rast, 1, -1);
392 
393  band = cu_add_band(rast, PT_32BUI, 1, 0);
394  CU_ASSERT(band != NULL);
395 
396  for (y = 0; y < maxY; y++) {
397  for (x = 0; x < maxX; x++) {
398  rt_band_set_pixel(band, x, y, 1, NULL);
399  }
400  }
401 
402  err = rt_raster_get_perimeter(rast, -1, &geom);
403  CU_ASSERT_EQUAL(err, ES_NONE);
404  CU_ASSERT(geom != NULL);
405  wkt = lwgeom_to_text(geom);
406  CU_ASSERT_STRING_EQUAL(wkt, "POLYGON((0 0,5 0,5 -5,0 -5,0 0))");
407  rtdealloc(wkt);
408  lwgeom_free(geom);
409  geom = NULL;
410 
411  /* row 0 is NODATA */
412  rt_band_set_pixel(band, 0, 0, 0, NULL);
413  rt_band_set_pixel(band, 1, 0, 0, NULL);
414  rt_band_set_pixel(band, 2, 0, 0, NULL);
415  rt_band_set_pixel(band, 3, 0, 0, NULL);
416  rt_band_set_pixel(band, 4, 0, 0, NULL);
417 
418  err = rt_raster_get_perimeter(rast, 0, &geom);
419  CU_ASSERT_EQUAL(err, ES_NONE);
420  CU_ASSERT(geom != NULL);
421  wkt = lwgeom_to_text(geom);
422  CU_ASSERT_STRING_EQUAL(wkt, "POLYGON((0 -1,5 -1,5 -5,0 -5,0 -1))");
423  rtdealloc(wkt);
424  lwgeom_free(geom);
425  geom = NULL;
426 
427  /* column 4 is NODATA */
428  /* pixel 4, 0 already set to NODATA */
429  rt_band_set_pixel(band, 4, 1, 0, NULL);
430  rt_band_set_pixel(band, 4, 2, 0, NULL);
431  rt_band_set_pixel(band, 4, 3, 0, NULL);
432  rt_band_set_pixel(band, 4, 4, 0, NULL);
433 
434  err = rt_raster_get_perimeter(rast, 0, &geom);
435  CU_ASSERT_EQUAL(err, ES_NONE);
436  CU_ASSERT(geom != NULL);
437  wkt = lwgeom_to_text(geom);
438  CU_ASSERT_STRING_EQUAL(wkt, "POLYGON((0 -1,4 -1,4 -5,0 -5,0 -1))");
439  rtdealloc(wkt);
440  lwgeom_free(geom);
441  geom = NULL;
442 
443  /* row 4 is NODATA */
444  rt_band_set_pixel(band, 0, 4, 0, NULL);
445  rt_band_set_pixel(band, 1, 4, 0, NULL);
446  rt_band_set_pixel(band, 2, 4, 0, NULL);
447  rt_band_set_pixel(band, 3, 4, 0, NULL);
448  /* pixel 4, 4 already set to NODATA */
449 
450  err = rt_raster_get_perimeter(rast, 0, &geom);
451  CU_ASSERT_EQUAL(err, ES_NONE);
452  CU_ASSERT(geom != NULL);
453  wkt = lwgeom_to_text(geom);
454  CU_ASSERT_STRING_EQUAL(wkt, "POLYGON((0 -1,4 -1,4 -4,0 -4,0 -1))");
455  rtdealloc(wkt);
456  lwgeom_free(geom);
457  geom = NULL;
458 
459  /* column 0 is NODATA */
460  /* pixel 0, 0 already set to NODATA*/
461  rt_band_set_pixel(band, 0, 1, 0, NULL);
462  rt_band_set_pixel(band, 0, 2, 0, NULL);
463  rt_band_set_pixel(band, 0, 3, 0, NULL);
464  /* pixel 0, 4 already set to NODATA*/
465 
466  err = rt_raster_get_perimeter(rast, 0, &geom);
467  CU_ASSERT_EQUAL(err, ES_NONE);
468  CU_ASSERT(geom != NULL);
469  wkt = lwgeom_to_text(geom);
470  CU_ASSERT_STRING_EQUAL(wkt, "POLYGON((1 -1,4 -1,4 -4,1 -4,1 -1))");
471  rtdealloc(wkt);
472  lwgeom_free(geom);
473  geom = NULL;
474 
475  /* columns 1 and 3 are NODATA */
476  /* pixel 1, 0 already set to NODATA */
477  rt_band_set_pixel(band, 1, 1, 0, NULL);
478  rt_band_set_pixel(band, 1, 2, 0, NULL);
479  rt_band_set_pixel(band, 1, 3, 0, NULL);
480  /* pixel 1, 4 already set to NODATA */
481  /* pixel 3, 0 already set to NODATA */
482  rt_band_set_pixel(band, 3, 1, 0, NULL);
483  rt_band_set_pixel(band, 3, 2, 0, NULL);
484  rt_band_set_pixel(band, 3, 3, 0, NULL);
485  /* pixel 3, 4 already set to NODATA */
486 
487  err = rt_raster_get_perimeter(rast, 0, &geom);
488  CU_ASSERT_EQUAL(err, ES_NONE);
489  CU_ASSERT(geom != NULL);
490  wkt = lwgeom_to_text(geom);
491  CU_ASSERT_STRING_EQUAL(wkt, "POLYGON((2 -1,3 -1,3 -4,2 -4,2 -1))");
492  rtdealloc(wkt);
493  lwgeom_free(geom);
494  geom = NULL;
495 
496  /* more pixels are NODATA */
497  rt_band_set_pixel(band, 2, 1, 0, NULL);
498  rt_band_set_pixel(band, 2, 3, 0, NULL);
499 
500  err = rt_raster_get_perimeter(rast, 0, &geom);
501  CU_ASSERT_EQUAL(err, ES_NONE);
502  CU_ASSERT(geom != NULL);
503  wkt = lwgeom_to_text(geom);
504  CU_ASSERT_STRING_EQUAL(wkt, "POLYGON((2 -2,3 -2,3 -3,2 -3,2 -2))");
505  rtdealloc(wkt);
506  lwgeom_free(geom);
507  geom = NULL;
508 
509  /* second band */
510  band = cu_add_band(rast, PT_32BUI, 1, 0);
511  CU_ASSERT(band != NULL);
512 
513  for (y = 0; y < maxY; y++) {
514  for (x = 0; x < maxX; x++) {
515  rt_band_set_pixel(band, x, y, 1, NULL);
516  }
517  }
518 
519  err = rt_raster_get_perimeter(rast, -1, &geom);
520  CU_ASSERT_EQUAL(err, ES_NONE);
521  CU_ASSERT(geom != NULL);
522  wkt = lwgeom_to_text(geom);
523  CU_ASSERT_STRING_EQUAL(wkt, "POLYGON((0 0,5 0,5 -5,0 -5,0 0))");
524  rtdealloc(wkt);
525  lwgeom_free(geom);
526  geom = NULL;
527 
529 }
530 
532  rt_raster rast;
533  rt_band band;
534  uint32_t x, y;
535  const uint32_t maxX = 10;
536  const uint32_t maxY = 10;
537  LWPOLY *poly = NULL;
538 
539  rast = rt_raster_new(maxX, maxY);
540  CU_ASSERT(rast != NULL);
541 
542  band = cu_add_band(rast, PT_32BUI, 1, 0);
543  CU_ASSERT(band != NULL);
544 
545  for (x = 0; x < maxX; x++) {
546  for (y = 0; y < maxY; y++) {
547  rt_band_set_pixel(band, x, y, 1, NULL);
548  }
549  }
550 
551  rt_band_set_pixel(band, 0, 0, 0, NULL);
552  rt_band_set_pixel(band, 3, 0, 0, NULL);
553  rt_band_set_pixel(band, 6, 0, 0, NULL);
554  rt_band_set_pixel(band, 9, 0, 0, NULL);
555  rt_band_set_pixel(band, 1, 2, 0, NULL);
556  rt_band_set_pixel(band, 4, 2, 0, NULL);
557  rt_band_set_pixel(band, 7, 2, 0, NULL);
558  rt_band_set_pixel(band, 2, 4, 0, NULL);
559  rt_band_set_pixel(band, 5, 4, 0, NULL);
560  rt_band_set_pixel(band, 8, 4, 0, NULL);
561  rt_band_set_pixel(band, 0, 6, 0, NULL);
562  rt_band_set_pixel(band, 3, 6, 0, NULL);
563  rt_band_set_pixel(band, 6, 6, 0, NULL);
564  rt_band_set_pixel(band, 9, 6, 0, NULL);
565  rt_band_set_pixel(band, 1, 8, 0, NULL);
566  rt_band_set_pixel(band, 4, 8, 0, NULL);
567  rt_band_set_pixel(band, 7, 8, 0, NULL);
568 
569  poly = rt_raster_pixel_as_polygon(rast, 1, 1);
570  CU_ASSERT(poly != NULL);
571  lwpoly_free(poly);
572 
574 }
575 
576 /* register tests */
577 void raster_geometry_suite_setup(void);
579 {
580  CU_pSuite suite = CU_add_suite("raster_geometry", NULL, NULL);
587 }
588 
static void test_raster_surface()
static void test_raster_envelope()
static void test_raster_convex_hull()
static void test_raster_envelope_geom()
static void test_raster_pixel_as_polygon()
static void test_raster_perimeter()
static char * lwgeom_to_text(const LWGEOM *lwgeom)
void raster_geometry_suite_setup(void)
#define PG_ADD_TEST(suite, testfunc)
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2005
void lwmpoly_free(LWMPOLY *mpoly)
Definition: lwmpoly.c:53
#define LINETYPE
Definition: liblwgeom.h:86
LWGEOM * lwmpoly_as_lwgeom(const LWMPOLY *obj)
Definition: lwgeom.c:285
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:85
double lwgeom_area(const LWGEOM *geom)
Definition: lwgeom.c:1872
#define WKT_ISO
Definition: liblwgeom.h:2075
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:123
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:676
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:904
void lwpoly_free(LWPOLY *poly)
Definition: lwpoly.c:175
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
Definition: lwgeom.c:206
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
Definition: rt_raster.c:356
void rt_raster_set_scale(rt_raster raster, double scaleX, double scaleY)
Set scale in projection units.
Definition: rt_raster.c:137
LWPOLY * rt_raster_pixel_as_polygon(rt_raster raster, int x, int y)
Get a raster pixel as a polygon.
Definition: rt_geometry.c:610
@ PT_32BUI
Definition: librtcore.h:194
void rt_raster_set_skews(rt_raster raster, double skewX, double skewY)
Set skews about the X and Y axis.
Definition: rt_raster.c:168
rt_errorstate rt_raster_surface(rt_raster raster, int nband, LWMPOLY **surface)
Get a raster as a surface (multipolygon).
Definition: rt_geometry.c:355
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
Definition: rt_raster.c:48
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:974
@ ES_NONE
Definition: librtcore.h:180
rt_errorstate rt_raster_get_convex_hull(rt_raster raster, LWGEOM **hull)
Get raster's convex hull.
Definition: rt_geometry.c:803
void rtdealloc(void *mem)
Definition: rt_context.c:186
rt_errorstate rt_raster_get_envelope_geom(rt_raster raster, LWGEOM **env)
Get raster's envelope as a geometry.
Definition: rt_geometry.c:670
rt_errorstate rt_raster_get_perimeter(rt_raster raster, int nband, LWGEOM **perimeter)
Get raster perimeter.
Definition: rt_geometry.c:182
rt_errorstate rt_raster_get_envelope(rt_raster raster, rt_envelope *env)
Get raster's envelope.
Definition: rt_raster.c:873
void rt_raster_set_offsets(rt_raster raster, double x, double y)
Set insertion points in projection units.
Definition: rt_raster.c:199
band
Definition: ovdump.py:57
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
rt_band cu_add_band(rt_raster raster, rt_pixtype pixtype, int hasnodata, double nodataval)
void cu_free_raster(rt_raster raster)
uint8_t type
Definition: liblwgeom.h:399
POINTARRAY ** rings
Definition: liblwgeom.h:460
uint32_t nrings
Definition: liblwgeom.h:458
int32_t srid
Definition: liblwgeom.h:457
double x
Definition: liblwgeom.h:355
double y
Definition: liblwgeom.h:355
uint32_t npoints
Definition: liblwgeom.h:374
double MinX
Definition: librtcore.h:165
double MaxX
Definition: librtcore.h:166
double MinY
Definition: librtcore.h:167
double MaxY
Definition: librtcore.h:168
unsigned int uint32_t
Definition: uthash.h:78