@Override public GHResponse route(GHRequest request) { GHResponse response = new GHResponse(); calcPaths(request, response); return response; }
@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); }
@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); } }
@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()); }
@Override public GHResponse route(GHRequest request) { GHResponse response = new GHResponse(); calcPaths(request, response); return response; }
@Override public GHResponse route(GHRequest request) { GHResponse response = new GHResponse(); calcPaths(request, response); return response; }
@Override public GHResponse route( GHRequest request ) { GHResponse response = new GHResponse(); calcPaths(request, response); return response; }
public class GraphHopperWithPaths extends GraphHopper { public List<Integer> routePaths(double startY, double startX, double endY, double endX){ //Examine a route and return edgeIDs that GraphHopper uses LocationIndex index = this.getLocationIndex(); GHRequest request = new GHRequest(startY, startX, endY, endX); GHResponse response = new GHResponse(); List<Path> paths = getPaths(request, response); List<Integer> edges = new ArrayList<Integer>(); for(Path p:paths){ for(EdgeIteratorState e:p.calcEdges()){ edges.add(e.getEdge()); } } if (response.hasErrors()) return null; //Get edges for start and end point as well QueryResult qr = index.findClosest(startY, startX, EdgeFilter.ALL_EDGES ); edges.add(qr.getClosestEdge().getEdge()); qr = index.findClosest(endY, endX, EdgeFilter.ALL_EDGES ); edges.add(qr.getClosestEdge().getEdge()); return edges; } }
GHResponse rsp = new GHResponse(); rsp.add(pathWrapper);