use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.
the class RoundTripRoutingTemplateTest method testCalcRoundTrip.
@Test
public void testCalcRoundTrip() throws Exception {
Weighting weighting = new FastestWeighting(carFE);
Graph g = createTestGraph(true);
RoundTripRoutingTemplate rTripRouting = new RoundTripRoutingTemplate(new GHRequest(), new GHResponse(), null, 1);
LocationIndex locationIndex = new LocationIndexTree(g, new RAMDirectory()).prepareIndex();
QueryResult qr4 = locationIndex.findClosest(0.05, 0.25, EdgeFilter.ALL_EDGES);
assertEquals(4, qr4.getClosestNode());
QueryResult qr5 = locationIndex.findClosest(0.00, 0.05, EdgeFilter.ALL_EDGES);
assertEquals(5, qr5.getClosestNode());
QueryResult qr6 = locationIndex.findClosest(0.00, 0.10, EdgeFilter.ALL_EDGES);
assertEquals(6, qr6.getClosestNode());
QueryGraph qGraph = new QueryGraph(g);
qGraph.lookup(qr4, qr5);
rTripRouting.setQueryResults(Arrays.asList(qr5, qr4, qr5));
List<Path> paths = rTripRouting.calcPaths(qGraph, new RoutingAlgorithmFactorySimple(), new AlgorithmOptions(DIJKSTRA_BI, weighting, tMode));
assertEquals(2, paths.size());
assertEquals(Helper.createTList(5, 6, 3, 4), paths.get(0).calcNodes());
assertEquals(Helper.createTList(4, 8, 7, 6, 5), paths.get(1).calcNodes());
qGraph = new QueryGraph(g);
qGraph.lookup(qr4, qr6);
rTripRouting.setQueryResults(Arrays.asList(qr6, qr4, qr6));
paths = rTripRouting.calcPaths(qGraph, new RoutingAlgorithmFactorySimple(), new AlgorithmOptions(DIJKSTRA_BI, weighting, tMode));
assertEquals(2, paths.size());
assertEquals(Helper.createTList(6, 3, 4), paths.get(0).calcNodes());
assertEquals(Helper.createTList(4, 8, 7, 6), paths.get(1).calcNodes());
}
use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.
the class TripFromLabel method parsePathIntoLegs.
// We are parsing a string of edges into a hierarchical trip.
// One could argue that one should never write a parser
// by hand, because it is always ugly, but use a parser library.
// The code would then read like a specification of what paths through the graph mean.
private List<Trip.Leg> parsePathIntoLegs(List<Label.Transition> path, GraphExplorer graph, Weighting weighting, Translation tr) {
if (path.size() <= 1) {
return Collections.emptyList();
}
if (GtfsStorage.EdgeType.ENTER_PT == path.get(1).edge.edgeType) {
final GtfsStorage.FeedIdWithTimezone feedIdWithTimezone = gtfsStorage.getTimeZones().get(path.get(1).edge.timeZoneId);
List<Trip.Leg> result = new ArrayList<>();
long boardTime = -1;
List<Label.Transition> partition = null;
for (int i = 1; i < path.size(); i++) {
Label.Transition transition = path.get(i);
Label.EdgeLabel edge = path.get(i).edge;
if (edge.edgeType == GtfsStorage.EdgeType.BOARD) {
boardTime = transition.label.currentTime;
partition = new ArrayList<>();
}
if (partition != null) {
partition.add(path.get(i));
}
if (EnumSet.of(GtfsStorage.EdgeType.TRANSFER, GtfsStorage.EdgeType.LEAVE_TIME_EXPANDED_NETWORK).contains(edge.edgeType)) {
Geometry lineString = lineStringFromEdges(partition);
GtfsRealtime.TripDescriptor tripDescriptor;
try {
tripDescriptor = GtfsRealtime.TripDescriptor.parseFrom(realtimeFeed.getTripDescriptor(partition.get(0).edge.edgeIteratorState.getEdge()));
} catch (InvalidProtocolBufferException e) {
throw new RuntimeException(e);
}
final StopsFromBoardHopDwellEdges stopsFromBoardHopDwellEdges = new StopsFromBoardHopDwellEdges(feedIdWithTimezone.feedId, tripDescriptor);
partition.stream().filter(e -> EnumSet.of(GtfsStorage.EdgeType.HOP, GtfsStorage.EdgeType.BOARD, GtfsStorage.EdgeType.DWELL).contains(e.edge.edgeType)).forEach(stopsFromBoardHopDwellEdges::next);
stopsFromBoardHopDwellEdges.finish();
List<Trip.Stop> stops = stopsFromBoardHopDwellEdges.stops;
result.add(new Trip.PtLeg(feedIdWithTimezone.feedId, partition.get(0).edge.nTransfers == 0, tripDescriptor.getTripId(), tripDescriptor.getRouteId(), edges(partition).map(edgeLabel -> edgeLabel.edgeIteratorState).collect(Collectors.toList()), stops, partition.stream().mapToDouble(t -> t.edge.distance).sum(), path.get(i - 1).label.currentTime - boardTime, lineString));
partition = null;
}
}
return result;
} else {
InstructionList instructions = new InstructionList(tr);
InstructionsFromEdges instructionsFromEdges = new InstructionsFromEdges(path.get(1).edge.edgeIteratorState.getBaseNode(), graph.getGraph(), weighting, weighting.getFlagEncoder(), graph.getNodeAccess(), tr, instructions);
int prevEdgeId = -1;
for (int i = 1; i < path.size(); i++) {
EdgeIteratorState edge = path.get(i).edge.edgeIteratorState;
instructionsFromEdges.next(edge, i, prevEdgeId);
prevEdgeId = edge.getEdge();
}
instructionsFromEdges.finish();
final Instant departureTime = Instant.ofEpochMilli(path.get(0).label.currentTime);
final Instant arrivalTime = Instant.ofEpochMilli(path.get(path.size() - 1).label.currentTime);
return Collections.singletonList(new Trip.WalkLeg("Walk", Date.from(departureTime), edges(path).map(edgeLabel -> edgeLabel.edgeIteratorState).collect(Collectors.toList()), lineStringFromEdges(path), edges(path).mapToDouble(edgeLabel -> edgeLabel.distance).sum(), instructions, Date.from(arrivalTime)));
}
}
use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.
the class GraphHopperOSMTest method createSquareGraphInstance.
private GraphHopper createSquareGraphInstance(boolean withCH) {
CarFlagEncoder carEncoder = new CarFlagEncoder();
EncodingManager encodingManager = new EncodingManager(carEncoder);
Weighting weighting = new FastestWeighting(carEncoder);
GraphHopperStorage g = new GraphHopperStorage(Collections.singletonList(weighting), new RAMDirectory(), encodingManager, false, new GraphExtension.NoOpExtension()).create(20);
// 2---3---4
// / | \
// 1----8----5
// / | /
// 0----7---6
NodeAccess na = g.getNodeAccess();
na.setNode(0, 0.000, 0.000);
na.setNode(1, 0.001, 0.000);
na.setNode(2, 0.002, 0.000);
na.setNode(3, 0.002, 0.001);
na.setNode(4, 0.002, 0.002);
na.setNode(5, 0.001, 0.002);
na.setNode(6, 0.000, 0.002);
na.setNode(7, 0.000, 0.001);
na.setNode(8, 0.001, 0.001);
g.edge(0, 1, 100, true);
g.edge(1, 2, 100, true);
g.edge(2, 3, 100, true);
g.edge(3, 4, 100, true);
g.edge(4, 5, 100, true);
g.edge(5, 6, 100, true);
g.edge(6, 7, 100, true);
g.edge(7, 0, 100, true);
g.edge(1, 8, 110, true);
g.edge(3, 8, 110, true);
g.edge(5, 8, 110, true);
g.edge(7, 8, 110, true);
GraphHopper tmp = new GraphHopperOSM().setCHEnabled(withCH).setEncodingManager(encodingManager);
tmp.getCHFactoryDecorator().setWeightingsAsStrings("fastest");
tmp.setGraphHopperStorage(g);
tmp.postProcessing();
return tmp;
}
use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.
the class PrepareContractionHierarchiesTest method testMultiplePreparationsDifferentView.
@Test
public void testMultiplePreparationsDifferentView() {
CarFlagEncoder tmpCarEncoder = new CarFlagEncoder();
BikeFlagEncoder tmpBikeEncoder = new BikeFlagEncoder();
EncodingManager tmpEncodingManager = new EncodingManager(tmpCarEncoder, tmpBikeEncoder);
Weighting carWeighting = new FastestWeighting(tmpCarEncoder);
Weighting bikeWeighting = new FastestWeighting(tmpBikeEncoder);
List<Weighting> chWeightings = Arrays.asList(carWeighting, bikeWeighting);
GraphHopperStorage ghStorage = new GraphHopperStorage(chWeightings, dir, tmpEncodingManager, false, new GraphExtension.NoOpExtension()).create(1000);
initShortcutsGraph(ghStorage);
EdgeIteratorState edge = GHUtility.getEdge(ghStorage, 9, 14);
edge.setFlags(tmpBikeEncoder.setAccess(edge.getFlags(), false, false));
ghStorage.freeze();
checkPath(ghStorage, carWeighting, 7, 5, Helper.createTList(3, 9, 14, 16, 13, 12));
// detour around blocked 9,14
checkPath(ghStorage, bikeWeighting, 9, 5, Helper.createTList(3, 10, 14, 16, 13, 12));
}
use of com.graphhopper.routing.weighting.Weighting in project graphhopper by graphhopper.
the class PrepareContractionHierarchiesTest method testUnpackingOrder_Fastest.
@Test
public void testUnpackingOrder_Fastest() {
GraphHopperStorage ghStorage = createGHStorage();
CHGraph lg = ghStorage.getGraph(CHGraph.class);
Weighting w = new FastestWeighting(carEncoder);
initUnpackingGraph(ghStorage, lg, w);
PrepareContractionHierarchies prepare = new PrepareContractionHierarchies(dir, ghStorage, lg, weighting, tMode);
RoutingAlgorithm algo = prepare.createAlgo(lg, new AlgorithmOptions(DIJKSTRA_BI, weighting, tMode));
Path p = algo.calcPath(10, 6);
assertEquals(7, p.getDistance(), 1e-1);
assertEquals(Helper.createTList(10, 0, 1, 2, 3, 4, 5, 6), p.calcNodes());
}
Aggregations