use of org.opentripplanner.routing.graph.Edge in project OpenTripPlanner by opentripplanner.
the class EuclideanRemainingWeightHeuristic method initialize.
@Override
public void initialize(RoutingRequest options, long abortTime) {
RoutingRequest req = options;
Vertex target = req.rctx.target;
this.transit = req.modes.isTransit();
maxStreetSpeed = req.getStreetSpeedUpperBound();
maxTransitSpeed = req.getTransitSpeedUpperBound();
if (target.getDegreeIn() == 1) {
Edge edge = Iterables.getOnlyElement(target.getIncoming());
if (edge instanceof FreeEdge) {
target = edge.getFromVertex();
}
}
lat = target.getLat();
lon = target.getLon();
requiredWalkDistance = determineRequiredWalkDistance(req);
walkReluctance = req.walkReluctance;
}
use of org.opentripplanner.routing.graph.Edge in project OpenTripPlanner by opentripplanner.
the class State method multipleOptionsBefore.
public boolean multipleOptionsBefore() {
boolean foundAlternatePaths = false;
TraverseMode requestedMode = getNonTransitMode();
for (Edge out : backState.vertex.getOutgoing()) {
if (out == backEdge) {
continue;
}
if (!(out instanceof StreetEdge)) {
continue;
}
State outState = out.traverse(backState);
if (outState == null) {
continue;
}
if (!outState.getBackMode().equals(requestedMode)) {
// walking a bike, so, not really an exit
continue;
}
// this section handles the case of an option which is only an option if you walk your
// bike. It is complicated because you will not need to walk your bike until one
// edge after the current edge.
// now, from here, try a continuing path.
Vertex tov = outState.getVertex();
boolean found = false;
for (Edge out2 : tov.getOutgoing()) {
State outState2 = out2.traverse(outState);
if (outState2 != null && !outState2.getBackMode().equals(requestedMode)) {
// walking a bike, so, not really an exit
continue;
}
found = true;
break;
}
if (!found) {
continue;
}
// there were paths we didn't take.
foundAlternatePaths = true;
break;
}
return foundAlternatePaths;
}
use of org.opentripplanner.routing.graph.Edge in project OpenTripPlanner by opentripplanner.
the class StreetVertex method getIntersectionName.
/**
* Creates intersection name out of all outgoing names
*
* This can be:
* - name of the street if it is only 1
* - unnamedStreed (localized in requested language) if it doesn't have a name
* - corner of 0 and 1 (localized corner of zero and first street in the corner)
*
* @param locale Wanted locale
* @return already localized street names and non-localized corner of x and unnamedStreet
*/
public I18NString getIntersectionName(Locale locale) {
I18NString calculatedName = null;
// generate names for corners when no name was given
Set<String> uniqueNameSet = new HashSet<String>();
for (Edge e : getOutgoing()) {
if (e instanceof StreetEdge) {
uniqueNameSet.add(e.getName(locale));
}
}
List<String> uniqueNames = new ArrayList<String>(uniqueNameSet);
if (uniqueNames.size() > 1) {
calculatedName = new LocalizedString("corner", new String[] { uniqueNames.get(0), uniqueNames.get(1) });
} else if (uniqueNames.size() == 1) {
calculatedName = new NonLocalizedString(uniqueNames.get(0));
} else {
calculatedName = new LocalizedString("unnamedStreet", (String[]) null);
}
return calculatedName;
}
use of org.opentripplanner.routing.graph.Edge in project OpenTripPlanner by opentripplanner.
the class MidblockMatchState method getNextStates.
@Override
public List<MatchState> getNextStates() {
ArrayList<MatchState> nextStates = new ArrayList<MatchState>();
if (routeIndex.getSegmentIndex() == routeGeometry.getNumPoints() - 1) {
// this has either hit the end, or gone off the end. It's not real clear which.
// for now, let's assume it means that the ending is somewhere along this edge,
// so we return an end state
Coordinate pt = routeIndex.getCoordinate(routeGeometry);
double error = distance(pt, edgeIndex.getCoordinate(edgeGeometry));
nextStates.add(new EndMatchState(this, error, 0));
return nextStates;
}
LinearIterator it = new LinearIterator(routeGeometry, routeIndex);
if (it.hasNext()) {
it.next();
LinearLocation routeSuccessor = it.getLocation();
// now we want to see where this new point is in terms of the edge's geometry
Coordinate newRouteCoord = routeSuccessor.getCoordinate(routeGeometry);
LinearLocation newEdgeIndex = indexedEdge.project(newRouteCoord);
Coordinate edgeCoord = newEdgeIndex.getCoordinate(edgeGeometry);
if (newEdgeIndex.compareTo(edgeIndex) <= 0) {
/* this should not require the try/catch, but there is a bug in JTS */
try {
LinearLocation projected2 = indexedEdge.indexOfAfter(edgeCoord, edgeIndex);
// another bug in JTS
if (Double.isNaN(projected2.getSegmentFraction())) {
// we are probably moving backwards
return Collections.emptyList();
} else {
newEdgeIndex = projected2;
if (newEdgeIndex.equals(edgeIndex)) {
return Collections.emptyList();
}
}
edgeCoord = newEdgeIndex.getCoordinate(edgeGeometry);
} catch (AssertionFailedException e) {
// we are not making progress, so just return an empty list
return Collections.emptyList();
}
}
if (newEdgeIndex.getSegmentIndex() == edgeGeometry.getNumPoints() - 1) {
// we might choose to continue from the end of the edge and a point mid-way
// along this route segment
// find nearest point that makes progress along the route
Vertex toVertex = edge.getToVertex();
Coordinate endCoord = toVertex.getCoordinate();
LocationIndexedLine indexedRoute = new LocationIndexedLine(routeGeometry);
// FIXME: it would be better to do this project/indexOfAfter in one step
// as the two-step version could snap to a bad place and be unable to escape.
LinearLocation routeProjectedEndIndex = indexedRoute.project(endCoord);
Coordinate routeProjectedEndCoord = routeProjectedEndIndex.getCoordinate(routeGeometry);
if (routeProjectedEndIndex.compareTo(routeIndex) <= 0) {
try {
routeProjectedEndIndex = indexedRoute.indexOfAfter(routeProjectedEndCoord, routeIndex);
if (Double.isNaN(routeProjectedEndIndex.getSegmentFraction())) {
// can't go forward
// this is bad, but not terrible
routeProjectedEndIndex = routeIndex;
// since we are advancing along the edge
}
} catch (AssertionFailedException e) {
routeProjectedEndIndex = routeIndex;
}
routeProjectedEndCoord = routeProjectedEndIndex.getCoordinate(routeGeometry);
}
double positionError = distance(routeProjectedEndCoord, endCoord);
double travelAlongRoute = distanceAlongGeometry(routeGeometry, routeIndex, routeProjectedEndIndex);
double travelAlongEdge = distanceAlongGeometry(edgeGeometry, edgeIndex, newEdgeIndex);
double travelError = Math.abs(travelAlongEdge - travelAlongRoute);
double error = positionError + travelError;
if (error > MAX_ERROR) {
// totally wrong
return nextStates;
}
for (Edge e : getOutgoingMatchableEdges(toVertex)) {
double cost = error + NEW_SEGMENT_PENALTY;
if (!carsCanTraverse(e)) {
cost += NO_TRAVERSE_PENALTY;
}
MatchState nextState = new MidblockMatchState(this, routeGeometry, e, routeProjectedEndIndex, new LinearLocation(), cost, travelAlongRoute);
nextStates.add(nextState);
}
} else {
double travelAlongEdge = distanceAlongGeometry(edgeGeometry, edgeIndex, newEdgeIndex);
double travelAlongRoute = distanceAlongGeometry(routeGeometry, routeIndex, routeSuccessor);
double travelError = Math.abs(travelAlongRoute - travelAlongEdge);
double positionError = distance(edgeCoord, newRouteCoord);
double error = travelError + positionError;
MatchState nextState = new MidblockMatchState(this, routeGeometry, edge, routeSuccessor, newEdgeIndex, error, travelAlongRoute);
nextStates.add(nextState);
// it's also possible that, although we have not yet reached the end of this edge,
// we are going to turn, because the route turns earlier than the edge. In that
// case, we jump to the corner, and our error is the distance from the route point
// and the corner
Vertex toVertex = edge.getToVertex();
double travelAlongOldEdge = distanceAlongGeometry(edgeGeometry, edgeIndex, null);
for (Edge e : getOutgoingMatchableEdges(toVertex)) {
Geometry newEdgeGeometry = e.getGeometry();
LocationIndexedLine newIndexedEdge = new LocationIndexedLine(newEdgeGeometry);
newEdgeIndex = newIndexedEdge.project(newRouteCoord);
Coordinate newEdgeCoord = newEdgeIndex.getCoordinate(newEdgeGeometry);
positionError = distance(newEdgeCoord, newRouteCoord);
travelAlongEdge = travelAlongOldEdge + distanceAlongGeometry(newEdgeGeometry, new LinearLocation(), newEdgeIndex);
travelError = Math.abs(travelAlongRoute - travelAlongEdge);
error = travelError + positionError;
if (error > MAX_ERROR) {
// totally wrong
return nextStates;
}
double cost = error + NEW_SEGMENT_PENALTY;
if (!carsCanTraverse(e)) {
cost += NO_TRAVERSE_PENALTY;
}
nextState = new MidblockMatchState(this, routeGeometry, e, routeSuccessor, new LinearLocation(), cost, travelAlongRoute);
nextStates.add(nextState);
}
}
return nextStates;
} else {
Coordinate routeCoord = routeIndex.getCoordinate(routeGeometry);
LinearLocation projected = indexedEdge.project(routeCoord);
double locationError = distance(projected.getCoordinate(edgeGeometry), routeCoord);
MatchState end = new EndMatchState(this, locationError, 0);
return Arrays.asList(end);
}
}
use of org.opentripplanner.routing.graph.Edge in project OpenTripPlanner by opentripplanner.
the class GraphPathToTripPlanConverter method generateItinerary.
/**
* Generate an itinerary from a {@link GraphPath}. This method first slices the list of states
* at the leg boundaries. These smaller state arrays are then used to generate legs. Finally the
* rest of the itinerary is generated based on the complete state array.
*
* @param path The graph path to base the itinerary on
* @param showIntermediateStops Whether to include intermediate stops in the itinerary or not
* @return The generated itinerary
*/
public static Itinerary generateItinerary(GraphPath path, boolean showIntermediateStops, boolean disableAlertFiltering, Locale requestedLocale) {
Itinerary itinerary = new Itinerary();
State[] states = new State[path.states.size()];
State lastState = path.states.getLast();
states = path.states.toArray(states);
Edge[] edges = new Edge[path.edges.size()];
edges = path.edges.toArray(edges);
Graph graph = path.getRoutingContext().graph;
FareService fareService = graph.getService(FareService.class);
State[][] legsStates = sliceStates(states);
if (fareService != null) {
itinerary.fare = fareService.getCost(path);
}
for (State[] legStates : legsStates) {
itinerary.addLeg(generateLeg(graph, legStates, showIntermediateStops, disableAlertFiltering, requestedLocale));
}
addWalkSteps(graph, itinerary.legs, legsStates, requestedLocale);
fixupLegs(itinerary.legs, legsStates);
itinerary.duration = lastState.getElapsedTimeSeconds();
itinerary.startTime = makeCalendar(states[0]);
itinerary.endTime = makeCalendar(lastState);
calculateTimes(itinerary, states);
calculateElevations(itinerary, edges);
itinerary.walkDistance = lastState.getWalkDistance();
itinerary.transfers = lastState.getNumBoardings();
if (itinerary.transfers > 0 && !(states[0].getVertex() instanceof OnboardDepartVertex)) {
itinerary.transfers--;
}
return itinerary;
}
Aggregations