Search in sources :

Example 21 with Snap

use of com.graphhopper.storage.index.Snap in project graphhopper by graphhopper.

the class QueryGraphTest method testGetEdgeProps.

@Test
public void testGetEdgeProps() {
    initGraph(g);
    EdgeIteratorState e1 = GHUtility.getEdge(g, 0, 2);
    Snap res1 = createLocationResult(0.5, 0, e1, 0, EDGE);
    QueryGraph queryGraph = lookup(res1);
    // get virtual edge
    e1 = GHUtility.getEdge(queryGraph, res1.getClosestNode(), 0);
    EdgeIteratorState e2 = queryGraph.getEdgeIteratorState(e1.getEdge(), Integer.MIN_VALUE);
    assertEquals(e1.getEdge(), e2.getEdge());
}
Also used : Snap(com.graphhopper.storage.index.Snap) Test(org.junit.jupiter.api.Test)

Example 22 with Snap

use of com.graphhopper.storage.index.Snap in project graphhopper by graphhopper.

the class QueryGraphTest method testOneWay.

@Test
public void testOneWay() {
    NodeAccess na = g.getNodeAccess();
    na.setNode(0, 0, 0);
    na.setNode(1, 0, 1);
    GHUtility.setSpeed(60, true, false, encoder, g.edge(0, 1).setDistance(10));
    EdgeIteratorState edge = GHUtility.getEdge(g, 0, 1);
    Snap res1 = createLocationResult(0.1, 0.1, edge, 0, EDGE);
    Snap res2 = createLocationResult(0.1, 0.9, edge, 0, EDGE);
    QueryGraph queryGraph = lookup(Arrays.asList(res2, res1));
    assertEquals(2, res1.getClosestNode());
    assertEquals(new GHPoint(0, 0.1), res1.getSnappedPoint());
    assertEquals(3, res2.getClosestNode());
    assertEquals(new GHPoint(0, 0.9), res2.getSnappedPoint());
    assertEquals(2, getPoints(queryGraph, 0, 2).size());
    assertEquals(2, getPoints(queryGraph, 2, 3).size());
    assertEquals(2, getPoints(queryGraph, 3, 1).size());
    assertNull(GHUtility.getEdge(queryGraph, 3, 0));
    assertNull(GHUtility.getEdge(queryGraph, 2, 1));
}
Also used : Snap(com.graphhopper.storage.index.Snap) GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.jupiter.api.Test)

Example 23 with Snap

use of com.graphhopper.storage.index.Snap in project graphhopper by graphhopper.

the class GtfsReader method connectStopsToStreetNetwork.

void connectStopsToStreetNetwork() {
    EncodingManager em = graph.getEncodingManager();
    FlagEncoder footEncoder = em.getEncoder("foot");
    final EdgeFilter filter = new DefaultSnapFilter(new FastestWeighting(footEncoder), em.getBooleanEncodedValue(Subnetwork.key("foot")));
    for (Stop stop : feed.stops.values()) {
        if (stop.location_type == 0) {
            // Only stops. Not interested in parent stations for now.
            Snap locationSnap = walkNetworkIndex.findClosest(stop.stop_lat, stop.stop_lon, filter);
            Integer stopNode;
            if (locationSnap.isValid()) {
                stopNode = gtfsStorage.getStreetToPt().get(locationSnap.getClosestNode());
                if (stopNode == null) {
                    stopNode = out.createNode();
                    indexBuilder.addToAllTilesOnLine(stopNode, stop.stop_lat, stop.stop_lon, stop.stop_lat, stop.stop_lon);
                    gtfsStorage.getPtToStreet().put(stopNode, locationSnap.getClosestNode());
                    gtfsStorage.getStreetToPt().put(locationSnap.getClosestNode(), stopNode);
                }
            } else {
                stopNode = out.createNode();
                indexBuilder.addToAllTilesOnLine(stopNode, stop.stop_lat, stop.stop_lon, stop.stop_lat, stop.stop_lon);
            }
            gtfsStorage.getStationNodes().put(new GtfsStorage.FeedIdWithStopId(id, stop.stop_id), stopNode);
        }
    }
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) DefaultSnapFilter(com.graphhopper.routing.util.DefaultSnapFilter) EdgeFilter(com.graphhopper.routing.util.EdgeFilter) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Snap(com.graphhopper.storage.index.Snap)

