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);
@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(expected = IllegalArgumentException.class) public void lookup_throwsIfNumberOfPointsInRequestNotOne() { RoundTripRoutingTemplate routingTemplate = new RoundTripRoutingTemplate( new GHRequest(Arrays.asList(ghPoint1, ghPoint2)), new GHResponse(), null, 1); routingTemplate.lookup(Collections.singletonList(ghPoint1), carFE); }
private static void warmUpCHSubNetwork(GraphHopper graphHopper, int iterations) { GraphHopperStorage ghStorage = graphHopper.getGraphHopperStorage(); Random rand = new Random(0); for (int i = 0; i < iterations; i++) { int startNode = rand.nextInt(graphHopper.getMaxVisitedNodes() + 1); int endNode = rand.nextInt(graphHopper.getMaxVisitedNodes() + 1); double fromLatitude = ghStorage.getNodeAccess().getLatitude(startNode); double fromLongitude = ghStorage.getNodeAccess().getLongitude(startNode); double toLatitude = ghStorage.getNodeAccess().getLatitude(endNode); double toLongitude = ghStorage.getNodeAccess().getLongitude(endNode); GHRequest request = new GHRequest(fromLatitude, fromLongitude, toLatitude, toLongitude); graphHopper.route(request); } }
double toLat = na.getLatitude(to); double toLon = na.getLongitude(to); GHRequest req = new GHRequest(fromLat, fromLon, toLat, toLon). setWeighting("fastest"). setVehicle(vehicle);
@Test public void testNoLoad() { GraphHopper instance = new GraphHopper(). setStoreOnFlush(false). setEncodingManager(encodingManager).setCHEnabled(false); try { instance.route(new GHRequest(42, 10.4, 42, 10)); assertTrue(false); } catch (Exception ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Do a successful call to load or importOrLoad before routing")); } instance = new GraphHopper().setEncodingManager(encodingManager); try { instance.route(new GHRequest(42, 10.4, 42, 10)); assertTrue(false); } catch (Exception ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Do a successful call to load or importOrLoad before routing")); } }
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(); }
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).
double toLat = na.getLatitude(to); double toLon = na.getLongitude(to); GHRequest req = new GHRequest(fromLat, fromLon, toLat, toLon). setWeighting("fastest"). setVehicle(vehicle).
loadGraph(graph); GHResponse rsp = graphHopper.route(new GHRequest(42, 10.4, 42, 10)); assertFalse(rsp.toString(), rsp.hasErrors()); assertEquals(1800, rsp.getBest().getTime()); rsp = graphHopper.route(new GHRequest(42, 10.4, 42, 10)); assertFalse(rsp.toString(), rsp.hasErrors()); assertEquals(8400, rsp.getBest().getTime());
@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()); }
@Test public void testCalcRoundTrip() throws Exception { Weighting weighting = new FastestWeighting(carFE); Graph g = createTestGraph(true); RoundTripRoutingTemplate rTripRouting = new RoundTripRoutingTemplate(new GHRequest(), new GHResponse(), null, 1); LocationIndex locationIndex = new LocationIndexTree(g, new RAMDirectory()).prepareIndex(); QueryResult qr4 = locationIndex.findClosest(0.05, 0.25, EdgeFilter.ALL_EDGES); assertEquals(4, qr4.getClosestNode()); QueryResult qr5 = locationIndex.findClosest(0.00, 0.05, EdgeFilter.ALL_EDGES); assertEquals(5, qr5.getClosestNode()); QueryResult qr6 = locationIndex.findClosest(0.00, 0.10, EdgeFilter.ALL_EDGES); assertEquals(6, qr6.getClosestNode()); QueryGraph qGraph = new QueryGraph(g); qGraph.lookup(qr4, qr5); rTripRouting.setQueryResults(Arrays.asList(qr5, qr4, qr5)); List<Path> paths = rTripRouting.calcPaths(qGraph, new RoutingAlgorithmFactorySimple(), new AlgorithmOptions(DIJKSTRA_BI, weighting, tMode)); assertEquals(2, paths.size()); assertEquals(IntArrayList.from(new int[]{5, 6, 3, 4}), paths.get(0).calcNodes()); assertEquals(IntArrayList.from(new int[]{4, 8, 7, 6, 5}), paths.get(1).calcNodes()); qGraph = new QueryGraph(g); qGraph.lookup(qr4, qr6); rTripRouting.setQueryResults(Arrays.asList(qr6, qr4, qr6)); paths = rTripRouting.calcPaths(qGraph, new RoutingAlgorithmFactorySimple(), new AlgorithmOptions(DIJKSTRA_BI, weighting, tMode)); assertEquals(2, paths.size()); assertEquals(IntArrayList.from(new int[]{6, 3, 4}), paths.get(0).calcNodes()); assertEquals(IntArrayList.from(new int[]{4, 8, 7, 6}), paths.get(1).calcNodes()); }
@Test public void testDisconnected179() { GraphHopperStorage graph = new GraphBuilder(encodingManager).create(); initGraph(graph); GraphHopper instance = new GraphHopper(). setStoreOnFlush(false). setEncodingManager(encodingManager).setCHEnabled(false). loadGraph(graph); GHResponse rsp = instance.route(new GHRequest(42, 10, 42, 10.4)); assertTrue(rsp.hasErrors()); try { rsp.getBest().getPoints(); assertTrue(false); } catch (Exception ex) { } instance.close(); }
@Test public void testLoad() { GraphHopperStorage graph = new GraphBuilder(encodingManager).create(); initGraph(graph); // do further changes: NodeAccess na = graph.getNodeAccess(); na.setNode(4, 41.9, 10.2); graph.edge(1, 2, 10, false); graph.edge(0, 4, 40, true); graph.edge(4, 3, 40, true); GraphHopper instance = new GraphHopper(). setStoreOnFlush(false). setEncodingManager(encodingManager).setCHEnabled(false). loadGraph(graph); // 3 -> 0 GHResponse rsp = instance.route(new GHRequest(42, 10.4, 42, 10)); assertFalse(rsp.hasErrors()); PathWrapper arsp = rsp.getBest(); assertEquals(80, arsp.getDistance(), 1e-6); PointList points = arsp.getPoints(); assertEquals(42, points.getLatitude(0), 1e-5); assertEquals(10.4, points.getLongitude(0), 1e-5); assertEquals(41.9, points.getLatitude(1), 1e-5); assertEquals(10.2, points.getLongitude(1), 1e-5); assertEquals(3, points.getSize()); instance.close(); }
new GHRequest().addPoint(new GHPoint(lat, lon)).addPoint...
GHRequest req = new GHRequest(); req.addPoint(new GHPoint(fromLat, fromLon)); req.addPoint(new GHPoint(via1lat, via1lon)); req.addPoint(new GHPoint(via2lat, via2lon)); req.addPoint(new GHPoint(toLat, toLon)); GHResponse resp = hopper.route(req);
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 static void warmUpCHSubNetwork(GraphHopper graphHopper, int iterations) { GraphHopperStorage ghStorage = graphHopper.getGraphHopperStorage(); Random rand = new Random(0); for (int i = 0; i < iterations; i++) { int startNode = rand.nextInt(graphHopper.getMaxVisitedNodes() + 1); int endNode = rand.nextInt(graphHopper.getMaxVisitedNodes() + 1); double fromLatitude = ghStorage.getNodeAccess().getLatitude(startNode); double fromLongitude = ghStorage.getNodeAccess().getLongitude(startNode); double toLatitude = ghStorage.getNodeAccess().getLatitude(endNode); double toLongitude = ghStorage.getNodeAccess().getLongitude(endNode); GHRequest request = new GHRequest(fromLatitude, fromLongitude, toLatitude, toLongitude); graphHopper.route(request); } }
public RoutingResult getRouteBetween(NavigationPosition from, NavigationPosition to, TravelMode travelMode) { initializeHopper(); long start = currentTimeMillis(); try { GHRequest request = new GHRequest(from.getLatitude(), from.getLongitude(), to.getLatitude(), to.getLongitude()); request.setVehicle(travelMode.getName().toUpperCase()); GHResponse response = hopper.route(request); if(response.hasErrors()) { String errors = printArrayToDialogString(response.getErrors().toArray(), false); log.severe(format("Error while routing between %s and %s: %s", from, to, errors)); throw new RuntimeException(errors); } PathWrapper best = response.getBest(); return new RoutingResult(asPositions(best.getPoints()), new DistanceAndTime(best.getDistance(), best.getTime() / 1000), true); } finally { long end = currentTimeMillis(); log.info("GraphHopper: routing from " + from + " to " + to + " took " + (end - start) + " milliseconds"); } }
GHRequest req = new GHRequest(32.070113, 34.790266, 32.067103, 34.777861) .setWeighting("fastest") .setVehicle("car"); GHResponse res = gh.route(req); if(res.hasErrors()) { // handle or throw exceptions res.getErrors() return; } // get path geometry information (latitude, longitude and optionally elevation) PointList pl = res.getPoints(); // distance of the full path, in meter double distance = res.getDistance(); // time of the full path, in milliseconds long millis = res.getTime(); // get information per turn instruction InstructionList il = res.getInstructions();