Coverage Report

Created: 2026-10-31 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/geos/src/linearref/ExtractLineByLocation.cpp
Line
Count
Source
1
/**********************************************************************
2
 *
3
 * GEOS - Geometry Engine Open Source
4
 * http://geos.osgeo.org
5
 *
6
 * Copyright (C) 2005-2006 Refractions Research Inc.
7
 * Copyright (C) 2001-2002 Vivid Solutions Inc.
8
 *
9
 * This is free software; you can redistribute and/or modify it under
10
 * the terms of the GNU Lesser General Public Licence as published
11
 * by the Free Software Foundation.
12
 * See the COPYING file for more information.
13
 *
14
 **********************************************************************
15
 *
16
 * Last port: linearref/ExtractLineByLocation.java rev. 1.35
17
 *
18
 **********************************************************************/
19
20
#include <geos/geom/Coordinate.h>
21
#include <geos/geom/CoordinateSequence.h>
22
#include <geos/geom/MultiLineString.h>
23
#include <geos/geom/LineString.h>
24
#include <geos/linearref/ExtractLineByLocation.h>
25
#include <geos/linearref/LinearIterator.h>
26
#include <geos/linearref/LinearLocation.h>
27
#include <geos/linearref/LengthLocationMap.h>
28
#include <geos/linearref/LengthIndexOfPoint.h>
29
#include <geos/linearref/LinearGeometryBuilder.h>
30
31
#include <cassert>
32
#include <typeinfo>
33
34
35
36
using namespace geos::geom;
37
38
namespace geos {
39
namespace linearref { // geos.linearref
40
41
42
std::unique_ptr<Geometry>
43
ExtractLineByLocation::extract(const Geometry* line, const LinearLocation& start, const LinearLocation& end)
44
0
{
45
0
    ExtractLineByLocation ls(line);
46
0
    return ls.extract(start, end);
47
0
}
48
49
ExtractLineByLocation::ExtractLineByLocation(const Geometry* p_line) :
50
0
    line(p_line) {}
51
52
53
std::unique_ptr<Geometry>
54
ExtractLineByLocation::extract(const LinearLocation& start, const LinearLocation& end)
55
0
{
56
0
    if(end.compareTo(start) < 0) {
57
0
        auto backwards = computeLinear(end, start);
58
0
        auto forwards = reverse(backwards.get());
59
0
        return forwards;
60
0
    }
61
0
    return computeLinear(start, end);
62
0
}
63
64
std::unique_ptr<Geometry>
65
ExtractLineByLocation::reverse(const Geometry* linear)
66
0
{
67
0
    const LineString* ls = dynamic_cast<const LineString*>(linear);
68
0
    if(ls) {
69
0
        return ls->reverse();
70
0
    }
71
0
    else {
72
0
        const MultiLineString* mls = dynamic_cast<const MultiLineString*>(linear);
73
0
        if(mls) {
74
0
            return mls->reverse();
75
0
        }
76
0
        else {
77
0
            assert(!static_cast<bool>("non-linear geometry encountered"));
78
0
            return nullptr;
79
0
        }
80
0
    }
81
0
}
82
83
std::unique_ptr<LineString>
84
ExtractLineByLocation::computeLine(const LinearLocation& start, const LinearLocation& end)
85
0
{
86
0
    auto coordinates = line->getCoordinates();
87
0
    CoordinateSequence newCoordinateArray;
88
89
0
    const std::size_t indexStep = 1;
90
0
    auto startSegmentIndex = start.getSegmentIndex();
91
92
0
    if(start.getSegmentFraction() > 0.0) {
93
0
        startSegmentIndex += indexStep;
94
0
    }
95
96
0
    auto lastSegmentIndex = end.getSegmentIndex();
97
0
    if(end.getSegmentFraction() == 1.0) {
98
0
        lastSegmentIndex += indexStep;
99
0
    }
100
101
0
    if(lastSegmentIndex >= coordinates->size()) {
102
0
        assert(!coordinates->isEmpty());
103
0
        lastSegmentIndex = coordinates->size() - indexStep;
104
0
    }
105
106
0
    if(! start.isVertex()) {
107
0
        newCoordinateArray.add(start.getCoordinate(line));
108
0
    }
109
110
0
    for(auto i = startSegmentIndex; i <= lastSegmentIndex; i++) {
111
0
        newCoordinateArray.add((*coordinates)[i]);
112
0
    }
113
114
0
    if(! end.isVertex()) {
115
0
        newCoordinateArray.add(end.getCoordinate(line));
116
0
    }
117
118
    // ensure there is at least one coordinate in the result
119
0
    if(newCoordinateArray.isEmpty()) {
120
0
        newCoordinateArray.add(start.getCoordinate(line));
121
0
    }
122
123
    /*
124
     * Ensure there is enough coordinates to build a valid line.
125
     * Make a 2-point line with duplicate coordinates, if necessary.
126
     * There will always be at least one coordinate in the coordList.
127
     */
128
0
    if(newCoordinateArray.size() <= 1) {
129
0
        newCoordinateArray.add(newCoordinateArray[0]);
130
0
    }
131
132
0
    return std::unique_ptr<LineString>(line->getFactory()->createLineString(newCoordinateArray));
133
0
}
134
135
std::unique_ptr<Geometry>
136
ExtractLineByLocation::computeLinear(const LinearLocation& start, const LinearLocation& end)
137
0
{
138
0
    LinearGeometryBuilder builder(line->getFactory());
139
0
    builder.setFixInvalidLines(true);
140
141
0
    if(! start.isVertex()) {
142
0
        builder.add(start.getCoordinate(line));
143
0
    }
144
145
0
    for(LinearIterator it(line, start); it.hasNext(); it.next()) {
146
0
        if(end.compareLocationValues(it.getComponentIndex(), it.getVertexIndex(), 0.0) < 0) {
147
0
            break;
148
0
        }
149
0
        Coordinate pt = it.getSegmentStart();
150
0
        builder.add(pt);
151
0
        if(it.isEndOfLine()) {
152
0
            builder.endLine();
153
0
        }
154
0
    }
155
0
    if(! end.isVertex()) {
156
0
        builder.add(end.getCoordinate(line));
157
0
    }
158
0
    return std::unique_ptr<Geometry>(builder.getGeometry());
159
0
}
160
}
161
}