PostGIS  3.0.6dev-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 "cu_tester.h"
19 
20 
21 static void test_misc_force_2d(void)
22 {
23  LWGEOM *geom;
24  LWGEOM *geom2d;
25  char *wkt_out;
26 
27  geom = lwgeom_from_wkt("CIRCULARSTRINGM(-5 0 4,0 5 3,5 0 2,10 -5 1,15 0 0)", LW_PARSER_CHECK_NONE);
28  geom2d = lwgeom_force_2d(geom);
29  wkt_out = lwgeom_to_ewkt(geom2d);
30  CU_ASSERT_STRING_EQUAL("CIRCULARSTRING(-5 0,0 5,5 0,10 -5,15 0)",wkt_out);
31  lwgeom_free(geom);
32  lwgeom_free(geom2d);
33  lwfree(wkt_out);
34 
35  geom = lwgeom_from_wkt("GEOMETRYCOLLECTION(POINT(0 0 0),LINESTRING(1 1 1,2 2 2),POLYGON((0 0 1,0 1 1,1 1 1,1 0 1,0 0 1)),CURVEPOLYGON(CIRCULARSTRING(0 0 0,1 1 1,2 2 2,1 1 1,0 0 0)))", LW_PARSER_CHECK_NONE);
36  geom2d = lwgeom_force_2d(geom);
37  wkt_out = lwgeom_to_ewkt(geom2d);
38  CU_ASSERT_STRING_EQUAL("GEOMETRYCOLLECTION(POINT(0 0),LINESTRING(1 1,2 2),POLYGON((0 0,0 1,1 1,1 0,0 0)),CURVEPOLYGON(CIRCULARSTRING(0 0,1 1,2 2,1 1,0 0)))",wkt_out);
39  lwgeom_free(geom);
40  lwgeom_free(geom2d);
41  lwfree(wkt_out);
42 }
43 
44 static void test_misc_simplify(void)
45 {
46  LWGEOM *geom;
47  LWGEOM *geom2d;
48  char *wkt_out;
49 
50  geom = lwgeom_from_wkt("LINESTRING(0 0,0 10,0 51,50 20,30 20,7 32)", LW_PARSER_CHECK_NONE);
51  geom2d = lwgeom_simplify(geom, 2, LW_FALSE);
52  wkt_out = lwgeom_to_ewkt(geom2d);
53  CU_ASSERT_STRING_EQUAL("LINESTRING(0 0,0 51,50 20,30 20,7 32)",wkt_out);
54  lwgeom_free(geom);
55  lwgeom_free(geom2d);
56  lwfree(wkt_out);
57 
58  geom = lwgeom_from_wkt("MULTILINESTRING((0 0,0 10,0 51,50 20,30 20,7 32))", LW_PARSER_CHECK_NONE);
59  geom2d = lwgeom_simplify(geom, 2, LW_FALSE);
60  wkt_out = lwgeom_to_ewkt(geom2d);
61  CU_ASSERT_STRING_EQUAL("MULTILINESTRING((0 0,0 51,50 20,30 20,7 32))",wkt_out);
62  lwgeom_free(geom);
63  lwgeom_free(geom2d);
64  lwfree(wkt_out);
65 
66  geom = lwgeom_from_wkt("POLYGON((0 0,1 1,1 3,0 4,-2 3,-1 1,0 0))", LW_PARSER_CHECK_NONE);
67  geom2d = lwgeom_simplify(geom, 1, LW_FALSE);
68  wkt_out = lwgeom_to_ewkt(geom2d);
69  CU_ASSERT_STRING_EQUAL("POLYGON((0 0,0 4,-2 3,0 0))", wkt_out);
70  lwgeom_free(geom);
71  lwgeom_free(geom2d);
72  lwfree(wkt_out);
73 }
74 
75 static void test_misc_count_vertices(void)
76 {
77  LWGEOM *geom;
78  int count;
79 
80  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);
82  CU_ASSERT_EQUAL(count,13);
83  lwgeom_free(geom);
84 
85  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);
87  CU_ASSERT_EQUAL(count,9);
88  lwgeom_free(geom);
89 
90  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);
92  CU_ASSERT_EQUAL(count,9);
93  lwgeom_free(geom);
94 
95 
96  geom = lwgeom_from_wkt("POLYGON((0 0,1 0,0 1,0 0))", LW_PARSER_CHECK_NONE);
98  CU_ASSERT_EQUAL(count,4);
99  lwgeom_free(geom);
100 
101  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);
103  CU_ASSERT_EQUAL(count,9);
104  lwgeom_free(geom);
105 }
106 
107 static void test_misc_area(void)
108 {
109  LWGEOM *geom;
110  double area;
111 
112  geom = lwgeom_from_wkt("LINESTRING EMPTY", LW_PARSER_CHECK_ALL);
113  area = lwgeom_area(geom);
114  CU_ASSERT_DOUBLE_EQUAL(area, 0.0, 0.0001);
115  lwgeom_free(geom);
116 }
117 
118 static void test_misc_wkb(void)
119 {
120  static char *wkb = "010A0000000200000001080000000700000000000000000000C00000000000000000000000000000F0BF000000000000F0BF00000000000000000000000000000000000000000000F03F000000000000F0BF000000000000004000000000000000000000000000000000000000000000004000000000000000C00000000000000000010200000005000000000000000000F0BF00000000000000000000000000000000000000000000E03F000000000000F03F00000000000000000000000000000000000000000000F03F000000000000F0BF0000000000000000";
122  char *str = lwgeom_to_wkt(geom, WKB_ISO, 8, 0);
123  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))");
124  lwfree(str);
125  lwgeom_free(geom);
126 
127 }
128 
129 
130 static void test_grid(void)
131 {
132  gridspec grid;
133  static char *wkt = "MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)))";
135  LWGEOM *geomgrid;
136  char *str;
137 
138  grid.ipx = grid.ipy = 0;
139  grid.xsize = grid.ysize = 20;
140 
141  geomgrid = lwgeom_grid(geom, &grid);
142  str = lwgeom_to_ewkt(geomgrid);
143  CU_ASSERT_STRING_EQUAL(str, "MULTIPOLYGON EMPTY");
144  lwfree(str);
145  lwgeom_free(geom);
146  lwgeom_free(geomgrid);
147 }
148 
149 static void do_grid_test(const char *wkt_in, const char *wkt_out, double size)
150 {
151  char *wkt_result, *wkt_norm;
152  gridspec grid;
155  wkt_norm = lwgeom_to_ewkt(go);
156  memset(&grid, 0, sizeof(gridspec));
157  grid.xsize = grid.ysize = grid.zsize = grid.msize = size;
158  lwgeom_grid_in_place(g, &grid);
159  wkt_result = lwgeom_to_ewkt(g);
160  // printf("%s ==%ld==> %s == %s\n", wkt_in, lround(size), wkt_result, wkt_out);
161  CU_ASSERT_STRING_EQUAL(wkt_result, wkt_norm);
162  lwfree(wkt_result);
163  lwfree(wkt_norm);
164  lwgeom_free(g);
165  lwgeom_free(go);
166 }
167 
168 static void test_grid_in_place(void)
169 {
170  do_grid_test(
171  "POINT ZM (5.1423999999 5.1423999999 5.1423999999 5.1423999999)",
172  "POINT(5.1424 5.1424 5.1424 5.1424)",
173  0.0001
174  );
175  do_grid_test(
176  "MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)))",
177  "MULTIPOLYGON EMPTY",
178  20
179  );
180  do_grid_test(
181  "MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)))",
182  "MULTIPOLYGON(((0 0,10 0,10 10, 0 10,0 0)))",
183  1
184  );
185  do_grid_test(
186  "LINESTRING(0 0,1 1, 2 2, 3 3, 4 4, 5 5)",
187  "LINESTRING(0 0,2 2,4 4)",
188  2
189  );
190  do_grid_test(
191  "MULTIPOINT(0 0,1 1, 2 2, 3 3, 4 4, 5 5)",
192  /* This preserves current behaviour, but is probably not right */
193  "MULTIPOINT(0 0,0 0,2 2,4 4,4 4,4 4)",
194  2
195  );
196  do_grid_test(
197  "MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(4 4, 4 5, 5 5, 5 4, 4 4)))",
198  "MULTIPOLYGON(((0 0,10 0,10 10, 0 10,0 0)))",
199  2
200  );
201  do_grid_test(
202  "MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(4 4, 4 5, 5 5, 5 4, 4 4)))",
203  "MULTIPOLYGON EMPTY",
204  20
205  );
206  do_grid_test(
207  "POINT Z (5 5 5)",
208  "POINT(0 0 0)",
209  20
210  );
211 }
212 
213 static void test_clone(void)
214 {
215  static char *wkt = "GEOMETRYCOLLECTION(MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0))),POINT(1 1),LINESTRING(2 3,4 5))";
217  LWGEOM *geom2;
218 
219  /* Free in "backwards" order */
220  geom2 = lwgeom_clone(geom1);
221  lwgeom_free(geom1);
222  lwgeom_free(geom2);
223 
224  /* Free in "forewards" order */
225  geom1 = lwgeom_from_wkt(wkt, LW_PARSER_CHECK_ALL);
226  geom2 = lwgeom_clone(geom1);
227  lwgeom_free(geom2);
228  lwgeom_free(geom1);
229 }
230 
231 static void test_lwmpoint_from_lwgeom(void)
232 {
233  /* This cast is so ugly, we only want to do it once. And not even that. */
234  LWGEOM* (*to_points)(LWGEOM*) = (LWGEOM* (*)(LWGEOM*)) &lwmpoint_from_lwgeom;
235 
236  do_fn_test(to_points, "MULTIPOLYGON (EMPTY)", "MULTIPOINT EMPTY");
237  do_fn_test(to_points, "POINT (30 10)", "MULTIPOINT ((30 10))");
238  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)");
239  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)");
240  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)");
241  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)");
242  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)");
243 }
244 
245 static void test_gbox_serialized_size(void)
246 {
247  lwflags_t flags = lwflags(0, 0, 0);
248  CU_ASSERT_EQUAL(gbox_serialized_size(flags),16);
249  FLAGS_SET_BBOX(flags, 1);
250  CU_ASSERT_EQUAL(gbox_serialized_size(flags),16);
251  FLAGS_SET_Z(flags, 1);
252  CU_ASSERT_EQUAL(gbox_serialized_size(flags),24);
253  FLAGS_SET_M(flags, 1);
254  CU_ASSERT_EQUAL(gbox_serialized_size(flags),32);
255  FLAGS_SET_GEODETIC(flags, 1);
256  CU_ASSERT_EQUAL(gbox_serialized_size(flags),24);
257 }
258 
259 
260 
261 /*
262 ** Used by the test harness to register the tests in this file.
263 */
264 void misc_suite_setup(void);
266 {
267  CU_pSuite suite = CU_add_suite("miscellaneous", NULL, NULL);
271  PG_ADD_TEST(suite, test_misc_area);
272  PG_ADD_TEST(suite, test_misc_wkb);
273  PG_ADD_TEST(suite, test_grid);
275  PG_ADD_TEST(suite, test_clone);
278 }
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_misc_force_2d(void)
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_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:2061
LWGEOM * lwgeom_simplify(const LWGEOM *igeom, double dist, int preserve_collapsed)
Simplification.
Definition: lwgeom.c:1848
#define LW_FALSE
Definition: liblwgeom.h:108
#define WKB_ISO
Definition: liblwgeom.h:2121
LWGEOM * lwgeom_from_hexwkb(const char *hexwkb, const char check)
Definition: lwin_wkb.c:849
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2060
uint16_t lwflags_t
Definition: liblwgeom.h:313
#define FLAGS_SET_BBOX(flags, value)
Definition: liblwgeom.h:188
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:547
double lwgeom_area(const LWGEOM *geom)
Definition: lwgeom.c:1863
uint32_t lwgeom_count_vertices(const LWGEOM *geom)
Count the total number of vertices in any LWGEOM.
Definition: lwgeom.c:1229
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:676
LWGEOM * lwgeom_clone(const LWGEOM *lwgeom)
Clone LWGEOM object.
Definition: lwgeom.c:473
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:189
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:187
#define FLAGS_SET_Z(flags, value)
Definition: liblwgeom.h:186
LWGEOM * lwgeom_grid(const LWGEOM *lwgeom, const gridspec *grid)
Definition: lwgeom.c:2245
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
Definition: lwgeom.c:775
void lwgeom_grid_in_place(LWGEOM *lwgeom, const gridspec *grid)
Definition: lwgeom.c:2144
#define str(s)
int count
Definition: genraster.py:57
double zsize
Definition: liblwgeom.h:1348
double ysize
Definition: liblwgeom.h:1347
double xsize
Definition: liblwgeom.h:1346
double ipx
Definition: liblwgeom.h:1342
double msize
Definition: liblwgeom.h:1349
double ipy
Definition: liblwgeom.h:1343
Snap-to-grid.
Definition: liblwgeom.h:1341