File: DouglasPeuckerSimplifierTest.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 (352 lines) | stat: -rw-r--r-- 10,131 bytes parent folder | download | duplicates (2)
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
//
// Test Suite for geos::simplify::DouglasPeuckerSimplifierTest

#include <tut/tut.hpp>
#include <utility.h>
// geos
#include <geos/io/WKTReader.h>
#include <geos/io/WKTWriter.h>
#include <geos/geom/PrecisionModel.h>
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/Geometry.h>
#include <geos/geom/Coordinate.h>
#include <geos/geom/CoordinateSequenceFilter.h>
#include <geos/simplify/DouglasPeuckerSimplifier.h>
#include <geos/util.h>
// std
#include <string>
#include <memory>

namespace tut {
using namespace geos::simplify;

//
// Test Group
//

// Common data used by tests
struct test_dpsimp_data {
    geos::io::WKTReader wktreader;
    geos::io::WKTWriter wktwriter;

    typedef geos::geom::Geometry::Ptr GeomPtr;

    test_dpsimp_data()
        :
        wktreader()
    {}

    void
    checkDP(const std::string& wkt, double tolerance, const std::string& wkt_expected)
    {
        GeomPtr g(wktreader.read(wkt));
        GeomPtr simplified = DouglasPeuckerSimplifier::simplify(g.get(), tolerance);

        ensure("Simplified geometry is invalid!", simplified->isValid());

        GeomPtr exp(wktreader.read(wkt_expected));
        ensure_equals_geometry(exp.get(), simplified.get());
    }

