use of com.graphhopper.routing.util.EdgeFilter in project graphhopper by graphhopper.
the class DijkstraOneToManyTest method testDifferentEdgeFilter.
@Test
public void testDifferentEdgeFilter() {
GraphHopperStorage g = new GraphBuilder(encodingManager).setCHGraph(new FastestWeighting(carEncoder)).create();
g.edge(4, 3, 10, true);
g.edge(3, 6, 10, true);
g.edge(4, 5, 10, true);
g.edge(5, 6, 10, true);
DijkstraOneToMany algo = (DijkstraOneToMany) createAlgo(g);
algo.setEdgeFilter(new EdgeFilter() {
@Override
public boolean accept(EdgeIteratorState iter) {
return iter.getAdjNode() != 5;
}
});
Path p = algo.calcPath(4, 6);
assertEquals(Helper.createTList(4, 3, 6), p.calcNodes());
// important call!
algo.clear();
algo.setEdgeFilter(new EdgeFilter() {
@Override
public boolean accept(EdgeIteratorState iter) {
return iter.getAdjNode() != 3;
}
});
p = algo.calcPath(4, 6);
assertEquals(Helper.createTList(4, 5, 6), p.calcNodes());
}
use of com.graphhopper.routing.util.EdgeFilter in project graphhopper by graphhopper.
the class GtfsReader method connectStopsToStreetNetwork.
private void connectStopsToStreetNetwork() {
EdgeFilter filter = new EverythingButPt(encoder);
for (Stop stop : feed.stops.values()) {
QueryResult locationQueryResult = walkNetworkIndex.findClosest(stop.stop_lat, stop.stop_lon, filter);
int streetNode;
if (!locationQueryResult.isValid()) {
streetNode = i++;
nodeAccess.setNode(streetNode, stop.stop_lat, stop.stop_lon);
graph.edge(streetNode, streetNode, 0.0, false);
} else {
streetNode = locationQueryResult.getClosestNode();
}
gtfsStorage.getStationNodes().put(stop.stop_id, streetNode);
}
}
use of com.graphhopper.routing.util.EdgeFilter in project graphhopper by graphhopper.
the class RoundTripRoutingTemplate method lookup.
@Override
public List<QueryResult> lookup(List<GHPoint> points, FlagEncoder encoder) {
if (points.size() != 1 || ghRequest.getPoints().size() != 1)
throw new IllegalArgumentException("For round trip calculation exactly one point is required");
final double distanceInMeter = ghRequest.getHints().getDouble(RoundTrip.DISTANCE, 10000);
final long seed = ghRequest.getHints().getLong(RoundTrip.SEED, 0L);
double initialHeading = ghRequest.getFavoredHeading(0);
final int roundTripPointCount = Math.min(20, ghRequest.getHints().getInt(RoundTrip.POINTS, 2 + (int) (distanceInMeter / 50000)));
final GHPoint start = points.get(0);
TourStrategy strategy = new MultiPointTour(new Random(seed), distanceInMeter, roundTripPointCount, initialHeading);
queryResults = new ArrayList<>(2 + strategy.getNumberOfGeneratedPoints());
EdgeFilter edgeFilter = new DefaultEdgeFilter(encoder);
QueryResult startQR = locationIndex.findClosest(start.lat, start.lon, edgeFilter);
if (!startQR.isValid())
throw new PointNotFoundException("Cannot find point 0: " + start, 0);
queryResults.add(startQR);
GHPoint last = start;
for (int i = 0; i < strategy.getNumberOfGeneratedPoints(); i++) {
double heading = strategy.getHeadingForIteration(i);
QueryResult result = generateValidPoint(last, strategy.getDistanceForIteration(i), heading, edgeFilter);
if (result == null) {
ghResponse.addError(new IllegalStateException("Could not find a valid point after " + maxRetries + " tries, for the point:" + last));
return Collections.emptyList();
}
last = result.getSnappedPoint();
queryResults.add(result);
}
queryResults.add(startQR);
return queryResults;
}
use of com.graphhopper.routing.util.EdgeFilter in project graphhopper by graphhopper.
the class LandmarkSuggestion method readLandmarks.
/**
* The expected format is lon,lat per line where lines starting with characters will be ignored. You can create
* such a file manually via geojson.io -> Save as CSV. Optionally add a second line with
* <pre>#BBOX:minLat,minLon,maxLat,maxLon</pre>
* <p>
* to specify an explicit bounding box. TODO: support GeoJSON instead.
*/
public static LandmarkSuggestion readLandmarks(String file, LocationIndex locationIndex) throws IOException {
// landmarks should be suited for all vehicles
EdgeFilter edgeFilter = EdgeFilter.ALL_EDGES;
List<String> lines = Helper.readFile(file);
List<Integer> landmarkNodeIds = new ArrayList<>();
BBox bbox = BBox.createInverse(false);
int lmSuggestionIdx = 0;
String errors = "";
for (String lmStr : lines) {
if (lmStr.startsWith("#BBOX:")) {
bbox = BBox.parseTwoPoints(lmStr.substring("#BBOX:".length()));
continue;
} else if (lmStr.isEmpty() || Character.isAlphabetic(lmStr.charAt(0))) {
continue;
}
GHPoint point = GHPoint.fromStringLonLat(lmStr);
if (point == null)
throw new RuntimeException("Invalid format " + lmStr + " for point " + lmSuggestionIdx);
lmSuggestionIdx++;
Snap result = locationIndex.findClosest(point.lat, point.lon, edgeFilter);
if (!result.isValid()) {
errors += "Cannot find close node found for landmark suggestion[" + lmSuggestionIdx + "]=" + point + ".\n";
continue;
}
bbox.update(point.lat, point.lon);
landmarkNodeIds.add(result.getClosestNode());
}
if (!errors.isEmpty())
throw new RuntimeException(errors);
return new LandmarkSuggestion(landmarkNodeIds, bbox);
}
use of com.graphhopper.routing.util.EdgeFilter in project graphhopper by graphhopper.
the class MapMatching method findCandidateSnapsInBBox.
private List<Snap> findCandidateSnapsInBBox(double queryLat, double queryLon, BBox queryShape) {
EdgeFilter edgeFilter = new DefaultSnapFilter(unwrappedWeighting, inSubnetworkEnc);
List<Snap> snaps = new ArrayList<>();
IntHashSet seenEdges = new IntHashSet();
IntHashSet seenNodes = new IntHashSet();
locationIndex.query(queryShape, edgeId -> {
EdgeIteratorState edge = graph.getEdgeIteratorStateForKey(edgeId * 2);
if (seenEdges.add(edgeId) && edgeFilter.accept(edge)) {
Snap snap = new Snap(queryLat, queryLon);
locationIndex.traverseEdge(queryLat, queryLon, edge, (node, normedDist, wayIndex, pos) -> {
if (normedDist < snap.getQueryDistance()) {
snap.setQueryDistance(normedDist);
snap.setClosestNode(node);
snap.setWayIndex(wayIndex);
snap.setSnappedPosition(pos);
}
});
double dist = DIST_PLANE.calcDenormalizedDist(snap.getQueryDistance());
snap.setClosestEdge(edge);
snap.setQueryDistance(dist);
if (snap.isValid() && (snap.getSnappedPosition() != Snap.Position.TOWER || seenNodes.add(snap.getClosestNode()))) {
snap.calcSnappedPoint(DistanceCalcEarth.DIST_EARTH);
if (queryShape.contains(snap.getSnappedPoint().lat, snap.getSnappedPoint().lon)) {
snaps.add(snap);
}
}
}
});
return snaps;
}
Aggregations