File: CoverageSimplifierTest.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 (506 lines) | stat: -rw-r--r-- 14,750 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//
// Test Suite for geos::coverage::CoverageSimplifierTest class.

#include <tut/tut.hpp>
#include <utility.h>

// geos
#include <geos/coverage/CoverageSimplifier.h>

using geos::coverage::CoverageSimplifier;

namespace tut {
//
// Test Group
//

// Common data used by all tests
struct test_coveragesimplifier_data {

    WKTReader r;
    WKTWriter w;
    std::vector<std::unique_ptr<Geometry>> geomArray;

    test_coveragesimplifier_data() {
    }

    void checkNoop(
        const std::vector<std::unique_ptr<Geometry>>& input)
    {
        std::vector<std::unique_ptr<Geometry>> actual = CoverageSimplifier::simplify(input, 0);

        // std::cout << w.write(*input[0]) << std::endl;
        // std::cout << w.write(*input[1]) << std::endl;
        // std::cout << "--" << std::endl;
        // std::cout << w.write(*actual[0]) << std::endl;
        // std::cout << w.write(*actual[1]) << std::endl;

        checkArrayEqual(input, actual);
    }

    void checkResult(
        const std::vector<std::unique_ptr<Geometry>>& input,
        double tolerance,
        const std::vector<std::unique_ptr<Geometry>>& expected)
    {
        std::vector<std::unique_ptr<Geometry>> actual = CoverageSimplifier::simplify(input, tolerance);
        checkArrayEqual(expected, actual);
    }

    void checkResultInner(
        const std::vector<std::unique_ptr<Geometry>>& input,
        double tolerance,
        const std::vector<std::unique_ptr<Geometry>>& expected)
    {
        std::vector<std::unique_ptr<Geometry>> actual = CoverageSimplifier::simplifyInner(input, tolerance);
        checkArrayEqual(expected, actual);
    }

    std::vector<std::unique_ptr<Geometry>>
    readArray(std::vector<std::string> wkts)
    {
        std::vector<std::unique_ptr<Geometry>> geometries;
        for (std::string& wkt : wkts) {
            auto geom = r.read(wkt);
            if (geom != nullptr) {
                geometries.push_back(std::move(geom));
            }
        }
        return geometries;
    }

