public void testTransfers() throws Exception { TransferTable transferTable = graph.getTransferTable(); // create dummy routes and trips Route fromRoute = new Route(); fromRoute.setId(new AgencyAndId("agency", "1")); Trip fromTrip = new Trip(); fromTrip.setId(new AgencyAndId("agency", "1.1")); fromTrip.setRoute(fromRoute); Route toRoute = new Route(); toRoute.setId(new AgencyAndId("agency", "2")); Trip toTrip = new Trip(); toTrip.setId(new AgencyAndId("agency", "2.1")); toTrip.setRoute(toRoute); Trip toTrip2 = new Trip(); toTrip2.setId(new AgencyAndId("agency", "2.2")); toTrip2.setRoute(toRoute); // find stops Stop stopK = ((TransitStopArrive) graph.getVertex("agency_K_arrive")).getStop(); Stop stopN = ((TransitStopDepart) graph.getVertex("agency_N_depart")).getStop(); Stop stopM = ((TransitStopDepart) graph.getVertex("agency_M_depart")).getStop(); assertTrue(transferTable.hasPreferredTransfers()); assertEquals( StopTransfer.UNKNOWN_TRANSFER, transferTable.getTransferTime(stopN, stopM, fromTrip, toTrip)); assertEquals( StopTransfer.FORBIDDEN_TRANSFER, transferTable.getTransferTime(stopK, stopM, fromTrip, toTrip)); assertEquals( StopTransfer.PREFERRED_TRANSFER, transferTable.getTransferTime(stopN, stopK, toTrip, toTrip2)); assertEquals( StopTransfer.TIMED_TRANSFER, transferTable.getTransferTime(stopN, stopK, fromTrip, toTrip)); assertEquals(15, transferTable.getTransferTime(stopN, stopK, fromTrip, toTrip2)); Vertex e_arrive = graph.getVertex("agency_E_arrive"); Vertex f_depart = graph.getVertex("agency_F_depart"); Edge edge = new TransferEdge(e_arrive, f_depart, 10000, 10000); long startTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 18, 0, 50, 0); Vertex stop_b = graph.getVertex("agency_B_depart"); Vertex stop_g = graph.getVertex("agency_G_arrive"); RoutingRequest options = new RoutingRequest(); options.dateTime = startTime; options.setRoutingContext(graph, stop_b, stop_g); ShortestPathTree spt = aStar.getShortestPathTree(options); GraphPath path = spt.getPath(stop_g, false); assertNotNull(path); assertTrue( "expected to use much later trip due to min transfer time", path.getEndTime() - startTime > 4.5 * 60 * 60); /* cleanup */ e_arrive.removeOutgoing(edge); f_depart.removeIncoming(edge); }
public synchronized void initIndexes() { if (vertexIndex != null) { return; } graphService.setLoadLevel(LoadLevel.DEBUG); Graph graph = graphService.getGraph(); vertexIndex = new STRtree(); edgeIndex = new STRtree(); for (Vertex v : graph.getVertices()) { Envelope vertexEnvelope = new Envelope(v.getCoordinate()); vertexIndex.insert(vertexEnvelope, v); for (Edge e : v.getOutgoing()) { Envelope envelope; Geometry geometry = e.getGeometry(); if (geometry == null) { envelope = vertexEnvelope; } else { envelope = geometry.getEnvelopeInternal(); } edgeIndex.insert(envelope, e); } } vertexIndex.build(); edgeIndex.build(); }
/** * The safest bike lane should have a safety weight no lower than the time weight of a flat * street. This method divides the safety lengths by the length ratio of the safest street, * ensuring this property. * * @param graph */ private void applyBikeSafetyFactor(Graph graph) { _log.info( GraphBuilderAnnotation.register( graph, Variety.GRAPHWIDE, "Multiplying all bike safety values by " + (1 / bestBikeSafety))); HashSet<Edge> seenEdges = new HashSet<Edge>(); for (Vertex vertex : graph.getVertices()) { for (Edge e : vertex.getOutgoing()) { if (!(e instanceof PlainStreetEdge)) { continue; } PlainStreetEdge pse = (PlainStreetEdge) e; if (!seenEdges.contains(e)) { seenEdges.add(e); pse.setBicycleSafetyEffectiveLength( pse.getBicycleSafetyEffectiveLength() / bestBikeSafety); } } for (Edge e : vertex.getIncoming()) { if (!(e instanceof PlainStreetEdge)) { continue; } PlainStreetEdge pse = (PlainStreetEdge) e; if (!seenEdges.contains(e)) { seenEdges.add(e); pse.setBicycleSafetyEffectiveLength( pse.getBicycleSafetyEffectiveLength() / bestBikeSafety); } } } }
public static int getPreviousArriveTime( RoutingRequest request, int arrivalTime, Vertex stopVertex) { int bestArrivalTime = -1; request.arriveBy = true; // find the alights for (Edge prealight : stopVertex.getIncoming()) { if (prealight instanceof PreAlightEdge) { Vertex arrival = prealight.getFromVertex(); // this is the arrival vertex for (Edge alight : arrival.getIncoming()) { if (alight instanceof TransitBoardAlight) { State state = new State(alight.getToVertex(), arrivalTime, request); State result = alight.traverse(state); if (result == null) continue; int time = (int) result.getTime(); if (time > bestArrivalTime) { bestArrivalTime = time; } } } } } request.arriveBy = false; return bestArrivalTime; }
public void testPickupDropoff() throws Exception { Vertex stop_o = graph.getVertex("agency_O_depart"); Vertex stop_p = graph.getVertex("agency_P"); int i = 0; for (@SuppressWarnings("unused") Edge e : stop_o.getOutgoing()) { ++i; } assertTrue(i == 3); long startTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 19, 12, 0, 0); RoutingRequest options = new RoutingRequest(); options.dateTime = startTime; options.setRoutingContext(graph, stop_o, stop_p); ShortestPathTree spt = aStar.getShortestPathTree(options); GraphPath path = spt.getPath(stop_p, false); assertNotNull(path); long endTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 19, 12, 10, 0); assertEquals(endTime, path.getEndTime()); startTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 19, 12, 0, 1); options.dateTime = startTime; options.setRoutingContext(graph, stop_o, stop_p); spt = aStar.getShortestPathTree(options); path = spt.getPath(stop_p, false); assertNotNull(path); endTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 19, 15, 10, 0); assertEquals(endTime, path.getEndTime()); }
/** * Tear down this routing context, removing any temporary edges. * * @returns the number of edges removed. */ public int destroy() { int nRemoved = 0; if (origin != null) nRemoved += origin.removeTemporaryEdges(); if (target != null) nRemoved += target.removeTemporaryEdges(); for (Vertex v : intermediateVertices) nRemoved += v.removeTemporaryEdges(); return nRemoved; }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final InferredEdge other = (InferredEdge) obj; if (endVertex == null) { if (other.endVertex != null) { return false; } } else if (!endVertex.equals(other.endVertex)) { return false; } if (startVertex == null) { if (other.startVertex != null) { return false; } } else if (!startVertex.equals(other.startVertex)) { return false; } return true; }
/** Generates a TripPlan from a set of paths */ public TripPlan generatePlan(List<GraphPath> paths, RoutingRequest request) { GraphPath exemplar = paths.get(0); Vertex tripStartVertex = exemplar.getStartVertex(); Vertex tripEndVertex = exemplar.getEndVertex(); String startName = tripStartVertex.getName(); String endName = tripEndVertex.getName(); // Use vertex labels if they don't have names if (startName == null) { startName = tripStartVertex.getLabel(); } if (endName == null) { endName = tripEndVertex.getLabel(); } Place from = new Place(tripStartVertex.getX(), tripStartVertex.getY(), startName); Place to = new Place(tripEndVertex.getX(), tripEndVertex.getY(), endName); TripPlan plan = new TripPlan(from, to, request.getDateTime()); for (GraphPath path : paths) { Itinerary itinerary = generateItinerary(path, request.getShowIntermediateStops()); plan.addItinerary(itinerary); } return plan; }
public void testRoutingOverMidnight() throws Exception { // this route only runs on weekdays Vertex stop_g = graph.getVertex("agency:G_depart"); Vertex stop_h = graph.getVertex("agency:H_arrive"); ShortestPathTree spt; GraphPath path; RoutingRequest options = new RoutingRequest(); // Friday evening options.dateTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 18, 23, 20, 0); options.setRoutingContext(graph, stop_g, stop_h); spt = aStar.getShortestPathTree(options); path = spt.getPath(stop_h, false); assertNotNull(path); assertEquals(4, path.states.size()); // Saturday morning long startTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 19, 0, 5, 0); options.dateTime = startTime; options.setRoutingContext(graph, stop_g.getLabel(), stop_h.getLabel()); spt = aStar.getShortestPathTree(options); path = spt.getPath(stop_h, false); assertNotNull(path); assertEquals(4, path.states.size()); long endTime = path.getEndTime(); assertTrue(endTime < startTime + 60 * 60); }
public void drawAnotation(GraphBuilderAnnotation anno) { Envelope env = new Envelope(); Edge e = anno.getReferencedEdge(); if (e != null) { this.enqueueHighlightedEdge(e); env.expandToInclude(e.getFromVertex().getCoordinate()); env.expandToInclude(e.getToVertex().getCoordinate()); } ArrayList<Vertex> vertices = new ArrayList<Vertex>(); Vertex v = anno.getReferencedVertex(); if (v != null) { env.expandToInclude(v.getCoordinate()); vertices.add(v); } if (e == null && v == null) return; // make it a little bigger, especially needed for STOP_UNLINKED env.expandBy(0.02); // highlight relevant things this.clearHighlights(); this.setHighlightedVertices(vertices); // zoom the graph display this.zoomToEnvelope(env); // and draw this.draw(); }
public static int getNextDepartTime( RoutingRequest request, int departureTime, Vertex stopVertex) { int bestArrivalTime = Integer.MAX_VALUE; request.arriveBy = false; // find the boards for (Edge preboard : stopVertex.getOutgoing()) { if (preboard instanceof PreBoardEdge) { Vertex departure = preboard.getToVertex(); // this is the departure vertex for (Edge board : departure.getOutgoing()) { if (board instanceof TransitBoardAlight) { State state = new State(board.getFromVertex(), departureTime, request); State result = board.traverse(state); if (result == null) continue; int time = (int) result.getTime(); if (time < bestArrivalTime) { bestArrivalTime = time; } } } } } request.arriveBy = true; return bestArrivalTime; }
public void testFrequencies() { Vertex stop_u = graph.getVertex("agency_U_depart"); Vertex stop_v = graph.getVertex("agency_V_arrive"); ShortestPathTree spt; GraphPath path; RoutingRequest options = new RoutingRequest(); options.setModes(new TraverseModeSet("TRANSIT")); options.dateTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 0, 0, 0); options.setRoutingContext(graph, stop_u, stop_v); // U to V - original stop times - shouldn't be used spt = aStar.getShortestPathTree(options); path = spt.getPath(stop_v, false); assertNotNull(path); assertEquals(4, path.states.size()); long endTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 6, 40, 0); assertEquals(endTime, path.getEndTime()); // U to V - first frequency options.dateTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 7, 0, 0); options.setRoutingContext(graph, stop_u, stop_v); spt = aStar.getShortestPathTree(options); path = spt.getPath(stop_v, false); assertNotNull(path); assertEquals(4, path.states.size()); endTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 7, 40, 0); assertEquals(endTime, path.getEndTime()); // U to V - second frequency options.dateTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 14, 0, 0); options.setRoutingContext(graph, stop_u, stop_v); spt = aStar.getShortestPathTree(options); path = spt.getPath(stop_v, false); assertNotNull(path); assertEquals(4, path.states.size()); endTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 14, 40, 0); assertEquals(endTime, path.getEndTime()); boolean boarded = false; for (FrequencyBoard e : filter(stop_u.getOutgoing(), FrequencyBoard.class)) { boarded = true; FrequencyBoard board = (FrequencyBoard) e; FrequencyBasedTripPattern pattern = board.getPattern(); int previousArrivalTime = pattern.getPreviousArrivalTime(0, 0, false, false, false); assertTrue(previousArrivalTime < 0); previousArrivalTime = pattern.getPreviousArrivalTime(0, 60 * 60 * 7 - 1, false, false, false); assertEquals(60 * 60 * 6, previousArrivalTime); previousArrivalTime = pattern.getPreviousArrivalTime(0, 60 * 60 * 11, false, false, false); assertEquals(60 * 60 * 10, previousArrivalTime); previousArrivalTime = pattern.getPreviousArrivalTime(0, 60 * 60 * 18, false, false, false); assertEquals(60 * 60 * 16, previousArrivalTime); } assertTrue(boarded); }
private Vertex getVertexByCrossStreets(String s1, String s2) { for (Vertex v : Context.getInstance().graph.getVertices()) { if (v.getName().contains(s1) && v.getName().contains(s2)) { return v; } } return null; }
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((endVertex == null) ? 0 : endVertex.hashCode()); result = prime * result + ((startVertex == null) ? 0 : startVertex.hashCode()); return result; }
public static DisjointSet<Vertex> getConnectedComponents(Graph graph) { DisjointSet<Vertex> components = new DisjointSet<Vertex>(); for (Vertex v : graph.getVertices()) { for (Edge e : v.getOutgoing()) { components.union(e.getFromVertex(), e.getToVertex()); } } return components; }
public void testBoardAlight() throws Exception { Vertex stop_a_depart = graph.getVertex("agency:A_depart"); Vertex stop_b_depart = graph.getVertex("agency:B_depart"); assertEquals(1, stop_a_depart.getDegreeOut()); assertEquals(3, stop_b_depart.getDegreeOut()); for (Edge e : stop_a_depart.getOutgoing()) { assertEquals(TransitBoardAlight.class, e.getClass()); assertTrue(((TransitBoardAlight) e).boarding); } TransitBoardAlight pb = (TransitBoardAlight) stop_a_depart.getOutgoing().iterator().next(); Vertex journey_a_1 = pb.getToVertex(); assertEquals(1, journey_a_1.getDegreeIn()); for (Edge e : journey_a_1.getOutgoing()) { if (e.getToVertex() instanceof TransitStop) { assertEquals(TransitBoardAlight.class, e.getClass()); } else { assertEquals(PatternHop.class, e.getClass()); } } }
public void testWheelchairAccessible() throws Exception { Vertex near_a = graph.getVertex("near_1_agency_entrance_a"); Vertex near_b = graph.getVertex("near_1_agency_entrance_b"); Vertex near_c = graph.getVertex("near_1_agency_C"); Vertex near_e = graph.getVertex("near_1_agency_E"); Vertex stop_d = graph.getVertex("agency:D"); Vertex split_d = null; for (StreetTransitLink e : Iterables.filter(stop_d.getOutgoing(), StreetTransitLink.class)) { split_d = e.getToVertex(); } RoutingRequest options = new RoutingRequest(); options.wheelchairAccessible = true; options.dateTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 18, 0, 0, 0); ShortestPathTree spt; GraphPath path; // stop B is accessible, so there should be a path. options.setRoutingContext(graph, near_a, near_b); spt = aStar.getShortestPathTree(options); path = spt.getPath(near_b, false); assertNotNull(path); // stop C is not accessible, so there should be no path. options.setRoutingContext(graph, near_a, near_c); spt = aStar.getShortestPathTree(options); path = spt.getPath(near_c, false); assertNull(path); // stop E has no accessibility information, but we should still be able to route to it. options.setRoutingContext(graph, near_a, near_e); spt = aStar.getShortestPathTree(options); path = spt.getPath(near_e, false); assertNotNull(path); // from stop A to stop D would normally be trip 1.1 to trip 2.1, arriving at 00:30. But trip // 2 is not accessible, so we'll do 1.1 to 3.1, arriving at 01:00 GregorianCalendar time = new GregorianCalendar(2009, 8, 18, 0, 0, 0); time.setTimeZone(TimeZone.getTimeZone("America/New_York")); options.dateTime = TestUtils.toSeconds(time); options.setRoutingContext(graph, near_a, split_d); spt = aStar.getShortestPathTree(options); time.add(Calendar.HOUR, 1); time.add(Calendar.SECOND, 1); // for the StreetTransitLink path = spt.getPath(split_d, false); assertNotNull(path); assertEquals(TestUtils.toSeconds(time), path.getEndTime()); }
public TargetBound(RoutingRequest options) { this.options = options; if (options.rctx.target != null) { this.realTarget = options.rctx.target; this.realTargetCoordinate = realTarget.getCoordinate(); this.distanceToNearestTransitStop = realTarget.getDistanceToNearestTransitStop(); bounders = new ArrayList<State>(); transitLocalStreets = options.rctx.graph.getService(TransitLocalStreetService.class); speedUpperBound = options.getSpeedUpperBound(); this.speedWeight = options.getWalkReluctance() / speedUpperBound; this.transferTimeInWalkDistance = options.getTransferSlack() / options.getWalkSpeed(); } }
public void reset(RoutingRequest options) { this.options = options; if (realTarget != options.rctx.target) { this.realTarget = options.rctx.target; this.realTargetCoordinate = realTarget.getCoordinate(); this.distanceToNearestTransitStop = realTarget.getDistanceToNearestTransitStop(); bounders = new ArrayList<State>(); Arrays.fill(distance, -1); } spt = new ArrayMultiShortestPathTree(options); transitLocalStreets = options.rctx.graph.getService(TransitLocalStreetService.class); speedUpperBound = options.getSpeedUpperBound(); this.speedWeight = options.getWalkReluctance() / speedUpperBound; }
/* Somewhat hackish convenience method to grab a hop edge on a particular route leaving a particular stop. */ private PatternHop getHopEdge(String stopId, String routeId) { Vertex stopDepartVertex = graph.getVertex("agency:" + stopId + "_depart"); for (Edge edge : stopDepartVertex.getOutgoing()) { if (edge instanceof TransitBoardAlight) { TransitBoardAlight tba = ((TransitBoardAlight) edge); if (tba.boarding && tba.getPattern().route.getId().getId().equals(routeId)) { for (Edge edge2 : tba.getToVertex().getOutgoing()) { if (edge2 instanceof PatternHop) { return (PatternHop) edge2; } } } } } return null; }
public void testBoardAlightStopIndex() { Vertex stop_b_arrive = graph.getVertex("agency:C_arrive"); Vertex stop_b_depart = graph.getVertex("agency:C_depart"); Map<TripPattern, Integer> stopIndex = new HashMap<TripPattern, Integer>(); for (Edge edge : stop_b_depart.getOutgoing()) { TransitBoardAlight tba = (TransitBoardAlight) edge; stopIndex.put(tba.getPattern(), tba.getStopIndex()); } for (Edge edge : stop_b_arrive.getIncoming()) { TransitBoardAlight tba = (TransitBoardAlight) edge; if (stopIndex.containsKey(tba.getPattern())) assertEquals((Integer) stopIndex.get(tba.getPattern()), new Integer(tba.getStopIndex())); } }
public void zoomToVertex(Vertex v) { Envelope e = new Envelope(); e.expandToInclude(v.getCoordinate()); e.expandBy(0.002); modelBounds = e; drawLevel = DRAW_ALL; }
private void labelState(State s, String str) { fill(240, 240, 240); Vertex v = s.getVertex(); drawVertex(v, 8); str += " " + shortDateFormat.format(new Date(s.getTime() * 1000)); str += " [" + (int) s.getWeight() + "]"; double x = toScreenX(v.getX()) + 10; double y = toScreenY(v.getY()); double dy = y - lastLabelY; if (dy == 0) { y = lastLabelY + 20; } else if (Math.abs(dy) < 20) { y = lastLabelY + Math.signum(dy) * 20; } text(str, (float) x, (float) y); lastLabelY = y; }
public double getTargetDistance(Vertex vertex) { int vertexIndex = vertex.getIndex(); if (vertexIndex < distance.length) { if (distance[vertexIndex] > 0.0) { return distance[vertexIndex]; } else { double d = distanceLibrary.fastDistance( realTargetCoordinate.y, realTargetCoordinate.x, vertex.getY(), vertex.getX()); distance[vertexIndex] = d; return d; } } else { return distanceLibrary.fastDistance( realTargetCoordinate.y, realTargetCoordinate.x, vertex.getY(), vertex.getX()); } }
protected List<Edge> getOutgoingMatchableEdges(Vertex vertex) { List<Edge> edges = new ArrayList<Edge>(); for (Edge e : vertex.getOutgoing()) { if (!(e instanceof StreetEdge)) continue; if (e.getGeometry() == null) continue; edges.add(e); } return edges; }
public PatternHop getHopOut(Vertex v) { for (TransitBoardAlight e : filter(v.getOutgoing(), TransitBoardAlight.class)) { if (!e.isBoarding()) continue; for (PatternHop f : filter(e.getToVertex().getOutgoing(), PatternHop.class)) { return f; } } return null; }
/** * Makes a new Place from a state. Contains information about time. * * @return */ private Place makePlace(State state, boolean time) { Vertex v = state.getVertex(); Coordinate endCoord = v.getCoordinate(); String name = v.getName(); Place place; if (time) { Calendar timeAtState = makeCalendar(state); place = new Place(endCoord.x, endCoord.y, name, timeAtState); } else { place = new Place(endCoord.x, endCoord.y, name); } if (v instanceof TransitVertex) { TransitVertex transitVertex = (TransitVertex) v; place.stopId = transitVertex.getStopId(); place.stopCode = transitVertex.getStopCode(); place.zoneId = state.getZone(); } return place; }
/* * Iterate through all vertices and their (outgoing) edges. If they are of 'interesting' types, add them to the corresponding spatial index. */ public synchronized void buildSpatialIndex() { vertexIndex = new STRtree(); edgeIndex = new STRtree(); Envelope env; // int xminx, xmax, ymin, ymax; for (Vertex v : graph.getVertices()) { Coordinate c = v.getCoordinate(); env = new Envelope(c); vertexIndex.insert(env, v); for (Edge e : v.getOutgoing()) { if (e.getGeometry() == null) continue; if (e instanceof PatternEdge || e instanceof StreetTransitLink || e instanceof StreetEdge) { env = e.getGeometry().getEnvelopeInternal(); edgeIndex.insert(env, e); } } } vertexIndex.build(); edgeIndex.build(); }
public void highlightVertex(Vertex v) { Coordinate c = v.getCoordinate(); double xd = 0, yd = 0; while (!modelBounds.contains(c)) { xd = modelBounds.getWidth() / 100; yd = modelBounds.getHeight() / 100; modelBounds.expandBy(xd, yd); } modelBounds.expandBy(xd, yd); highlightedVertex = v; drawLevel = DRAW_ALL; }
/** * Internals of getRegionsForVertex; keeps track of seen vertices to avoid loops. * * @param regionData * @param vertex * @param seen * @param depth * @return */ private static HashSet<Integer> getRegionsForVertex( RegionData regionData, Vertex vertex, HashSet<Vertex> seen, int depth) { seen.add(vertex); HashSet<Integer> regions = new HashSet<Integer>(); int region = vertex.getGroupIndex(); if (region >= 0) { regions.add(region); } else { for (Edge e : vertex.getOutgoing()) { final Vertex tov = e.getToVertex(); if (!seen.contains(tov)) regions.addAll(getRegionsForVertex(regionData, tov, seen, depth + 1)); } for (Edge e : vertex.getIncoming()) { final Vertex fromv = e.getFromVertex(); if (!seen.contains(fromv)) regions.addAll(getRegionsForVertex(regionData, fromv, seen, depth + 1)); } } return regions; }