    void
    checkDPNoChange(const std::string& wkt, double tolerance)
    {
        checkDP(wkt, tolerance, wkt);
    }
};

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

group test_dpsimp_group("geos::simplify::DouglasPeuckerSimplifier");

//
// Test Cases
//

// 1 - testPolygonWithFlatVertices
template<>
template<>
void object::test<1>()
{
    checkDP("POLYGON ((20 220, 40 220, 60 220, 80 220, 100 220, 120 220, 140 220, 140 180, 100 180, 60 180, 20 180, 20 220))",
        10.0,
        "POLYGON ((20 220, 140 220, 140 180, 20 180, 20 220))");
}

// 2 - PolygonReductionWithSplit
template<>
template<>
void object::test<2>()
{
    checkDP("POLYGON ((40 240, 160 241, 280 240, 280 160, 160 240, 40 140, 40 240))",
        1,
        "MULTIPOLYGON (((40 240, 160 240, 40 140, 40 240)), ((160 240, 280 240, 280 160, 160 240)))");
}

// 3 - PolygonReduction
template<>
template<>
void object::test<3>()
{
    checkDP("POLYGON ((120 120, 121 121, 122 122, 220 120, 180 199, 160 200, 140 199, 120 120))",
        10,
        "POLYGON ((120 120, 220 120, 180 199, 160 200, 140 199, 120 120))");
}

// 4 - PolygonWithTouchingHole
template<>
template<>
void object::test<4>()
{
    checkDP("POLYGON ((80 200, 240 200, 240 60, 80 60, 80 200), (120 120, 220 120, 180 199, 160 200, 140 199, 120 120))",
        10,
        "POLYGON ((80 200, 240 200, 240 60, 80 60, 80 200), (120 120, 220 120, 180 199, 160 200, 140 199, 120 120))");
}

// 5 - FlattishPolygon
template<>
template<>
void object::test<5>()
{
    checkDP("POLYGON ((0 0, 50 0, 53 0, 55 0, 100 0, 70 1,  60 1, 50 1, 40 1, 0 0))",
        10,
        "POLYGON EMPTY");
}

// 6 - TinySquare
template<>
template<>
void object::test<6>()
{
    checkDP("POLYGON ((0 5, 5 5, 5 0, 0 0, 0 1, 0 5))",
        10,
        "POLYGON EMPTY");
}

// TinyHole
template<>
template<>
void object::test<7>()
{
    checkDP("POLYGON ((10 10, 10 310, 370 310, 370 10, 10 10), (160 190, 180 190, 180 170, 160 190))",
        30,
        "POLYGON ((10 10, 10 310, 370 310, 370 10, 10 10))");
}

// 7 - TinyLineString
template<>
template<>
void object::test<8>()
{
    checkDP("LINESTRING (0 5, 1 5, 2 5, 5 5)",
        10,
        "LINESTRING (0 5, 5 5)");
}

// 8 - MultiPoint
template<>
template<>
void object::test<9>()
{
    checkDPNoChange("MULTIPOINT(80 200, 240 200, 240 60, 80 60, 80 200, 140 199, 120 120)",
        10);
}

// 9 - MultiLineString
template<>
template<>
void object::test<10>()
{
    checkDP("MULTILINESTRING((0 0, 50 0, 70 0, 80 0, 100 0), (0 0, 50 1, 60 1, 100 0) )",
        10,
        "MULTILINESTRING ((0 0, 100 0), (0 0, 100 0))");
}

// 10 - GeometryCollection
template<>
template<>
void object::test<11>()
{
    checkDPNoChange("GEOMETRYCOLLECTION (MULTIPOINT (80 200, 240 200, 240 60, 80 60, 80 200, 140 199, 120 120), POLYGON ((80 200, 240 200, 240 60, 80 60, 80 200)), LINESTRING (80 200, 240 200, 240 60, 80 60, 80 200, 140 199, 120 120))",
      10.0);
}

// 11 - A kind of reversed simplification
template<>
template<>
void object::test<12>()
{
    using namespace geos::geom;

    std::string
    wkt("MULTIPOLYGON(((0.561648 1,1 1,1 0,0.468083 0,0.52758 0.00800554,0.599683 0.0280924, 0.601611 0.265374, \
                        0.622693 0.316765,0.69507 0.357497,0.695623 0.429711,0.655111 0.502298, 0.696467 0.543147,0.840712 0.593546, \
                        0.882583 0.66546,0.852357 0.748213,0.84264 0.789567,0 .832667,0.832667 0.841202,0.740538 0.873004, \
                        0.617349 0.905045,0.566576 0.977697,0.561648 1)),((0 0.801979,0.0308575 0.786234,0.0705513 0.631135, \
                        0.141616 0.527248,0.233985 0.505872,0.264777 0.526263,0.336631 0.505009,0.356603 0.422321,0.355803 0.350038, \
                        0.375252 0.205364,0.415206 0.0709182,0.45479 0,0 0,0 0,0 0.801979)))");

    GeomPtr g(wktreader.read(wkt));
    std::size_t const gN = g->getNumPoints();
    ensure_equals(gN, std::size_t(37));

    // 1) Simplify with 1/2048
    double const d1 = 1 / 2048.0;
    GeomPtr simplified1 = DouglasPeuckerSimplifier::simplify(g.get(), d1);
    ensure(simplified1->isValid());
    ensure(simplified1->equals(g.get()));
    std::size_t const simplifiedN1 = simplified1->getNumPoints();
    ensure_equals(simplifiedN1, std::size_t(36));
    //std::string const simplifiedWkd = wktwriter.write(simplified1.get());

    // 2) Multiply points by 2047
    struct Multiplier : public CoordinateSequenceFilter {
        double f;
        Multiplier(double p_f) : f(p_f) {}
        void
        filter_rw(CoordinateSequence& seq, std::size_t i) override
        {
            seq.setOrdinate(i, CoordinateSequence::X, seq[i].x * f);
            seq.setOrdinate(i, CoordinateSequence::Y, seq[i].y * f);
        }
        void
        filter_ro(const CoordinateSequence& seq, std::size_t i) override
        {
            ::geos::ignore_unused_variable_warning(seq);
            ::geos::ignore_unused_variable_warning(i);
        }
        bool
        isDone() const override
        {
            return false;
        }
        bool
        isGeometryChanged() const override
        {
            return true;
        }
    };

    Multiplier m(2047);
    g->apply_rw(m);
    std::size_t const multipliedN = g->getNumPoints();
    ensure_equals(multipliedN, std::size_t(37));
    //std::string const multipliedWkt = wktwriter.write(g.get());

    // 3) Simplify with 1.0
    double const d2 = 1.0;
    GeomPtr simplified2 = DouglasPeuckerSimplifier::simplify(g.get(), d2);
    ensure(simplified2->isValid());
    ensure(simplified2->equals(g.get()));
    std::size_t const simplifiedN2 = simplified2->getNumPoints();
    ensure_equals(simplifiedN2, std::size_t(36));
    //std::string const simplifiedWkt2 = wktwriter.write(simplified2.get());
}

// 13 - Polygon with inner ring whose extent is less than the simplify distance (#741)
template<>
template<>
void object::test<13>()
{
    checkDP(
        "POLYGON ((0 0,0 1,1 1,0 0),(0.1 0.1,0.2 0.1,0.2 0.2,0.1 0.1))",
        0.5,
        "POLYGON ((0 0,0 1,1 1,0 0))"
        );
}

/**
* Test that a polygon made invalid by simplification
* is fixed in a sensible way.
* Fixed by buffer(0) area-base orientation
* See https://github.com/locationtech/jts/issues/498
*/
template<>
template<>
void object::test<14>()
{
    checkDP(
        "POLYGON ((21.32686 47.78723, 21.32386 47.79023, 21.32186 47.80223, 21.31486 47.81023, 21.32786 47.81123, 21.33986 47.80223, 21.33886 47.81123, 21.32686 47.82023, 21.32586 47.82723, 21.32786 47.82323, 21.33886 47.82623, 21.34186 47.82123, 21.36386 47.82223, 21.40686 47.81723, 21.32686 47.78723))",
        0.0036,
        "POLYGON ((21.32686 47.78723, 21.31486 47.81023, 21.32786 47.81123, 21.33986 47.80223, 21.328068201892744 47.823286782334385, 21.33886 47.82623, 21.34186 47.82123, 21.40686 47.81723, 21.32686 47.78723))"
        );
}