    void
    checkArrayEqual(
        const std::vector<std::unique_ptr<Geometry>>& input,
        const std::vector<std::unique_ptr<Geometry>>& expected)
    {
        ensure("arrays same size", input.size() == expected.size());
        for (std::size_t i = 0; i < input.size(); i++) {
            ensure_equals_geometry(input[i].get(), expected[i].get());
        }
    }

};


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

group test_coveragesimplifier_data("geos::coverage::CoverageSimplifier");


// testNoopSimple2
template<>
template<>
void object::test<1> ()
{
    checkNoop(readArray({
        "POLYGON ((100 100, 200 200, 300 100, 200 101, 100 100))",
        "POLYGON ((150 0, 100 100, 200 101, 300 100, 250 0, 150 0))" })
    );
}

// testNoopSimple3
template<>
template<>
void object::test<2> ()
{
    checkNoop(readArray({
        "POLYGON ((100 300, 200 200, 100 200, 100 300))",
        "POLYGON ((100 200, 200 200, 200 100, 100 100, 100 200))",
        "POLYGON ((100 100, 200 100, 150 50, 100 100))" })
    );
}

// testNoopHole
template<>
template<>
void object::test<3> ()
{
    checkNoop(readArray({
        "POLYGON ((10 90, 90 90, 90 10, 10 10, 10 90), (20 80, 80 80, 80 20, 20 20, 20 80))",
        "POLYGON ((80 20, 20 20, 20 80, 80 80, 80 20))" })
    );
}

// testNoopMulti
template<>
template<>
void object::test<4> ()
{
    checkNoop(readArray({
        "MULTIPOLYGON (((10 10, 10 50, 50 50, 50 10, 10 10)), ((90 90, 90 50, 50 50, 50 90, 90 90)))",
        "MULTIPOLYGON (((10 90, 50 90, 50 50, 10 50, 10 90)), ((90 10, 50 10, 50 50, 90 50, 90 10)))" })
    );
}

//---------------------------------------------

// testSimple2
template<>
template<>
void object::test<5> ()
{
    checkResult(readArray({
        "POLYGON ((100 100, 200 200, 300 100, 200 101, 100 100))",
        "POLYGON ((150 0, 100 100, 200 101, 300 100, 250 0, 150 0))" }),
        10,
        readArray({
            "POLYGON ((100 100, 200 200, 300 100, 100 100))",
            "POLYGON ((150 0, 100 100, 300 100, 250 0, 150 0))" })
    );
}

// testSingleRingNoCollapse
template<>
template<>
void object::test<6> ()
{
    checkResult(readArray({
        "POLYGON ((10 50, 60 90, 70 50, 60 10, 10 50))" }),
        100000,
        readArray({
            "POLYGON ((10 50, 60 90, 60 10, 10 50))" })
    );
}

/**
* Checks that a polygon on the edge of the coverage does not collapse
* under maximal simplification
*/
// testMultiEdgeRingNoCollapse
template<>
template<>
void object::test<7> ()
{
    checkResult(readArray({
        "POLYGON ((50 250, 200 200, 180 170, 200 150, 50 50, 50 250))",
        "POLYGON ((200 200, 180 170, 200 150, 200 200))" }),
        40,
        readArray({
            "POLYGON ((50 250, 200 200, 180 170, 200 150, 50 50, 50 250))",
            "POLYGON ((200 200, 180 170, 200 150, 200 200))" })
    );
}

// testFilledHole
template<>
template<>
void object::test<8> ()
{
    checkResult(readArray({
            "POLYGON ((20 30, 20 80, 60 50, 80 20, 50 20, 20 30))",
            "POLYGON ((10 90, 90 90, 90 10, 10 10, 10 90), (50 20, 20 30, 20 80, 60 50, 80 20, 50 20))"
        }),
        28,
        readArray({
            "POLYGON ((20 30, 20 80, 80 20, 20 30))",
            "POLYGON ((10 10, 10 90, 90 90, 90 10, 10 10), (20 30, 80 20, 20 80, 20 30))"
        })
    );
}

// testTouchingHoles
template<>
template<>
void object::test<9> ()
{
    checkResult(readArray({
            "POLYGON (( 0 0, 0 11, 19 11, 19 0, 0 0 ), ( 4 5, 12 5, 12 6, 10 6, 10 8, 9 8, 9 9, 7 9, 7 8, 6 8, 6 6, 4 6, 4 5 ), ( 12 6, 14 6, 14 9, 13 9, 13 7, 12 7, 12 6 ))",
            "POLYGON (( 12 6, 12 5, 4 5, 4 6, 6 6, 6 8, 7 8, 7 9, 9 9, 9 8, 10 8, 10 6, 12 6 ))",
            "POLYGON (( 12 6, 12 7, 13 7, 13 9, 14 9, 14 6, 12 6 ))" }),
        1.0,
        readArray({
            "POLYGON ((0 0, 0 11, 19 11, 19 0, 0 0), (4 5, 12 5, 12 6, 10 6, 9 9, 6 8, 6 6, 4 5), (12 6, 14 6, 14 9, 12 6))",
            "POLYGON ((4 5, 6 6, 6 8, 9 9, 10 6, 12 6, 12 5, 4 5))",
            "POLYGON ((12 6, 14 9, 14 6, 12 6))"
        })
    );
}

// testHoleTouchingShell
template<>
template<>
void object::test<10> ()
{
    checkResultInner(readArray({
            "POLYGON ((200 300, 300 300, 300 100, 100 100, 100 300, 200 300), (170 220, 170 160, 200 140, 200 250, 170 220), (170 250, 200 250, 200 300, 170 250))",
            "POLYGON ((170 220, 200 250, 200 140, 170 160, 170 220))",
            "POLYGON ((170 250, 200 300, 200 250, 170 250))" }),
        100.0,
        readArray({
            "POLYGON ((100 100, 100 300, 200 300, 300 300, 300 100, 100 100), (170 160, 200 140, 200 250, 170 160), (170 250, 200 250, 200 300, 170 250))",
            "POLYGON ((170 160, 200 250, 200 140, 170 160))",
            "POLYGON ((200 250, 200 300, 170 250, 200 250))" })
    );
}

// testHolesTouchingHolesAndShellInner
template<>
template<>
void object::test<11> ()
{
    checkResultInner(readArray({
            "POLYGON (( 8 5, 9 4, 9 2, 1 2, 1 4, 2 4, 2 5, 1 5, 1 8, 9 8, 9 6, 8 5 ), ( 8 5, 7 6, 6 6, 6 4, 7 4, 8 5 ), ( 7 6, 8 6, 7 7, 7 6 ), ( 6 6, 6 7, 5 6, 6 6 ), ( 6 4, 5 4, 6 3, 6 4 ), ( 7 4, 7 3, 8 4, 7 4 ))" }),
        4.0,
        readArray({
            "POLYGON (( 8 5, 9 4, 9 2, 1 2, 1 4, 2 4, 2 5, 1 5, 1 8, 9 8, 9 6, 8 5 ), ( 8 5, 7 6, 6 6, 6 4, 7 4, 8 5 ), ( 7 6, 8 6, 7 7, 7 6 ), ( 6 6, 6 7, 5 6, 6 6 ), ( 6 4, 5 4, 6 3, 6 4 ), ( 7 4, 7 3, 8 4, 7 4 ))" })
    );
}

// testHolesTouchingHolesAndShell
template<>
template<>
void object::test<12> ()
{
    checkResult(readArray({
            "POLYGON (( 8 5, 9 4, 9 2, 1 2, 1 4, 2 4, 2 5, 1 5, 1 8, 9 8, 9 6, 8 5 ), ( 8 5, 7 6, 6 6, 6 4, 7 4, 8 5 ), ( 7 6, 8 6, 7 7, 7 6 ), ( 6 6, 6 7, 5 6, 6 6 ), ( 6 4, 5 4, 6 3, 6 4 ), ( 7 4, 7 3, 8 4, 7 4 ))" }),
        4.0,
        readArray({
            "POLYGON (( 1 2, 1 8, 9 8, 8 5, 9 2, 1 2 ), ( 5 4, 6 3, 6 4, 5 4 ), ( 5 6, 6 6, 6 7, 5 6 ), ( 6 4, 7 4, 8 5, 7 6, 6 6, 6 4 ), ( 7 3, 8 4, 7 4, 7 3 ), ( 7 6, 8 6, 7 7, 7 6 ))" })
    );
}

// testMultiPolygonWithTouchingShellsInner
template<>
template<>
void object::test<13> ()
{
    checkResultInner(
        readArray({
            "MULTIPOLYGON ((( 2 7, 2 8, 3 8, 3 7, 2 7 )), (( 1 6, 1 7, 2 7, 2 6, 1 6 )), (( 0 7, 0 8, 1 8, 1 7, 0 7 )), (( 0 5, 0 6, 1 6, 1 5, 0 5 )), (( 2 5, 2 6, 3 6, 3 5, 2 5 )))"
        }),
        1.0,
        readArray({
            "MULTIPOLYGON ((( 2 7, 2 8, 3 8, 3 7, 2 7 )), (( 1 6, 1 7, 2 7, 2 6, 1 6 )), (( 0 7, 0 8, 1 8, 1 7, 0 7 )), (( 0 5, 0 6, 1 6, 1 5, 0 5 )), (( 2 5, 2 6, 3 6, 3 5, 2 5 )))"
        })
    );
}

// testMultiPolygonWithTouchingShells
template<>
template<>
void object::test<14> ()
{
    checkResult(
        readArray({
            "MULTIPOLYGON ((( 2 7, 2 8, 3 8, 3 7, 2 7 )), (( 1 6, 1 7, 2 7, 2 6, 1 6 )), (( 0 7, 0 8, 1 8, 1 7, 0 7 )), (( 0 5, 0 6, 1 6, 1 5, 0 5 )), (( 2 5, 2 6, 3 6, 3 5, 2 5 )))" }),
        1.0,
        readArray({
            "MULTIPOLYGON ((( 2 7, 3 8, 3 7, 2 7 )), (( 1 6, 1 7, 2 7, 2 6, 1 6 )), (( 1 7, 0 8, 1 8, 1 7 )), (( 1 6, 0 5, 0 6, 1 6 )), (( 2 6, 3 5, 2 5, 2 6 )))" })
    );
}

// testTouchingShellsInner
template<>
template<>
void object::test<15> ()
{
    checkResultInner(readArray({
            "POLYGON ((0 0, 0 5, 5 6, 10 5, 10 0, 0 0))",
            "POLYGON ((0 10, 5 6, 10 10, 0 10))" }),
        4.0,
        readArray({
            "POLYGON ((0 0, 0 5, 5 6, 10 5, 10 0, 0 0))",
            "POLYGON ((0 10, 5 6, 10 10, 0 10))" })
    );
}

// testShellSimplificationAtStartingNode
template<>
template<>
void object::test<16> ()
{
    checkResult(readArray({
            "POLYGON (( 1 5, 1 7, 5 7, 5 3, 2 3, 1 5 ))" }),
        1.5,
        readArray({
            "POLYGON ((1 7, 5 7, 5 3, 2 3, 1 7))" })
    );
}

// testSimplifyInnerAtStartingNode
template<>
template<>
void object::test<17> ()
{
    checkResultInner(readArray({
        "POLYGON (( 0 5, 0 9, 6 9, 6 2, 1 2, 0 5 ), ( 1 5, 2 3, 5 3, 5 7, 1 7, 1 5 ))",
            "POLYGON (( 1 5, 1 7, 5 7, 5 3, 2 3, 1 5 ))" }),
        1.5,
        readArray({
            "POLYGON ((0 5, 0 9, 6 9, 6 2, 1 2, 0 5), (1 7, 2 3, 5 3, 5 7, 1 7))",
            "POLYGON ((1 7, 5 7, 5 3, 2 3, 1 7))" })
    );
}

// testSimplifyAllAtStartingNode
template<>
template<>
void object::test<18> ()
{
    checkResult(readArray({
            "POLYGON (( 0 5, 0 9, 6 9, 6 2, 1 2, 0 5 ), ( 1 5, 2 3, 5 3, 5 7, 1 7, 1 5 ))",
            "POLYGON (( 1 5, 1 7, 5 7, 5 3, 2 3, 1 5 ))" }),
        1.5,
        readArray({
            "POLYGON ((0 9, 6 9, 6 2, 1 2, 0 9), (1 7, 2 3, 5 3, 5 7, 1 7))",
            "POLYGON ((1 7, 5 7, 5 3, 2 3, 1 7))" })
    );
}

//---------------------------------
// testInnerSimple
template<>
template<>
void object::test<19> ()
{
    checkResultInner(readArray({
        "POLYGON ((50 50, 50 150, 100 190, 100 200, 200 200, 160 150, 120 120, 90 80, 50 50))",
        "POLYGON ((100 0, 50 50, 90 80, 120 120, 160 150, 200 200, 250 100, 170 50, 100 0))" }),
        100,
        readArray({
            "POLYGON ((50 50, 50 150, 100 190, 100 200, 200 200, 50 50))",
            "POLYGON ((200 200, 50 50, 100 0, 170 50, 250 100, 200 200))" })
    );

}

// testRepeatedPointRemoved
template<>
template<>
void object::test<20> ()
{
    checkResult(readArray({
        "POLYGON ((5 9, 6.5 6.5, 9 5, 5 5, 5 5, 5 9))" }),
        2,
        readArray({
            "POLYGON ((5 5, 5 9, 9 5, 5 5))" })
    );
}

// testRepeatedPointCollapseToLine
template<>
template<>
void object::test<21> ()
{
checkResult(readArray({
    "MULTIPOLYGON (((10 10, 10 20, 20 19, 30 20, 30 10, 10 10)), ((10 30, 20 29, 30 30, 30 20, 20 19, 10 20, 10 30)), ((10 20, 20 19, 20 19, 10 20)))" }),
    5,
    readArray({
        "MULTIPOLYGON (((10 20, 20 19, 30 20, 30 10, 10 10, 10 20)), ((30 20, 20 19, 10 20, 10 30, 30 30, 30 20)), ((10 20, 20 19, 10 20)))" })
);
}

// testRepeatedPointCollapseToPoint()
template<>
template<>
void object::test<22> ()
{
    checkResult(readArray({
        "MULTIPOLYGON (((10 10, 10 20, 20 19, 30 20, 30 10, 10 10)), ((10 30, 20 29, 30 30, 30 20, 20 19, 10 20, 10 30)), ((20 19, 20 19, 20 19)))" }),
        5,
        readArray({
            "MULTIPOLYGON (((10 10, 10 20, 20 19, 30 20, 30 10, 10 10)), ((10 20, 10 30, 30 30, 30 20, 20 19, 10 20)), ((20 19, 20 19, 20 19)))" })
    );
}

// testRepeatedPointCollapseToPoint2
template<>
template<>
void object::test<23> ()
{
    checkResult(readArray({
        "MULTIPOLYGON (((100 200, 150 195, 200 200, 200 100, 100 100, 100 200)), ((150 195, 150 195, 150 195, 150 195)))" }),
        40,
        readArray({
            "MULTIPOLYGON (((150 195, 200 200, 200 100, 100 100, 100 200, 150 195)), ((150 195, 150 195, 150 195, 150 195)))" })
    );
}

// testAllEmpty()
template<>
template<>
void object::test<24> ()
{
    checkResult(readArray({
        "POLYGON EMPTY",
        "POLYGON EMPTY" }),
        1,
        readArray({
            "POLYGON EMPTY",
            "POLYGON EMPTY" })
    );
}

// testOneEmpty
template<>
template<>
void object::test<25> ()
{
    checkResult(readArray({
        "POLYGON ((1 9, 5 9.1, 9 9, 9 1, 1 1, 1 9))",
        "POLYGON EMPTY" }),
        1,
        readArray({
            "POLYGON ((1 9, 9 9, 9 1, 1 1, 1 9))",
            "POLYGON EMPTY" })
    );
}

// testEmptyHole()
template<>
template<>
void object::test<26> ()
{
    checkResult(readArray({
        "POLYGON ((1 9, 5 9.1, 9 9, 9 1, 1 1, 1 9), EMPTY)",
        "POLYGON EMPTY"  }),
        1,
        readArray({
            "POLYGON ((1 9, 9 9, 9 1, 1 1, 1 9), EMPTY)",
            "POLYGON EMPTY" })
    );
}

// Test non-polygon inputs (GH-1031)
template<>
template<>
void object::test<30> ()
{
    auto input = readArray({
        "MULTILINESTRING ((200 100, 100 100, 200 200), (200 200, 200 100), (200 200, 300 100, 200 100))"
        });
    try {
        std::vector<std::unique_ptr<Geometry>> result =
        CoverageSimplifier::simplify(input, 10);
    }
    catch (geos::util::IllegalArgumentException&) {
        ensure("caught IllegalArgumentException", true);
        return;
    }
    ensure("did not throw IllegalArgumentException", false);
}


// Test non-polygon inputs (GH-1250)
template<>
template<>
void object::test<31> ()
{
    auto input = readArray({
        "GEOMETRYCOLLECTION (POLYGON ((0 10, 10 10, 10 7, 10 3, 10 0, 0 0, 0 10)), GEOMETRYCOLLECTION (POLYGON ((10 10, 20 10, 20 0, 10 0, 10 3, 10 7, 10 10))))"
        });
    try {
        std::vector<std::unique_ptr<Geometry>> result =
        CoverageSimplifier::simplify(input, 10);
    }
    catch (geos::util::IllegalArgumentException&) {
        ensure("caught IllegalArgumentException", true);
        return;
    }
    ensure("did not throw IllegalArgumentException", false);
}


} // namespace tut