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());
}
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));
}
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);
}
}
}
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;
}
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");
}
Aggregations