Search in sources :

Example 71 with Snap

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

the class NearestResource method doGet.

@GET
public Response doGet(@QueryParam("point") GHPoint point, @QueryParam("elevation") @DefaultValue("false") boolean elevation) {
    Snap snap = index.findClosest(point.lat, point.lon, EdgeFilter.ALL_EDGES);
    if (snap.isValid()) {
        GHPoint3D snappedPoint = snap.getSnappedPoint();
        double[] coordinates = hasElevation && elevation ? new double[] { snappedPoint.lon, snappedPoint.lat, snappedPoint.ele } : new double[] { snappedPoint.lon, snappedPoint.lat };
        return new Response(coordinates, calc.calcDist(point.lat, point.lon, snappedPoint.lat, snappedPoint.lon));
    } else {
        throw new WebApplicationException("Nearest point cannot be found!");
    }
}
Also used : GHPoint3D(com.graphhopper.util.shapes.GHPoint3D) Snap(com.graphhopper.storage.index.Snap)

Example 72 with Snap

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

the class OSMReaderTest method testCrossBoundary_issue667.

@Test
public void testCrossBoundary_issue667() {
    GraphHopper hopper = new GraphHopperFacade("test-osm-waterway.xml").importOrLoad();
    Snap snap = hopper.getLocationIndex().findClosest(0.1, 179.5, EdgeFilter.ALL_EDGES);
    assertTrue(snap.isValid());
    assertEquals(0.1, snap.getSnappedPoint().lat, 0.1);
    assertEquals(179.5, snap.getSnappedPoint().lon, 0.1);
    assertEquals(11, snap.getClosestEdge().getDistance() / 1000, 1);
    snap = hopper.getLocationIndex().findClosest(0.1, -179.6, EdgeFilter.ALL_EDGES);
    assertTrue(snap.isValid());
    assertEquals(0.1, snap.getSnappedPoint().lat, 0.1);
    assertEquals(-179.6, snap.getSnappedPoint().lon, 0.1);
    assertEquals(112, snap.getClosestEdge().getDistance() / 1000, 1);
}
Also used : GraphHopper(com.graphhopper.GraphHopper) Snap(com.graphhopper.storage.index.Snap) Test(org.junit.jupiter.api.Test) GraphHopperTest(com.graphhopper.GraphHopperTest)

Example 73 with Snap

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

the class GraphHopperTest method issue2306_2.

@Test
public void issue2306_2() {
    // This is the same test as above, but without increasing the search radius.
    // As I am writing this, we find _no_ match here. But since the search radius
    // is a meta-parameter that could go away at some point, I say that _if_ we find a match,
    // it should be a close one. (And not a far away one, as happened in issue2306.)
    final String profile = "profile";
    final String vehicle = "car";
    GraphHopper hopper = new GraphHopper().setGraphHopperLocation(GH_LOCATION).setOSMFile("../map-matching/files/leipzig_germany.osm.pbf").setProfiles(new Profile("profile").setVehicle(vehicle).setWeighting("fastest")).setMinNetworkSize(200);
    hopper.importOrLoad();
    Weighting weighting = hopper.createWeighting(hopper.getProfile(profile), new PMap());
    EdgeFilter edgeFilter = new DefaultSnapFilter(weighting, hopper.getEncodingManager().getBooleanEncodedValue(Subnetwork.key(profile)));
    Snap snap = hopper.getLocationIndex().findClosest(51.229248, 12.328892, edgeFilter);
    if (snap.isValid()) {
        assertTrue(snap.getQueryDistance() < 3_000);
    }
}
Also used : Weighting(com.graphhopper.routing.weighting.Weighting) Snap(com.graphhopper.storage.index.Snap) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) Profile(com.graphhopper.config.Profile) CHProfile(com.graphhopper.config.CHProfile) LMProfile(com.graphhopper.config.LMProfile) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 74 with Snap

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

the class MapMatching method findCandidateSnaps.

public List<Snap> findCandidateSnaps(final double queryLat, final double queryLon) {
    double rLon = (measurementErrorSigma * 360.0 / DistanceCalcEarth.DIST_EARTH.calcCircumference(queryLat));
    double rLat = measurementErrorSigma / DistanceCalcEarth.METERS_PER_DEGREE;
    Envelope envelope = new Envelope(queryLon, queryLon, queryLat, queryLat);
    for (int i = 0; i < 50; i++) {
        envelope.expandBy(rLon, rLat);
        List<Snap> snaps = findCandidateSnapsInBBox(queryLat, queryLon, BBox.fromEnvelope(envelope));
        if (!snaps.isEmpty()) {
            return snaps;
        }
    }
    return Collections.emptyList();
}
Also used : Envelope(org.locationtech.jts.geom.Envelope) Snap(com.graphhopper.storage.index.Snap)

Example 75 with Snap

use of com.graphhopper.storage.index.Snap 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

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