Search in sources :

Example 16 with Weighting

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());
}
Also used : GHResponse(com.graphhopper.GHResponse) LocationIndex(com.graphhopper.storage.index.LocationIndex) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) QueryResult(com.graphhopper.storage.index.QueryResult) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) GHRequest(com.graphhopper.GHRequest) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Test(org.junit.Test)

Example 17 with Weighting

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)));
    }
}
Also used : java.util(java.util) Stop(com.conveyal.gtfs.model.Stop) LoggerFactory(org.slf4j.LoggerFactory) LocalDateTime(java.time.LocalDateTime) InstructionsFromEdges(com.graphhopper.routing.InstructionsFromEdges) Duration(java.time.Duration) Geometry(com.vividsolutions.jts.geom.Geometry) Fares(com.graphhopper.gtfs.fare.Fares) StreamSupport(java.util.stream.StreamSupport) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) GtfsRealtime(com.google.transit.realtime.GtfsRealtime) Coordinate(com.vividsolutions.jts.geom.Coordinate) SECONDS(java.time.temporal.ChronoUnit.SECONDS) com.graphhopper.util(com.graphhopper.util) Logger(org.slf4j.Logger) GTFSFeed(com.conveyal.gtfs.GTFSFeed) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Label.reverseEdges(com.graphhopper.reader.gtfs.Label.reverseEdges) Fun(org.mapdb.Fun) Stream(java.util.stream.Stream) StopTime(com.conveyal.gtfs.model.StopTime) PathWrapper(com.graphhopper.PathWrapper) Weighting(com.graphhopper.routing.weighting.Weighting) Trip(com.graphhopper.Trip) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) InstructionsFromEdges(com.graphhopper.routing.InstructionsFromEdges) Stop(com.conveyal.gtfs.model.Stop) Trip(com.graphhopper.Trip) Instant(java.time.Instant) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) GtfsRealtime(com.google.transit.realtime.GtfsRealtime) Geometry(com.vividsolutions.jts.geom.Geometry)

Example 18 with Weighting

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;
}
Also used : FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) AbstractWeighting(com.graphhopper.routing.weighting.AbstractWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) GraphHopper(com.graphhopper.GraphHopper)

Example 19 with Weighting

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));
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) BikeFlagEncoder(com.graphhopper.routing.util.BikeFlagEncoder) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) Test(org.junit.Test)

Example 20 with Weighting

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());
}
Also used : FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Test(org.junit.Test)

Aggregations

Weighting (com.graphhopper.routing.weighting.Weighting)24 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)16 Test (org.junit.Test)16 ShortestWeighting (com.graphhopper.routing.weighting.ShortestWeighting)8 AbstractWeighting (com.graphhopper.routing.weighting.AbstractWeighting)6 LocationIndex (com.graphhopper.storage.index.LocationIndex)5 QueryResult (com.graphhopper.storage.index.QueryResult)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 GraphHopper (com.graphhopper.GraphHopper)3 Graph (com.graphhopper.storage.Graph)3 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)3 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)3 GHRequest (com.graphhopper.GHRequest)2 GHResponse (com.graphhopper.GHResponse)2 GraphHopperOSM (com.graphhopper.reader.osm.GraphHopperOSM)2 BikeFlagEncoder (com.graphhopper.routing.util.BikeFlagEncoder)2 CarFlagEncoder (com.graphhopper.routing.util.CarFlagEncoder)2 EncodingManager (com.graphhopper.routing.util.EncodingManager)2 AlgoHelperEntry (com.graphhopper.routing.util.TestAlgoCollector.AlgoHelperEntry)2