PostGIS  3.4.0dev-r@@SVN_REVISION@@
liblwgeom/cunit/cu_misc.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  * Copyright 2008 Paul Ramsey
6  *
7  * This is free software; you can redistribute and/or modify it under
8  * the terms of the GNU General Public Licence. See the COPYING file.
9  *
10  **********************************************************************/
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include "CUnit/Basic.h"
16 
17 #include "liblwgeom_internal.h"
18 #include "optionlist.h"
19 #include "stringlist.h"
20 #include "cu_tester.h"
21 
22 
23 static void test_misc_simplify(void)
24 {
25  LWGEOM *geom;
26  LWGEOM *geom2d;
27  char *wkt_out;
28 
29  geom = lwgeom_from_wkt("LINESTRING(0 0,0 10,0 51,50 20,30 20,7 32)", LW_PARSER_CHECK_NONE);
30  geom2d = lwgeom_simplify(geom, 2, LW_FALSE);
31  wkt_out = lwgeom_to_ewkt(geom2d);
32  CU_ASSERT_STRING_EQUAL("LINESTRING(0 0,0 51,50 20,30 20,7 32)",wkt_out);
33  lwgeom_free(geom);
34  lwgeom_free(geom2d);
35  lwfree(wkt_out);
36 
37  geom = lwgeom_from_wkt("MULTILINESTRING((0 0,0 10,0 51,50 20,30 20,7 32))", LW_PARSER_CHECK_NONE);
38  geom2d = lwgeom_simplify(geom, 2, LW_FALSE);
39  wkt_out = lwgeom_to_ewkt(geom2d);
40  CU_ASSERT_STRING_EQUAL("MULTILINESTRING((0 0,0 51,50 20,30 20,7 32))",wkt_out);
41  lwgeom_free(geom);
42  lwgeom_free(geom2d);
43  lwfree(wkt_out);
44 
45  geom = lwgeom_from_wkt("POLYGON((0 0,1 1,1 3,0 4,-2 3,-1 1,0 0))", LW_PARSER_CHECK_NONE);
46  geom2d = lwgeom_simplify(geom, 1, LW_FALSE);
47  wkt_out = lwgeom_to_ewkt(geom2d);
48  CU_ASSERT_STRING_EQUAL("POLYGON((0 0,0 4,-2 3,0 0))", wkt_out);
49  lwgeom_free(geom);
50  lwgeom_free(geom2d);
51  lwfree(wkt_out);
52 }
53 
54 static void test_misc_startpoint(void)
55 {
56  LWGEOM *geom;
57  POINT4D p = {0};
58 
59  geom = lwgeom_from_wkt("POINT(1 2)", LW_PARSER_CHECK_NONE);
60  CU_ASSERT(lwgeom_startpoint(geom, &p) == LW_SUCCESS);
61  CU_ASSERT_EQUAL(p.x, 1);
62  CU_ASSERT_EQUAL(p.y, 2);
63  lwgeom_free(geom);
64 
65  geom = lwgeom_from_wkt("LINESTRING(10 20, 30 40)", LW_PARSER_CHECK_NONE);
66  CU_ASSERT(lwgeom_startpoint(geom, &p) == LW_SUCCESS);
67  CU_ASSERT_EQUAL(p.x, 10);
68  CU_ASSERT_EQUAL(p.y, 20);
69  lwgeom_free(geom);
70 
71  geom = lwgeom_from_wkt("POLYGON((1 2, 3 4, 5 6, 1 2))", LW_PARSER_CHECK_NONE);
72  CU_ASSERT(lwgeom_startpoint(geom, &p) == LW_SUCCESS);
73  CU_ASSERT_EQUAL(p.x, 1);
74  CU_ASSERT_EQUAL(p.y, 2);
75  lwgeom_free(geom);
76 
77  geom = lwgeom_from_wkt("GEOMETRYCOLLECTION(LINESTRING(100 200, 300 400), POINT(10 20))", LW_PARSER_CHECK_NONE);
78  CU_ASSERT(lwgeom_startpoint(geom, &p) == LW_SUCCESS);
79  CU_ASSERT_EQUAL(p.x, 100);
80  CU_ASSERT_EQUAL(p.y, 200);
81  lwgeom_free(geom);
82 }
83 
84 static void test_misc_count_vertices(void)
85 {
86  LWGEOM *geom;
87  int count;
88 
89  geom = lwgeom_from_wkt("GEOMETRYCOLLECTION(POINT(0 0),LINESTRING(0 0,1 1),POLYGON((0 0,0 1,1 0,0 0)),CIRCULARSTRING(0 0,0 1,1 1),CURVEPOLYGON(CIRCULARSTRING(0 0,0 1,1 1)))", LW_PARSER_CHECK_NONE);
91  CU_ASSERT_EQUAL(count,13);
92  lwgeom_free(geom);
93 
94  geom = lwgeom_from_wkt("GEOMETRYCOLLECTION(CIRCULARSTRING(0 0,0 1,1 1),POINT(0 0),CURVEPOLYGON(CIRCULARSTRING(0 0,0 1,1 1,1 0,0 0)))", LW_PARSER_CHECK_NONE);
96  CU_ASSERT_EQUAL(count,9);
97  lwgeom_free(geom);
98 
99  geom = lwgeom_from_wkt("CURVEPOLYGON((0 0,1 0,0 1,0 0),CIRCULARSTRING(0 0,1 0,1 1,1 0,0 0))", LW_PARSER_CHECK_NONE);
101  CU_ASSERT_EQUAL(count,9);
102  lwgeom_free(geom);
103 
104 
105  geom = lwgeom_from_wkt("POLYGON((0 0,1 0,0 1,0 0))", LW_PARSER_CHECK_NONE);
107  CU_ASSERT_EQUAL(count,4);
108  lwgeom_free(geom);
109 
110  geom = lwgeom_from_wkt("CURVEPOLYGON((0 0,1 0,0 1,0 0),CIRCULARSTRING(0 0,1 0,1 1,1 0,0 0))", LW_PARSER_CHECK_NONE);
112  CU_ASSERT_EQUAL(count,9);
113  lwgeom_free(geom);
114 }
115 
116 static void test_misc_area(void)
117 {
118  LWGEOM *geom;
119  double area;
120 
121  geom = lwgeom_from_wkt("LINESTRING EMPTY", LW_PARSER_CHECK_ALL);
122  area = lwgeom_area(geom);
123  CU_ASSERT_DOUBLE_EQUAL(area, 0.0, 0.0001);
124  lwgeom_free(geom);
125 }
126 
127 static void test_misc_wkb(void)
128 {
129  static char *wkb = "010A0000000200000001080000000700000000000000000000C00000000000000000000000000000F0BF000000000000F0BF00000000000000000000000000000000000000000000F03F000000000000F0BF000000000000004000000000000000000000000000000000000000000000004000000000000000C00000000000000000010200000005000000000000000000F0BF00000000000000000000000000000000000000000000E03F000000000000F03F00000000000000000000000000000000000000000000F03F000000000000F0BF0000000000000000";
131  char *str = lwgeom_to_wkt(geom, WKB_ISO, 8, 0);
132  CU_ASSERT_STRING_EQUAL(str, "CURVEPOLYGON(CIRCULARSTRING(-2 0,-1 -1,0 0,1 -1,2 0,0 2,-2 0),(-1 0,0 0.5,1 0,0 1,-1 0))");
133  lwfree(str);
134  lwgeom_free(geom);
135 
136 }
137 
138 
139 static void test_grid(void)
140 {
141  gridspec grid;
142  static char *wkt = "MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)))";
144  LWGEOM *geomgrid;
145  char *str;
146 
147  grid.ipx = grid.ipy = 0;
148  grid.xsize = grid.ysize = 20;
149 
150  geomgrid = lwgeom_grid(geom, &grid);
151  str = lwgeom_to_ewkt(geomgrid);
152  CU_ASSERT_STRING_EQUAL(str, "MULTIPOLYGON EMPTY");
153  lwfree(str);
154  lwgeom_free(geom);
155  lwgeom_free(geomgrid);
156 }
157 
158 static void do_grid_test(const char *wkt_in, const char *wkt_out, double size)
159 {
160  char *wkt_result, *wkt_norm;
161  gridspec grid;
164  wkt_norm = lwgeom_to_ewkt(go);
165  memset(&grid, 0, sizeof(gridspec));
166  grid.xsize = grid.ysize = grid.zsize = grid.msize = size;
167  lwgeom_grid_in_place(g, &grid);
168  wkt_result = lwgeom_to_ewkt(g);
169  // printf("%s ==%ld==> %s == %s\n", wkt_in, lround(size), wkt_result, wkt_out);
170  CU_ASSERT_STRING_EQUAL(wkt_result, wkt_norm);
171  lwfree(wkt_result);
172  lwfree(wkt_norm);
173  lwgeom_free(g);
174  lwgeom_free(go);
175 }
176 
177 static void test_grid_in_place(void)
178 {
179  do_grid_test(
180  "POINT ZM (5.1423999999 5.1423999999 5.1423999999 5.1423999999)",
181  "POINT(5.1424 5.1424 5.1424 5.1424)",
182  0.0001
183  );
184  do_grid_test(
185  "MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)))",
186  "MULTIPOLYGON EMPTY",
187  20
188  );
189  do_grid_test(
190  "MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)))",
191  "MULTIPOLYGON(((0 0,10 0,10 10, 0 10,0 0)))",
192  1
193  );
194  do_grid_test(
195  "LINESTRING(0 0,1 1, 2 2, 3 3, 4 4, 5 5)",
196  "LINESTRING(0 0,2 2,4 4)",
197  2
198  );
199  do_grid_test(
200  "MULTIPOINT(0 0,1 1, 2 2, 3 3, 4 4, 5 5)",
201  /* This preserves current behaviour, but is probably not right */
202  "MULTIPOINT(0 0,0 0,2 2,4 4,4 4,4 4)",
203  2
204  );
205  do_grid_test(
206  "MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(4 4, 4 5, 5 5, 5 4, 4 4)))",
207  "MULTIPOLYGON(((0 0,10 0,10 10, 0 10,0 0)))",
208  2
209  );
210  do_grid_test(
211  "MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(4 4, 4 5, 5 5, 5 4, 4 4)))",
212  "MULTIPOLYGON EMPTY",
213  20
214  );
215  do_grid_test(
216  "POINT Z (5 5 5)",
217  "POINT(0 0 0)",
218  20
219  );
220 }
221 
222 static void test_clone(void)
223 {
224  static char *wkt = "GEOMETRYCOLLECTION(MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0))),POINT(1 1),LINESTRING(2 3,4 5))";
226  LWGEOM *geom2;
227 
228  /* Free in "backwards" order */
229  geom2 = lwgeom_clone(geom1);
230  lwgeom_free(geom1);
231  lwgeom_free(geom2);
232 
233  /* Free in "forewards" order */
234  geom1 = lwgeom_from_wkt(wkt, LW_PARSER_CHECK_ALL);
235  geom2 = lwgeom_clone(geom1);
236  lwgeom_free(geom2);
237  lwgeom_free(geom1);
238 }
239 
240 static void test_lwmpoint_from_lwgeom(void)
241 {
242  /* This cast is so ugly, we only want to do it once. And not even that. */
243  LWGEOM* (*to_points)(LWGEOM*) = (LWGEOM* (*)(LWGEOM*)) &lwmpoint_from_lwgeom;
244 
245  do_fn_test(to_points, "MULTIPOLYGON (EMPTY)", "MULTIPOINT EMPTY");
246  do_fn_test(to_points, "POINT (30 10)", "MULTIPOINT ((30 10))");
247  do_fn_test(to_points, "LINESTRING Z (30 10 4,10 30 5,40 40 6)", "MULTIPOINT Z (30 10 4,10 30 5, 40 40 6)");
248  do_fn_test(to_points, "POLYGON((35 10,45 45,15 40,10 20,35 10),(20 30,35 35,30 20,20 30))", "MULTIPOINT(35 10,45 45,15 40,10 20,35 10,20 30,35 35,30 20,20 30)");
249  do_fn_test(to_points, "MULTIPOINT M (10 40 1,40 30 2,20 20 3,30 10 4)", "MULTIPOINT M (10 40 1,40 30 2,20 20 3,30 10 4)");
250  do_fn_test(to_points, "COMPOUNDCURVE(CIRCULARSTRING(0 0,2 0, 2 1, 2 3, 4 3),(4 3, 4 5, 1 4, 0 0))", "MULTIPOINT(0 0, 2 0, 2 1, 2 3, 4 3, 4 3, 4 5, 1 4, 0 0)");
251  do_fn_test(to_points, "TIN(((80 130,50 160,80 70,80 130)),((50 160,10 190,10 70,50 160)))", "MULTIPOINT (80 130, 50 160, 80 70, 80 130, 50 160, 10 190, 10 70, 50 160)");
252 }
253 
254 static void test_gbox_serialized_size(void)
255 {
256  lwflags_t flags = lwflags(0, 0, 0);
257  CU_ASSERT_EQUAL(gbox_serialized_size(flags),16);
258  FLAGS_SET_BBOX(flags, 1);
259  CU_ASSERT_EQUAL(gbox_serialized_size(flags),16);
260  FLAGS_SET_Z(flags, 1);
261  CU_ASSERT_EQUAL(gbox_serialized_size(flags),24);
262  FLAGS_SET_M(flags, 1);
263  CU_ASSERT_EQUAL(gbox_serialized_size(flags),32);
264  FLAGS_SET_GEODETIC(flags, 1);
265  CU_ASSERT_EQUAL(gbox_serialized_size(flags),24);
266 }
267 
268 static void test_optionlist(void)
269 {
270  size_t sz;
271  const char* value;
272  // zero out all the pointers so our list ends up null-terminated
273  char *olist[OPTION_LIST_SIZE];
274  char input[128];
275  memset(olist, 0, sizeof(olist));
276  // input string needs to be writeable because we are inserting nulls
277  strcpy(input, "key1=value1 key2=value2 ");
278  option_list_parse(input, olist);
279 
280  value = option_list_search(olist, "key1");
281  // printf("value: %s\n", value);
282  CU_ASSERT_STRING_EQUAL("value1", value);
283  value = option_list_search(olist, "key2");
284  CU_ASSERT_STRING_EQUAL("value2", value);
285  value = option_list_search(olist, "key3");
286  CU_ASSERT_EQUAL(NULL, value);
287 
288  sz = option_list_length(olist);
289  CU_ASSERT_EQUAL(4, sz);
290 
291  memset(olist, 0, sizeof(olist));
292  strcpy(input, " ");
293  option_list_parse(input, olist);
294  value = option_list_search(olist, "key1");
295  CU_ASSERT_EQUAL(NULL, value);
296 
297  memset(olist, 0, sizeof(olist));
298  strcpy(input, " key3= ");
299  option_list_parse(input, olist);
300  sz = option_list_length(olist);
301  CU_ASSERT_EQUAL(2, sz);
302 
303  strcpy(input, " key1=value1 key2='value2 value3' ");
304  memset(olist, 0, sizeof(olist));
305  option_list_gdal_parse(input, olist);
306  sz = option_list_length(olist);
307  CU_ASSERT_EQUAL(2, sz);
308  CU_ASSERT_STRING_EQUAL("key1=value1", olist[0]);
309  CU_ASSERT_STRING_EQUAL("key2='value2 value3'", olist[1]);
310 }
311 
312 
313 static void test_stringlist(void)
314 {
315  stringlist_t s;
316  stringlist_init(&s);
317 
318  CU_ASSERT_EQUAL(stringlist_length(&s), 0);
319  stringlist_add_string_nosort(&s, "first string");
320  stringlist_add_string_nosort(&s, "second string");
321  stringlist_add_string_nosort(&s, "third string");
322  CU_ASSERT_EQUAL(stringlist_length(&s), 3);
323  CU_ASSERT_STRING_EQUAL(stringlist_get(&s, 0), "first string");
324  stringlist_add_string_nosort(&s, "an initial string");
325  stringlist_sort(&s);
326  CU_ASSERT_STRING_EQUAL(stringlist_get(&s, 0), "an initial string");
327  CU_ASSERT_STRING_EQUAL(stringlist_find(&s, "third string"), "third string");
328  CU_ASSERT_EQUAL(stringlist_find(&s, "nothing_matches"), NULL);
329  stringlist_add_string_nosort(&s, "fourth string");
330  stringlist_add_string_nosort(&s, "fifth string");
331  stringlist_add_string_nosort(&s, "sixth string");
332  stringlist_add_string_nosort(&s, "seventh string");
333  stringlist_add_string_nosort(&s, "eighth string");
334  stringlist_sort(&s);
335  CU_ASSERT_STRING_EQUAL(stringlist_find(&s, "fifth string"), "fifth string");
337 }
338 
339 
340 /*
341 ** Used by the test harness to register the tests in this file.
342 */
343 void misc_suite_setup(void);
345 {
346  CU_pSuite suite = CU_add_suite("miscellaneous", NULL, NULL);
350  PG_ADD_TEST(suite, test_misc_area);
351  PG_ADD_TEST(suite, test_misc_wkb);
352  PG_ADD_TEST(suite, test_grid);
354  PG_ADD_TEST(suite, test_clone);
359 }
char * s
Definition: cu_in_wkt.c:23
size_t gbox_serialized_size(lwflags_t flags)
Return the number of bytes necessary to hold a GBOX of this dimension in serialized form.
Definition: gbox.c:440
static void test_lwmpoint_from_lwgeom(void)
static void test_clone(void)
static void test_misc_area(void)
void misc_suite_setup(void)
static void do_grid_test(const char *wkt_in, const char *wkt_out, double size)
static void test_misc_simplify(void)
static void test_grid_in_place(void)
static void test_misc_wkb(void)
static void test_stringlist(void)
static void test_misc_startpoint(void)
static void test_optionlist(void)
static void test_gbox_serialized_size(void)
static void test_misc_count_vertices(void)
static void test_grid(void)
void do_fn_test(LWGEOM *(*transfn)(LWGEOM *), char *input_wkt, char *expected_wkt)
#define PG_ADD_TEST(suite, testfunc)
#define LW_PARSER_CHECK_ALL
Definition: liblwgeom.h:2115
LWGEOM * lwgeom_simplify(const LWGEOM *igeom, double dist, int preserve_collapsed)
Simplification.
Definition: lwgeom.c:1870
#define LW_FALSE
Definition: liblwgeom.h:94
int lwgeom_startpoint(const LWGEOM *lwgeom, POINT4D *pt)
Definition: lwgeom.c:2135
#define WKB_ISO
Definition: liblwgeom.h:2175
LWGEOM * lwgeom_from_hexwkb(const char *hexwkb, const char check)
Definition: lwin_wkb.c:858
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1155
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2114
#define LW_SUCCESS
Definition: liblwgeom.h:97
uint16_t lwflags_t
Definition: liblwgeom.h:299
#define FLAGS_SET_BBOX(flags, value)
Definition: liblwgeom.h:174
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:565
double lwgeom_area(const LWGEOM *geom)
Definition: lwgeom.c:1885
uint32_t lwgeom_count_vertices(const LWGEOM *geom)
Count the total number of vertices in any LWGEOM.
Definition: lwgeom.c:1246
void lwfree(void *mem)
Definition: lwutil.c:242
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:708
LWGEOM * lwgeom_clone(const LWGEOM *lwgeom)
Clone LWGEOM object.
Definition: lwgeom.c:491
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:905
LWMPOINT * lwmpoint_from_lwgeom(const LWGEOM *g)
Definition: lwmpoint.c:93
#define FLAGS_SET_GEODETIC(flags, value)
Definition: liblwgeom.h:175
lwflags_t lwflags(int hasz, int hasm, int geodetic)
Construct a new flags bitmask.
Definition: lwutil.c:471
#define FLAGS_SET_M(flags, value)
Definition: liblwgeom.h:173
#define FLAGS_SET_Z(flags, value)
Definition: liblwgeom.h:172
LWGEOM * lwgeom_grid(const LWGEOM *lwgeom, const gridspec *grid)
Definition: lwgeom.c:2267
void lwgeom_grid_in_place(LWGEOM *lwgeom, const gridspec *grid)
Definition: lwgeom.c:2166
#define str(s)
int value
Definition: genraster.py:62
int count
Definition: genraster.py:57
void option_list_gdal_parse(char *input, char **olist)
Definition: optionlist.c:121
size_t option_list_length(char **olist)
Returns the total number of keys and values in the list.
Definition: optionlist.c:73
const char * option_list_search(char **olist, const char *key)
Returns null if the key cannot be found.
Definition: optionlist.c:54
void option_list_parse(char *input, char **olist)
option_list is a null-terminated list of strings, where every odd string is a key and every even stri...
Definition: optionlist.c:86
#define OPTION_LIST_SIZE
Definition: optionlist.h:31
void stringlist_init(stringlist_t *s)
Definition: stringlist.c:44
size_t stringlist_length(stringlist_t *s)
Definition: stringlist.c:137
void stringlist_release(stringlist_t *s)
Definition: stringlist.c:50
const char * stringlist_find(stringlist_t *s, const char *key)
Definition: stringlist.c:129
void stringlist_add_string_nosort(stringlist_t *s, const char *string)
Definition: stringlist.c:117
const char * stringlist_get(stringlist_t *s, size_t i)
Definition: stringlist.c:143
void stringlist_sort(stringlist_t *s)
Definition: stringlist.c:123
double x
Definition: liblwgeom.h:414
double y
Definition: liblwgeom.h:414
double zsize
Definition: liblwgeom.h:1382
double ysize
Definition: liblwgeom.h:1381
double xsize
Definition: liblwgeom.h:1380
double ipx
Definition: liblwgeom.h:1376
double msize
Definition: liblwgeom.h:1383
double ipy
Definition: liblwgeom.h:1377
Snap-to-grid.
Definition: liblwgeom.h:1375