Regnerate geos wrapper with correct version for this branch
[geos.git] / src / linearref / LengthIndexedLine.cpp
blobac9470d1de07a8cb80d2d8dcffb6dd0f1ddf0f54
1 /**********************************************************************
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
6 * Copyright (C) 2011 Sandro Santilli <strk@keybit.net>
7 * Copyright (C) 2005-2006 Refractions Research Inc.
8 * Copyright (C) 2001-2002 Vivid Solutions Inc.
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.
15 **********************************************************************
17 * Last port: linearref/LengthIndexedLine.java r463
19 **********************************************************************/
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>
29 using namespace std;
31 using namespace geos::geom;
33 namespace geos
35 namespace linearref // geos.linearref
38 LengthIndexedLine::LengthIndexedLine(const Geometry* linearGeom) :
39 linearGeom(linearGeom) {}
41 Coordinate LengthIndexedLine::extractPoint(double index) const
43 LinearLocation loc = LengthLocationMap::getLocation(linearGeom, index);
44 Coordinate coord = loc.getCoordinate(linearGeom);
45 return coord;
48 Coordinate LengthIndexedLine::extractPoint(double index, double offsetDistance) const
50 LinearLocation loc = LengthLocationMap::getLocation(linearGeom, index);
51 Coordinate ret;
52 loc.getSegment(linearGeom)->pointAlongOffset(loc.getSegmentFraction(), offsetDistance, ret);
53 return ret;
57 Geometry *
58 LengthIndexedLine::extractLine(double startIndex, double endIndex) const
60 const LocationIndexedLine lil(linearGeom);
61 const double startIndex2 = clampIndex(startIndex);
62 const double endIndex2 = clampIndex(endIndex);
63 // if extracted line is zero-length, resolve start lower as well to
64 // ensure they are equal
65 const bool resolveStartLower = ( startIndex2 == endIndex2 );
66 const LinearLocation startLoc = locationOf(startIndex2, resolveStartLower);
67 const LinearLocation endLoc = locationOf(endIndex2);
68 // LinearLocation endLoc = locationOf(endIndex2, true);
69 // LinearLocation startLoc = locationOf(startIndex2);
70 return ExtractLineByLocation::extract(linearGeom, startLoc, endLoc);
73 LinearLocation LengthIndexedLine::locationOf(double index) const
75 return LengthLocationMap::getLocation(linearGeom, index);
78 LinearLocation
79 LengthIndexedLine::locationOf(double index, bool resolveLower) const
81 return LengthLocationMap::getLocation(linearGeom, index, resolveLower);
85 double LengthIndexedLine::indexOf(const Coordinate& pt) const
87 return LengthIndexOfPoint::indexOf(linearGeom, pt);
91 double LengthIndexedLine::indexOfAfter(const Coordinate& pt, double minIndex) const
93 return LengthIndexOfPoint::indexOfAfter(linearGeom, pt, minIndex);
97 double* LengthIndexedLine::indicesOf(const Geometry* subLine) const
99 LinearLocation* locIndex = LocationIndexOfLine::indicesOf(linearGeom, subLine);
100 double* index = new double[2];
101 index[0] = LengthLocationMap::getLength(linearGeom, locIndex[0]);
102 index[1] = LengthLocationMap::getLength(linearGeom, locIndex[1]);
103 delete [] locIndex;
104 return index;
108 double LengthIndexedLine::project(const Coordinate& pt) const
110 return LengthIndexOfPoint::indexOf(linearGeom, pt);
113 double LengthIndexedLine::getStartIndex() const
115 return 0.0;
118 double LengthIndexedLine::getEndIndex() const
120 return linearGeom->getLength();
123 bool LengthIndexedLine::isValidIndex(double index) const
125 return (index >= getStartIndex()
126 && index <= getEndIndex());
129 /* public */
130 double
131 LengthIndexedLine::clampIndex(double index) const
133 double posIndex = positiveIndex(index);
134 double startIndex = getStartIndex();
135 if (posIndex < startIndex) return startIndex;
137 double endIndex = getEndIndex();
138 if (posIndex > endIndex) return endIndex;
140 return posIndex;
143 /* private */
144 double
145 LengthIndexedLine::positiveIndex(double index) const
147 if (index >= 0.0) return index;
148 return linearGeom->getLength() + index;
151 } // geos.linearref
152 } // geos