Skip to content

Commit

Permalink
Some refactoring for call-n-ride
Browse files Browse the repository at this point in the history
  • Loading branch information
sdjacobs committed Aug 21, 2017
1 parent fdad9cb commit db87705
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 45 deletions.
Expand Up @@ -17,26 +17,23 @@ the License, or (props, at your option) any later version.
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.linearref.LengthIndexedLine;
import org.onebusaway.gtfs.model.Stop;
import org.opentripplanner.api.resource.CoordinateArrayListSequence;
import org.opentripplanner.common.geometry.GeometryUtils;
import org.opentripplanner.common.geometry.SphericalDistanceLibrary;
import org.opentripplanner.routing.algorithm.strategies.SearchTerminationStrategy;
import org.opentripplanner.routing.core.RoutingContext;
import org.opentripplanner.routing.core.RoutingRequest;
import org.opentripplanner.routing.core.State;
import org.opentripplanner.routing.core.TraverseMode;
import org.opentripplanner.routing.edgetype.PatternHop;
import org.opentripplanner.routing.edgetype.flex.TemporaryPartialPatternHop;
import org.opentripplanner.routing.graph.Edge;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.location.TemporaryStreetLocation;
import org.opentripplanner.routing.graph.Vertex;
import org.opentripplanner.routing.spt.GraphPath;
import org.opentripplanner.routing.vertextype.PatternArriveVertex;
import org.opentripplanner.routing.vertextype.PatternDepartVertex;
import org.opentripplanner.routing.vertextype.PatternStopVertex;
import org.opentripplanner.routing.vertextype.StreetVertex;

import static org.opentripplanner.api.resource.GraphPathToTripPlanConverter.makeCoordinates;

/**
* Add temporary vertices/edges for deviated-route service.
*/
Expand Down Expand Up @@ -68,7 +65,7 @@ public TemporaryPartialPatternHop makeHopNewTo(RoutingRequest opt, State state,
double startIndex = line.getStartIndex();
double endIndex = line.project(state.getBackEdge().getToVertex().getCoordinate());
return new TemporaryPartialPatternHop(hop, (PatternStopVertex) hop.getFromVertex(), to, hop.getBeginStop(), toStop,
startIndex, endIndex, null, 0, geometry(path), path.getDuration(), opt.flagStopBufferSize);
startIndex, endIndex, null, 0, path.getGeometry(), path.getDuration(), opt.flagStopBufferSize);
}

@Override
Expand All @@ -79,7 +76,7 @@ public TemporaryPartialPatternHop makeHopNewFrom(RoutingRequest opt, State state
double startIndex = line.project(state.getBackEdge().getFromVertex().getCoordinate());
double endIndex = line.getEndIndex();
return new TemporaryPartialPatternHop(hop, from, (PatternStopVertex) hop.getToVertex(), fromStop, hop.getEndStop(),
startIndex, endIndex, geometry(path), path.getDuration(), null, 0, opt.flagStopBufferSize);
startIndex, endIndex, path.getGeometry(), path.getDuration(), null, 0, opt.flagStopBufferSize);
}

@Override
Expand All @@ -94,11 +91,13 @@ public TemporaryPartialPatternHop shortenEnd(RoutingRequest opt, State state, Te
// we may want to create a ~direct~ hop.
// let's say, create a direct hop if the distance we would travel on the route is < 100m todo
if (tooLittleOnRoute(originalHop, line, startIndex, endIndex)) {
createDirectHop(opt, originalHop);
StreetVertex fromVertex = findFirstStreetVertex(opt.rctx, false);
StreetVertex toVertex = findFirstStreetVertex(opt.rctx, true);
createDirectHop(opt, originalHop, fromVertex, toVertex);
return null;
} else {
return new TemporaryPartialPatternHop(originalHop, (PatternStopVertex) hop.getFromVertex(), to, hop.getBeginStop(), toStop,
startIndex, endIndex, hop.getStartGeometry(), hop.getStartVehicleTime(), geometry(path), path.getDuration(), opt.flagStopBufferSize);
startIndex, endIndex, hop.getStartGeometry(), hop.getStartVehicleTime(), path.getGeometry(), path.getDuration(), opt.flagStopBufferSize);
}
}

Expand Down Expand Up @@ -126,20 +125,15 @@ public StreetVertex getLocationForTemporaryStop(State s, PatternHop hop) {
return findFirstStreetVertex(s);
}

