File: GEOSClipByRectTest.cpp

package info (click to toggle)
geos 3.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,212 kB
  • sloc: cpp: 199,103; xml: 56,065; ansic: 6,162; sh: 287; makefile: 26
file content (366 lines) | stat: -rw-r--r-- 8,190 bytes parent folder | download
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//
// Test Suite for C-API GEOSClipByRect

#include <tut/tut.hpp>
// geos
#include <geos_c.h>
// std
#include <cstdio>
#include <cstdlib>

#include "capi_test_utils.h"

namespace tut {
//
// Test Group
//

// Common data used in test cases.
struct test_capigeosclipbyrect_data : public capitest::utility {

    void
    checkClipByRect(
        const char* wktInput,
        double xmin, double ymin,
        double xmax, double ymax,
        const char* wktExpected)
    {
        input_ = fromWKT(wktInput);
        result_ = GEOSClipByRect(input_, xmin, ymin, xmax, ymax);
        expected_ = fromWKT(wktExpected);

        bool equal = GEOSEquals(result_, expected_) == 1;
        if (!equal) {
            wkt_ = GEOSWKTWriter_write(wktw_, expected_);
            std::printf("EXP: %s\n", wkt_);
            GEOSFree(wkt_);
            wkt_ = GEOSWKTWriter_write(wktw_, result_);
            std::printf("OBT: %s\n", wkt_);
        }
        ensure(equal);
    }

