Search in sources :

Example 76 with Snap

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

the class PtLocationSnapper method findByStopId.

private Snap findByStopId(GHStationLocation station, int indexForErrorMessage) {
    for (Map.Entry<String, GTFSFeed> entry : gtfsStorage.getGtfsFeeds().entrySet()) {
        final Integer node = gtfsStorage.getStationNodes().get(new GtfsStorage.FeedIdWithStopId(entry.getKey(), station.stop_id));
        if (node != null) {
            Stop stop = gtfsStorage.getGtfsFeeds().get(entry.getKey()).stops.get(station.stop_id);
            final Snap stationSnap = new Snap(stop.stop_lat, stop.stop_lon);
            stationSnap.setClosestNode(node);
            return stationSnap;
        }
    }
    throw new PointNotFoundException("Cannot find station: " + station.stop_id, indexForErrorMessage);
}
Also used : Stop(com.conveyal.gtfs.model.Stop) PointNotFoundException(com.graphhopper.util.exceptions.PointNotFoundException) Map(java.util.Map) GTFSFeed(com.conveyal.gtfs.GTFSFeed) Snap(com.graphhopper.storage.index.Snap)

Example 77 with Snap

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

the class PtLocationSnapper method snapAll.

public Result snapAll(List<GHLocation> locations, List<EdgeFilter> snapFilters) {
    PointList points = new PointList(2, false);
    ArrayList<Snap> pointSnaps = new ArrayList<>();
    ArrayList<Supplier<Label.NodeId>> allSnaps = new ArrayList<>();
    for (int i = 0; i < locations.size(); i++) {
        GHLocation location = locations.get(i);
        if (location instanceof GHPointLocation) {
            GHPoint point = ((GHPointLocation) location).ghPoint;
            final Snap closest = locationIndex.findClosest(point.lat, point.lon, snapFilters.get(i));
            if (!closest.isValid()) {
                IntHashSet result = new IntHashSet();
                gtfsStorage.getStopIndex().findEdgeIdsInNeighborhood(point.lat, point.lon, 0, result::add);
                gtfsStorage.getStopIndex().findEdgeIdsInNeighborhood(point.lat, point.lon, 1, result::add);
                if (result.isEmpty()) {
                    throw new PointNotFoundException("Cannot find point: " + point, i);
                }
                IntCursor stopNodeId = result.iterator().next();
                for (Map.Entry<GtfsStorage.FeedIdWithStopId, Integer> e : gtfsStorage.getStationNodes().entrySet()) {
                    if (e.getValue() == stopNodeId.value) {
                        Stop stop = gtfsStorage.getGtfsFeeds().get(e.getKey().feedId).stops.get(e.getKey().stopId);
                        final Snap stopSnap = new Snap(stop.stop_lat, stop.stop_lon);
                        stopSnap.setClosestNode(stopNodeId.value);
                        allSnaps.add(() -> new Label.NodeId(Optional.ofNullable(gtfsStorage.getPtToStreet().get(stopSnap.getClosestNode())).orElse(-1), stopSnap.getClosestNode()));
                        points.add(stopSnap.getQueryPoint().lat, stopSnap.getQueryPoint().lon);
                    }
                }
            } else {
                pointSnaps.add(closest);
                allSnaps.add(() -> new Label.NodeId(closest.getClosestNode(), Optional.ofNullable(gtfsStorage.getStreetToPt().get(closest.getClosestNode())).orElse(-1)));
                points.add(closest.getSnappedPoint());
            }
        } else if (location instanceof GHStationLocation) {
            final Snap stopSnap = findByStopId((GHStationLocation) location, i);
            allSnaps.add(() -> new Label.NodeId(Optional.ofNullable(gtfsStorage.getPtToStreet().get(stopSnap.getClosestNode())).orElse(-1), stopSnap.getClosestNode()));
            points.add(stopSnap.getQueryPoint().lat, stopSnap.getQueryPoint().lon);
        }
    }
    // modifies pointSnaps!
    QueryGraph queryGraph = QueryGraph.create(graphHopperStorage, pointSnaps);
    List<Label.NodeId> nodes = new ArrayList<>();
    for (Supplier<Label.NodeId> supplier : allSnaps) {
        nodes.add(supplier.get());
    }
    return new Result(queryGraph, nodes, points);
}
Also used : PointList(com.graphhopper.util.PointList) Stop(com.conveyal.gtfs.model.Stop) PointNotFoundException(com.graphhopper.util.exceptions.PointNotFoundException) ArrayList(java.util.ArrayList) IntHashSet(com.carrotsearch.hppc.IntHashSet) Snap(com.graphhopper.storage.index.Snap) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) Supplier(java.util.function.Supplier) GHPoint(com.graphhopper.util.shapes.GHPoint) GHPoint(com.graphhopper.util.shapes.GHPoint) Map(java.util.Map) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph)

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