File: LengthIndexedLineTest.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 (531 lines) | stat: -rw-r--r-- 13,857 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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//
// Ported from JTS junit/linearref/AbstractIndexedLineTestCase.java r466
// and  junit/linearref/LengthIndexedLineTestCase.java r466

#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> // required for use in unique_ptr
#include <geos/geom/LineString.h>
#include <geos/geom/Coordinate.h>
#include <geos/linearref/LengthIndexedLine.h>

// std
#include <cmath>
#include <string>
#include <memory>

using namespace geos::geom;
using namespace geos::linearref;
using namespace std;

/**
 * Tests the {@link LocationIndexedLine} class
 */
namespace tut {

typedef unique_ptr<Geometry> GeomPtr;
static const double TOLERANCE_DIST = 0.001;

struct test_lengthindexedline_data {
    test_lengthindexedline_data()
        : reader(), writer()
    {
    }

    geos::io::WKTReader reader;
    geos::io::WKTWriter writer;

    void
    checkExpected(Geometry* result, string const& expected)
    {
        GeomPtr subLine(reader.read(expected));
        checkExpected(result, subLine.get());
    }

    void
    checkExpected(Geometry* result, const Geometry* expected)
    {
        bool isEqual = result->equalsExact(expected, 1.0e-5);
        ensure_equals("Expect: " + writer.write(expected) + " Obtained: " + writer.write(result), isEqual, true);
    }

    void
    runIndicesOfThenExtract(string const& inputStr, string const& subLineStr)
    {
        GeomPtr input(reader.read(inputStr));
        GeomPtr subLine(reader.read(subLineStr));
        GeomPtr result(indicesOfThenExtract(input.get(), subLine.get()));

        checkExpected(result.get(), subLine.get());
    }

    bool
    indexOfAfterCheck(Geometry* linearGeom, Coordinate testPt)
    {
        LengthIndexedLine indexedLine(linearGeom);

        // check locations are consecutive
        double loc1 = indexedLine.indexOf(testPt);
        double loc2 = indexedLine.indexOfAfter(testPt, loc1);
        if(loc2 <= loc1) {
            return false;
        }

        // check extracted points are the same as the input
        Coordinate pt1 = indexedLine.extractPoint(loc1);
        Coordinate pt2 = indexedLine.extractPoint(loc2);
        if(! pt1.equals2D(testPt)) {
            return false;
        }
        if(! pt2.equals2D(testPt)) {
            return false;
        }

        return true;
    }

    void
    runIndexOfAfterTest(string const& inputStr, string const& testPtWKT)
    {
        GeomPtr input(reader.read(inputStr));
        GeomPtr testPoint(reader.read(testPtWKT));
        const Coordinate testPt(*testPoint->getCoordinate());
        bool resultOK = indexOfAfterCheck(input.get(), testPt);
        ensure(resultOK);
    }

    void
    runOffsetTest(string const& inputWKT, string const& testPtWKT,
                  double offsetDistance, string const& expectedPtWKT)
    {
        GeomPtr input(reader.read(inputWKT));
        GeomPtr testPoint(reader.read(testPtWKT));
        GeomPtr expectedPoint(reader.read(expectedPtWKT));
        const Coordinate testPt(*testPoint->getCoordinate());
        const Coordinate expectedPt(*expectedPoint->getCoordinate());
        Coordinate offsetPt = extractOffsetAt(input.get(), testPt, offsetDistance);

        bool isOk = offsetPt.distance(expectedPt) < TOLERANCE_DIST;
        if(! isOk) {
            cout << "Expected = " << *expectedPoint << "  Actual = " << offsetPt << endl;
        }
        ensure(isOk);
    }

    Coordinate
    extractOffsetAt(Geometry* linearGeom, Coordinate testPt, double offsetDistance)
    {
        LengthIndexedLine indexedLine(linearGeom);
        double index = indexedLine.indexOf(testPt);
        return indexedLine.extractPoint(index, offsetDistance);
    }

