Search in sources :

Example 11 with LinearLocation

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();
}
Also used : TemporaryStreetLocation(org.opentripplanner.routing.location.TemporaryStreetLocation) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) LinearLocation(org.locationtech.jts.linearref.LinearLocation) NonLocalizedString(org.opentripplanner.util.NonLocalizedString) GraphPath(org.opentripplanner.routing.spt.GraphPath) RoutingRequest(org.opentripplanner.routing.api.request.RoutingRequest) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) Edge(org.opentripplanner.routing.graph.Edge) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 12 with LinearLocation

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);
}
Also used : CoordinateSequence(org.locationtech.jts.geom.CoordinateSequence) P2(org.opentripplanner.common.model.P2) LineString(org.locationtech.jts.geom.LineString) Coordinate(org.locationtech.jts.geom.Coordinate) LocationIndexedLine(org.locationtech.jts.linearref.LocationIndexedLine) LinearLocation(org.locationtech.jts.linearref.LinearLocation)

Example 13 with LinearLocation

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());
        }
    }
}
Also used : LineString(org.locationtech.jts.geom.LineString) Coordinate(org.locationtech.jts.geom.Coordinate) LocationIndexedLine(org.locationtech.jts.linearref.LocationIndexedLine) LinearLocation(org.locationtech.jts.linearref.LinearLocation) TransitStopVertex(org.opentripplanner.routing.vertextype.TransitStopVertex) StreetVertex(org.opentripplanner.routing.vertextype.StreetVertex) SplitterVertex(org.opentripplanner.routing.vertextype.SplitterVertex) TemporarySplitterVertex(org.opentripplanner.routing.vertextype.TemporarySplitterVertex) TemporaryVertex(org.opentripplanner.routing.vertextype.TemporaryVertex) AreaEdge(org.opentripplanner.routing.edgetype.AreaEdge)

Example 14 with LinearLocation

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;
}
Also used : CoordinateSequence(org.locationtech.jts.geom.CoordinateSequence) PackedCoordinateSequence(org.opentripplanner.common.geometry.PackedCoordinateSequence) LineString(org.locationtech.jts.geom.LineString) Coordinate(org.locationtech.jts.geom.Coordinate) LinearLocation(org.locationtech.jts.linearref.LinearLocation) LocationIndexedLine(org.locationtech.jts.linearref.LocationIndexedLine) ShapePoint(org.opentripplanner.model.ShapePoint)

Example 15 with LinearLocation

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;
}
Also used : Coordinate(org.locationtech.jts.geom.Coordinate) LinearLocation(org.locationtech.jts.linearref.LinearLocation) StopLocation(org.opentripplanner.model.StopLocation) LinkedList(java.util.LinkedList) StopTime(org.opentripplanner.model.StopTime)

Aggregations

LinearLocation (org.locationtech.jts.linearref.LinearLocation)15 Coordinate (org.locationtech.jts.geom.Coordinate)7 LocationIndexedLine (org.locationtech.jts.linearref.LocationIndexedLine)7 LineString (org.locationtech.jts.geom.LineString)6 Edge (org.opentripplanner.routing.graph.Edge)6 HashSet (java.util.HashSet)5 StreetEdge (org.opentripplanner.routing.edgetype.StreetEdge)5 Test (org.junit.Test)4 RoutingRequest (org.opentripplanner.routing.api.request.RoutingRequest)4 TemporaryStreetLocation (org.opentripplanner.routing.location.TemporaryStreetLocation)4 NonLocalizedString (org.opentripplanner.util.NonLocalizedString)4 GraphPath (org.opentripplanner.routing.spt.GraphPath)3 ShortestPathTree (org.opentripplanner.routing.spt.ShortestPathTree)3 CoordinateSequence (org.locationtech.jts.geom.CoordinateSequence)2 Geometry (org.locationtech.jts.geom.Geometry)2 P2 (org.opentripplanner.common.model.P2)2 ShapePoint (org.opentripplanner.model.ShapePoint)2 State (org.opentripplanner.routing.core.State)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1