private StreetVertex findFirstStreetVertex(State state) {
StreetVertex v = null;
for (State s = state; s != null; s = s.getBackState()) {
if (s.getVertex() instanceof StreetVertex && ! (s.getVertex() instanceof TemporaryStreetLocation)) {
v = (StreetVertex) s.getVertex();
}
}
return v;
private StreetVertex findFirstStreetVertex(State s) {
return findFirstStreetVertex(s.getOptions().rctx, s.getOptions().arriveBy);
}

private static LineString geometry(GraphPath path) {
CoordinateArrayListSequence coordinates = makeCoordinates(path.edges.toArray(new Edge[0]));
return GeometryUtils.getGeometryFactory().createLineString(coordinates);
private StreetVertex findFirstStreetVertex(RoutingContext rctx, boolean reverse) {
Vertex v = reverse ? rctx.toVertex : rctx.fromVertex;
if (!(v instanceof StreetVertex)) {
throw new RuntimeException("Not implemented: GTFS-flex from or to a stop.");
}
return (StreetVertex) v;
}


}
Expand Up @@ -43,7 +43,6 @@ the License, or (props, at your option) any later version.
import org.opentripplanner.routing.graph.Edge;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graph.Vertex;
import org.opentripplanner.routing.location.TemporaryStreetLocation;
import org.opentripplanner.routing.spt.GraphPath;
import org.opentripplanner.routing.vertextype.PatternArriveVertex;
import org.opentripplanner.routing.vertextype.PatternDepartVertex;
Expand All @@ -63,11 +62,9 @@ the License, or (props, at your option) any later version.

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Queue;
import java.util.stream.Collectors;

import static org.opentripplanner.api.resource.GraphPathToTripPlanConverter.makeCoordinates;
Expand Down Expand Up @@ -347,7 +344,7 @@ private void createHopsFromTemporaryStop(RoutingRequest rr, State state, Tempora
createBoardEdge(rr, transitStopDepart, patternDepartVertex, hop);
}

public void createDirectHop(RoutingRequest rr, PatternHop originalPatternHop) {
public void createDirectHop(RoutingRequest rr, PatternHop originalPatternHop, StreetVertex fromVertex, StreetVertex toVertex) {
AStar astar = new AStar();
RoutingRequest request = rr.clone();
request.setArriveBy(false);
Expand All @@ -361,8 +358,8 @@ public void createDirectHop(RoutingRequest rr, PatternHop originalPatternHop) {
throw new RuntimeException("No path to target");
}
GraphPath path = paths.iterator().next();
TemporaryTransitStop fromStop = getTemporaryStop(getStreetVertex(rr.getRoutingContext().origin, true), null, rr.rctx, rr);
TemporaryTransitStop toStop = getTemporaryStop(getStreetVertex(rr.getRoutingContext().target, false), null, rr.rctx, rr);
TemporaryTransitStop fromStop = getTemporaryStop(fromVertex, null, rr.rctx, rr);
TemporaryTransitStop toStop = getTemporaryStop(toVertex, null, rr.rctx, rr);

TransitStopDepart transitStopDepart = createTransitStopDepart(rr, fromStop);
TransitStopArrive transitStopArrive = createTransitStopArrive(rr, toStop);
Expand Down Expand Up @@ -458,23 +455,6 @@ private TemporaryPatternArriveVertex createPatternArriveVertex(RoutingRequest rr
return patternArriveVertex;
}


private StreetVertex getStreetVertex(Vertex vertex, boolean forwards) {
Queue<Vertex> queue = new LinkedList<>();
queue.add(vertex);
while (!queue.isEmpty()) {
Vertex v = queue.poll();
if (v instanceof StreetVertex && !(v instanceof TemporaryStreetLocation)) {
return (StreetVertex) v;
}
for (Edge e : (forwards ? v.getOutgoing() : v.getIncoming())) {
Vertex w = forwards ? e.getToVertex() : e.getFromVertex();
queue.add(w);
}
}
return null;
}

private Collection<TemporaryPartialPatternHop> findTemporaryPatternHops(RoutingRequest options, PatternHop patternHop) {
Collection<TemporaryPartialPatternHop> ret = new ArrayList<TemporaryPartialPatternHop>();
for (TemporaryEdge e : options.rctx.temporaryEdges) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/opentripplanner/routing/spt/GraphPath.java
Expand Up @@ -16,15 +16,20 @@ the License, or (at your option) any later version.
import java.util.LinkedList;
import java.util.List;

import com.vividsolutions.jts.geom.LineString;
import org.onebusaway.gtfs.model.AgencyAndId;
import org.onebusaway.gtfs.model.Trip;
import org.opentripplanner.api.resource.CoordinateArrayListSequence;
import org.opentripplanner.common.geometry.GeometryUtils;
import org.opentripplanner.routing.core.RoutingContext;
import org.opentripplanner.routing.core.State;
import org.opentripplanner.routing.graph.Edge;
import org.opentripplanner.routing.graph.Vertex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.opentripplanner.api.resource.GraphPathToTripPlanConverter.makeCoordinates;

/**
* A shortest path on the graph.
*/
Expand Down Expand Up @@ -213,4 +218,9 @@ public RoutingContext getRoutingContext() {
return rctx;
}

public LineString getGeometry() {
CoordinateArrayListSequence coordinates = makeCoordinates(edges.toArray(new Edge[0]));
return GeometryUtils.getGeometryFactory().createLineString(coordinates);
}

}

0 comments on commit db87705

Please sign in to comment.