    void
    checkClipByRectIdentical(
        const char* wktInput,
        double xmin, double ymin,
        double xmax, double ymax,
        const char* wktExpected)
    {
        input_ = fromWKT(wktInput);
        result_ = GEOSClipByRect(input_, xmin, ymin, xmax, ymax);
        expected_ = fromWKT(wktExpected);

        bool equal =  GEOSEqualsIdentical(result_, expected_) == 1;
        if (!equal) {
            wkt_ = GEOSWKTWriter_write(wktw_, expected_);
            std::printf("EXP: %s\n", wkt_);
            GEOSFree(wkt_);
            wkt_ = GEOSWKTWriter_write(wktw_, result_);
            std::printf("OBT: %s\n", wkt_);
        }
        ensure(equal);
    }
};

typedef test_group<test_capigeosclipbyrect_data> group;
typedef group::object object;

group test_capigeosclipbyrect_group("capi::GEOSClipByRect");

//
// Test Cases
//

/// Point outside
template<>
template<>
void object::test<1>()
{
    checkClipByRect(
        "POINT(0 0)",
        10, 10, 20, 20,
        "POINT EMPTY"
        );
}

/// Point inside
template<>
template<>
void object::test<2>()
{
    checkClipByRectIdentical(
        "POINT(15 15)",
        10, 10, 20, 20,
        "POINT(15 15)"
        );
}

/// Point on boundary
template<>
template<>
void object::test<3>()
{
    checkClipByRect(
        "POINT(15 10)",
        10, 10, 20, 20,
        "POINT EMPTY"
        );
}

/// Line outside
template<>
template<>
void object::test<4>()
{
    checkClipByRect(
        "LINESTRING(0 0, -5 5)",
        10, 10, 20, 20,
        "LINESTRING EMPTY"
        );
}

/// Line inside
template<>
template<>
void object::test<5>()
{
    checkClipByRectIdentical(
        "LINESTRING(15 15, 16 15)",
        10, 10, 20, 20,
        "LINESTRING(15 15, 16 15)"
        );
}

/// Line on boundary
template<>
template<>
void object::test<6>()
{
    checkClipByRect(
        "LINESTRING(10 15, 10 10, 15 10)",
        10, 10, 20, 20,
        "LINESTRING EMPTY"
        );
}

/// Line splitting rectangle
template<>
template<>
void object::test<7>()
{
    checkClipByRectIdentical(
        "LINESTRING(10 5, 25 20)",
        10, 10, 20, 20,
        "LINESTRING (15 10, 20 15)"
        );
}

/// Polygon shell (CCW) fully on rectangle boundary
template<>
template<>
void object::test<8>()
{
    checkClipByRect(
        "POLYGON((10 10, 20 10, 20 20, 10 20, 10 10))",
        10, 10, 20, 20,
        "POLYGON((10 10, 20 10, 20 20, 10 20, 10 10))"
        );
}

/// Polygon shell (CW) fully on rectangle boundary
template<>
template<>
void object::test<9>()
{
    checkClipByRect(
        "POLYGON((10 10, 10 20, 20 20, 20 10, 10 10))",
        10, 10, 20, 20,
        "POLYGON((10 10, 20 10, 20 20, 10 20, 10 10))"
        );
}

/// Polygon hole (CCW) fully on rectangle boundary
template<>
template<>
void object::test<10>()
{
    checkClipByRect(
        "POLYGON((0 0, 0 30, 30 30, 30 0, 0 0),(10 10, 20 10, 20 20, 10 20, 10 10))",
        10, 10, 20, 20,
        "POLYGON EMPTY"
        );
}

/// Polygon hole (CW) fully on rectangle boundary
template<>
template<>
void object::test<11>()
{
    checkClipByRect(
        "POLYGON((0 0, 0 30, 30 30, 30 0, 0 0),(10 10, 10 20, 20 20, 20 10, 10 10))",
        10, 10, 20, 20,
        "POLYGON EMPTY"
        );
}

/// Polygon fully within rectangle
template<>
template<>
void object::test<12>()
{
    const char* wkt = "POLYGON((1 1, 1 30, 30 30, 30 1, 1 1),(10 10, 20 10, 20 20, 10 20, 10 10))";
    checkClipByRectIdentical(
        wkt,
        0, 0, 40, 40,
        wkt);
}

/// Polygon overlapping rectangle
template<>
template<>
void object::test<13>()
{
    checkClipByRectIdentical(
        "POLYGON((0 0, 0 30, 30 30, 30 0, 0 0),(10 10, 20 10, 20 20, 10 20, 10 10))",
        5, 5, 15, 15,
        "POLYGON ((5 5, 5 15, 10 15, 10 10, 15 10, 15 5, 5 5))"
        );
}

/// Clipping invalid polygon
template<>
template<>
void object::test<14>
()
{
    geom1_ = fromWKT("POLYGON((1410 2055, 1410 2056, 1410 2057, 1410 2055))");
    geom2_ = GEOSClipByRect(geom1_, -8, -8, 2056, 2056);
    if (geom2_ != nullptr) {
        wkt_ = GEOSWKTWriter_write(wktw_, geom2_);
        std::printf("OBT: %s\n", wkt_);
    }
    ensure(nullptr == geom2_);
}

// Polygon fully covering rectangle
// https://trac.osgeo.org/postgis/ticket/4904
template<>
template<>
void object::test<15>
()
{
    //  POLYGON((0 0,10 0,10 10,0 10))
    //  Clip by ST_MakeEnvelope(2,2,5,5)
    GEOSCoordSequence *cs = GEOSCoordSeq_create(5, 2);
    GEOSCoordSeq_setXY(cs, 0,  0,  0);
    GEOSCoordSeq_setXY(cs, 1, 10,  0);
    GEOSCoordSeq_setXY(cs, 2, 10, 10);
    GEOSCoordSeq_setXY(cs, 3,  0, 10);
    GEOSCoordSeq_setXY(cs, 4,  0,  0);
    GEOSGeometry *shell = GEOSGeom_createLinearRing(cs); // take ownership of cs
    input_ = GEOSGeom_createPolygon(shell, NULL, 0); // take ownership of shell
    result_ = GEOSClipByRect(input_, 2, 2, 5, 5);
    expected_ = GEOSGeomFromWKT("POLYGON ((2 2, 2 5, 5 5, 5 2, 2 2))");
    ensure_geometry_equals(result_, expected_);
}

/// Empty combinations - always return GEOMETRYCOLLECTION EMPTY
template<>
template<>
void object::test<16>
()
{
    std::vector<const char*> variants{
        "POINT EMPTY",
        "LINESTRING EMPTY",
        "POLYGON EMPTY",
        "MULTIPOINT EMPTY",
        "MULTILINESTRING EMPTY",
        "MULTIPOLYGON EMPTY",
        "GEOMETRYCOLLECTION EMPTY",
        "LINEARRING EMPTY",
    };
    for (const auto& wkt : variants) {
        GEOSGeometry* a = fromWKT(wkt);
        GEOSGeometry* b = GEOSClipByRect(a, 0, 0, 1, 1);
        ensure("GEOS_GEOMETRYCOLLECTION", GEOSGeomTypeId(b) == GEOS_GEOMETRYCOLLECTION);
        GEOSGeom_destroy(a);
        GEOSGeom_destroy(b);
    }
}

template<>
template<>
void object::test<17>()
{
    input_ = fromWKT("CIRCULARSTRING (0 0, 1 1, 2 0)");
    ensure(input_ != nullptr);

    result_ = GEOSClipByRect(input_, 0, 0, 1, 1);
    ensure(result_ == nullptr);
}

/// Point Z inside
template<>
template<>
void object::test<18>()
{
    checkClipByRectIdentical(
        "POINT Z (15 15 100)",
        10, 10, 20, 20,
        "POINT Z (15 15 100)"
        );
}

/// Line Z inside
template<>
template<>
void object::test<19>()
{
    checkClipByRectIdentical(
        "LINESTRING Z (15 15 0, 16 15 100)",
        10, 10, 20, 20,
        "LINESTRING Z (15 15 0, 16 15 100)"
        );
}

/// Line Z splitting rectangle
template<>
template<>
void object::test<20>()
{
    checkClipByRectIdentical(
        "LINESTRING Z (0 15 0, 100 15 100)",
        10, 10, 20, 20,
        "LINESTRING Z (10 15 10, 20 15 20)"
        );
}

/// Polygon Z overlapping rectangle
template<>
template<>
void object::test<21>()
{
    checkClipByRectIdentical(
        "POLYGON Z ((0 0 100, 0 30 100, 30 30 100, 30 0 100, 0 0 100),(10 10 100, 20 10 100, 20 20 100, 10 20 100, 10 10 100))",
        5, 5, 15, 15,
        "POLYGON Z ((5 5 100, 5 15 100, 10 15 100, 10 10 100, 15 10 100, 15 5 100, 5 5 100))"
        );
}

/// Polygon Z enclosing rectangle
template<>
template<>
void object::test<22>()
{
    checkClipByRectIdentical(
        "POLYGON Z ((0 0 100, 0 30 100, 30 30 100, 30 0 100, 0 0 100))",
        5, 5, 15, 15,
        "POLYGON Z ((5 5 100, 5 15 100, 15 15 100, 15 5 100, 5 5 100))"
        );
}


} // namespace tut