Search in sources :

Example 6 with EdgeFilter

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());
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) EdgeFilter(com.graphhopper.routing.util.EdgeFilter) GraphBuilder(com.graphhopper.storage.GraphBuilder) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Test(org.junit.Test)

Example 7 with EdgeFilter

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);
    }
}
Also used : QueryResult(com.graphhopper.storage.index.QueryResult) Stop(com.conveyal.gtfs.model.Stop) EdgeFilter(com.graphhopper.routing.util.EdgeFilter)

Example 8 with EdgeFilter

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;
}
Also used : PointNotFoundException(com.graphhopper.util.exceptions.PointNotFoundException) TourStrategy(com.graphhopper.routing.util.tour.TourStrategy) GHPoint(com.graphhopper.util.shapes.GHPoint) DefaultEdgeFilter(com.graphhopper.routing.util.DefaultEdgeFilter) QueryResult(com.graphhopper.storage.index.QueryResult) MultiPointTour(com.graphhopper.routing.util.tour.MultiPointTour) Random(java.util.Random) EdgeFilter(com.graphhopper.routing.util.EdgeFilter) DefaultEdgeFilter(com.graphhopper.routing.util.DefaultEdgeFilter) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 9 with EdgeFilter

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);
}
Also used : BBox(com.graphhopper.util.shapes.BBox) EdgeFilter(com.graphhopper.routing.util.EdgeFilter) ArrayList(java.util.ArrayList) GHPoint(com.graphhopper.util.shapes.GHPoint) Snap(com.graphhopper.storage.index.Snap) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 10 with EdgeFilter

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;
}
Also used : DefaultSnapFilter(com.graphhopper.routing.util.DefaultSnapFilter) VirtualEdgeIteratorState(com.graphhopper.routing.querygraph.VirtualEdgeIteratorState) EdgeFilter(com.graphhopper.routing.util.EdgeFilter) IntHashSet(com.carrotsearch.hppc.IntHashSet) Snap(com.graphhopper.storage.index.Snap)

Aggregations

EdgeFilter (com.graphhopper.routing.util.EdgeFilter)10 DefaultEdgeFilter (com.graphhopper.routing.util.DefaultEdgeFilter)4 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)3 Snap (com.graphhopper.storage.index.Snap)3 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)3 IntHashSet (com.carrotsearch.hppc.IntHashSet)2 DefaultSnapFilter (com.graphhopper.routing.util.DefaultSnapFilter)2 EncodingManager (com.graphhopper.routing.util.EncodingManager)2 GraphBuilder (com.graphhopper.storage.GraphBuilder)2 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)2 QueryResult (com.graphhopper.storage.index.QueryResult)2 BBox (com.graphhopper.util.shapes.BBox)2 GHPoint (com.graphhopper.util.shapes.GHPoint)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 IntArrayList (com.carrotsearch.hppc.IntArrayList)1 IntIntHashMap (com.carrotsearch.hppc.IntIntHashMap)1 IntLongHashMap (com.carrotsearch.hppc.IntLongHashMap)1 IntCursor (com.carrotsearch.hppc.cursors.IntCursor)1 GTFSFeed (com.conveyal.gtfs.GTFSFeed)1