private OkHttpClient getClientForRequest(GHRequest request) { OkHttpClient client = this.downloader; if (request.getHints().has(TIMEOUT)) { long timeout = request.getHints().getLong(TIMEOUT, DEFAULT_TIMEOUT); client = client.newBuilder() .connectTimeout(timeout, TimeUnit.MILLISECONDS) .readTimeout(timeout, TimeUnit.MILLISECONDS) .build(); } return client; }
@Override public List<Path> calcPaths(QueryGraph queryGraph, RoutingAlgorithmFactory algoFactory, AlgorithmOptions algoOpts) { boolean withViaTurnPenalty = ghRequest.getHints().getBool(Routing.PASS_THROUGH, false); if (withViaTurnPenalty) throw new IllegalArgumentException("Alternative paths and " + PASS_THROUGH + " at the same time is currently not supported"); return super.calcPaths(queryGraph, algoFactory, algoOpts); }
@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; }
@Override public GHResponse route(GHRequest request) { ResponseBody rspBody = null; try { Request okRequest = createRequest(request); rspBody = getClientForRequest(request).newCall(okRequest).execute().body(); JsonNode json = objectMapper.reader().readTree(rspBody.byteStream()); GHResponse res = new GHResponse(); res.addErrors(readErrors(json)); if (res.hasErrors()) return res; JsonNode paths = json.get("paths"); boolean tmpElevation = request.getHints().getBool("elevation", elevation); boolean tmpTurnDescription = request.getHints().getBool("turn_description", true); for (JsonNode path : paths) { PathWrapper altRsp = createPathWrapper(path, tmpElevation, tmpTurnDescription); res.add(altRsp); } return res; } catch (Exception ex) { throw new RuntimeException("Problem while fetching path " + request.getPoints() + ": " + ex.getMessage(), ex); } finally { Helper.close(rspBody); } }
private Request createRequest(GHRequest request) { boolean tmpInstructions = request.getHints().getBool("instructions", instructions); boolean tmpCalcPoints = request.getHints().getBool("calc_points", calcPoints); String tmpOptimize = request.getHints().get("optimize", optimize); boolean tmpElevation = request.getHints().getBool("elevation", elevation); String type = request.getHints().get("type", "json"); for (Map.Entry<String, String> entry : request.getHints().toMap().entrySet()) { String urlKey = entry.getKey(); String urlValue = entry.getValue();
setVehicle(vehicle); req.getHints().put(CH.DISABLE, !ch). put("stall_on_demand", sod). put(Landmark.DISABLE, !lm).
protected PathWrapper doInBackground(Void... v) { StopWatch sw = new StopWatch().start(); GHRequest req = new GHRequest(fromLat, fromLon, toLat, toLon). setAlgorithm(Algorithms.DIJKSTRA_BI); req.getHints(). put(Routing.INSTRUCTIONS, "false"); GHResponse resp = hopper.route(req); time = sw.stop().getSeconds(); return resp.getBest(); }
@Override public List<Path> calcPaths(QueryGraph queryGraph, RoutingAlgorithmFactory algoFactory, AlgorithmOptions algoOpts) { long visitedNodesSum = 0L; boolean viaTurnPenalty = ghRequest.getHints().getBool(Routing.PASS_THROUGH, false); int pointCounts = ghRequest.getPoints().size(); pathList = new ArrayList<>(pointCounts - 1);
req.getHints().put(Landmark.DISABLE, true); GHResponse originalRsp = hopper.route(req);
setVehicle(vehicle). setAlgorithm(DIJKSTRA_BI); noSodReq.getHints().put("stall_on_demand", false);
throw new IllegalArgumentException("Vehicle not supported: " + vehicle + ". Supported are: " + encodingManager.toString()); HintsMap hints = request.getHints(); String tModeStr = hints.get("traversal_mode", traversalMode.toString()); TraversalMode tMode = TraversalMode.fromString(tModeStr);
@Test public void testLookupAndCalcPaths_simpleSquareGraph() { Graph g = createSquareGraph(); // start at node 0 and head south, make sure the round trip is long enough to reach most southern node 6 GHPoint start = new GHPoint(1, -1); double heading = 180.0; int numPoints = 2; double roundTripDistance = 670000; GHRequest ghRequest = new GHRequest(Collections.singletonList(start), Collections.singletonList(heading)); ghRequest.getHints().put(Parameters.Algorithms.RoundTrip.POINTS, numPoints); ghRequest.getHints().put(Parameters.Algorithms.RoundTrip.DISTANCE, roundTripDistance); LocationIndex locationIndex = new LocationIndexTree(g, new RAMDirectory()).prepareIndex(); RoundTripRoutingTemplate routingTemplate = new RoundTripRoutingTemplate(ghRequest, new GHResponse(), locationIndex, 1); List<QueryResult> stagePoints = routingTemplate.lookup(ghRequest.getPoints(), carFE); assertEquals(3, stagePoints.size()); assertEquals(0, stagePoints.get(0).getClosestNode()); assertEquals(6, stagePoints.get(1).getClosestNode()); assertEquals(0, stagePoints.get(2).getClosestNode()); QueryGraph queryGraph = new QueryGraph(g); queryGraph.lookup(stagePoints); Weighting weighting = new FastestWeighting(carFE); List<Path> paths = routingTemplate.calcPaths( queryGraph, new RoutingAlgorithmFactorySimple(), new AlgorithmOptions(DIJKSTRA_BI, weighting, tMode)); // make sure the resulting paths are connected and form a round trip starting and ending at the start node 0 assertEquals(2, paths.size()); assertEquals(IntArrayList.from(new int[]{0, 7, 6, 5}), paths.get(0).calcNodes()); assertEquals(IntArrayList.from(new int[]{5, 4, 3, 2, 1, 0}), paths.get(1).calcNodes()); }
@Override public List<Path> calcPaths(QueryGraph queryGraph, RoutingAlgorithmFactory algoFactory, AlgorithmOptions algoOpts) { boolean withViaTurnPenalty = ghRequest.getHints().getBool(Routing.PASS_THROUGH, false); if (withViaTurnPenalty) throw new IllegalArgumentException("Alternative paths and " + PASS_THROUGH + " at the same time is currently not supported"); return super.calcPaths(queryGraph, algoFactory, algoOpts); }
@Override public List<Path> calcPaths(QueryGraph queryGraph, RoutingAlgorithmFactory algoFactory, AlgorithmOptions algoOpts) { boolean withViaTurnPenalty = ghRequest.getHints().getBool(Routing.PASS_THROUGH, false); if (withViaTurnPenalty) throw new IllegalArgumentException("Alternative paths and " + PASS_THROUGH + " at the same time is currently not supported"); return super.calcPaths(queryGraph, algoFactory, algoOpts); }
@Override public List<Path> calcPaths( QueryGraph queryGraph, RoutingAlgorithmFactory algoFactory, AlgorithmOptions algoOpts ) { boolean withViaTurnPenalty = ghRequest.getHints().getBool(Routing.PASS_THROUGH, false); if (withViaTurnPenalty) throw new IllegalStateException("Alternative paths and a viaTurnPenalty at the same time is currently not supported"); return super.calcPaths(queryGraph, algoFactory, algoOpts); }
final double distanceInMeter = ghRequest.getHints().getDouble(RoundTrip.DISTANCE, 10000); final long seed = ghRequest.getHints().getLong(RoundTrip.SEED, 0L); final double initialHeading = ghRequest.getHints().getDouble(RoundTrip.HEADING, Double.NaN); final int roundTripPointCount = Math.min(20, ghRequest.getHints().getInt(Algorithms.ROUND_TRIP + ".points", 2 + (int) (distanceInMeter / 50000))); final GHPoint start = ghRequest.getPoints().get(0);
@Override public List<QueryResult> lookup(List<GHPoint> points, FlagEncoder encoder) { if (points.isEmpty()) throw new IllegalStateException("For round trip calculation one point is required"); final double distanceInMeter = ghRequest.getHints().getDouble(RoundTrip.DISTANCE, 10000); final long seed = ghRequest.getHints().getLong(RoundTrip.SEED, 0L); final double initialHeading = ghRequest.getHints().getDouble(RoundTrip.HEADING, Double.NaN); final int roundTripPointCount = Math.min(20, ghRequest.getHints().getInt(Algorithms.ROUND_TRIP + ".points", 2 + (int) (distanceInMeter / 50000))); final GHPoint start = ghRequest.getPoints().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 = points.get(0); 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; }
@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; }
GHRequest req = new GHRequest(fromLat, fromLon, toLat, toLon). setAlgorithm(AlgorithmOptions.DIJKSTRA_BI); req.getHints(). put("instructions", "false"); GHResponse resp = hopper.route(req);