Example 24 with Snap

use of com.graphhopper.storage.index.Snap in project graphhopper by graphhopper.

the class IsochroneExample method main.

public static void main(String[] args) {
    String relDir = args.length == 1 ? args[0] : "";
    GraphHopper hopper = createGraphHopperInstance(relDir + "core/files/andorra.osm.pbf");
    // get encoder from GraphHopper instance
    EncodingManager encodingManager = hopper.getEncodingManager();
    FlagEncoder encoder = encodingManager.getEncoder("car");
    // snap some GPS coordinates to the routing graph and build a query graph
    FastestWeighting weighting = new FastestWeighting(encoder);
    Snap snap = hopper.getLocationIndex().findClosest(42.508679, 1.532078, new DefaultSnapFilter(weighting, encodingManager.getBooleanEncodedValue(Subnetwork.key("car"))));
    QueryGraph queryGraph = QueryGraph.create(hopper.getGraphHopperStorage(), snap);
    // run the isochrone calculation
    ShortestPathTree tree = new ShortestPathTree(queryGraph, weighting, false, TraversalMode.NODE_BASED);
    // find all nodes that are within a radius of 120s
    tree.setTimeLimit(120_000);
    AtomicInteger counter = new AtomicInteger(0);
    // you need to specify a callback to define what should be done
    tree.search(snap.getClosestNode(), label -> {
        // see IsoLabel.java for more properties
        // System.out.println("node: " + label.node + ", time: " + label.time + ", distance: " + label.distance);
        counter.incrementAndGet();
    });
    assert counter.get() > 200;
}
Also used : ShortestPathTree(com.graphhopper.isochrone.algorithm.ShortestPathTree) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) GraphHopper(com.graphhopper.GraphHopper) Snap(com.graphhopper.storage.index.Snap) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph)

Example 25 with Snap

use of com.graphhopper.storage.index.Snap in project graphhopper by graphhopper.

the class LocationIndexExample method lowLevelLocationIndex.

public static void lowLevelLocationIndex() {
    // If you don't use the GraphHopper class you have to use the low level API:
    GraphHopperStorage graph = new GraphBuilder(EncodingManager.create(new CarFlagEncoder())).create();
    graph.edge(0, 1).setName("test edge");
    graph.getNodeAccess().setNode(0, 12, 42);
    graph.getNodeAccess().setNode(1, 12.01, 42.01);
    LocationIndexTree index = new LocationIndexTree(graph.getBaseGraph(), graph.getDirectory());
    index.setResolution(300);
    index.setMaxRegionSearch(4);
    if (!index.loadExisting())
        index.prepareIndex();
    Snap snap = index.findClosest(12, 42, EdgeFilter.ALL_EDGES);
    EdgeIteratorState edge = snap.getClosestEdge();
    assert edge.getName().equals("test edge");
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) GraphBuilder(com.graphhopper.storage.GraphBuilder) Snap(com.graphhopper.storage.index.Snap) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree)

Aggregations

Snap (com.graphhopper.storage.index.Snap)77 Test (org.junit.jupiter.api.Test)39 QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)31 GHPoint (com.graphhopper.util.shapes.GHPoint)22 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)20 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)13 QueryRoutingCHGraph (com.graphhopper.routing.querygraph.QueryRoutingCHGraph)12 Weighting (com.graphhopper.routing.weighting.Weighting)11 ArrayList (java.util.ArrayList)11 Path (com.graphhopper.routing.Path)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)8 RoutingAlgorithm (com.graphhopper.routing.RoutingAlgorithm)6 DecimalEncodedValue (com.graphhopper.routing.ev.DecimalEncodedValue)6 LocationIndex (com.graphhopper.storage.index.LocationIndex)6 IntArrayList (com.carrotsearch.hppc.IntArrayList)5 GraphHopper (com.graphhopper.GraphHopper)5 Profile (com.graphhopper.config.Profile)5 ValueSource (org.junit.jupiter.params.provider.ValueSource)5 DefaultSnapFilter (com.graphhopper.routing.util.DefaultSnapFilter)4