    void
    checkExtractLine(const char* wkt, double start, double end, const char* expected)
    {
        string wktstr(wkt);
        GeomPtr linearGeom(reader.read(wktstr));
        LengthIndexedLine indexedLine(linearGeom.get());
        GeomPtr result(indexedLine.extractLine(start, end));
        checkExpected(result.get(), expected);
    }


    std::unique_ptr<Geometry>
    indicesOfThenExtract(Geometry* linearGeom, Geometry* subLine)
    {
        LengthIndexedLine indexedLine(linearGeom);
        double* loc = indexedLine.indicesOf(subLine);
        auto result = indexedLine.extractLine(loc[0], loc[1]);
        delete [] loc;
        return result;
    }

}; // struct test_lengthindexedline_data

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

group test_lengthindexedline_group("geos::linearref::LengthIndexedLine");

// testML
template<>
template<>
void object::test<1>
()
{
    runIndicesOfThenExtract("MULTILINESTRING ((0 0, 10 10), (20 20, 30 30))",
                            "MULTILINESTRING ((1 1, 10 10), (20 20, 25 25))");
}

// testPartOfSegmentNoVertex
template<>
template<>
void object::test<2>
()
{
    runIndicesOfThenExtract("LINESTRING (0 0, 10 10, 20 20)",
                            "LINESTRING (1 1, 9 9)");
}

// testPartOfSegmentContainingVertex()
template<>
template<>
void object::test<3>
()
{
    runIndicesOfThenExtract("LINESTRING (0 0, 10 10, 20 20)",
                            "LINESTRING (5 5, 10 10, 15 15)");
}

/**
 * Tests that duplicate coordinates are handled correctly.
 *
 * @throws Exception
 */
// testPartOfSegmentContainingDuplicateCoords
template<>
template<>
void object::test<4>
()
{
    runIndicesOfThenExtract("LINESTRING (0 0, 10 10, 10 10, 20 20)",
                            "LINESTRING (5 5, 10 10, 10 10, 15 15)");
}

/**
 * Following tests check that correct portion of loop is identified.
 * This requires that the correct vertex for (0,0) is selected.
 */

// testLoopWithStartSubLine
template<>
template<>
void object::test<5>
()
{
    runIndicesOfThenExtract("LINESTRING (0 0, 0 10, 10 10, 10 0, 0 0)",
                            "LINESTRING (0 0, 0 10, 10 10)");
}

// testLoopWithEndingSubLine()
template<>
template<>
void object::test<6>
()
{
    runIndicesOfThenExtract("LINESTRING (0 0, 0 10, 10 10, 10 0, 0 0)",
                            "LINESTRING (10 10, 10 0, 0 0)");
}

// test a subline equal to the parent loop
// testLoopWithIdenticalSubLine()
template<>
template<>
void object::test<7>
()
{
    runIndicesOfThenExtract("LINESTRING (0 0, 0 10, 10 10, 10 0, 0 0)",
                            "LINESTRING (0 0, 0 10, 10 10, 10 0, 0 0)");
}

// test a zero-length subline equal to the start point
// testZeroLenSubLineAtStart()
template<>
template<>
void object::test<8>
()
{
    runIndicesOfThenExtract("LINESTRING (0 0, 0 10, 10 10, 10 0, 0 0)",
                            "LINESTRING (0 0, 0 0)");
}

// test a zero-length subline equal to a mid point
// testZeroLenSubLineAtMidVertex()
template<>
template<>
void object::test<9>
()
{
    runIndicesOfThenExtract("LINESTRING (0 0, 0 10, 10 10, 10 0, 0 0)",
                            "LINESTRING (10 10, 10 10)");
}

// testIndexOfAfterSquare()
template<>
template<>
void object::test<10>
()
{
    runIndexOfAfterTest("LINESTRING (0 0, 0 10, 10 10, 10 0, 0 0)",
                        "POINT (0 0)");
}

// testIndexOfAfterRibbon()
template<>
template<>
void object::test<11>
()
{
    runIndexOfAfterTest("LINESTRING (0 0, 0 60, 50 60, 50 20, -20 20)",
                        "POINT (0 20)");
}

// testOffsetStartPoint()
template<>
template<>
void object::test<12>
()
{
    runOffsetTest("LINESTRING (0 0, 10 10, 10 10, 20 20)", "POINT(0 0)", 1.0,
                  "POINT (-0.7071067811865475 0.7071067811865475)");
    runOffsetTest("LINESTRING (0 0, 10 10, 10 10, 20 20)", "POINT(0 0)", -1.0,
                  "POINT (0.7071067811865475 -0.7071067811865475)");
    runOffsetTest("LINESTRING (0 0, 10 10, 10 10, 20 20)", "POINT(10 10)", 5.0,
                  "POINT (6.464466094067262 13.535533905932738)");
    runOffsetTest("LINESTRING (0 0, 10 10, 10 10, 20 20)", "POINT(10 10)", -5.0,
                  "POINT (13.535533905932738 6.464466094067262)");
}

// testExtractLineBothIndicesAtEndpointXXX()
template<>
template<>
void object::test<13>
()
{
    checkExtractLine(
        "MULTILINESTRING ((0 0, 10 0), (20 0, 25 0, 30 0))",
        -10, 10, "LINESTRING (10 0, 10 0)"
    );
}


// testExtractLineBeyondRange()
template<> template<>
void object::test<14>
()
{
    checkExtractLine("LINESTRING (0 0, 10 10)", -100, 100, "LINESTRING (0 0, 10 10)");
}

// testExtractLineReverse()
template<>
template<>
void object::test<15>
()
{
    checkExtractLine("LINESTRING (0 0, 10 0)", 9, 1, "LINESTRING (9 0, 1 0)");
}

// testExtractLineReverseMulti()
template<>
template<>
void object::test<16>
()
{
    checkExtractLine("MULTILINESTRING ((0 0, 10 0), (20 0, 25 0, 30 0))",
                     19, 1, "MULTILINESTRING ((10 0, 1 0), (29 0, 25 0, 20 0))");
}

// testExtractLineNegative()
template<>
template<>
void object::test<17>
()
{
    checkExtractLine("LINESTRING (0 0, 10 0)", -9, -1, "LINESTRING (1 0, 9 0)");
}

// testExtractLineNegativeReverse()
template<>
template<>
void object::test<18>
()
{
    checkExtractLine("LINESTRING (0 0, 10 0)", -1, -9, "LINESTRING (9 0, 1 0)");
}

// testExtractLineIndexAtEndpoint()
template<>
template<>
void object::test<19>
()
{
    checkExtractLine("MULTILINESTRING ((0 0, 10 0), (20 0, 25 0, 30 0))",
                     10, -1, "LINESTRING (20 0, 25 0, 29 0)");
}

// testExtractLineBothIndicesAtEndpoint()
template<>
template<>
void object::test<20>
()
{
    checkExtractLine("MULTILINESTRING ((0 0, 10 0), (20 0, 25 0, 30 0))",
                     10, 10, "LINESTRING (10 0, 10 0)");
}

// testExtractLineBothIndicesAtEndpointNegative()
template<>
template<>
void object::test<21>
()
{
    checkExtractLine("MULTILINESTRING ((0 0, 10 0), (20 0, 25 0, 30 0))",
                     -10, 10, "LINESTRING (10 0, 10 0)");
}

// testExtractPointBeyondRange()
template<>
template<>
void object::test<22>
()
{
    GeomPtr linearGeom(reader.read("LINESTRING (0 0, 10 10)"));
    LengthIndexedLine indexedLine(linearGeom.get());
    Coordinate pt = indexedLine.extractPoint(100);
    ensure(pt == Coordinate(10, 10));

    Coordinate pt2 = indexedLine.extractPoint(0);
    ensure(pt2 == Coordinate(0, 0));
}

// testProjectPointWithDuplicateCoords()
template<>
template<>
void object::test<23>
()
{
    GeomPtr linearGeom(reader.read("LINESTRING (0 0, 10 0, 10 0, 20 0)"));
    LengthIndexedLine indexedLine(linearGeom.get());
    double projIndex = indexedLine.project(Coordinate(10, 1));
    ensure(projIndex == 10.0);
}

/**
 * Tests that z values are interpolated
 *
 */
// testComputeZ()
template<>
template<>
void object::test<24>
()
{
    GeomPtr linearGeom(reader.read("LINESTRING (0 0 0, 10 10 10)"));
    LengthIndexedLine indexedLine(linearGeom.get());
    double projIndex = indexedLine.project(Coordinate(5, 5));
    Coordinate projPt = indexedLine.extractPoint(projIndex);
    //    System.out.println(projPt);
    ensure(projPt.equals3D(Coordinate(5, 5, 5)));
}

/**
 * Tests that if the input does not have Z ordinates, neither does the output.
 *
 */
// testComputeZNaN()
template<>
template<>
void object::test<25>
()
{

    GeomPtr linearGeom(reader.read("LINESTRING (0 0, 10 10)"));
    LengthIndexedLine indexedLine(linearGeom.get());
    double projIndex = indexedLine.project(Coordinate(5, 5));
    Coordinate projPt = indexedLine.extractPoint(projIndex);
    ensure(0 != std::isnan(projPt.z));
}

/**
 * From GEOS Ticket #323
 */
// testProjectExtractPoint()
template<>
template<>
void object::test<26>
()
{
    GeomPtr linearGeom(reader.read("MULTILINESTRING ((0 2, 0 0), (-1 1, 1 1))"));
    LengthIndexedLine indexedLine(linearGeom.get());
    double index = indexedLine.project(Coordinate(1, 0));
    Coordinate pt = indexedLine.extractPoint(index);
    ensure_equals(pt, Coordinate(0, 0));
}

/**
 * Tests that leading and trailing zero-length sublines are trimmed in
 * the computed result, and that zero-length extracts return the lowest
 * extracted zero-length line
 */
// testExtractLineIndexAtEndpointWithZeroLenComponents()
template<> template<>
void object::test<27>
()
{
    checkExtractLine(
        "MULTILINESTRING ((0 0, 10 0), (10 0, 10 0), (20 0, 25 0, 30 0))",
        10, -1, "LINESTRING (20 0, 25 0, 29 0)");

    checkExtractLine(
        "MULTILINESTRING ((0 0, 10 0), (10 0, 10 0), (20 0, 25 0, 30 0))",
        5, 10, "LINESTRING (5 0, 10 0)");

    checkExtractLine(
        "MULTILINESTRING ((0 0,10 0),(10 0,10 0),(10 0,10 0),(20 0,25 0,30 0))",
        10, 10, "LINESTRING (10 0, 10 0)");

    checkExtractLine(
        "MULTILINESTRING((0 0,10 0),(10 0,10 0),(10 0,10 0),(10 0,10 0),(20 0,25 0,30 0))",
        10, -10, "LINESTRING (10 0, 10 0)");
}

#if 0
template<>
template<>
void object::test<28>()
{
    GeomPtr linearGeom(reader.read(
        "MULTILINESTRING ((0 -2, 0 2),(-2 0, 2 0))"
        ));
    LengthIndexedLine indexedLine(linearGeom.get());

    double projIndex = indexedLine.project(Coordinate(2, 1.9));
    ensure_equals(projIndex, 8);
    Coordinate projPt = indexedLine.extractPoint(projIndex);
    ensure_equals(projPt, Coordinate(2, 0));

    projIndex = indexedLine.project(Coordinate(2, 2.1));
    ensure_equals(projIndex, 4);
    projPt = indexedLine.extractPoint(projIndex);
    ensure_equals(projPt, Coordinate(0, 2));
}
#endif

template<>
template<>
void object::test<29>()
{
    GeomPtr linearGeom(reader.read("LINESTRING EMPTY"));
    LengthIndexedLine indexedLine(linearGeom.get());
    Coordinate pt = indexedLine.extractPoint(100);
    ensure(pt.isNull());
}

// testExtractLineIndexAtEndpointOfTouchingLines()
template<>
template<>
void object::test<30>
()
{
    runIndicesOfThenExtract("MULTILINESTRING((0 0, 0 50), (0 50, 0 100))",
                            "LINESTRING (0 50, 0 60)");
}

} // namespace tut