PostGIS  2.5.7dev-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 
67 static void test_misc_count_vertices(void)
68 {
69  LWGEOM *geom;
70  int count;
71 
72  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);
74  CU_ASSERT_EQUAL(count,13);
75  lwgeom_free(geom);
76 
77  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);
79  CU_ASSERT_EQUAL(count,9);
80  lwgeom_free(geom);
81 
82  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);
84  CU_ASSERT_EQUAL(count,9);
85  lwgeom_free(geom);
86 
87 
88  geom = lwgeom_from_wkt("POLYGON((0 0,1 0,0 1,0 0))", LW_PARSER_CHECK_NONE);
90  CU_ASSERT_EQUAL(count,4);
91  lwgeom_free(geom);
92 
93  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);
95  CU_ASSERT_EQUAL(count,9);
96  lwgeom_free(geom);
97 }
98 
99 static void test_misc_area(void)
100 {
101  LWGEOM *geom;
102  double area;
103 
104  geom = lwgeom_from_wkt("LINESTRING EMPTY", LW_PARSER_CHECK_ALL);
105  area = lwgeom_area(geom);
106  CU_ASSERT_DOUBLE_EQUAL(area, 0.0, 0.0001);
107  lwgeom_free(geom);
108 }
109 
110 static void test_misc_wkb(void)
111 {
112  static char *wkb = "010A0000000200000001080000000700000000000000000000C00000000000000000000000000000F0BF000000000000F0BF00000000000000000000000000000000000000000000F03F000000000000F0BF000000000000004000000000000000000000000000000000000000000000004000000000000000C00000000000000000010200000005000000000000000000F0BF00000000000000000000000000000000000000000000E03F000000000000F03F00000000000000000000000000000000000000000000F03F000000000000F0BF0000000000000000";
114  char *str = lwgeom_to_wkt(geom, WKB_ISO, 8, 0);
115  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))");
116  lwfree(str);
117  lwgeom_free(geom);
118 
119 }
120 
121 
122 static void test_grid(void)
123 {
124  gridspec grid;
125  static char *wkt = "MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)))";
127  LWGEOM *geomgrid;
128  char *str;
129 
130  grid.ipx = grid.ipy = 0;
131  grid.xsize = grid.ysize = 20;
132 
133  geomgrid = lwgeom_grid(geom, &grid);
134  str = lwgeom_to_ewkt(geomgrid);
135  CU_ASSERT_STRING_EQUAL(str, "MULTIPOLYGON EMPTY");
136  lwfree(str);
137  lwgeom_free(geom);
138  lwgeom_free(geomgrid);
139 }
140 
141 static void do_grid_test(const char *wkt_in, const char *wkt_out, double size)
142 {
143  char *wkt_result, *wkt_norm;
144  gridspec grid;
147  wkt_norm = lwgeom_to_ewkt(go);
148  memset(&grid, 0, sizeof(gridspec));
149  grid.xsize = grid.ysize = grid.zsize = grid.msize = size;
150  lwgeom_grid_in_place(g, &grid);
151  wkt_result = lwgeom_to_ewkt(g);
152  // printf("%s ==%ld==> %s == %s\n", wkt_in, lround(size), wkt_result, wkt_out);
153  CU_ASSERT_STRING_EQUAL(wkt_result, wkt_norm);
154  lwfree(wkt_result);
155  lwfree(wkt_norm);
156  lwgeom_free(g);
157  lwgeom_free(go);
158 }
159 
160 static void test_grid_in_place(void)
161 {
162  do_grid_test(
163  "POINT ZM (5.1423999999 5.1423999999 5.1423999999 5.1423999999)",
164  "POINT(5.1424 5.1424 5.1424 5.1424)",
165  0.0001
166  );
167  do_grid_test(
168  "MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)))",
169  "MULTIPOLYGON EMPTY",
170  20
171  );
172  do_grid_test(
173  "MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)))",
174  "MULTIPOLYGON(((0 0,10 0,10 10, 0 10,0 0)))",
175  1
176  );
177  do_grid_test(
178  "LINESTRING(0 0,1 1, 2 2, 3 3, 4 4, 5 5)",
179  "LINESTRING(0 0,2 2,4 4)",
180  2
181  );
182  do_grid_test(
183  "MULTIPOINT(0 0,1 1, 2 2, 3 3, 4 4, 5 5)",
184  /* This preserves current behaviour, but is probably not right */
185  "MULTIPOINT(0 0,0 0,2 2,4 4,4 4,4 4)",
186  2
187  );
188  do_grid_test(
189  "MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(4 4, 4 5, 5 5, 5 4, 4 4)))",
190  "MULTIPOLYGON(((0 0,10 0,10 10, 0 10,0 0)))",
191  2
192  );
193  do_grid_test(
194  "MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(4 4, 4 5, 5 5, 5 4, 4 4)))",
195  "MULTIPOLYGON EMPTY",
196  20
197  );
198  do_grid_test(
199  "POINT Z (5 5 5)",
200  "POINT(0 0 0)",
201  20
202  );
203 }
204 
205 static void test_clone(void)
206 {
207  static char *wkt = "GEOMETRYCOLLECTION(MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0))),POINT(1 1),LINESTRING(2 3,4 5))";
209  LWGEOM *geom2;
210 
211  /* Free in "backwards" order */
212  geom2 = lwgeom_clone(geom1);
213  lwgeom_free(geom1);
214  lwgeom_free(geom2);
215 
216  /* Free in "forewards" order */
217  geom1 = lwgeom_from_wkt(wkt, LW_PARSER_CHECK_ALL);
218  geom2 = lwgeom_clone(geom1);
219  lwgeom_free(geom2);
220  lwgeom_free(geom1);
221 }
222 
223 static void test_lwmpoint_from_lwgeom(void)
224 {
225  /* This cast is so ugly, we only want to do it once. And not even that. */
226  LWGEOM* (*to_points)(LWGEOM*) = (LWGEOM* (*)(LWGEOM*)) &lwmpoint_from_lwgeom;
227 
228  do_fn_test(to_points, "MULTIPOLYGON (EMPTY)", "MULTIPOINT EMPTY");
229  do_fn_test(to_points, "POINT (30 10)", "MULTIPOINT ((30 10))");
230  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)");
231  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)");
232  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)");
233  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)");
234  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)");
235 }
236 
237 /*
238 ** Used by the test harness to register the tests in this file.
239 */
240 void misc_suite_setup(void);
242 {
243  CU_pSuite suite = CU_add_suite("miscellaneous", NULL, NULL);
247  PG_ADD_TEST(suite, test_misc_area);
248  PG_ADD_TEST(suite, test_misc_wkb);
249  PG_ADD_TEST(suite, test_grid);
251  PG_ADD_TEST(suite, test_clone);
253 }
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_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:2006
LWGEOM * lwgeom_simplify(const LWGEOM *igeom, double dist, int preserve_collapsed)
Definition: lwgeom.c:1857
#define LW_FALSE
Definition: liblwgeom.h:77
#define WKB_ISO
Definition: liblwgeom.h:2066
LWGEOM * lwgeom_from_hexwkb(const char *hexwkb, const char check)
Definition: lwin_wkb.c:809
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2005
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:556
double lwgeom_area(const LWGEOM *geom)
Definition: lwgeom.c:1872
uint32_t lwgeom_count_vertices(const LWGEOM *geom)
Count the total number of vertices in any LWGEOM.
Definition: lwgeom.c:1235
void lwfree(void *mem)
Definition: lwutil.c:244
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:482
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:904
LWMPOINT * lwmpoint_from_lwgeom(const LWGEOM *g)
Definition: lwmpoint.c:93
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
Definition: lwgeom.c:784
LWGEOM * lwgeom_grid(const LWGEOM *lwgeom, const gridspec *grid)
Definition: lwgeom.c:2253
void lwgeom_grid_in_place(LWGEOM *lwgeom, const gridspec *grid)
Definition: lwgeom.c:2152
Datum area(PG_FUNCTION_ARGS)
int count
Definition: genraster.py:56
Snap to grid.