List<Double> paddedHeadings = new ArrayList<>(Collections.nCopies(requestPoints.size(), Double.NaN)); paddedHeadings.set(0, favoredHeadings.get(0)); request = new GHRequest(requestPoints, paddedHeadings); } else { request = new GHRequest(requestPoints, favoredHeadings); request = new GHRequest(requestPoints); initHints(request.getHints(), uriInfo.getQueryParameters()); request.setVehicle(vehicleStr). setWeighting(weighting). setAlgorithm(algoStr). setLocale(localeStr). setPointHints(pointHints). setPathDetails(pathDetails). getHints(). put(CALC_POINTS, calcPoints). put(INSTRUCTIONS, instructions).
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); for (GHPoint p : request.getPoints()) { places += "point=" + round6(p.lat) + "," + round6(p.lon) + "&"; String type = request.getHints().get("type", "json"); + "&points_encoded=true" + "&calc_points=" + tmpCalcPoints + "&algorithm=" + request.getAlgorithm() + "&locale=" + request.getLocale().toString() + "&elevation=" + tmpElevation + "&optimize=" + tmpOptimize; if (!request.getVehicle().isEmpty()) { url += "&vehicle=" + request.getVehicle(); for (String details : request.getPathDetails()) { url += "&" + Parameters.DETAILS.PATH_DETAILS + "=" + details; for (String hint : request.getPointHints()) { url += "&point_hint=" + WebHelper.encodeURL(hint); for (Map.Entry<String, String> entry : request.getHints().toMap().entrySet()) { String urlKey = entry.getKey();
/** * Add stopover point to routing request. * <p> * * @param point geographical position (see GHPoint) */ public GHRequest addPoint(GHPoint point) { addPoint(point, Double.NaN); return this; }
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<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; }
String vehicle = request.getVehicle(); if (vehicle.isEmpty()) { vehicle = getDefaultVehicle().toString(); request.setVehicle(vehicle); HintsMap hints = request.getHints(); String tModeStr = hints.get("traversal_mode", traversalMode.toString()); TraversalMode tMode = TraversalMode.fromString(tModeStr); String algoStr = request.getAlgorithm(); if (algoStr.isEmpty()) algoStr = chFactoryDecorator.isEnabled() && !disableCH ? DIJKSTRA_BI : ASTAR_BI; List<GHPoint> points = request.getPoints(); Locale locale = request.getLocale(); Translation tr = trMap.getWithFallBack(locale); for (int i = 0; i < maxRetries; i++) { if (!forceCHHeading && request.hasFavoredHeading(0)) throw new IllegalArgumentException("Heading is not (fully) supported for CHGraph. See issue #483"); setDouglasPeucker(peucker). setEnableInstructions(tmpEnableInstructions). setPathDetailsBuilders(pathBuilderFactory, request.getPathDetails()). setSimplifyResponse(simplifyResponse && wayPointMaxDistance > 0); if (request.hasFavoredHeading(0)) pathMerger.setFavoredHeading(request.getFavoredHeading(0));
String vehicle = request.getVehicle(); if (vehicle.isEmpty()) request.setVehicle(vehicle); + "Supported are: " + getEncodingManager()); HintsMap hints = request.getHints(); String tModeStr = hints.get("traversal_mode", traversalMode.toString()); TraversalMode tMode = TraversalMode.fromString(tModeStr); List<GHPoint> points = request.getPoints(); String algoStr = request.getAlgorithm().isEmpty() ? DIJKSTRA_BI : request.getAlgorithm(); List<QueryResult> qResults = null; int maxRetries = routingTemplate.getMaxRetries(); Locale locale = request.getLocale(); Translation tr = trMap.getWithFallBack(locale); for (int i = 0; i < maxRetries; i++) if (!forceCHHeading && request.hasFavoredHeading(0)) throw new IllegalArgumentException("Heading is not (fully) supported for CHGraph. See issue #483"); else if (!(tmpAlgoFactory instanceof PrepareContractionHierarchies))
double toLat = na.getLatitude(to); double toLon = na.getLongitude(to); GHRequest sodReq = new GHRequest(fromLat, fromLon, toLat, toLon). setWeighting("fastest"). setVehicle(vehicle). setAlgorithm(DIJKSTRA_BI); GHRequest noSodReq = new GHRequest(fromLat, fromLon, toLat, toLon). setWeighting("fastest"). setVehicle(vehicle). setAlgorithm(DIJKSTRA_BI); noSodReq.getHints().put("stall_on_demand", false);
double toLat = na.getLatitude(to); double toLon = na.getLongitude(to); GHRequest req = new GHRequest(fromLat, fromLon, toLat, toLon). setWeighting("fastest"). setVehicle(vehicle); req.getHints().put(CH.DISABLE, !ch). put("stall_on_demand", sod). put(Landmark.DISABLE, !lm). req.setPathDetails(Arrays.asList(Parameters.DETAILS.AVERAGE_SPEED));
@Test(expected = IllegalArgumentException.class) public void lookup_throwsIfNumberOfGivenPointsNotOne() { RoundTripRoutingTemplate routingTemplate = new RoundTripRoutingTemplate( new GHRequest(Collections.singletonList(ghPoint1)), new GHResponse(), null, 1); routingTemplate.lookup(Arrays.asList(ghPoint1, ghPoint2), carFE); }
@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 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); } }
public GHResponse getResponse(GHPoint from, GHPoint to) { // The flag encoder's toString method returns the vehicle type GHRequest req = new GHRequest(from, to).setVehicle(flagEncoder.toString()); GHResponse rsp = hopper.route(req); if (rsp.hasErrors()) { return null; } return rsp; }
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; }
public String export(GHRequest ghRequest) { String str = "Creating request failed"; try { Request okRequest = createRequest(ghRequest); str = getClientForRequest(ghRequest).newCall(okRequest).execute().body().string(); return str; } catch (Exception ex) { throw new RuntimeException("Problem while fetching export " + ghRequest.getPoints() + ", error: " + ex.getMessage() + " response: " + str, ex); } } }
public GHRequest setLocale(String localeStr) { return setLocale(Helper.getLocale(localeStr)); }
GHRequest l_request = new GHRequest( p_start.getLatitude(), p_start.getLongitude(), p_end.getLatitude(), p_end.getLongitude() ); l_request.setAlgorithm( CConfiguration.getInstance().get().RoutingAlgorithm ); return m_graphhopper.route(l_request);
String vehicle = request.getVehicle(); if (vehicle.isEmpty()) { vehicle = getDefaultVehicle().toString(); request.setVehicle(vehicle); HintsMap hints = request.getHints(); String tModeStr = hints.get("traversal_mode", traversalMode.toString()); TraversalMode tMode = TraversalMode.fromString(tModeStr); String algoStr = request.getAlgorithm(); if (algoStr.isEmpty()) algoStr = chFactoryDecorator.isEnabled() && !disableCH ? DIJKSTRA_BI : ASTAR_BI; List<GHPoint> points = request.getPoints(); Locale locale = request.getLocale(); Translation tr = trMap.getWithFallBack(locale); for (int i = 0; i < maxRetries; i++) { if (!forceCHHeading && request.hasFavoredHeading(0)) throw new IllegalArgumentException("Heading is not (fully) supported for CHGraph. See issue #483"); setDouglasPeucker(peucker). setEnableInstructions(tmpEnableInstructions). setPathDetailsBuilders(pathBuilderFactory, request.getPathDetails()). setSimplifyResponse(simplifyResponse && wayPointMaxDistance > 0); if (request.hasFavoredHeading(0)) pathMerger.setFavoredHeading(request.getFavoredHeading(0));