PostGIS  2.5.7dev-r@@SVN_REVISION@@
cu_gdal.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 
27 static void test_gdal_configured() {
28  CU_ASSERT(rt_util_gdal_configured());
29 }
30 
31 static void test_gdal_drivers() {
32  uint32_t i;
33  uint32_t size;
34  rt_gdaldriver drv = NULL;
35 
36  drv = (rt_gdaldriver) rt_raster_gdal_drivers(&size, 1);
37  CU_ASSERT(drv != NULL);
38 
39  for (i = 0; i < size; i++) {
40  CU_ASSERT(strlen(drv[i].short_name));
41  rtdealloc(drv[i].short_name);
42  rtdealloc(drv[i].long_name);
43  rtdealloc(drv[i].create_options);
44  }
45 
46  rtdealloc(drv);
47 }
48 
49 static void test_gdal_rasterize() {
51  char srs[] = "PROJCS[\"unnamed\",GEOGCS[\"unnamed ellipse\",DATUM[\"unknown\",SPHEROID[\"unnamed\",6370997,0]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433]],PROJECTION[\"Lambert_Azimuthal_Equal_Area\"],PARAMETER[\"latitude_of_center\",45],PARAMETER[\"longitude_of_center\",-100],PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",0],UNIT[\"Meter\",1],AUTHORITY[\"EPSG\",\"2163\"]]";
52  const char wkb_hex[] = "010300000001000000050000000000000080841ec100000000600122410000000080841ec100000000804f22410000000040e81dc100000000804f22410000000040e81dc100000000600122410000000080841ec10000000060012241";
53  const char *pos = wkb_hex;
54  unsigned char *wkb = NULL;
55  int wkb_len = 0;
56  int i;
57  double scale_x = 100;
58  double scale_y = -100;
59 
60  rt_pixtype pixtype[] = {PT_8BUI};
61  double init[] = {0};
62  double value[] = {1};
63  double nodata[] = {0};
64  uint8_t nodata_mask[] = {1};
65 
66  /* hex to byte */
67  wkb_len = (int) ceil(((double) strlen(wkb_hex)) / 2);
68  wkb = (unsigned char *) rtalloc(sizeof(unsigned char) * wkb_len);
69  for (i = 0; i < wkb_len; i++) {
70  int b = 0;
71  sscanf(pos, "%2x", &b);
72  wkb[i] = (unsigned char)b;
73  pos += 2;
74  }
75 
77  wkb,
78  wkb_len, srs,
79  1, pixtype,
80  init, value,
81  nodata, nodata_mask,
82  NULL, NULL,
83  &scale_x, &scale_y,
84  NULL, NULL,
85  NULL, NULL,
86  NULL, NULL,
87  NULL
88  );
89 
90  CU_ASSERT(raster != NULL);
91  CU_ASSERT_EQUAL(rt_raster_get_width(raster), 100);
92  CU_ASSERT_EQUAL(rt_raster_get_height(raster), 100);
93  CU_ASSERT_NOT_EQUAL(rt_raster_get_num_bands(raster), 0);
94  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(raster), -500000, DBL_EPSILON);
95  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(raster), 600000, DBL_EPSILON);
96 
97  rtdealloc(wkb);
99 }
100 
101 static rt_raster fillRasterToPolygonize(int hasnodata, double nodataval) {
102  rt_band band = NULL;
103  rt_pixtype pixtype = PT_32BF;
104 
105  /* Create raster */
106  uint16_t width = 9;
107  uint16_t height = 9;
108 
109  rt_raster raster = rt_raster_new(width, height);
111 
112  band = cu_add_band(raster, pixtype, hasnodata, nodataval);
113  CU_ASSERT(band != NULL);
114 
115  {
116  int x, y;
117  for (x = 0; x < rt_band_get_width(band); ++x)
118  for (y = 0; y < rt_band_get_height(band); ++y)
119  rt_band_set_pixel(band, x, y, 0.0, NULL);
120  }
121 
122  rt_band_set_pixel(band, 3, 1, 1.8, NULL);
123  rt_band_set_pixel(band, 4, 1, 1.8, NULL);
124  rt_band_set_pixel(band, 5, 1, 2.8, NULL);
125  rt_band_set_pixel(band, 2, 2, 1.8, NULL);
126  rt_band_set_pixel(band, 3, 2, 1.8, NULL);
127  rt_band_set_pixel(band, 4, 2, 1.8, NULL);
128  rt_band_set_pixel(band, 5, 2, 2.8, NULL);
129  rt_band_set_pixel(band, 6, 2, 2.8, NULL);
130  rt_band_set_pixel(band, 1, 3, 1.8, NULL);
131  rt_band_set_pixel(band, 2, 3, 1.8, NULL);
132  rt_band_set_pixel(band, 6, 3, 2.8, NULL);
133  rt_band_set_pixel(band, 7, 3, 2.8, NULL);
134  rt_band_set_pixel(band, 1, 4, 1.8, NULL);
135  rt_band_set_pixel(band, 2, 4, 1.8, NULL);
136  rt_band_set_pixel(band, 6, 4, 2.8, NULL);
137  rt_band_set_pixel(band, 7, 4, 2.8, NULL);
138  rt_band_set_pixel(band, 1, 5, 1.8, NULL);
139  rt_band_set_pixel(band, 2, 5, 1.8, NULL);
140  rt_band_set_pixel(band, 6, 5, 2.8, NULL);
141  rt_band_set_pixel(band, 7, 5, 2.8, NULL);
142  rt_band_set_pixel(band, 2, 6, 1.8, NULL);
143  rt_band_set_pixel(band, 3, 6, 1.8, NULL);
144  rt_band_set_pixel(band, 4, 6, 1.8, NULL);
145  rt_band_set_pixel(band, 5, 6, 2.8, NULL);
146  rt_band_set_pixel(band, 6, 6, 2.8, NULL);
147  rt_band_set_pixel(band, 3, 7, 1.8, NULL);
148  rt_band_set_pixel(band, 4, 7, 1.8, NULL);
149  rt_band_set_pixel(band, 5, 7, 2.8, NULL);
150 
151  return raster;
152 }
153 
154 static void test_gdal_polygonize() {
155  int i;
156  rt_raster rt;
157  int nPols = 0;
158  rt_geomval gv = NULL;
159  LWGEOM *gexpected, *gobserved;
160  gexpected = lwgeom_from_wkt("POLYGON((3 1,3 2,2 2,2 3,1 3,1 6,2 6,2 7,3 7,3 8,5 8,5 6,3 6,3 3,4 3,5 3,5 1,3 1))",
162 
163  rt = fillRasterToPolygonize(1, -1.0);
164  CU_ASSERT(rt_raster_has_band(rt, 0));
165 
166  nPols = 0;
167  gv = rt_raster_gdal_polygonize(rt, 0, TRUE, &nPols);
168 
169  CU_ASSERT_DOUBLE_EQUAL(gv[0].val, 1.8, FLT_EPSILON);
170 
171  gobserved = (LWGEOM *)gv[0].geom;
172 
173  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
174 
175  CU_ASSERT_DOUBLE_EQUAL(gv[1].val, 0.0, FLT_EPSILON);
176  gobserved = (LWGEOM *)gv[1].geom;
177  gexpected = lwgeom_from_wkt("POLYGON((3 3,3 6,6 6,6 3,3 3))", LW_PARSER_CHECK_NONE);
178  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON );
179 
180  CU_ASSERT_DOUBLE_EQUAL(gv[2].val, 2.8, FLT_EPSILON);
181  gobserved = (LWGEOM *)gv[2].geom;
182  gexpected = lwgeom_from_wkt("POLYGON((5 1,5 3,6 3,6 6,5 6,5 8,6 8,6 7,7 7,7 6,8 6,8 3,7 3,7 2,6 2,6 1,5 1))",
184  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
185 
186  gobserved = (LWGEOM *)gv[3].geom;
187  gexpected = lwgeom_from_wkt(
188  "POLYGON((0 0,0 9,9 9,9 0,0 0),(6 7,6 8,3 8,3 7,2 7,2 6,1 6,1 3,2 3,2 2,3 2,3 1,6 1,6 2,7 2,7 3,8 3,8 6,7 6,7 7,6 7))",
190  CU_ASSERT_DOUBLE_EQUAL(gv[3].val, 0.0, FLT_EPSILON);
191  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
192 
193  for (i = 0; i < nPols; i++) lwgeom_free((LWGEOM *) gv[i].geom);
194  rtdealloc(gv);
196 
197  /* Second test: NODATA value = 1.8 */
198  rt = fillRasterToPolygonize(1, 1.8);
199 
200  /* We can check rt_raster_has_band here too */
201  CU_ASSERT(rt_raster_has_band(rt, 0));
202 
203  nPols = 0;
204  gv = rt_raster_gdal_polygonize(rt, 0, TRUE, &nPols);
205 
206  /*
207  for (i = 0; i < nPols; i++) {
208  wkt = lwgeom_to_text((const LWGEOM *) gv[i].geom);
209  printf("(i, val, geom) = (%d, %f, %s)\n", i, gv[i].val, wkt);
210  rtdealloc(wkt);
211  }
212  */
213 
214  CU_ASSERT_DOUBLE_EQUAL(gv[1].val, 0.0, FLT_EPSILON);
215  gobserved = (LWGEOM *)gv[1].geom;
216  gexpected = lwgeom_from_wkt("POLYGON((3 3,3 6,6 6,6 3,3 3))", LW_PARSER_CHECK_NONE);
217  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
218  //wkt = lwgeom_to_text((const LWGEOM *) gv[1].geom);
219  //CU_ASSERT_STRING_EQUAL(wkt, "POLYGON((3 3,3 6,6 6,6 3,3 3))");
220  //rtdealloc(wkt);
221 
222  CU_ASSERT_DOUBLE_EQUAL(gv[2].val, 2.8, FLT_EPSILON);
223  gobserved = (LWGEOM *)gv[2].geom;
224  gexpected = lwgeom_from_wkt("POLYGON((5 1,5 3,6 3,6 6,5 6,5 8,6 8,6 7,7 7,7 6,8 6,8 3,7 3,7 2,6 2,6 1,5 1))",
226  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
227  //wkt = lwgeom_to_text((const LWGEOM *) gv[2].geom);
228  //CU_ASSERT_STRING_EQUAL(wkt, "POLYGON((5 1,5 3,6 3,6 6,5 6,5 8,6 8,6 7,7 7,7 6,8 6,8 3,7 3,7 2,6 2,6 1,5 1))");
229  //rtdealloc(wkt);
230 
231  CU_ASSERT_DOUBLE_EQUAL(gv[3].val, 0.0, FLT_EPSILON);
232  gobserved = (LWGEOM *)gv[3].geom;
233  gexpected = lwgeom_from_wkt(
234  "POLYGON((0 0,0 9,9 9,9 0,0 0),(6 7,6 8,3 8,3 7,2 7,2 6,1 6,1 3,2 3,2 2,3 2,3 1,6 1,6 2,7 2,7 3,8 3,8 6,7 6,7 7,6 7))",
236  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
237 
238  for (i = 0; i < nPols; i++) lwgeom_free((LWGEOM *) gv[i].geom);
239  rtdealloc(gv);
241 
242  /* Third test: NODATA value = 2.8 */
243  rt = fillRasterToPolygonize(1, 2.8);
244 
245  /* We can check rt_raster_has_band here too */
246  CU_ASSERT(rt_raster_has_band(rt, 0));
247 
248  nPols = 0;
249  gv = rt_raster_gdal_polygonize(rt, 0, TRUE, &nPols);
250 
251  /*
252  for (i = 0; i < nPols; i++) {
253  wkt = lwgeom_to_text((const LWGEOM *) gv[i].geom);
254  printf("(i, val, geom) = (%d, %f, %s)\n", i, gv[i].val, wkt);
255  rtdealloc(wkt);
256  }
257  */
258 
259  CU_ASSERT_DOUBLE_EQUAL(gv[3].val, 0.0, FLT_EPSILON);
260  gobserved = (LWGEOM *)gv[3].geom;
261  gexpected = lwgeom_from_wkt(
262  "POLYGON((0 0,0 9,9 9,9 0,0 0),(6 7,6 8,3 8,3 7,2 7,2 6,1 6,1 3,2 3,2 2,3 2,3 1,6 1,6 2,7 2,7 3,8 3,8 6,7 6,7 7,6 7))",
264  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
265 
266  gobserved = (LWGEOM *)gv[0].geom;
267  gexpected = lwgeom_from_wkt(
268  "POLYGON((3 1,3 2,2 2,2 3,1 3,1 6,2 6,2 7,3 7,3 8,5 8,5 6,3 6,3 3,4 3,5 3,5 1,3 1))", LW_PARSER_CHECK_NONE);
269  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
270 
271  CU_ASSERT_DOUBLE_EQUAL(gv[1].val, 0.0, FLT_EPSILON);
272  gobserved = (LWGEOM *)gv[1].geom;
273  gexpected = lwgeom_from_wkt("POLYGON((3 3,3 6,6 6,6 3,3 3))", LW_PARSER_CHECK_NONE);
274  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
275 
276  for (i = 0; i < nPols; i++) lwgeom_free((LWGEOM *) gv[i].geom);
277  rtdealloc(gv);
279 
280  /* Fourth test: NODATA value = 0 */
281  rt = fillRasterToPolygonize(1, 0.0);
282  /* We can check rt_raster_has_band here too */
283  CU_ASSERT(rt_raster_has_band(rt, 0));
284 
285  nPols = 0;
286  gv = rt_raster_gdal_polygonize(rt, 0, TRUE, &nPols);
287 
288  /*
289  for (i = 0; i < nPols; i++) {
290  wkt = lwgeom_to_text((const LWGEOM *) gv[i].geom);
291  printf("(i, val, geom) = (%d, %f, %s)\n", i, gv[i].val, wkt);
292  rtdealloc(wkt);
293  }
294  */
295 
296  CU_ASSERT_DOUBLE_EQUAL(gv[0].val, 1.8, FLT_EPSILON);
297  gobserved = (LWGEOM *)gv[0].geom;
298  gexpected = lwgeom_from_wkt("POLYGON((3 1,3 2,2 2,2 3,1 3,1 6,2 6,2 7,3 7,3 8,5 8,5 6,3 6,3 3,4 3,5 3,5 1,3 1))", LW_PARSER_CHECK_NONE);
299  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
300 
301  CU_ASSERT_DOUBLE_EQUAL(gv[1].val, 2.8, FLT_EPSILON);
302  gobserved = (LWGEOM *)gv[1].geom;
303  gexpected = lwgeom_from_wkt("POLYGON((5 1,5 3,6 3,6 6,5 6,5 8,6 8,6 7,7 7,7 6,8 6,8 3,7 3,7 2,6 2,6 1,5 1))",
305  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
306 
307  for (i = 0; i < nPols; i++) lwgeom_free((LWGEOM *) gv[i].geom);
308  rtdealloc(gv);
310 
311  /* Last test: There is no NODATA value (all values are valid) */
312  rt = fillRasterToPolygonize(0, 0.0);
313  /* We can check rt_raster_has_band here too */
314  CU_ASSERT(rt_raster_has_band(rt, 0));
315 
316  nPols = 0;
317  gv = rt_raster_gdal_polygonize(rt, 0, TRUE, &nPols);
318 
319  /*
320  for (i = 0; i < nPols; i++) {
321  wkt = lwgeom_to_text((const LWGEOM *) gv[i].geom);
322  printf("(i, val, geom) = (%d, %f, %s)\n", i, gv[i].val, wkt);
323  rtdealloc(wkt);
324  }
325  */
326 
327  CU_ASSERT_DOUBLE_EQUAL(gv[0].val, 1.8, FLT_EPSILON);
328  gobserved = (LWGEOM *)gv[0].geom;
329  gexpected = lwgeom_from_wkt(
330  "POLYGON((3 1,3 2,2 2,2 3,1 3,1 6,2 6,2 7,3 7,3 8,5 8,5 6,3 6,3 3,4 3,5 3,5 1,3 1))", LW_PARSER_CHECK_NONE);
331  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
332 
333  CU_ASSERT_DOUBLE_EQUAL(gv[1].val, 0.0, FLT_EPSILON);
334  gobserved = (LWGEOM *)gv[1].geom;
335  gexpected = lwgeom_from_wkt("POLYGON((3 3,3 6,6 6,6 3,3 3))", LW_PARSER_CHECK_NONE);
336  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
337 
338  CU_ASSERT_DOUBLE_EQUAL(gv[2].val, 2.8, FLT_EPSILON);
339  gobserved = (LWGEOM *)gv[2].geom;
340  gexpected = lwgeom_from_wkt("POLYGON((5 1,5 3,6 3,6 6,5 6,5 8,6 8,6 7,7 7,7 6,8 6,8 3,7 3,7 2,6 2,6 1,5 1))",
342  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
343 
344  CU_ASSERT_DOUBLE_EQUAL(gv[3].val, 0.0, FLT_EPSILON);
345  gobserved = (LWGEOM *)gv[3].geom;
346  gexpected = lwgeom_from_wkt(
347  "POLYGON((0 0,0 9,9 9,9 0,0 0),(6 7,6 8,3 8,3 7,2 7,2 6,1 6,1 3,2 3,2 2,3 2,3 1,6 1,6 2,7 2,7 3,8 3,8 6,7 6,7 7,6 7))",
349  CU_ASSERT_DOUBLE_EQUAL(lwgeom_area(gobserved), lwgeom_area(gexpected), FLT_EPSILON);
350 
351  for (i = 0; i < nPols; i++) lwgeom_free((LWGEOM *) gv[i].geom);
352  rtdealloc(gv);
354 }
355 
356 static void test_raster_to_gdal() {
357  rt_pixtype pixtype = PT_64BF;
358  rt_raster raster = NULL;
359  rt_band band = NULL;
360  uint32_t x;
361  uint32_t width = 100;
362  uint32_t y;
363  uint32_t height = 100;
364  char srs[] = "PROJCS[\"unnamed\",GEOGCS[\"unnamed ellipse\",DATUM[\"unknown\",SPHEROID[\"unnamed\",6370997,0]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433]],PROJECTION[\"Lambert_Azimuthal_Equal_Area\"],PARAMETER[\"latitude_of_center\",45],PARAMETER[\"longitude_of_center\",-100],PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",0],UNIT[\"Meter\",1],AUTHORITY[\"EPSG\",\"2163\"]]";
365 
366  uint64_t gdalSize;
367  uint8_t *gdal = NULL;
368 
369  raster = rt_raster_new(width, height);
370  CU_ASSERT(raster != NULL); /* or we're out of virtual memory */
371 
372  band = cu_add_band(raster, pixtype, 1, 0);
373  CU_ASSERT(band != NULL);
374 
375  rt_raster_set_offsets(raster, -500000, 600000);
376  rt_raster_set_scale(raster, 1000, -1000);
377 
378  for (x = 0; x < width; x++) {
379  for (y = 0; y < height; y++) {
380  rt_band_set_pixel(band, x, y, (((double) x * y) + (x + y) + (x + y * x)) / (x + y + 1), NULL);
381  }
382  }
383 
384  gdal = rt_raster_to_gdal(raster, srs, "GTiff", NULL, &gdalSize);
385  /*printf("gdalSize: %d\n", (int) gdalSize);*/
386  CU_ASSERT(gdalSize);
387 
388  /*
389  FILE *fh = NULL;
390  fh = fopen("/tmp/out.tif", "w");
391  fwrite(gdal, sizeof(uint8_t), gdalSize, fh);
392  fclose(fh);
393  */
394 
395  if (gdal) CPLFree(gdal);
396 
398 
399  raster = rt_raster_new(width, height);
400  CU_ASSERT(raster != NULL); /* or we're out of virtual memory */
401 
402  band = cu_add_band(raster, pixtype, 1, 0);
403  CU_ASSERT(band != NULL);
404 
405  rt_raster_set_offsets(raster, -500000, 600000);
406  rt_raster_set_scale(raster, 1000, -1000);
407 
408  for (x = 0; x < width; x++) {
409  for (y = 0; y < height; y++) {
410  rt_band_set_pixel(band, x, y, x, NULL);
411  }
412  }
413 
414  /* add check that band isn't NODATA */
415  CU_ASSERT_EQUAL(rt_band_check_is_nodata(band), FALSE);
416 
417  gdal = rt_raster_to_gdal(raster, srs, "PNG", NULL, &gdalSize);
418  /*printf("gdalSize: %d\n", (int) gdalSize);*/
419  CU_ASSERT(gdalSize);
420 
421  if (gdal) CPLFree(gdal);
422 
423  gdal = rt_raster_to_gdal(raster, srs, "PCIDSK", NULL, &gdalSize);
424  CU_ASSERT(gdal == NULL);
425 
427 }
428 
429 static void test_gdal_to_raster() {
430  rt_pixtype pixtype = PT_64BF;
431  rt_band band = NULL;
432 
434  rt_raster rast;
435  const uint32_t width = 100;
436  const uint32_t height = 100;
437  uint32_t x;
438  uint32_t y;
439  int v;
440  double values[width][height];
441  int rtn = 0;
442  double value;
443 
444  GDALDriverH gddrv = NULL;
445  int destroy = 0;
446  GDALDatasetH gdds = NULL;
447 
448  raster = rt_raster_new(width, height);
449  CU_ASSERT(raster != NULL); /* or we're out of virtual memory */
450 
451  band = cu_add_band(raster, pixtype, 1, 0);
452  CU_ASSERT(band != NULL);
453 
454  for (x = 0; x < width; x++) {
455  for (y = 0; y < height; y++) {
456  values[x][y] = (((double) x * y) + (x + y) + (x + y * x)) / (x + y + 1);
457  rt_band_set_pixel(band, x, y, values[x][y], NULL);
458  }
459  }
460 
461  gdds = rt_raster_to_gdal_mem(raster, NULL, NULL, NULL, 0, &gddrv, &destroy);
462  CU_ASSERT(gddrv != NULL);
463  CU_ASSERT_EQUAL(destroy, 0);
464  CU_ASSERT(gdds != NULL);
465  CU_ASSERT_EQUAL((uint32_t)GDALGetRasterXSize(gdds), width);
466  CU_ASSERT_EQUAL((uint32_t)GDALGetRasterYSize(gdds), height);
467 
469  CU_ASSERT(rast != NULL);
470  CU_ASSERT_EQUAL(rt_raster_get_num_bands(rast), 1);
471 
473  CU_ASSERT(band != NULL);
474 
475  for (x = 0; x < width; x++) {
476  for (y = 0; y < height; y++) {
477  rtn = rt_band_get_pixel(band, x, y, &value, NULL);
478  CU_ASSERT_EQUAL(rtn, ES_NONE);
479  CU_ASSERT_DOUBLE_EQUAL(value, values[x][y], DBL_EPSILON);
480  }
481  }
482 
483  GDALClose(gdds);
484  gdds = NULL;
485  gddrv = NULL;
486 
489 
490  raster = rt_raster_new(width, height);
491  CU_ASSERT(raster != NULL); /* or we're out of virtual memory */
492 
493  pixtype = PT_8BSI;
494  band = cu_add_band(raster, pixtype, 1, 0);
495  CU_ASSERT(band != NULL);
496 
497  v = -127;
498  for (x = 0; x < width; x++) {
499  for (y = 0; y < height; y++) {
500  values[x][y] = v++;
501  rt_band_set_pixel(band, x, y, values[x][y], NULL);
502  if (v == 128)
503  v = -127;
504  }
505  }
506 
507  gdds = rt_raster_to_gdal_mem(raster, NULL, NULL, NULL, 0, &gddrv, &destroy);
508  CU_ASSERT(gddrv != NULL);
509  CU_ASSERT_EQUAL(destroy, 0);
510  CU_ASSERT(gdds != NULL);
511  CU_ASSERT_EQUAL((uint32_t)GDALGetRasterXSize(gdds), width);
512  CU_ASSERT_EQUAL((uint32_t)GDALGetRasterYSize(gdds), height);
513 
515  CU_ASSERT(rast != NULL);
516  CU_ASSERT_EQUAL(rt_raster_get_num_bands(rast), 1);
517 
519  CU_ASSERT(band != NULL);
520  CU_ASSERT_EQUAL(rt_band_get_pixtype(band), PT_16BSI);
521 
522  for (x = 0; x < width; x++) {
523  for (y = 0; y < height; y++) {
524  rtn = rt_band_get_pixel(band, x, y, &value, NULL);
525  CU_ASSERT_EQUAL(rtn, ES_NONE);
526  CU_ASSERT_DOUBLE_EQUAL(value, values[x][y], 1.);
527  }
528  }
529 
530  GDALClose(gdds);
531  gdds = NULL;
532  gddrv = NULL;
533 
536 }
537 
538 static void test_gdal_warp() {
539  rt_pixtype pixtype = PT_64BF;
540  rt_band band = NULL;
541 
543  rt_raster rast;
544  uint32_t x;
545  uint32_t width = 100;
546  uint32_t y;
547  uint32_t height = 100;
548  double value = 0;
549 
550  char src_srs[] = "PROJCS[\"unnamed\",GEOGCS[\"unnamed ellipse\",DATUM[\"unknown\",SPHEROID[\"unnamed\",6370997,0]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433]],PROJECTION[\"Lambert_Azimuthal_Equal_Area\"],PARAMETER[\"latitude_of_center\",45],PARAMETER[\"longitude_of_center\",-100],PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",0],UNIT[\"Meter\",1],AUTHORITY[\"EPSG\",\"2163\"]]";
551 
552  char dst_srs[] = "PROJCS[\"NAD83 / California Albers\",GEOGCS[\"NAD83\",DATUM[\"North_American_Datum_1983\",SPHEROID[\"GRS 1980\",6378137,298.257222101,AUTHORITY[\"EPSG\",\"7019\"]],AUTHORITY[\"EPSG\",\"6269\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4269\"]],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],PROJECTION[\"Albers_Conic_Equal_Area\"],PARAMETER[\"standard_parallel_1\",34],PARAMETER[\"standard_parallel_2\",40.5],PARAMETER[\"latitude_of_center\",0],PARAMETER[\"longitude_of_center\",-120],PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",-4000000],AUTHORITY[\"EPSG\",\"3310\"],AXIS[\"X\",EAST],AXIS[\"Y\",NORTH]]";
553 
554  raster = rt_raster_new(width, height);
555  CU_ASSERT(raster != NULL); /* or we're out of virtual memory */
556 
557  band = cu_add_band(raster, pixtype, 1, 0);
558  CU_ASSERT(band != NULL);
559 
560  rt_raster_set_offsets(raster, -500000, 600000);
561  rt_raster_set_scale(raster, 1000, -1000);
562 
563  for (x = 0; x < width; x++) {
564  for (y = 0; y < height; y++) {
565  rt_band_set_pixel(band, x, y, (((double) x * y) + (x + y) + (x + y * x)) / (x + y + 1), NULL);
566  }
567  }
568 
570  raster,
571  src_srs, dst_srs,
572  NULL, NULL,
573  NULL, NULL,
574  NULL, NULL,
575  NULL, NULL,
576  NULL, NULL,
577  GRA_NearestNeighbour, -1
578  );
579  CU_ASSERT(rast != NULL);
580  CU_ASSERT_EQUAL(rt_raster_get_width(rast), 122);
581  CU_ASSERT_EQUAL(rt_raster_get_height(rast), 116);
582  CU_ASSERT_NOT_EQUAL(rt_raster_get_num_bands(rast), 0);
583 
585  CU_ASSERT(band != NULL);
586 
587  CU_ASSERT(rt_band_get_hasnodata_flag(band));
589  CU_ASSERT_DOUBLE_EQUAL(value, 0., DBL_EPSILON);
590 
591  CU_ASSERT_EQUAL(rt_band_get_pixel(band, 0, 0, &value, NULL), ES_NONE);
592  CU_ASSERT_DOUBLE_EQUAL(value, 0., DBL_EPSILON);
593 
596 }
597 
598 /* register tests */
599 void gdal_suite_setup(void);
601 {
602  CU_pSuite suite = CU_add_suite("gdal", NULL, NULL);
609  PG_ADD_TEST(suite, test_gdal_warp);
610 }
611 
static rt_raster fillRasterToPolygonize(int hasnodata, double nodataval)
Definition: cu_gdal.c:101
static void test_gdal_warp()
Definition: cu_gdal.c:538
static void test_gdal_rasterize()
Definition: cu_gdal.c:49
static void test_gdal_to_raster()
Definition: cu_gdal.c:429
static void test_raster_to_gdal()
Definition: cu_gdal.c:356
void gdal_suite_setup(void)
Definition: cu_gdal.c:600
static void test_gdal_drivers()
Definition: cu_gdal.c:31
static void test_gdal_configured()
Definition: cu_gdal.c:27
static void test_gdal_polygonize()
Definition: cu_gdal.c:154
#define TRUE
Definition: dbfopen.c:169
#define FALSE
Definition: dbfopen.c:168
#define PG_ADD_TEST(suite, testfunc)
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2005
double lwgeom_area(const LWGEOM *geom)
Definition: lwgeom.c:1872
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:904
void * rtalloc(size_t size)
Wrappers used for managing memory.
Definition: rt_context.c:171
uint16_t rt_band_get_width(rt_band band)
Return width of this band.
Definition: rt_band.c:640
double rt_raster_get_x_offset(rt_raster raster)
Get raster x offset, in projection units.
Definition: rt_raster.c:213
int rt_util_gdal_configured(void)
Definition: rt_util.c:315
rt_gdaldriver rt_raster_gdal_drivers(uint32_t *drv_count, uint8_t cancc)
Returns a set of available GDAL drivers.
Definition: rt_raster.c:1716
void rt_raster_set_scale(rt_raster raster, double scaleX, double scaleY)
Set scale in projection units.
Definition: rt_raster.c:137
uint8_t * rt_raster_to_gdal(rt_raster raster, const char *srs, char *format, char **options, uint64_t *gdalsize)
Return formatted GDAL raster from raster.
Definition: rt_raster.c:1598
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
Definition: rt_band.c:674
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
rt_pixtype
Definition: librtcore.h:185
@ PT_32BF
Definition: librtcore.h:195
@ PT_8BSI
Definition: librtcore.h:189
@ PT_16BSI
Definition: librtcore.h:191
@ PT_64BF
Definition: librtcore.h:196
@ PT_8BUI
Definition: librtcore.h:190
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
Definition: rt_raster.c:48
int rt_raster_has_band(rt_raster raster, int nband)
Return TRUE if the raster has a band of this number.
Definition: rt_raster.c:1347
rt_raster rt_raster_from_gdal_dataset(GDALDatasetH ds)
Return a raster from a GDAL dataset.
Definition: rt_raster.c:2182
rt_geomval rt_raster_gdal_polygonize(rt_raster raster, int nband, int exclude_nodata_value, int *pnElements)
Returns a set of "geomval" value, one for each group of pixel sharing the same value for the provided...
Definition: rt_geometry.c:940
int rt_band_check_is_nodata(rt_band band)
Returns TRUE if the band is only nodata values.
Definition: rt_band.c:1752
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_raster rt_raster_gdal_rasterize(const unsigned char *wkb, uint32_t wkb_len, const char *srs, uint32_t num_bands, rt_pixtype *pixtype, double *init, double *value, double *nodata, uint8_t *hasnodata, int *width, int *height, double *scale_x, double *scale_y, double *ul_xw, double *ul_yw, double *grid_xw, double *grid_yw, double *skew_x, double *skew_y, char **options)
Return a raster of the provided geometry.
Definition: rt_raster.c:2509
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
struct rt_gdaldriver_t * rt_gdaldriver
Definition: librtcore.h:154
uint16_t rt_raster_get_height(rt_raster raster)
Definition: rt_raster.c:129
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
Definition: rt_band.c:1730
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
Definition: rt_band.c:631
uint16_t rt_raster_get_width(rt_raster raster)
Definition: rt_raster.c:121
void rtdealloc(void *mem)
Definition: rt_context.c:186
rt_raster rt_raster_gdal_warp(rt_raster raster, const char *src_srs, const char *dst_srs, double *scale_x, double *scale_y, int *width, int *height, double *ul_xw, double *ul_yw, double *grid_xw, double *grid_yw, double *skew_x, double *skew_y, GDALResampleAlg resample_alg, double max_err)
Return a warped raster using GDAL Warp API.
Definition: rt_warp.c:178
GDALDatasetH rt_raster_to_gdal_mem(rt_raster raster, const char *srs, uint32_t *bandNums, int *excludeNodataValues, int count, GDALDriverH *rtn_drv, int *destroy_rtn_drv)
Return GDAL dataset using GDAL MEM driver from raster.
Definition: rt_raster.c:1826
void rt_raster_set_offsets(rt_raster raster, double x, double y)
Set insertion points in projection units.
Definition: rt_raster.c:199
uint16_t rt_band_get_height(rt_band band)
Return height of this band.
Definition: rt_band.c:649
double rt_raster_get_y_offset(rt_raster raster)
Get raster y offset, in projection units.
Definition: rt_raster.c:222
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:381
int value
Definition: genraster.py:61
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)
unsigned int uint32_t
Definition: uthash.h:78
unsigned char uint8_t
Definition: uthash.h:79