PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches
cu_homogenize.c
Go to the documentation of this file.
1/**********************************************************************
2 *
3 * PostGIS - Spatial Types for PostgreSQL
4 * http://postgis.net
5 * Copyright 2009 Olivier Courtin <olivier.courtin@oslandia.com>
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
20static void do_geom_test(char * in, char * out)
21{
22 LWGEOM *g, *h;
23 char *tmp;
24
26 h = lwgeom_homogenize(g);
27 tmp = lwgeom_to_ewkt(h);
28 if (strcmp(tmp, out))
29 fprintf(stderr, "\nIn: %s\nOut: %s\nExp: %s\n",
30 in, tmp, out);
31 ASSERT_STRING_EQUAL(tmp, out);
32 lwfree(tmp);
33 lwgeom_free(g);
34 /* See http://trac.osgeo.org/postgis/ticket/1104 */
35 lwgeom_free(h);
36}
37
38
39static void test_coll_point(void)
40{
41 do_geom_test("GEOMETRYCOLLECTION(POINT(1 2))",
42 "POINT(1 2)");
43
44 do_geom_test("GEOMETRYCOLLECTION(POINT(1 2),POINT(3 4))",
45 "MULTIPOINT(1 2,3 4)");
46
47 do_geom_test("GEOMETRYCOLLECTION(POINT(1 2),POINT(3 4),POINT(5 6))",
48 "MULTIPOINT(1 2,3 4,5 6)");
49
50 do_geom_test("GEOMETRYCOLLECTION(MULTIPOINT(1 2,3 4),POINT(5 6))",
51 "MULTIPOINT(1 2,3 4,5 6)");
52
53 do_geom_test("GEOMETRYCOLLECTION(POINT(1 2),MULTIPOINT(3 4,5 6))",
54 "MULTIPOINT(1 2,3 4,5 6)");
55
56 do_geom_test("GEOMETRYCOLLECTION(MULTIPOINT(1 2,3 4),MULTIPOINT(5 6,7 8))",
57 "MULTIPOINT(1 2,3 4,5 6,7 8)");
58}
59
60
61static void test_coll_line(void)
62{
63 do_geom_test("GEOMETRYCOLLECTION(LINESTRING(1 2,3 4))",
64 "LINESTRING(1 2,3 4)");
65
66 do_geom_test("GEOMETRYCOLLECTION(LINESTRING(1 2,3 4),LINESTRING(5 6,7 8))",
67 "MULTILINESTRING((1 2,3 4),(5 6,7 8))");
68
69 do_geom_test("GEOMETRYCOLLECTION(LINESTRING(1 2,3 4),LINESTRING(5 6,7 8),LINESTRING(9 10,11 12))",
70 "MULTILINESTRING((1 2,3 4),(5 6,7 8),(9 10,11 12))");
71
72 do_geom_test("GEOMETRYCOLLECTION(MULTILINESTRING((1 2,3 4),(5 6,7 8)),LINESTRING(9 10,11 12))",
73 "MULTILINESTRING((1 2,3 4),(5 6,7 8),(9 10,11 12))");
74
75 do_geom_test("GEOMETRYCOLLECTION(LINESTRING(1 2,3 4),MULTILINESTRING((5 6,7 8),(9 10,11 12)))",
76 "MULTILINESTRING((1 2,3 4),(5 6,7 8),(9 10,11 12))");
77
78 do_geom_test("GEOMETRYCOLLECTION(MULTILINESTRING((1 2,3 4),(5 6,7 8)),MULTILINESTRING((9 10,11 12),(13 14,15 16)))",
79 "MULTILINESTRING((1 2,3 4),(5 6,7 8),(9 10,11 12),(13 14,15 16))");
80}
81
82
83static void test_coll_poly(void)
84{
85 do_geom_test("GEOMETRYCOLLECTION(POLYGON((1 2,3 4,5 6,1 2)))",
86 "POLYGON((1 2,3 4,5 6,1 2))");
87
88 do_geom_test("GEOMETRYCOLLECTION(POLYGON((1 2,3 4,5 6,1 2)),POLYGON((7 8,9 10,11 12,7 8)))",
89 "MULTIPOLYGON(((1 2,3 4,5 6,1 2)),((7 8,9 10,11 12,7 8)))");
90
91 do_geom_test("GEOMETRYCOLLECTION(POLYGON((1 2,3 4,5 6,1 2)),POLYGON((7 8,9 10,11 12,7 8)),POLYGON((13 14,15 16,17 18,13 14)))",
92 "MULTIPOLYGON(((1 2,3 4,5 6,1 2)),((7 8,9 10,11 12,7 8)),((13 14,15 16,17 18,13 14)))");
93
94 do_geom_test("GEOMETRYCOLLECTION(MULTIPOLYGON(((1 2,3 4,5 6,1 2)),((7 8,9 10,11 12,7 8))),POLYGON((13 14,15 16,17 18,13 14)))",
95 "MULTIPOLYGON(((1 2,3 4,5 6,1 2)),((7 8,9 10,11 12,7 8)),((13 14,15 16,17 18,13 14)))");
96
97 do_geom_test("GEOMETRYCOLLECTION(POLYGON((1 2,3 4,5 6,1 2)),MULTIPOLYGON(((7 8,9 10,11 12,7 8)),((13 14,15 16,17 18,13 14))))",
98 "MULTIPOLYGON(((1 2,3 4,5 6,1 2)),((7 8,9 10,11 12,7 8)),((13 14,15 16,17 18,13 14)))");
99
100 do_geom_test("GEOMETRYCOLLECTION(MULTIPOLYGON(((1 2,3 4,5 6,1 2)),((7 8,9 10,11 12,7 8))),MULTIPOLYGON(((13 14,15 16,17 18,13 14)),((19 20,21 22,23 24,19 20))))",
101 "MULTIPOLYGON(((1 2,3 4,5 6,1 2)),((7 8,9 10,11 12,7 8)),((13 14,15 16,17 18,13 14)),((19 20,21 22,23 24,19 20)))");
102}
103
104
105static void test_coll_coll(void)
106{
107 /* Two different types together must produce a Collection as output */
108 do_geom_test("GEOMETRYCOLLECTION(POINT(1 2),LINESTRING(3 4,5 6))",
109 "GEOMETRYCOLLECTION(POINT(1 2),LINESTRING(3 4,5 6))");
110
111 do_geom_test("GEOMETRYCOLLECTION(LINESTRING(1 2,3 4),POLYGON((5 6,7 8,9 10,5 6)))",
112 "GEOMETRYCOLLECTION(LINESTRING(1 2,3 4),POLYGON((5 6,7 8,9 10,5 6)))");
113
114
115 /* Ability to produce a single MULTI with same type */
116 do_geom_test("GEOMETRYCOLLECTION(POINT(1 2),LINESTRING(3 4,5 6),POINT(7 8))",
117 "GEOMETRYCOLLECTION(MULTIPOINT(1 2,7 8),LINESTRING(3 4,5 6))");
118
119 do_geom_test("GEOMETRYCOLLECTION(POINT(1 2),LINESTRING(3 4,5 6),MULTIPOINT(7 8,9 10))",
120 "GEOMETRYCOLLECTION(MULTIPOINT(1 2,7 8,9 10),LINESTRING(3 4,5 6))");
121
122
123 /* Recursive Collection handle */
124 do_geom_test("GEOMETRYCOLLECTION(GEOMETRYCOLLECTION(GEOMETRYCOLLECTION(POINT(1 2))))",
125 "POINT(1 2)");
126
127 do_geom_test("GEOMETRYCOLLECTION(POINT(1 2),GEOMETRYCOLLECTION(LINESTRING(3 4,5 6)))",
128 "GEOMETRYCOLLECTION(POINT(1 2),LINESTRING(3 4,5 6))");
129
130
131 /* EMPTY Collection */
132 do_geom_test("GEOMETRYCOLLECTION EMPTY",
133 "GEOMETRYCOLLECTION EMPTY");
134
135
136 /* Recursive EMPTY Collection */
137 do_geom_test("GEOMETRYCOLLECTION(GEOMETRYCOLLECTION EMPTY)",
138 "GEOMETRYCOLLECTION EMPTY");
139
140 do_geom_test("GEOMETRYCOLLECTION(GEOMETRYCOLLECTION(GEOMETRYCOLLECTION EMPTY))",
141 "GEOMETRYCOLLECTION EMPTY");
142}
143
144static void test_coll_curve(void)
145{
146 /* Two different types together must produce a Collection as output */
147 do_geom_test("GEOMETRYCOLLECTION(CIRCULARSTRING(0 0,1 1,2 2))",
148 "CIRCULARSTRING(0 0,1 1,2 2)");
149
150 do_geom_test("GEOMETRYCOLLECTION(CIRCULARSTRING(0 0,1 1,2 2),CIRCULARSTRING(0 0,1 1,2 2))",
151 "MULTICURVE(CIRCULARSTRING(0 0,1 1,2 2),CIRCULARSTRING(0 0,1 1,2 2))");
152}
153
154static void test_geom(void)
155{
156 /* Already simple geometry */
157 do_geom_test("POINT(1 2)",
158 "POINT(1 2)");
159
160 do_geom_test("LINESTRING(1 2,3 4)",
161 "LINESTRING(1 2,3 4)");
162
163 do_geom_test("POLYGON((1 2,3 4,5 6,1 2))",
164 "POLYGON((1 2,3 4,5 6,1 2))");
165
166 do_geom_test("POLYGON((1 2,3 4,5 6,1 2),(7 8,9 10,11 12,7 8))",
167 "POLYGON((1 2,3 4,5 6,1 2),(7 8,9 10,11 12,7 8))");
168
169
170 /* Empty geometry */
171 do_geom_test("GEOMETRYCOLLECTION EMPTY",
172 "GEOMETRYCOLLECTION EMPTY");
173
174
175 /* A MULTI with a single geometry inside */
176 do_geom_test("MULTIPOINT(1 2)",
177 "POINT(1 2)");
178
179 do_geom_test("MULTILINESTRING((1 2,3 4))",
180 "LINESTRING(1 2,3 4)");
181
182 do_geom_test("MULTIPOLYGON(((1 2,3 4,5 6,1 2)))",
183 "POLYGON((1 2,3 4,5 6,1 2))");
184
185 do_geom_test("MULTIPOLYGON(((1 2,3 4,5 6,1 2),(7 8,9 10,11 12,7 8)))",
186 "POLYGON((1 2,3 4,5 6,1 2),(7 8,9 10,11 12,7 8))");
187
188
189 /* A real MULTI */
190 do_geom_test("MULTIPOINT(1 2,3 4)",
191 "MULTIPOINT(1 2,3 4)");
192
193 do_geom_test("MULTILINESTRING((1 2,3 4),(5 6,7 8))",
194 "MULTILINESTRING((1 2,3 4),(5 6,7 8))");
195
196 do_geom_test("MULTIPOLYGON(((1 2,3 4,5 6,1 2)),((7 8,9 10,11 12,7 8)))",
197 "MULTIPOLYGON(((1 2,3 4,5 6,1 2)),((7 8,9 10,11 12,7 8)))");
198
199 do_geom_test("MULTIPOLYGON(((1 2,3 4,5 6,1 2),(7 8,9 10,11 12,7 8)),((13 14,15 16,17 18,13 14)))",
200 "MULTIPOLYGON(((1 2,3 4,5 6,1 2),(7 8,9 10,11 12,7 8)),((13 14,15 16,17 18,13 14)))");
201
202
203 /* A Collection */
204 do_geom_test("GEOMETRYCOLLECTION(POINT(1 2),LINESTRING(3 4,5 6))",
205 "GEOMETRYCOLLECTION(POINT(1 2),LINESTRING(3 4,5 6))");
206
207
208 /* SRID */
209 do_geom_test("SRID=4326;GEOMETRYCOLLECTION EMPTY",
210 "SRID=4326;GEOMETRYCOLLECTION EMPTY");
211
212 /* See http://trac.osgeo.org/postgis/ticket/2129 */
213 do_geom_test("SRID=4326;GEOMETRYCOLLECTION(MULTIPOINT(0 0))",
214 "SRID=4326;POINT(0 0)");
215
216 /* See http://trac.osgeo.org/postgis/ticket/2129 */
217 do_geom_test("SRID=4326;GEOMETRYCOLLECTION(MULTIPOINT(0 0, 1 2))",
218 "SRID=4326;MULTIPOINT(0 0,1 2)");
219
220 /* See http://trac.osgeo.org/postgis/ticket/2129 */
221 do_geom_test("SRID=4326;GEOMETRYCOLLECTION(POINT(0 0),LINESTRING(0 0,10 0))",
222 "SRID=4326;GEOMETRYCOLLECTION(POINT(0 0),LINESTRING(0 0,10 0))");
223
224 do_geom_test("SRID=4326;POINT(1 2)",
225 "SRID=4326;POINT(1 2)");
226
227 do_geom_test("SRID=4326;MULTIPOINT(1 2)",
228 "SRID=4326;POINT(1 2)");
229
230 do_geom_test("SRID=4326;MULTIPOINT(1 2,3 4)",
231 "SRID=4326;MULTIPOINT(1 2,3 4)");
232
233 do_geom_test("SRID=4326;MULTILINESTRING((1 2,3 4))",
234 "SRID=4326;LINESTRING(1 2,3 4)");
235
236 do_geom_test("SRID=4326;MULTILINESTRING((1 2,3 4),(5 6,7 8))",
237 "SRID=4326;MULTILINESTRING((1 2,3 4),(5 6,7 8))");
238
239 /* 3D and 4D */
240 do_geom_test("POINT(1 2 3)",
241 "POINT(1 2 3)");
242
243 do_geom_test("POINTM(1 2 3)",
244 "POINTM(1 2 3)");
245
246 do_geom_test("POINT(1 2 3 4)",
247 "POINT(1 2 3 4)");
248}
249
250/*
251** Used by test harness to register the tests in this file.
252*/
253void homogenize_suite_setup(void);
255{
256 CU_pSuite suite = CU_add_suite("homogenize", NULL, NULL);
261 PG_ADD_TEST(suite, test_geom);
263}
static void test_coll_line(void)
static void test_coll_poly(void)
static void test_coll_coll(void)
void homogenize_suite_setup(void)
static void test_coll_curve(void)
static void test_coll_point(void)
static void test_geom(void)
static void do_geom_test(char *in, char *out)
#define PG_ADD_TEST(suite, testfunc)
#define ASSERT_STRING_EQUAL(o, e)
void lwgeom_free(LWGEOM *geom)
Definition lwgeom.c:1246
#define LW_PARSER_CHECK_NONE
Definition liblwgeom.h:2149
LWGEOM * lwgeom_homogenize(const LWGEOM *geom)
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an allocated string.
Definition lwgeom.c:593
void lwfree(void *mem)
Definition lwutil.c:248
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition lwin_wkt.c:940