PostGIS  2.5.7dev-r@@SVN_REVISION@@
cu_raster_wkb.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  * Copyright (C) 2009 Sandro Santilli <strk@kbt.io>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  *
23  */
24 
25 #include "CUnit/Basic.h"
26 #include "cu_tester.h"
27 
28 static void test_raster_wkb() {
29  /* will use default allocators and message handlers */
30  rt_raster raster = NULL;
31  const char *hexwkb = NULL;
32  const char *out = NULL;
33  uint32_t len = 0;
34  int i = 0;
35 
36  /* ------------------------------------------------------ */
37  /* No bands, 7x8 - little endian */
38  /* ------------------------------------------------------ */
39 
40  hexwkb =
41 "01" /* little endian (uint8 ndr) */
42 "0000" /* version (uint16 0) */
43 "0000" /* nBands (uint16 0) */
44 "000000000000F03F" /* scaleX (float64 1) */
45 "0000000000000040" /* scaleY (float64 2) */
46 "0000000000000840" /* ipX (float64 3) */
47 "0000000000001040" /* ipY (float64 4) */
48 "0000000000001440" /* skewX (float64 5) */
49 "0000000000001840" /* skewY (float64 6) */
50 "0A000000" /* SRID (int32 10) */
51 "0700" /* width (uint16 7) */
52 "0800" /* height (uint16 8) */
53  ;
54 
55  raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
56  CU_ASSERT(raster != NULL);
57  CU_ASSERT_EQUAL(rt_raster_get_num_bands(raster), 0);
58  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(raster), 1, DBL_EPSILON);
59  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(raster), 2, DBL_EPSILON);
60  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(raster), 3, DBL_EPSILON);
61  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(raster), 4, DBL_EPSILON);
62  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(raster), 5, DBL_EPSILON);
63  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(raster), 6, DBL_EPSILON);
64  CU_ASSERT_EQUAL(rt_raster_get_srid(raster), 10);
65  CU_ASSERT_EQUAL(rt_raster_get_width(raster), 7);
66  CU_ASSERT_EQUAL(rt_raster_get_height(raster), 8);
67 
68  out = rt_raster_to_hexwkb(raster, FALSE, &len);
69 /*
70  printf(" in hexwkb len: %d\n", strlen(hexwkb));
71  printf("out hexwkb len: %d\n", len);
72  printf(" in hexwkb: %s\n", hexwkb);
73  printf("out hexwkb: %s\n", out);
74 */
75  CU_ASSERT_EQUAL(len, strlen(hexwkb));
76 /* would depend on machine endian...
77  CU_ASSERT_STRING_EQUAL(hexwkb, out);
78 */
79  free((/*no const*/ void*)out);
80 
81  {
82  void *serialized;
83  rt_raster rast2;
84 
85  serialized = rt_raster_serialize(raster);
86  rast2 = rt_raster_deserialize(serialized, FALSE);
87 
88  cu_free_raster(rast2);
89  free(serialized);
90  }
91 
93 
94  /* ------------------------------------------------------ */
95  /* No bands, 7x8 - big endian */
96  /* ------------------------------------------------------ */
97 
98  hexwkb =
99 "00" /* big endian (uint8 xdr) */
100 "0000" /* version (uint16 0) */
101 "0000" /* nBands (uint16 0) */
102 "3FF0000000000000" /* scaleX (float64 1) */
103 "4000000000000000" /* scaleY (float64 2) */
104 "4008000000000000" /* ipX (float64 3) */
105 "4010000000000000" /* ipY (float64 4) */
106 "4014000000000000" /* skewX (float64 5) */
107 "4018000000000000" /* skewY (float64 6) */
108 "0000000A" /* SRID (int32 10) */
109 "0007" /* width (uint16 7) */
110 "0008" /* height (uint16 8) */
111  ;
112 
113  raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
114  CU_ASSERT(raster != NULL);
115  CU_ASSERT_EQUAL(rt_raster_get_num_bands(raster), 0);
116  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(raster), 1, DBL_EPSILON);
117  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(raster), 2, DBL_EPSILON);
118  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(raster), 3, DBL_EPSILON);
119  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(raster), 4, DBL_EPSILON);
120  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(raster), 5, DBL_EPSILON);
121  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(raster), 6, DBL_EPSILON);
122  CU_ASSERT_EQUAL(rt_raster_get_srid(raster), 10);
123  CU_ASSERT_EQUAL(rt_raster_get_width(raster), 7);
124  CU_ASSERT_EQUAL(rt_raster_get_height(raster), 8);
125 
126  out = rt_raster_to_hexwkb(raster, FALSE, &len);
127 /*
128  printf(" in hexwkb len: %u\n", (uint32_t) strlen(hexwkb));
129  printf("out hexwkb len: %u\n", len);
130  printf(" in hexwkb: %s\n", hexwkb);
131  printf("out hexwkb: %s\n", out);
132 */
133  CU_ASSERT_EQUAL(len, strlen(hexwkb));
134 /* would depend on machine endian...
135  CU_ASSERT_STRING_EQUAL(hexwkb, out);
136 */
137 
139  free((/*no const*/ void*)out);
140 
141  /* ------------------------------------------------------ */
142  /* 1x1, little endian, band0(1bb) */
143  /* ------------------------------------------------------ */
144 
145  hexwkb =
146 "01" /* little endian (uint8 ndr) */
147 "0000" /* version (uint16 0) */
148 "0100" /* nBands (uint16 1) */
149 "000000000000F03F" /* scaleX (float64 1) */
150 "0000000000000040" /* scaleY (float64 2) */
151 "0000000000000840" /* ipX (float64 3) */
152 "0000000000001040" /* ipY (float64 4) */
153 "0000000000001440" /* skewX (float64 5) */
154 "0000000000001840" /* skewY (float64 6) */
155 "0A000000" /* SRID (int32 10) */
156 "0100" /* width (uint16 1) */
157 "0100" /* height (uint16 1) */
158 "40" /* First band type (1BB, in memory, hasnodata) */
159 "00" /* nodata value (0) */
160 "01" /* pix(0,0) == 1 */
161  ;
162 
163  raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
164  CU_ASSERT(raster != NULL);
165  CU_ASSERT_EQUAL(rt_raster_get_num_bands(raster), 1);
166  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(raster), 1, DBL_EPSILON);
167  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(raster), 2, DBL_EPSILON);
168  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(raster), 3, DBL_EPSILON);
169  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(raster), 4, DBL_EPSILON);
170  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(raster), 5, DBL_EPSILON);
171  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(raster), 6, DBL_EPSILON);
172  CU_ASSERT_EQUAL(rt_raster_get_srid(raster), 10);
173  CU_ASSERT_EQUAL(rt_raster_get_width(raster), 1);
174  CU_ASSERT_EQUAL(rt_raster_get_height(raster), 1);
175  {
176  double val;
177  int failure;
179  CU_ASSERT(band != NULL);
180  CU_ASSERT_EQUAL(rt_band_get_pixtype(band), PT_1BB);
181  CU_ASSERT(!rt_band_is_offline(band));
182  CU_ASSERT(rt_band_get_hasnodata_flag(band));
183  rt_band_get_nodata(band, &val);
184  CU_ASSERT_DOUBLE_EQUAL(val, 0, DBL_EPSILON);
185  failure = rt_band_get_pixel(band, 0, 0, &val, NULL);
186  CU_ASSERT_EQUAL(failure, ES_NONE);
187  CU_ASSERT_DOUBLE_EQUAL(val, 1, DBL_EPSILON);
188  }
189 
190  out = rt_raster_to_hexwkb(raster, FALSE, &len);
191 /*
192  printf(" in hexwkb len: %u\n", (uint32_t) strlen(hexwkb));
193  printf("out hexwkb len: %u\n", len);
194 */
195  CU_ASSERT_EQUAL(len, strlen(hexwkb));
196 /* would depend on machine endian...
197  CU_ASSERT_STRING_EQUAL(hexwkb, out);
198 */
199 
201  free((/*no const*/ void*)out);
202 
203  /* ------------------------------------------------------ */
204  /* 3x2, big endian, band0(8BSI) */
205  /* ------------------------------------------------------ */
206 
207  hexwkb =
208 "01" /* little endian (uint8 ndr) */
209 "0000" /* version (uint16 0) */
210 "0100" /* nBands (uint16 1) */
211 "000000000000F03F" /* scaleX (float64 1) */
212 "0000000000000040" /* scaleY (float64 2) */
213 "0000000000000840" /* ipX (float64 3) */
214 "0000000000001040" /* ipY (float64 4) */
215 "0000000000001440" /* skewX (float64 5) */
216 "0000000000001840" /* skewY (float64 6) */
217 "0A000000" /* SRID (int32 10) */
218 "0300" /* width (uint16 3) */
219 "0200" /* height (uint16 2) */
220 "43" /* First band type (8BSI, in memory, hasnodata) */
221 "FF" /* nodata value (-1) */
222 "FF" /* pix(0,0) == -1 */
223 "00" /* pix(1,0) == 0 */
224 "01" /* pix(2,0) == 1 */
225 "7F" /* pix(0,1) == 127 */
226 "0A" /* pix(1,1) == 10 */
227 "02" /* pix(2,1) == 2 */
228  ;
229 
230  raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
231  CU_ASSERT(raster != NULL);
232  CU_ASSERT_EQUAL(rt_raster_get_num_bands(raster), 1);
233  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(raster), 1, DBL_EPSILON);
234  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(raster), 2, DBL_EPSILON);
235  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(raster), 3, DBL_EPSILON);
236  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(raster), 4, DBL_EPSILON);
237  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(raster), 5, DBL_EPSILON);
238  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(raster), 6, DBL_EPSILON);
239  CU_ASSERT_EQUAL(rt_raster_get_srid(raster), 10);
240  CU_ASSERT_EQUAL(rt_raster_get_width(raster), 3);
241  CU_ASSERT_EQUAL(rt_raster_get_height(raster), 2);
242  {
243  double val;
244  int failure;
245 
247  CU_ASSERT(band != NULL);
248  CU_ASSERT_EQUAL(rt_band_get_pixtype(band), PT_8BSI);
249  CU_ASSERT(!rt_band_is_offline(band));
250  CU_ASSERT(rt_band_get_hasnodata_flag(band));
251  rt_band_get_nodata(band, &val);
252  CU_ASSERT_DOUBLE_EQUAL(val, -1, DBL_EPSILON);
253 
254  failure = rt_band_get_pixel(band, 0, 0, &val, NULL);
255  CU_ASSERT_EQUAL(failure, ES_NONE);
256  CU_ASSERT_DOUBLE_EQUAL(val, -1, DBL_EPSILON);
257 
258  failure = rt_band_get_pixel(band, 1, 0, &val, NULL);
259  CU_ASSERT_EQUAL(failure, ES_NONE);
260  CU_ASSERT_DOUBLE_EQUAL(val, 0, DBL_EPSILON);
261 
262  failure = rt_band_get_pixel(band, 2, 0, &val, NULL);
263  CU_ASSERT_EQUAL(failure, ES_NONE);
264  CU_ASSERT_DOUBLE_EQUAL(val, 1, DBL_EPSILON);
265 
266  failure = rt_band_get_pixel(band, 0, 1, &val, NULL);
267  CU_ASSERT_EQUAL(failure, ES_NONE);
268  CU_ASSERT_DOUBLE_EQUAL(val, 127, DBL_EPSILON);
269 
270  failure = rt_band_get_pixel(band, 1, 1, &val, NULL);
271  CU_ASSERT_EQUAL(failure, ES_NONE);
272  CU_ASSERT_DOUBLE_EQUAL(val, 10, DBL_EPSILON);
273 
274  failure = rt_band_get_pixel(band, 2, 1, &val, NULL);
275  CU_ASSERT_EQUAL(failure, ES_NONE);
276  CU_ASSERT_DOUBLE_EQUAL(val, 2, DBL_EPSILON);
277  }
278 
279  out = rt_raster_to_hexwkb(raster, FALSE, &len);
280 /*
281  printf(" in hexwkb len: %u\n", (uint32_t) strlen(hexwkb));
282  printf("out hexwkb len: %u\n", len);
283 */
284  CU_ASSERT_EQUAL(len, strlen(hexwkb));
285 /* would depend on machine endian...
286  CU_ASSERT_STRING_EQUAL(hexwkb, out);
287 */
288 
289  free((/*no const*/ void*)out);
290 
291  {
292  void *serialized;
293  rt_raster rast2;
294 
295  serialized = rt_raster_serialize(raster);
296  rast2 = rt_raster_deserialize(serialized, FALSE);
297 
298  cu_free_raster(rast2);
299  free(serialized);
300 
301  }
302 
304 
305  /* ------------------------------------------------------ */
306  /* 3x2, little endian, band0(16BSI) */
307  /* ------------------------------------------------------ */
308 
309  hexwkb =
310 "01" /* little endian (uint8 ndr) */
311 "0000" /* version (uint16 0) */
312 "0100" /* nBands (uint16 1) */
313 "000000000000F03F" /* scaleX (float64 1) */
314 "0000000000000040" /* scaleY (float64 2) */
315 "0000000000000840" /* ipX (float64 3) */
316 "0000000000001040" /* ipY (float64 4) */
317 "0000000000001440" /* skewX (float64 5) */
318 "0000000000001840" /* skewY (float64 6) */
319 "0A000000" /* SRID (int32 10) */
320 "0300" /* width (uint16 3) */
321 "0200" /* height (uint16 2) */
322 "05" /* First band type (16BSI, in memory) */
323 "FFFF" /* nodata value (-1) */
324 "FFFF" /* pix(0,0) == -1 */
325 "0000" /* pix(1,0) == 0 */
326 "F0FF" /* pix(2,0) == -16 */
327 "7F00" /* pix(0,1) == 127 */
328 "0A00" /* pix(1,1) == 10 */
329 "0200" /* pix(2,1) == 2 */
330  ;
331 
332  raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
333  CU_ASSERT(raster != NULL);
334  CU_ASSERT_EQUAL(rt_raster_get_num_bands(raster), 1);
335  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(raster), 1, DBL_EPSILON);
336  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(raster), 2, DBL_EPSILON);
337  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(raster), 3, DBL_EPSILON);
338  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(raster), 4, DBL_EPSILON);
339  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(raster), 5, DBL_EPSILON);
340  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(raster), 6, DBL_EPSILON);
341  CU_ASSERT_EQUAL(rt_raster_get_srid(raster), 10);
342  CU_ASSERT_EQUAL(rt_raster_get_width(raster), 3);
343  CU_ASSERT_EQUAL(rt_raster_get_height(raster), 2);
344  {
345  double val;
346  int failure;
348  CU_ASSERT(band != NULL);
349  CU_ASSERT_EQUAL(rt_band_get_pixtype(band), PT_16BSI);
350  CU_ASSERT(!rt_band_is_offline(band));
351  CU_ASSERT(!rt_band_get_hasnodata_flag(band));
352 
353  failure = rt_band_get_pixel(band, 0, 0, &val, NULL);
354  CU_ASSERT_EQUAL(failure, ES_NONE);
355  CU_ASSERT_DOUBLE_EQUAL(val, -1, DBL_EPSILON);
356 
357  failure = rt_band_get_pixel(band, 1, 0, &val, NULL);
358  CU_ASSERT_EQUAL(failure, ES_NONE);
359  CU_ASSERT_DOUBLE_EQUAL(val, 0, DBL_EPSILON);
360 
361  failure = rt_band_get_pixel(band, 2, 0, &val, NULL);
362  CU_ASSERT_EQUAL(failure, ES_NONE);
363  CU_ASSERT_DOUBLE_EQUAL(val, -16, DBL_EPSILON);
364 
365  failure = rt_band_get_pixel(band, 0, 1, &val, NULL);
366  CU_ASSERT_EQUAL(failure, ES_NONE);
367  CU_ASSERT_DOUBLE_EQUAL(val, 127, DBL_EPSILON);
368 
369  failure = rt_band_get_pixel(band, 1, 1, &val, NULL);
370  CU_ASSERT_EQUAL(failure, ES_NONE);
371  CU_ASSERT_DOUBLE_EQUAL(val, 10, DBL_EPSILON);
372 
373  failure = rt_band_get_pixel(band, 2, 1, &val, NULL);
374  CU_ASSERT_EQUAL(failure, ES_NONE);
375  CU_ASSERT_DOUBLE_EQUAL(val, 2, DBL_EPSILON);
376  }
377 
378  out = rt_raster_to_hexwkb(raster, FALSE, &len);
379 /*
380  printf(" in hexwkb len: %u\n", (uint32_t) strlen(hexwkb));
381  printf("out hexwkb len: %u\n", len);
382 */
383  CU_ASSERT_EQUAL(len, strlen(hexwkb));
384 /* would depend on machine endian
385  CU_ASSERT_STRING_EQUAL(hexwkb, out);
386 */
387 
389  free((/*no const*/ void*)out);
390 
391  /* ------------------------------------------------------ */
392  /* 3x2, big endian, band0(16BSI) */
393  /* ------------------------------------------------------ */
394 
395  hexwkb =
396 "00" /* big endian (uint8 xdr) */
397 "0000" /* version (uint16 0) */
398 "0001" /* nBands (uint16 1) */
399 "3FF0000000000000" /* scaleX (float64 1) */
400 "4000000000000000" /* scaleY (float64 2) */
401 "4008000000000000" /* ipX (float64 3) */
402 "4010000000000000" /* ipY (float64 4) */
403 "4014000000000000" /* skewX (float64 5) */
404 "4018000000000000" /* skewY (float64 6) */
405 "0000000A" /* SRID (int32 10) */
406 "0003" /* width (uint16 3) */
407 "0002" /* height (uint16 2) */
408 "05" /* First band type (16BSI, in memory) */
409 "FFFF" /* nodata value (-1) */
410 "FFFF" /* pix(0,0) == -1 */
411 "0000" /* pix(1,0) == 0 */
412 "FFF0" /* pix(2,0) == -16 */
413 "007F" /* pix(0,1) == 127 */
414 "000A" /* pix(1,1) == 10 */
415 "0002" /* pix(2,1) == 2 */
416  ;
417 
418  raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
419  CU_ASSERT(raster != NULL);
420  CU_ASSERT_EQUAL(rt_raster_get_num_bands(raster), 1);
421  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(raster), 1, DBL_EPSILON);
422  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(raster), 2, DBL_EPSILON);
423  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(raster), 3, DBL_EPSILON);
424  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(raster), 4, DBL_EPSILON);
425  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(raster), 5, DBL_EPSILON);
426  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(raster), 6, DBL_EPSILON);
427  CU_ASSERT_EQUAL(rt_raster_get_srid(raster), 10);
428  CU_ASSERT_EQUAL(rt_raster_get_width(raster), 3);
429  CU_ASSERT_EQUAL(rt_raster_get_height(raster), 2);
430  {
431  double val;
432  int failure;
434  CU_ASSERT(band != NULL);
435  CU_ASSERT_EQUAL(rt_band_get_pixtype(band), PT_16BSI);
436  CU_ASSERT(!rt_band_is_offline(band));
437  CU_ASSERT(!rt_band_get_hasnodata_flag(band));
438 
439  failure = rt_band_get_pixel(band, 0, 0, &val, NULL);
440  CU_ASSERT_EQUAL(failure, ES_NONE);
441  CU_ASSERT_DOUBLE_EQUAL(val, -1, DBL_EPSILON);
442 
443  failure = rt_band_get_pixel(band, 1, 0, &val, NULL);
444  CU_ASSERT_EQUAL(failure, ES_NONE);
445  CU_ASSERT_DOUBLE_EQUAL(val, 0, DBL_EPSILON);
446 
447  failure = rt_band_get_pixel(band, 2, 0, &val, NULL);
448  CU_ASSERT_EQUAL(failure, ES_NONE);
449  CU_ASSERT_DOUBLE_EQUAL(val, -16, DBL_EPSILON);
450 
451  failure = rt_band_get_pixel(band, 0, 1, &val, NULL);
452  CU_ASSERT_EQUAL(failure, ES_NONE);
453  CU_ASSERT_DOUBLE_EQUAL(val, 127, DBL_EPSILON);
454 
455  failure = rt_band_get_pixel(band, 1, 1, &val, NULL);
456  CU_ASSERT_EQUAL(failure, ES_NONE);
457  CU_ASSERT_DOUBLE_EQUAL(val, 10, DBL_EPSILON);
458 
459  failure = rt_band_get_pixel(band, 2, 1, &val, NULL);
460  CU_ASSERT_EQUAL(failure, ES_NONE);
461  CU_ASSERT_DOUBLE_EQUAL(val, 2, DBL_EPSILON);
462  }
463 
464  out = rt_raster_to_hexwkb(raster, FALSE, &len);
465 /*
466  printf(" in hexwkb len: %u\n", (uint32_t) strlen(hexwkb));
467  printf("out hexwkb len: %u\n", len);
468 */
469  CU_ASSERT_EQUAL(len, strlen(hexwkb));
470 /* would depend on machine endian
471  CU_ASSERT_STRING_EQUAL(hexwkb, out);
472 */
473 
475  free((/*no const*/ void*)out);
476 
477  /* ------------------------------------------------------ */
478  /* 3x2, bit endian, band0(16BSI ext: 3;/tmp/t.tif) */
479  /* ------------------------------------------------------ */
480 
481  hexwkb =
482 "00" /* big endian (uint8 xdr) */
483 "0000" /* version (uint16 0) */
484 "0001" /* nBands (uint16 1) */
485 "3FF0000000000000" /* scaleX (float64 1) */
486 "4000000000000000" /* scaleY (float64 2) */
487 "4008000000000000" /* ipX (float64 3) */
488 "4010000000000000" /* ipY (float64 4) */
489 "4014000000000000" /* skewX (float64 5) */
490 "4018000000000000" /* skewY (float64 6) */
491 "0000000A" /* SRID (int32 10) */
492 "0003" /* width (uint16 3) */
493 "0002" /* height (uint16 2) */
494 "C5" /* First band type (16BSI, on disk, hasnodata) */
495 "FFFF" /* nodata value (-1) */
496 "03" /* ext band num == 3 */
497 /* ext band path == /tmp/t.tif */
498 "2F746D702F742E74696600"
499  ;
500 
501  raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
502  CU_ASSERT(raster != NULL);
503  CU_ASSERT_EQUAL(rt_raster_get_num_bands(raster), 1);
504  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(raster), 1, DBL_EPSILON);
505  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(raster), 2, DBL_EPSILON);
506  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(raster), 3, DBL_EPSILON);
507  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(raster), 4, DBL_EPSILON);
508  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(raster), 5, DBL_EPSILON);
509  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(raster), 6, DBL_EPSILON);
510  CU_ASSERT_EQUAL(rt_raster_get_srid(raster), 10);
511  CU_ASSERT_EQUAL(rt_raster_get_width(raster), 3);
512  CU_ASSERT_EQUAL(rt_raster_get_height(raster), 2);
513  {
514  double val;
515  uint8_t bandnum = 0;
517  CU_ASSERT(band != NULL);
518  CU_ASSERT_EQUAL(rt_band_get_pixtype(band), PT_16BSI);
519  CU_ASSERT(rt_band_is_offline(band));
520  CU_ASSERT(rt_band_get_hasnodata_flag(band));
521  rt_band_get_nodata(band, &val);
522  CU_ASSERT_DOUBLE_EQUAL(val, -1, DBL_EPSILON);
523  CU_ASSERT_STRING_EQUAL(rt_band_get_ext_path(band), "/tmp/t.tif");
524  CU_ASSERT_EQUAL(rt_band_get_ext_band_num(band, &bandnum), ES_NONE);
525  CU_ASSERT_EQUAL(bandnum, 3);
526  }
527 
528  out = rt_raster_to_hexwkb(raster, FALSE, &len);
529 /*
530  printf(" in hexwkb len: %u\n", (uint32_t) strlen(hexwkb));
531  printf("out hexwkb len: %u\n", len);
532 */
533  CU_ASSERT_EQUAL(len, strlen(hexwkb));
534 /* would depend on machine endian
535  CU_ASSERT_STRING_EQUAL(hexwkb, out);
536 */
537 
539  free((/*no const*/ void*)out);
540 
541  /* ------------------------------------------------------ */
542  /* 1x3, little endian, band0 16BSI, nodata 1, srid -1 */
543  /* ------------------------------------------------------ */
544 
545  hexwkb =
546 "01" /* little endian (uint8 ndr) */
547 "0000" /* version (uint16 0) */
548 "0100" /* nBands (uint16 1) */
549 "0000000000805640" /* scaleX (float64 90.0) */
550 "00000000008056C0" /* scaleY (float64 -90.0) */
551 "000000001C992D41" /* ipX (float64 969870.0) */
552 "00000000E49E2341" /* ipY (float64 642930.0) */
553 "0000000000000000" /* skewX (float64 0) */
554 "0000000000000000" /* skewY (float64 0) */
555 "FFFFFFFF" /* SRID (int32 -1) */
556 "0300" /* width (uint16 3) */
557 "0100" /* height (uint16 1) */
558 "45" /* First band type (16BSI, in memory, hasnodata) */
559 "0100" /* nodata value (1) */
560 "0100" /* pix(0,0) == 1 */
561 "B401" /* pix(1,0) == 436 */
562 "AF01" /* pix(2,0) == 431 */
563  ;
564 
565  raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
566  CU_ASSERT(raster != NULL);
567  CU_ASSERT_EQUAL(rt_raster_get_num_bands(raster), 1);
568  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(raster), 90, DBL_EPSILON);
569  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(raster), -90, DBL_EPSILON);
570  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(raster), 969870.0, DBL_EPSILON);
571  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(raster), 642930.0, DBL_EPSILON);
572  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(raster), 0, DBL_EPSILON);
573  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(raster), 0, DBL_EPSILON);
574  CU_ASSERT_EQUAL(rt_raster_get_srid(raster), 0);
575  CU_ASSERT_EQUAL(rt_raster_get_width(raster), 3);
576  CU_ASSERT_EQUAL(rt_raster_get_height(raster), 1);
577  {
578  double val;
579  int failure;
581  CU_ASSERT(band != NULL);
582  CU_ASSERT_EQUAL(rt_band_get_pixtype(band), PT_16BSI);
583  CU_ASSERT(!rt_band_is_offline(band));
584  CU_ASSERT(rt_band_get_hasnodata_flag(band));
585  rt_band_get_nodata(band, &val);
586  CU_ASSERT_DOUBLE_EQUAL(val, 1, DBL_EPSILON);
587 
588  failure = rt_band_get_pixel(band, 0, 0, &val, NULL);
589  CU_ASSERT_EQUAL(failure, ES_NONE);
590  CU_ASSERT_DOUBLE_EQUAL(val, 1, DBL_EPSILON);
591 
592  failure = rt_band_get_pixel(band, 1, 0, &val, NULL);
593  CU_ASSERT_EQUAL(failure, ES_NONE);
594  CU_ASSERT_DOUBLE_EQUAL(val, 436, DBL_EPSILON);
595 
596  failure = rt_band_get_pixel(band, 2, 0, &val, NULL);
597  CU_ASSERT_EQUAL(failure, ES_NONE);
598  CU_ASSERT_DOUBLE_EQUAL(val, 431, DBL_EPSILON);
599  }
600 
601  out = rt_raster_to_hexwkb(raster, FALSE, &len);
602 /*
603  printf(" in hexwkb len: %d\n", strlen(hexwkb));
604  printf("out hexwkb len: %d\n", len);
605  printf(" in hexwkb: %s\n", hexwkb);
606  printf("out hexwkb: %s\n", out);
607 */
608  CU_ASSERT_EQUAL(len, strlen(hexwkb));
609 /* would depend on machine endian
610  CU_ASSERT_STRING_EQUAL(hexwkb, out);
611 */
612  free((/*no const*/ void*)out);
613 
614  {
615  void *serialized;
616  rt_raster rast2;
617 
618  serialized = rt_raster_serialize(raster);
619  rast2 = rt_raster_deserialize(serialized, FALSE);
620 
621  cu_free_raster(rast2);
622  free(serialized);
623  }
624 
626 
627  /* ------------------------------------------------------ */
628  /* 5x5, little endian, 3 x band 8BUI (RGB), */
629  /* nodata 0, srid -1 */
630  /* Test case completes regress/bug_test_car5.sql */
631  /* Test case repeated 4 times to mimic 4 tiles insertion */
632  /* ------------------------------------------------------ */
633  for (i = 0; i < 5; ++i)
634  {
635 
636  hexwkb =
637 "01" /* little endian (uint8 ndr) */
638 "0000" /* version (uint16 0) */
639 "0300" /* nBands (uint16 3) */
640 "9A9999999999A93F" /* scaleX (float64 0.050000) */
641 "9A9999999999A9BF" /* scaleY (float64 -0.050000) */
642 "000000E02B274A41" /* ipX (float64 3427927.750000) */
643 "0000000077195641" /* ipY (float64 5793244.000000) */
644 "0000000000000000" /* skewX (float64 0.000000) */
645 "0000000000000000" /* skewY (float64 0.000000) */
646 "FFFFFFFF" /* srid (int32 -1) */
647 "0500" /* width (uint16 5) */
648 "0500" /* height (uint16 5) */
649 "44" /* 1st band pixel type (8BUI, in memory, hasnodata) */
650 "00" /* 1st band nodata 0 */
651 "FDFEFDFEFEFDFEFEFDF9FAFEFEFCF9FBFDFEFEFDFCFAFEFEFE" /* 1st band pixels */
652 "44" /* 2nd band pixel type (8BUI, in memory, hasnodata) */
653 "00" /* 2nd band nodata 0 */
654 "4E627AADD16076B4F9FE6370A9F5FE59637AB0E54F58617087" /* 2nd band pixels */
655 "44" /* 3rd band pixel type (8BUI, in memory, hasnodata) */
656 "00" /* 3rd band nodata 0 */
657 "46566487A1506CA2E3FA5A6CAFFBFE4D566DA4CB3E454C5665" /* 3rd band pixels */
658 ;
659 
660  raster = rt_raster_from_hexwkb(hexwkb, strlen(hexwkb));
661  CU_ASSERT(raster != NULL);
662  CU_ASSERT_EQUAL(rt_raster_get_num_bands(raster), 3);
663  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(raster), 0.05, DBL_EPSILON);
664  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(raster), -0.05, DBL_EPSILON);
665  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(raster), 3427927.75, DBL_EPSILON);
666  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(raster), 5793244.00, DBL_EPSILON);
667  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(raster), 0.0, DBL_EPSILON);
668  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(raster), 0.0, DBL_EPSILON);
669  CU_ASSERT_EQUAL(rt_raster_get_srid(raster), 0);
670  CU_ASSERT_EQUAL(rt_raster_get_width(raster), 5);
671  CU_ASSERT_EQUAL(rt_raster_get_height(raster), 5);
672  {
673  /* Test 1st band */
674  double val;
675  int failure;
677  CU_ASSERT(band != NULL);
678  CU_ASSERT_EQUAL(rt_band_get_pixtype(band), PT_8BUI);
679  CU_ASSERT(!rt_band_is_offline(band));
680  CU_ASSERT(rt_band_get_hasnodata_flag(band));
681  rt_band_get_nodata(band, &val);
682  CU_ASSERT_DOUBLE_EQUAL(val, 0, DBL_EPSILON);
683 
684  failure = rt_band_get_pixel(band, 0, 0, &val, NULL);
685  CU_ASSERT_EQUAL(failure, ES_NONE);
686  CU_ASSERT_DOUBLE_EQUAL(val, 253, DBL_EPSILON);
687 
688  failure = rt_band_get_pixel(band, 1, 0, &val, NULL);
689  CU_ASSERT_EQUAL(failure, ES_NONE);
690  CU_ASSERT_DOUBLE_EQUAL(val, 254, DBL_EPSILON);
691 
692  failure = rt_band_get_pixel(band, 2, 0, &val, NULL);
693  CU_ASSERT_EQUAL(failure, ES_NONE);
694  CU_ASSERT_DOUBLE_EQUAL(val, 253, DBL_EPSILON);
695 
696  failure = rt_band_get_pixel(band, 3, 0, &val, NULL);
697  CU_ASSERT_EQUAL(failure, ES_NONE);
698  CU_ASSERT_DOUBLE_EQUAL(val, 254, DBL_EPSILON);
699 
700  failure = rt_band_get_pixel(band, 4, 0, &val, NULL);
701  CU_ASSERT_EQUAL(failure, ES_NONE);
702  CU_ASSERT_DOUBLE_EQUAL(val, 254, DBL_EPSILON);
703  }
704 
705  {
706  /* Test 2nd band */
707  double val;
708  int failure;
710  CU_ASSERT(band != NULL);
711  CU_ASSERT_EQUAL(rt_band_get_pixtype(band), PT_8BUI);
712  CU_ASSERT(!rt_band_is_offline(band));
713  CU_ASSERT(rt_band_get_hasnodata_flag(band));
714  rt_band_get_nodata(band, &val);
715  CU_ASSERT_DOUBLE_EQUAL(val, 0, DBL_EPSILON);
716 
717  failure = rt_band_get_pixel(band, 0, 0, &val, NULL);
718  CU_ASSERT_EQUAL(failure, ES_NONE);
719  CU_ASSERT_DOUBLE_EQUAL(val, 78, DBL_EPSILON);
720 
721  failure = rt_band_get_pixel(band, 1, 0, &val, NULL);
722  CU_ASSERT_EQUAL(failure, ES_NONE);
723  CU_ASSERT_DOUBLE_EQUAL(val, 98, DBL_EPSILON);
724 
725  failure = rt_band_get_pixel(band, 2, 0, &val, NULL);
726  CU_ASSERT_EQUAL(failure, ES_NONE);
727  CU_ASSERT_DOUBLE_EQUAL(val, 122, DBL_EPSILON);
728 
729  failure = rt_band_get_pixel(band, 3, 0, &val, NULL);
730  CU_ASSERT_EQUAL(failure, ES_NONE);
731  CU_ASSERT_DOUBLE_EQUAL(val, 173, DBL_EPSILON);
732 
733  failure = rt_band_get_pixel(band, 4, 0, &val, NULL);
734  CU_ASSERT_EQUAL(failure, ES_NONE);
735  CU_ASSERT_DOUBLE_EQUAL(val, 209, DBL_EPSILON);
736  }
737 
738  {
739  /* Test 3rd band */
740  double val;
741  int failure;
743  CU_ASSERT(band != NULL);
744  CU_ASSERT_EQUAL(rt_band_get_pixtype(band), PT_8BUI);
745  CU_ASSERT(!rt_band_is_offline(band));
746  CU_ASSERT(rt_band_get_hasnodata_flag(band));
747  rt_band_get_nodata(band, &val);
748  CU_ASSERT_DOUBLE_EQUAL(val, 0, DBL_EPSILON);
749 
750  failure = rt_band_get_pixel(band, 0, 0, &val, NULL);
751  CU_ASSERT_EQUAL(failure, ES_NONE);
752  CU_ASSERT_DOUBLE_EQUAL(val, 70, DBL_EPSILON);
753 
754  failure = rt_band_get_pixel(band, 1, 0, &val, NULL);
755  CU_ASSERT_EQUAL(failure, ES_NONE);
756  CU_ASSERT_DOUBLE_EQUAL(val, 86, DBL_EPSILON);
757 
758  failure = rt_band_get_pixel(band, 2, 0, &val, NULL);
759  CU_ASSERT_EQUAL(failure, ES_NONE);
760  CU_ASSERT_DOUBLE_EQUAL(val, 100, DBL_EPSILON);
761 
762  failure = rt_band_get_pixel(band, 3, 0, &val, NULL);
763  CU_ASSERT_EQUAL(failure, ES_NONE);
764  CU_ASSERT_DOUBLE_EQUAL(val, 135, DBL_EPSILON);
765 
766  failure = rt_band_get_pixel(band, 4, 0, &val, NULL);
767  CU_ASSERT_EQUAL(failure, ES_NONE);
768  CU_ASSERT_DOUBLE_EQUAL(val, 161, DBL_EPSILON);
769  }
770 
771  out = rt_raster_to_hexwkb(raster, FALSE, &len);
772 /*
773  printf(" in hexwkb len: %u\n", (uint32_t) strlen(hexwkb));
774  printf("out hexwkb len: %u\n", len);
775 */
776  CU_ASSERT_EQUAL(len, strlen(hexwkb));
777 /* would depend on machine endian
778  CU_ASSERT_STRING_EQUAL(hexwkb, out);
779 */
780 
781  free((/*no const*/ void*)out);
782  {
783  void *serialized;
784  rt_raster rast2;
785 
786  serialized = rt_raster_serialize(raster);
787  rast2 = rt_raster_deserialize(serialized, FALSE);
788 
789  cu_free_raster(rast2);
790  free(serialized);
791  }
793 
794  } /* for-loop running car5 tests */
795 
796  /* ------------------------------------------------------ */
797  /* TODO: New test cases */
798  /* ------------------------------------------------------ */
799 
800  /* new test case */
801 
802  /* ------------------------------------------------------ */
803  /* Success summary */
804  /* ------------------------------------------------------ */
805 
806 /*
807  printf("All tests successful !\n");
808 */
809 }
810 
811 /* register tests */
812 void raster_wkb_suite_setup(void);
814 {
815  CU_pSuite suite = CU_add_suite("raster_wkb", NULL, NULL);
817 }
818 
void raster_wkb_suite_setup(void)
static void test_raster_wkb()
Definition: cu_raster_wkb.c:28
#define FALSE
Definition: dbfopen.c:168
#define PG_ADD_TEST(suite, testfunc)
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
Definition: rt_raster.c:356
double rt_raster_get_x_skew(rt_raster raster)
Get skew about the X axis.
Definition: rt_raster.c:181
double rt_raster_get_x_offset(rt_raster raster)
Get raster x offset, in projection units.
Definition: rt_raster.c:213
const char * rt_band_get_ext_path(rt_band band)
Return band's external path (only valid when rt_band_is_offline returns non-zero).
Definition: rt_band.c:363
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
Definition: rt_band.c:674
char * rt_raster_to_hexwkb(rt_raster raster, int outasin, uint32_t *hexwkbsize)
Return this raster in HEXWKB form (null-terminated hex)
Definition: rt_wkb.c:679
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
@ PT_1BB
Definition: librtcore.h:186
@ PT_8BSI
Definition: librtcore.h:189
@ PT_16BSI
Definition: librtcore.h:191
@ PT_8BUI
Definition: librtcore.h:190
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_serialize.c:521
double rt_raster_get_x_scale(rt_raster raster)
Get scale X in projection units.
Definition: rt_raster.c:150
@ ES_NONE
Definition: librtcore.h:180
rt_raster rt_raster_from_hexwkb(const char *hexwkb, uint32_t hexwkbsize)
Construct an rt_raster from a text HEXWKB representation.
Definition: rt_wkb.c:406
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
uint16_t rt_raster_get_height(rt_raster raster)
Definition: rt_raster.c:129
rt_errorstate rt_band_get_ext_band_num(rt_band band, uint8_t *bandnum)
Return bands' external band number (only valid when rt_band_is_offline returns non-zero).
Definition: rt_band.c:376
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
int rt_band_is_offline(rt_band band)
Return non-zero if the given band data is on the filesystem.
Definition: rt_band.c:329
double rt_raster_get_y_scale(rt_raster raster)
Get scale Y in projection units.
Definition: rt_raster.c:159
double rt_raster_get_y_skew(rt_raster raster)
Get skew about the Y axis.
Definition: rt_raster.c:190
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:725
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
void free(void *)
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
void cu_free_raster(rt_raster raster)
unsigned int uint32_t
Definition: uthash.h:78
unsigned char uint8_t
Definition: uthash.h:79