use of org.locationtech.jts.linearref.LinearLocation in project OpenTripPlanner by opentripplanner.
the class TestHalfEdges method testRouteToSameEdge.
@Test
public void testRouteToSameEdge() {
RoutingRequest options = new RoutingRequest();
HashSet<Edge> turns = new HashSet<Edge>();
turns.add(left);
turns.add(leftBack);
TemporaryStreetLocation start = StreetVertexIndex.createTemporaryStreetLocation(graph, "start", new NonLocalizedString("start"), filter(turns, StreetEdge.class), new LinearLocation(0, 0.4).getCoordinate(left.getGeometry()), false);
TemporaryStreetLocation end = StreetVertexIndex.createTemporaryStreetLocation(graph, "end", new NonLocalizedString("end"), filter(turns, StreetEdge.class), new LinearLocation(0, 0.8).getCoordinate(left.getGeometry()), true);
assertEquals(start.getX(), end.getX(), 0.0001);
assertTrue(start.getY() < end.getY());
Collection<Edge> edges = end.getIncoming();
assertEquals(2, edges.size());
long startTime = TestUtils.dateInSeconds("America/New_York", 2009, 11, 1, 12, 34, 25);
options.dateTime = startTime;
options.setRoutingContext(graph, start, end);
options.setMaxWalkDistance(Double.MAX_VALUE);
ShortestPathTree spt = aStar.getShortestPathTree(options);
GraphPath path = spt.getPath(end, false);
assertNotNull("There must be a path from start to end", path);
assertEquals(1, path.edges.size());
options.cleanup();
}
use of org.locationtech.jts.linearref.LinearLocation in project OpenTripPlanner by opentripplanner.
the class GeometryUtils method splitGeometryAtFraction.
/**
* Splits the input geometry into two LineStrings at a fraction of the distance covered.
*/
public static P2<LineString> splitGeometryAtFraction(Geometry geometry, double fraction) {
LineString empty = new LineString(null, gf);
Coordinate[] coordinates = geometry.getCoordinates();
CoordinateSequence sequence = gf.getCoordinateSequenceFactory().create(coordinates);
LineString total = new LineString(sequence, gf);
if (coordinates.length < 2)
return new P2<LineString>(empty, empty);
if (fraction <= 0)
return new P2<LineString>(empty, total);
if (fraction >= 1)
return new P2<LineString>(total, empty);
double totalDistance = total.getLength();
double requestedDistance = totalDistance * fraction;
// An index in JTS can actually refer to any point along the line. It is NOT an array index.
LocationIndexedLine line = new LocationIndexedLine(geometry);
LinearLocation l = LengthLocationMap.getLocation(geometry, requestedDistance);
LineString beginning = (LineString) line.extractLine(line.getStartIndex(), l);
LineString ending = (LineString) line.extractLine(l, line.getEndIndex());
return new P2<LineString>(beginning, ending);
}
use of org.locationtech.jts.linearref.LinearLocation in project OpenTripPlanner by opentripplanner.
the class SimpleStreetSplitter method link.
/**
* split the edge and link in the transit stop
*/
private void link(Vertex tstop, StreetEdge edge, double xscale, RoutingRequest options) {
// TODO: we've already built this line string, we should save it
LineString orig = edge.getGeometry();
LineString transformed = equirectangularProject(orig, xscale);
LocationIndexedLine il = new LocationIndexedLine(transformed);
LinearLocation ll = il.project(new Coordinate(tstop.getLon() * xscale, tstop.getLat()));
// street to use the same vertices. Otherwise the order the stops are loaded in will affect where they are snapped.
if (ll.getSegmentIndex() == 0 && ll.getSegmentFraction() < 1e-8) {
makeLinkEdges(tstop, (StreetVertex) edge.getFromVertex());
} else // past the last point
if (ll.getSegmentIndex() == orig.getNumPoints() - 1) {
makeLinkEdges(tstop, (StreetVertex) edge.getToVertex());
} else // nPoints - 2: -1 to correct for index vs count, -1 to account for fencepost problem
if (ll.getSegmentIndex() == orig.getNumPoints() - 2 && ll.getSegmentFraction() > 1 - 1e-8) {
makeLinkEdges(tstop, (StreetVertex) edge.getToVertex());
} else {
TemporaryVertex temporaryVertex = null;
boolean endVertex = false;
if (tstop instanceof TemporaryVertex) {
temporaryVertex = (TemporaryVertex) tstop;
endVertex = temporaryVertex.isEndVertex();
}
// split the edge, get the split vertex
SplitterVertex v0 = split(edge, ll, temporaryVertex != null, endVertex);
makeLinkEdges(tstop, v0);
// edges that were missed by WalkableAreaBuilder
if (edge instanceof AreaEdge && tstop instanceof TransitStopVertex && this.addExtraEdgesToAreas) {
linkTransitToAreaVertices(v0, ((AreaEdge) edge).getArea());
}
}
}
use of org.locationtech.jts.linearref.LinearLocation in project OpenTripPlanner by opentripplanner.
the class GeometryAndBlockProcessor method getGeometriesByShape.
private LineString[] getGeometriesByShape(List<StopTime> stopTimes, FeedScopedId shapeId, LineString shape, List<LinearLocation> locations) {
LineString[] geoms = new LineString[stopTimes.size() - 1];
Iterator<LinearLocation> locationIt = locations.iterator();
LinearLocation endLocation = locationIt.next();
double distanceSoFar = 0;
int last = 0;
for (int i = 0; i < stopTimes.size() - 1; ++i) {
LinearLocation startLocation = endLocation;
endLocation = locationIt.next();
// it does not matter at all if this is accurate so long as it is consistent
for (int j = last; j < startLocation.getSegmentIndex(); ++j) {
Coordinate from = shape.getCoordinateN(j);
Coordinate to = shape.getCoordinateN(j + 1);
double xd = from.x - to.x;
double yd = from.y - to.y;
distanceSoFar += FastMath.sqrt(xd * xd + yd * yd);
}
last = startLocation.getSegmentIndex();
double startIndex = distanceSoFar + startLocation.getSegmentFraction() * startLocation.getSegmentLength(shape);
// advance distanceSoFar up to start of segment containing endLocation
for (int j = last; j < endLocation.getSegmentIndex(); ++j) {
Coordinate from = shape.getCoordinateN(j);
Coordinate to = shape.getCoordinateN(j + 1);
double xd = from.x - to.x;
double yd = from.y - to.y;
distanceSoFar += FastMath.sqrt(xd * xd + yd * yd);
}
last = startLocation.getSegmentIndex();
double endIndex = distanceSoFar + endLocation.getSegmentFraction() * endLocation.getSegmentLength(shape);
ShapeSegmentKey key = new ShapeSegmentKey(shapeId, startIndex, endIndex);
LineString geometry = geometriesByShapeSegmentKey.get(key);
if (geometry == null) {
LocationIndexedLine locationIndexed = new LocationIndexedLine(shape);
geometry = (LineString) locationIndexed.extractLine(startLocation, endLocation);
// Pack the resulting line string
CoordinateSequence sequence = new PackedCoordinateSequence.Double(geometry.getCoordinates(), 2);
geometry = geometryFactory.createLineString(sequence);
}
geoms[i] = geometry;
}
return geoms;
}
use of org.locationtech.jts.linearref.LinearLocation in project OpenTripPlanner by opentripplanner.
the class GeometryAndBlockProcessor method getStopLocations.
/**
* Find a consistent, increasing list of LinearLocations along a shape for a set of stops.
* Handles loops routes.
*/
private List<LinearLocation> getStopLocations(List<List<IndexedLineSegment>> possibleSegmentsForStop, List<StopTime> stopTimes, int index, int prevSegmentIndex) {
if (index == stopTimes.size()) {
return new LinkedList<>();
}
StopTime st = stopTimes.get(index);
StopLocation stop = st.getStop();
Coordinate stopCoord = stop.getCoordinate().asJtsCoordinate();
for (IndexedLineSegment segment : possibleSegmentsForStop.get(index)) {
if (segment.index < prevSegmentIndex) {
// can't go backwards along line
continue;
}
List<LinearLocation> locations = getStopLocations(possibleSegmentsForStop, stopTimes, index + 1, segment.index);
if (locations != null) {
LinearLocation location = new LinearLocation(0, segment.index, segment.fraction(stopCoord));
locations.add(0, location);
// we found one!
return locations;
}
}
return null;
}
Aggregations