  /**
   * Test that a collapsed polygon is removed.
   * Tests regression caused by unported JTS code.
   *
   * See https://trac.osgeo.org/geos/ticket/1115
   */
template<>
template<>
void object::test<15>()
{
    checkDP(
        "MULTIPOLYGON (((-76.02716827 36.55671692, -75.99866486 36.55665207, -75.91191864 36.54253006, -75.92480469 36.47397614, -75.97727966 36.4780159, -75.97628784 36.51792526, -76.02716827 36.55671692)), ((-75.90198517 36.55619812, -75.8781662 36.55587387, -75.77315521 36.22925568, -75.78317261 36.22519302, -75.90198517 36.55619812)))",
        0.05,
        "POLYGON ((-76.02716827 36.55671692, -75.91191864 36.54253006, -75.92480469 36.47397614, -76.02716827 36.55671692))"
        );
}

// Test that start point of a polygon can be removed
template<>
template<>
void object::test<16>()
{
    checkDP("POLYGON ((1 0, 2 0, 2 2, 0 2, 0 0, 1 0))",
        0,
        "POLYGON (( 0 0, 2 0, 2 2, 0 2, 0 0))");
}

// Test that start point of a closed LineString is not changed
template<>
template<>
void object::test<17>()
{
    checkDPNoChange("LINESTRING (1 0, 2 0, 2 2, 0 2, 0 0, 1 0)",
        0);
}

// testPolygonRemoveFlatEndpoint
// see https://trac.osgeo.org/geos/ticket/1064
template<>
template<>
void object::test<18>()
{
    checkDP(
      "POLYGON ((42 42, 0 42, 0 100, 42 100, 100 42, 42 42))",
        1,
        "POLYGON ((100 42, 0 42, 0 100, 42 100, 100 42))"
        );
}

// testPolygonEndpointCollapse
template<>
template<>
void object::test<19>()
{
    checkDP(
        "POLYGON ((5 2, 9 1, 1 1, 5 2))",
        1,
        "POLYGON EMPTY"
        );
}

// https://github.com/libgeos/geos/issues/1078
template<>
template<>
void object::test<20>()
{
    auto gfact = GeometryFactory::getDefaultInstance();
    auto pt = gfact->createPoint(Coordinate{0,  0});

    try {
        DouglasPeuckerSimplifier::simplify(pt.get(), std::numeric_limits<double>::quiet_NaN());
        fail("Exception not thrown.");
    } catch (const geos::util::IllegalArgumentException&) {
    }
}

} // namespace tut