@GET public Response doGet(@QueryParam("point") GHPoint point, @QueryParam("elevation") @DefaultValue("false") boolean elevation) { QueryResult qr = index.findClosest(point.lat, point.lon, EdgeFilter.ALL_EDGES); if (qr.isValid()) { GHPoint3D snappedPoint = qr.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!"); } }
void queryIndex(Graph g, LocationIndex idx, double lat, double lon, double expectedDist) { QueryResult res = idx.findClosest(lat, lon, EdgeFilter.ALL_EDGES); if (!res.isValid()) { errors.add("node not found for " + lat + "," + lon); return; } GHPoint found = res.getSnappedPoint(); double dist = distCalc.calcDist(lat, lon, found.lat, found.lon); if (Math.abs(dist - expectedDist) > .1) { errors.add("queried lat,lon=" + (float) lat + "," + (float) lon + " (found: " + (float) found.lat + "," + (float) found.lon + ")" + "\n expected distance:" + expectedDist + ", but was:" + dist); } }
@Override public List<QueryResult> lookup(List<GHPoint> points, FlagEncoder encoder) { if (points.size() < 2) throw new IllegalArgumentException("At least 2 points have to be specified, but was:" + points.size()); EdgeFilter edgeFilter = DefaultEdgeFilter.allEdges(encoder); queryResults = new ArrayList<>(points.size()); for (int placeIndex = 0; placeIndex < points.size(); placeIndex++) { GHPoint point = points.get(placeIndex); QueryResult qr = null; if (ghRequest.hasPointHints()) qr = locationIndex.findClosest(point.lat, point.lon, new NameSimilarityEdgeFilter(edgeFilter, ghRequest.getPointHints().get(placeIndex))); if (qr == null || !qr.isValid()) qr = locationIndex.findClosest(point.lat, point.lon, edgeFilter); if (!qr.isValid()) ghResponse.addError(new PointNotFoundException("Cannot find point " + placeIndex + ": " + point, placeIndex)); queryResults.add(qr); } return queryResults; }
/** * This method fills the edgeIds hash with edgeIds found close (exact match) to the specified lat,lon */ public void findClosestEdge(GHIntHashSet edgeIds, double lat, double lon, EdgeFilter filter) { QueryResult qr = locationIndex.findClosest(lat, lon, filter); if (qr.isValid()) edgeIds.add(qr.getClosestEdge().getEdge()); }
if (!result.isValid()) { errors += "Cannot find close node found for landmark suggestion[" + lmSuggestionIdx + "]=" + point + ".\n"; continue;
@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 = DefaultEdgeFilter.allEdges(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; }
LocationIndex locationIndex = graphHopper.getLocationIndex(); QueryResult qr = locationIndex.findClosest(point.lat, point.lon, edgeFilter); if (!qr.isValid()) throw new IllegalArgumentException("Point not found:" + point);
private QueryResult generateValidPoint(GHPoint from, double distanceInMeters, double heading, EdgeFilter edgeFilter) { int tryCount = 0; while (true) { GHPoint generatedPoint = Helper.DIST_EARTH.projectCoordinate(from.getLat(), from.getLon(), distanceInMeters, heading); QueryResult qr = locationIndex.findClosest(generatedPoint.getLat(), generatedPoint.getLon(), edgeFilter); if (qr.isValid()) return qr; tryCount++; distanceInMeters *= 0.95; if (tryCount >= maxRetries) return null; } }
QueryResult queryResult = locationIndex.findClosest(source.lat, source.lon, filter); queryGraph.lookup(Collections.singletonList(queryResult)); if (!queryResult.isValid()) { throw new IllegalArgumentException("Cannot find point: " + source);
private QueryResult findClosest(GHPoint point, int indexForErrorMessage) { final EdgeFilter filter = DefaultEdgeFilter.allEdges(graphHopperStorage.getEncodingManager().getEncoder("foot")); QueryResult source = locationIndex.findClosest(point.lat, point.lon, filter); if (!source.isValid()) { throw new PointNotFoundException("Cannot find point: " + point, indexForErrorMessage); } if (flagEncoder.getEdgeType(source.getClosestEdge().getFlags()) != GtfsStorage.EdgeType.HIGHWAY) { throw new RuntimeException(flagEncoder.getEdgeType(source.getClosestEdge().getFlags()).name()); } return source; }
void connectStopsToStreetNetwork() { FlagEncoder footEncoder = ((GraphHopperStorage) graph).getEncodingManager().getEncoder("foot"); final EdgeFilter filter = DefaultEdgeFilter.allEdges(footEncoder); for (Stop stop : feed.stops.values()) { if (stop.location_type == 0) { // Only stops. Not interested in parent stations for now. 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); EdgeIteratorState edge = graph.edge(streetNode, streetNode); edge.setFlags(encoder.setAccess(edge.getFlags(), true, false)); edge.setFlags(footEncoder.setAccess(edge.getFlags(), true, false)); edge.setFlags(footEncoder.setSpeed(edge.getFlags(), 5.0)); } else { streetNode = locationQueryResult.getClosestNode(); } gtfsStorage.getStationNodes().put(stop.stop_id, streetNode); } } }
if (qr.isValid()) {
QueryResult qr = locationIndex.findClosest(center.getLat(), center.getLon(), filter); if (!qr.isValid()) throw new IllegalArgumentException("Shape " + shape + " does not cover graph");
@Test public void testSnappedPointAndGeometry() { Graph graph = createTestGraph(encodingManager); LocationIndex index = createIndex(graph, -1); // query directly the tower node QueryResult res = index.findClosest(-0.4, 0.9, EdgeFilter.ALL_EDGES); assertTrue(res.isValid()); assertEquals(new GHPoint(-0.4, 0.9), res.getSnappedPoint()); res = index.findClosest(-0.6, 1.6, EdgeFilter.ALL_EDGES); assertTrue(res.isValid()); assertEquals(new GHPoint(-0.6, 1.6), res.getSnappedPoint()); // query the edge (1,3). The edge (0,4) has 27674 as distance res = index.findClosest(-0.2, 0.3, EdgeFilter.ALL_EDGES); assertTrue(res.isValid()); assertEquals(26936, res.getQueryDistance(), 1); assertEquals(new GHPoint(-0.441624, 0.317259), res.getSnappedPoint()); }
@Override public int findID( double lat, double lon ) { QueryResult res = findClosest(lat, lon, EdgeFilter.ALL_EDGES); if (!res.isValid()) return -1; return res.getClosestNode(); }
private QueryResult[] queryPositions(GHPoint[] points, List<QueryResult> validResults) { QueryResult[] queryResults = new QueryResult[points.length]; for (int i = 0; i < points.length; i++) { queryResults[i] = createSnapToResult(points[i].getLat(), points[i].getLon()); if (queryResults[i].isValid()) { validResults.add(queryResults[i]); } } return queryResults; }
/** * This method fills the edgeIds hash with edgeIds found close (exact match) to the specified lat,lon */ public void findClosestEdge(GHIntHashSet edgeIds, double lat, double lon, EdgeFilter filter) { QueryResult qr = locationIndex.findClosest(lat, lon, filter); if (qr.isValid()) edgeIds.add(qr.getClosestEdge().getEdge()); }
/** * This method fills the edgeIds hash with edgeIds found close (exact match) to the specified lat,lon */ public void findClosestEdge(GHIntHashSet edgeIds, double lat, double lon, EdgeFilter filter) { QueryResult qr = locationIndex.findClosest(lat, lon, filter); if (qr.isValid()) edgeIds.add(qr.getClosestEdge().getEdge()); }