-
Notifications
You must be signed in to change notification settings - Fork 363
/
GEOSGeom_transformXYTest.cpp
308 lines (240 loc) · 8.15 KB
/
GEOSGeom_transformXYTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//
// Test Suite for C-API GEOSGeom_transformXY
#include <tut/tut.hpp>
// geos
#include <geos_c.h>
// std
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include "capi_test_utils.h"
namespace tut {
struct test_capi_geosgeom_transformxy : public capitest::utility {};
typedef test_group<test_capi_geosgeom_transformxy> group;
typedef group::object object;
group test_capi_geosgeom_transformxy_group("capi::GEOSGeom_transformXY");
static int SCALE_2_3(double* x, double* y, void* userdata) {
(*x) *= 2;
(*y) *= 3;
(void)(userdata); // make unused parameter warning go away
return 1;
}
// callback that doesn't update coordinates should return original values
template <>
template <>
void object::test<1>() {
GEOSGeometry* geom = GEOSGeomFromWKT("POINT (1 1)");
GEOSGeometry* out = GEOSGeom_transformXY(
geom,
[](double* x, double* y, void* userdata) {
(void)(x); // make unused parameter warning go away
(void)(y); // make unused parameter warning go away
(void)(userdata); // make unused parameter warning go away
return 1;
},
nullptr);
ensure(out != nullptr);
ensure_equals(GEOSEqualsExact(out, geom, 0), 1);
GEOSGeom_destroy(geom);
GEOSGeom_destroy(out);
}
// failed callback should return NULL
template <>
template <>
void object::test<2>() {
GEOSGeometry* geom = GEOSGeomFromWKT("POINT (1 1)");
GEOSGeometry* out = GEOSGeom_transformXY(
geom,
[](double* x, double* y, void* userdata) {
(void)(x); // make unused parameter warning go away
(void)(y); // make unused parameter warning go away
(void)(userdata); // make unused parameter warning go away
return 0; // indicates error
},
nullptr);
ensure(out == nullptr);
GEOSGeom_destroy(geom);
}
// callback should modify point coordinates
template <>
template <>
void object::test<3>() {
GEOSGeometry* geom = GEOSGeomFromWKT("POINT (1 1)");
GEOSGeometry* out = GEOSGeom_transformXY(geom, SCALE_2_3, nullptr);
ensure(out != nullptr);
ensure_geometry_equals(out, "POINT (2 3)");
double xmin, ymin, xmax, ymax;
GEOSGeom_getExtent(out, &xmin, &ymin, &xmax, &ymax);
ensure_equals(xmin, 2);
ensure_equals(ymin, 3);
ensure_equals(xmax, 2);
ensure_equals(ymax, 3);
GEOSGeom_destroy(geom);
GEOSGeom_destroy(out);
}
// callback should modify linestring coordinates
template <>
template <>
void object::test<4>() {
GEOSGeometry* geom = GEOSGeomFromWKT("LINESTRING (1 1, 2 2)");
GEOSGeometry* out = GEOSGeom_transformXY(geom, SCALE_2_3, nullptr);
ensure(out != nullptr);
ensure_geometry_equals(out, "LINESTRING (2 3, 4 6)");
double xmin, ymin, xmax, ymax;
GEOSGeom_getExtent(out, &xmin, &ymin, &xmax, &ymax);
ensure_equals(xmin, 2);
ensure_equals(ymin, 3);
ensure_equals(xmax, 4);
ensure_equals(ymax, 6);
GEOSGeom_destroy(geom);
GEOSGeom_destroy(out);
}
// callback should modify polygon coordinates
template <>
template <>
void object::test<5>() {
GEOSGeometry* geom = GEOSGeomFromWKT(
"POLYGON ((1 1, 1 10, 10 10, 10 1, 1 1), (2 2, 2 4, 4 4, 4 2, 2 2))");
GEOSGeometry* out = GEOSGeom_transformXY(geom, SCALE_2_3, nullptr);
ensure(out != nullptr);
ensure_geometry_equals(out,
"POLYGON ((2 3, 2 30, 20 30, 20 3, 2 3), (4 6, 4 12, 8 12, 8 6, 4 6))");
double xmin, ymin, xmax, ymax;
GEOSGeom_getExtent(out, &xmin, &ymin, &xmax, &ymax);
ensure_equals(xmin, 2);
ensure_equals(ymin, 3);
ensure_equals(xmax, 20);
ensure_equals(ymax, 30);
GEOSGeom_destroy(geom);
GEOSGeom_destroy(out);
}
// callback should modify multi point coordinates
template <>
template <>
void object::test<6>() {
GEOSGeometry* geom = GEOSGeomFromWKT(
"MULTIPOINT ((1 1), (2 2))");
GEOSGeometry* out = GEOSGeom_transformXY(geom, SCALE_2_3, nullptr);
ensure(out != nullptr);
ensure_geometry_equals(out,
"MULTIPOINT ((2 3), (4 6))");
GEOSGeom_destroy(geom);
GEOSGeom_destroy(out);
}
// callback should modify multi linestring coordinates
template <>
template <>
void object::test<7>() {
GEOSGeometry* geom = GEOSGeomFromWKT(
"MULTILINESTRING ((1 1, 2 2), (3 3, 4 4))");
GEOSGeometry* out = GEOSGeom_transformXY(geom, SCALE_2_3, nullptr);
ensure(out != nullptr);
ensure_geometry_equals(out,
"MULTILINESTRING ((2 3, 4 6), (6 9, 8 12 ))");
GEOSGeom_destroy(geom);
GEOSGeom_destroy(out);
}
// callback should modify multi polygon coordinates
template <>
template <>
void object::test<8>() {
GEOSGeometry* geom = GEOSGeomFromWKT(
"MULTIPOLYGON (((1 1, 1 10, 10 10, 10 1, 1 1), (2 2, 2 4, 4 4, 4 2, 2 2)), ((0 0, 0 100, 100 100, 100 0, 0 0)))");
GEOSGeometry* out = GEOSGeom_transformXY(geom, SCALE_2_3, nullptr);
ensure(out != nullptr);
ensure_geometry_equals(out,
"MULTIPOLYGON (((2 3, 2 30, 20 30, 20 3, 2 3), (4 6, 4 12, 8 12, 8 6, 4 6)), ((0 0, 0 300, 200 300, 200 0, 0 0)))");
GEOSGeom_destroy(geom);
GEOSGeom_destroy(out);
}
// callback should modify geometry collection coordinates
template <>
template <>
void object::test<9>() {
GEOSGeometry* geom = GEOSGeomFromWKT(
"GEOMETRYCOLLECTION (POINT (1 1), LINESTRING (1 1, 2 2), POLYGON ((1 1, 1 2, 2 2, 2 1, 1 1)))");
GEOSGeometry* out = GEOSGeom_transformXY(geom, SCALE_2_3, nullptr);
ensure(out != nullptr);
ensure_geometry_equals(out, "GEOMETRYCOLLECTION (POINT (2 3), LINESTRING (2 3, 4 6), POLYGON ((2 3, 2 6, 4 6, 4 3, 2 3)))");
GEOSGeom_destroy(geom);
GEOSGeom_destroy(out);
}
// should not fail for empty geometry
template <>
template <>
void object::test<10>() {
GEOSGeometry* geom = GEOSGeomFromWKT("POINT EMPTY");
GEOSGeometry* out = GEOSGeom_transformXY(geom, SCALE_2_3, nullptr);
ensure(out != nullptr);
ensure_geometry_equals(out, "POINT EMPTY");
GEOSGeom_destroy(geom);
GEOSGeom_destroy(out);
}
// should retain original coords even if they collapse to same coordinate
template <>
template <>
void object::test<11>() {
GEOSGeometry* geom = GEOSGeomFromWKT("LINESTRING (1 1, 2 2)");
GEOSGeometry* out = GEOSGeom_transformXY(
geom,
[](double* x, double* y, void* userdata) {
*x = 0;
*y = 0;
(void)(userdata); // make unused parameter warning go away
return 1;
},
nullptr);
ensure(out != nullptr);
ensure_equals(GEOSGetNumCoordinates(out), 2);
// Cannot construct WKT for this case, must test coords directly
const GEOSCoordSequence* seq = GEOSGeom_getCoordSeq(out);
double x, y;
GEOSCoordSeq_getXY(seq, 0, &x, &y);
ensure_equals(x, 0);
ensure_equals(y, 0);
GEOSCoordSeq_getXY(seq, 1, &x, &y);
ensure_equals(x, 0);
ensure_equals(y, 0);
GEOSGeom_destroy(geom);
GEOSGeom_destroy(out);
}
// should pass through userdata
template <>
template <>
void object::test<12>() {
GEOSGeometry* geom = GEOSGeomFromWKT("LINESTRING (1 1, 2 2)");
double userdata_scale = 5.0;
GEOSGeometry* out = GEOSGeom_transformXY(
geom,
[](double* x, double* y, void* userdata) {
double scale = *(double *)(userdata);
(*x) *= scale;
(*y) *= scale;
return 1;
},
(void *)(&userdata_scale));
ensure(out != nullptr);
ensure_geometry_equals(out, "LINESTRING (5 5, 10 10)");
GEOSGeom_destroy(geom);
GEOSGeom_destroy(out);
}
// transform should preserve existing Z coordinate values
template <>
template <>
void object::test<13>() {
GEOSGeometry* geom = GEOSGeomFromWKT("POINT Z (1 1 4)");
GEOSGeometry* out = GEOSGeom_transformXY(geom, SCALE_2_3, nullptr);
ensure(out != nullptr);
ensure_geometry_equals(out, "POINT Z (2 3 4)");
GEOSGeom_destroy(geom);
GEOSGeom_destroy(out);
}
template <>
template <>
void object::test<14>() {
input_ = GEOSGeomFromWKT("CIRCULARSTRING (0 0, 1 1, 2 0)");
ensure(input_);
result_ = GEOSGeom_transformXY(input_, SCALE_2_3, nullptr);
ensure_equals(toWKT(result_), "CIRCULARSTRING (0 0, 2 3, 4 0)");
}
} // namespace tut