/src/geos/src/linearref/LengthIndexedLine.cpp
Line | Count | Source |
1 | | /********************************************************************** |
2 | | * |
3 | | * GEOS - Geometry Engine Open Source |
4 | | * http://geos.osgeo.org |
5 | | * |
6 | | * Copyright (C) 2011 Sandro Santilli <strk@kbt.io> |
7 | | * Copyright (C) 2005-2006 Refractions Research Inc. |
8 | | * Copyright (C) 2001-2002 Vivid Solutions Inc. |
9 | | * |
10 | | * This is free software; you can redistribute and/or modify it under |
11 | | * the terms of the GNU Lesser General Public Licence as published |
12 | | * by the Free Software Foundation. |
13 | | * See the COPYING file for more information. |
14 | | * |
15 | | ********************************************************************** |
16 | | * |
17 | | * Last port: linearref/LengthIndexedLine.java r463 |
18 | | * |
19 | | **********************************************************************/ |
20 | | |
21 | | #include <geos/linearref/ExtractLineByLocation.h> |
22 | | #include <geos/linearref/LengthIndexedLine.h> |
23 | | #include <geos/linearref/LocationIndexedLine.h> |
24 | | #include <geos/linearref/LinearLocation.h> |
25 | | #include <geos/linearref/LengthLocationMap.h> |
26 | | #include <geos/linearref/LengthIndexOfPoint.h> |
27 | | #include <geos/linearref/LocationIndexOfLine.h> |
28 | | |
29 | | |
30 | | |
31 | | using namespace geos::geom; |
32 | | |
33 | | namespace geos { |
34 | | namespace linearref { // geos.linearref |
35 | | |
36 | | LengthIndexedLine::LengthIndexedLine(const Geometry* p_linearGeom) : |
37 | 0 | linearGeom(p_linearGeom) {} |
38 | | |
39 | | CoordinateXYZM |
40 | | LengthIndexedLine::extractPoint(double index) const |
41 | 0 | { |
42 | 0 | LinearLocation loc = LengthLocationMap::getLocation(linearGeom, index); |
43 | 0 | CoordinateXYZM coord = loc.getCoordinate(linearGeom); |
44 | 0 | return coord; |
45 | 0 | } |
46 | | |
47 | | Coordinate |
48 | | LengthIndexedLine::extractPoint(double index, double offsetDistance) const |
49 | 0 | { |
50 | 0 | LinearLocation loc = LengthLocationMap::getLocation(linearGeom, index); |
51 | 0 | Coordinate ret; |
52 | 0 | loc.getSegment(linearGeom)->pointAlongOffset(loc.getSegmentFraction(), offsetDistance, ret); |
53 | 0 | return ret; |
54 | 0 | } |
55 | | |
56 | | |
57 | | std::unique_ptr<Geometry> |
58 | | LengthIndexedLine::extractLine(double startIndex, double endIndex) const |
59 | 0 | { |
60 | 0 | if (std::isnan(startIndex)) { |
61 | 0 | throw util::IllegalArgumentException("startIndex is NaN"); |
62 | 0 | } |
63 | 0 | if (std::isnan(endIndex)) { |
64 | 0 | throw util::IllegalArgumentException("endIndex is NaN"); |
65 | 0 | } |
66 | | |
67 | 0 | const LocationIndexedLine lil(linearGeom); |
68 | 0 | const double startIndex2 = clampIndex(startIndex); |
69 | 0 | const double endIndex2 = clampIndex(endIndex); |
70 | | // if extracted line is zero-length, resolve start lower as well to |
71 | | // ensure they are equal |
72 | 0 | const bool resolveStartLower = (startIndex2 == endIndex2); |
73 | 0 | const LinearLocation startLoc = locationOf(startIndex2, resolveStartLower); |
74 | 0 | const LinearLocation endLoc = locationOf(endIndex2); |
75 | | // LinearLocation std::endloc = locationOf(endIndex2, true); |
76 | | // LinearLocation startLoc = locationOf(startIndex2); |
77 | 0 | return ExtractLineByLocation::extract(linearGeom, startLoc, endLoc); |
78 | 0 | } |
79 | | |
80 | | LinearLocation |
81 | | LengthIndexedLine::locationOf(double index) const |
82 | 0 | { |
83 | 0 | return LengthLocationMap::getLocation(linearGeom, index); |
84 | 0 | } |
85 | | |
86 | | LinearLocation |
87 | | LengthIndexedLine::locationOf(double index, bool resolveLower) const |
88 | 0 | { |
89 | 0 | return LengthLocationMap::getLocation(linearGeom, index, resolveLower); |
90 | 0 | } |
91 | | |
92 | | |
93 | | double |
94 | | LengthIndexedLine::indexOf(const Coordinate& pt) const |
95 | 0 | { |
96 | 0 | return LengthIndexOfPoint::indexOf(linearGeom, pt); |
97 | 0 | } |
98 | | |
99 | | |
100 | | double |
101 | | LengthIndexedLine::indexOfAfter(const Coordinate& pt, double minIndex) const |
102 | 0 | { |
103 | 0 | return LengthIndexOfPoint::indexOfAfter(linearGeom, pt, minIndex); |
104 | 0 | } |
105 | | |
106 | | |
107 | | double* |
108 | | LengthIndexedLine::indicesOf(const Geometry* subLine) const |
109 | 0 | { |
110 | 0 | LinearLocation* locIndex = LocationIndexOfLine::indicesOf(linearGeom, subLine); |
111 | 0 | double* index = new double[2]; |
112 | 0 | index[0] = LengthLocationMap::getLength(linearGeom, locIndex[0]); |
113 | 0 | index[1] = LengthLocationMap::getLength(linearGeom, locIndex[1]); |
114 | 0 | delete [] locIndex; |
115 | 0 | return index; |
116 | 0 | } |
117 | | |
118 | | |
119 | | double |
120 | | LengthIndexedLine::project(const Coordinate& pt) const |
121 | 0 | { |
122 | 0 | return LengthIndexOfPoint::indexOf(linearGeom, pt); |
123 | 0 | } |
124 | | |
125 | | double |
126 | | LengthIndexedLine::getStartIndex() const |
127 | 0 | { |
128 | 0 | return 0.0; |
129 | 0 | } |
130 | | |
131 | | double |
132 | | LengthIndexedLine::getEndIndex() const |
133 | 0 | { |
134 | 0 | return linearGeom->getLength(); |
135 | 0 | } |
136 | | |
137 | | bool |
138 | | LengthIndexedLine::isValidIndex(double index) const |
139 | 0 | { |
140 | 0 | return (index >= getStartIndex() |
141 | 0 | && index <= getEndIndex()); |
142 | 0 | } |
143 | | |
144 | | /* public */ |
145 | | double |
146 | | LengthIndexedLine::clampIndex(double index) const |
147 | 0 | { |
148 | 0 | double posIndex = positiveIndex(index); |
149 | 0 | double startIndex = getStartIndex(); |
150 | 0 | if(posIndex < startIndex) { |
151 | 0 | return startIndex; |
152 | 0 | } |
153 | | |
154 | 0 | double endIndex = getEndIndex(); |
155 | 0 | if(posIndex > endIndex) { |
156 | 0 | return endIndex; |
157 | 0 | } |
158 | | |
159 | 0 | return posIndex; |
160 | 0 | } |
161 | | |
162 | | /* private */ |
163 | | double |
164 | | LengthIndexedLine::positiveIndex(double index) const |
165 | 0 | { |
166 | 0 | if(index >= 0.0) { |
167 | 0 | return index; |
168 | 0 | } |
169 | 0 | return linearGeom->getLength() + index; |
170 | 0 | } |
171 | | |
172 | | } // geos.linearref |
173 | | } // geos |