for (int pathIndex = 0; pathIndex < paths.size(); pathIndex++) { Path path = paths.get(pathIndex); if (!path.isFound()) { allFound = false; continue; description.addAll(path.getDescription()); fullTimeInMillis += path.getTime(); fullDistance += path.getDistance(); fullWeight += path.getWeight(); if (enableInstructions) { InstructionList il = path.calcInstructions(tr); PointList tmpPoints = path.calcPoints(); if (fullPoints.isEmpty()) fullPoints = new PointList(tmpPoints.size(), tmpPoints.is3D()); altRsp.addPathDetails(path.calcDetails(requestedPathDetails, pathBuilderFactory, origPoints)); origPoints = fullPoints.size(); allFound = allFound && path.isFound();
@Override protected Path extractPath() { return new Path(graph, weighting). setWeight(currEdge.weight).setSPTEntry(currEdge).extract(); }
/** * This method adds the traversal IDs of the specified path as set to the specified map. */ AtomicInteger addToMap(GHIntObjectHashMap<IntSet> map, Path path) { IntSet set = new GHIntHashSet(); final AtomicInteger startTID = new AtomicInteger(-1); for (EdgeIteratorState iterState : path.calcEdges()) { int tid = traversalMode.createTraversalId(iterState, false); set.add(tid); if (startTID.get() < 0) { // for node based traversal we need to explicitely add base node as starting node and to list if (!traversalMode.isEdgeBased()) { tid = iterState.getBaseNode(); set.add(tid); } startTID.set(tid); } } map.put(startTID.get(), set); return startTID; } }
@Override protected Path extractPath() { if (currEdge == null || !finished()) { return createEmptyPath(); } return new Path(graph, weighting).setSPTEntry(currEdge).extract(); }
@Test public void testCalcFootPath() { AlgorithmOptions opts = AlgorithmOptions.start(). weighting(new ShortestWeighting(footEncoder)).build(); GraphHopperStorage ghStorage = createGHStorage(encodingManager, Arrays.asList(opts.getWeighting()), false); initFootVsCar(ghStorage); Path p1 = createAlgo(ghStorage, opts). calcPath(0, 7); assertEquals(p1.toString(), 17000, p1.getDistance(), 1e-6); assertEquals(p1.toString(), 12240 * 1000, p1.getTime()); assertEquals(IntArrayList.from(new int[]{0, 4, 5, 7}), p1.calcNodes()); }
@Test public void testCalcInstructionsIgnoreContinue() { // Follow a couple of straight edges, including a name change Path p = new Dijkstra(roundaboutGraph.g, new ShortestWeighting(encoder), TraversalMode.NODE_BASED) .calcPath(4, 11); assertTrue(p.isFound()); InstructionList wayList = p.calcInstructions(tr); // Contain only start and finish instruction, no CONTINUE assertEquals(2, wayList.size()); }
@Test public void testViaEdges_BiGraph() { GraphHopperStorage graph = createGHStorage(false); initBiGraph(graph); // 0-7 to 4-3 Path p = calcPathViaQuery(graph, 0.0009, 0, 0.001, 0.001105); assertEquals(p.toString(), IntArrayList.from(new int[]{10, 7, 6, 8, 3, 9}), p.calcNodes()); assertEquals(p.toString(), 324.11, p.getDistance(), 0.01); // 0-1 to 2-3 p = calcPathViaQuery(graph, 0.001, 0.0001, 0.010, 0.0011); assertEquals(p.toString(), IntArrayList.from(new int[]{0, 7, 6, 8, 3, 9}), p.calcNodes()); assertEquals(p.toString(), 1335.35, p.getDistance(), 0.01); }
Path path = new Path(g, new FastestWeighting(encoder)); SPTEntry e1 = new SPTEntry(edge2.getEdge(), 2, 1); e1.parent = new SPTEntry(edge1.getEdge(), 1, 1); e1.parent.parent = new SPTEntry(-1, 0, 1); path.setSPTEntry(e1); path.extract(); assertPList(Helper.createPointList(0, 0.1, 8, 1, 9, 1, 1, 0.1, 10, 1, 11, 1, 2, 0.1), path.calcPoints()); InstructionList instr = path.calcInstructions(tr); List<Map<String, Object>> res = instr.createJson(); Map<String, Object> tmp = res.get(0); assertEquals("[6, 6]", tmp.get("interval").toString()); int lastIndex = (Integer) ((List) res.get(res.size() - 1).get("interval")).get(0); assertEquals(path.calcPoints().size() - 1, lastIndex); path = new Path(g, new FastestWeighting(encoder)); e1 = new SPTEntry(edge2.getEdge(), 2, 1); e1.parent = new SPTEntry(edge1.getEdge(), 1, 1); e1.parent.parent = new SPTEntry(-1, 0, 1); path.setSPTEntry(e1); path.extract(); instr = path.calcInstructions(tr); res = instr.createJson(); assertEquals("[3, 6]", tmp.get("interval").toString()); lastIndex = (Integer) ((List) res.get(res.size() - 1).get("interval")).get(0); assertEquals(path.calcPoints().size() - 1, lastIndex);
private MatchResult computeMatchResult(List<SequenceState<GPXExtension, GPXEntry, Path>> seq, Map<String, EdgeIteratorState> virtualEdgesMap, List<GPXEntry> gpxList, QueryGraph queryGraph) { double distance = 0.0; long time = 0; for (SequenceState<GPXExtension, GPXEntry, Path> transitionAndState : seq) { if (transitionAndState.transitionDescriptor != null) { distance += transitionAndState.transitionDescriptor.getDistance(); time += transitionAndState.transitionDescriptor.getTime(); } } List<EdgeIteratorState> edges = new ArrayList<>(); for (SequenceState<GPXExtension, GPXEntry, Path> state : seq) { if (state.transitionDescriptor != null) { edges.addAll(state.transitionDescriptor.calcEdges()); } } Path mergedPath = new MapMatchedPath(queryGraph.getBaseGraph(), weighting, edges); List<EdgeMatch> edgeMatches = computeEdgeMatches(seq, virtualEdgesMap); MatchResult matchResult = new MatchResult(edgeMatches); matchResult.setMergedPath(mergedPath); matchResult.setMatchMillis(time); matchResult.setMatchLength(distance); matchResult.setGPXEntriesMillis(durationMillis(gpxList)); matchResult.setGPXEntriesLength(gpxLength(gpxList)); return matchResult; }
private Path plotPath(Path tmpPath, Graphics2D g2, int w) { if (!tmpPath.isFound()) { logger.info("nothing found " + w); return tmpPath; } double prevLat = Double.NaN; double prevLon = Double.NaN; boolean plotNodes = false; IntIndexedContainer nodes = tmpPath.calcNodes(); if (plotNodes) { for (int i = 0; i < nodes.size(); i++) { plotNodeName(g2, nodes.get(i)); } } PointList list = tmpPath.calcPoints(); for (int i = 0; i < list.getSize(); i++) { double lat = list.getLatitude(i); double lon = list.getLongitude(i); if (!Double.isNaN(prevLat)) { mg.plotEdge(g2, prevLat, prevLon, lat, lon, w); } else { mg.plot(g2, lat, lon, w); } prevLat = lat; prevLon = lon; } logger.info("dist:" + tmpPath.getDistance() + ", path points(" + list.getSize() + ")"); return tmpPath; }
@Test public void testFound() { GraphHopperStorage g = new GraphBuilder(carManager).create(); Path p = new Path(g, new FastestWeighting(encoder)); assertFalse(p.isFound()); assertEquals(0, p.getDistance(), 1e-7); assertEquals(0, p.calcNodes().size()); g.close(); }
public void testBug1(GraphHopperStorage g) { Path p = createAlgo(g).calcPath(34, 36); assertEquals(IntArrayList.from(new int[]{34, 35, 36}), p.calcNodes()); assertEquals(3, p.calcNodes().size()); assertEquals(17, p.getDistance(), 1e-5); }
Path p = createAlgo(graph, opts).calcPath(0, 10); assertEquals(IntArrayList.from(new int[]{0, 4, 6, 10}), p.calcNodes()); QueryGraph qGraph = new QueryGraph(getGraph(graph, fakeWeighting)).lookup(from, to); p = factory.createAlgo(qGraph, fakeOpts).calcPath(from.getClosestNode(), to.getClosestNode()); assertEquals(IntArrayList.from(new int[]{12, 0, 1, 2, 11, 7, 10, 13}), p.calcNodes()); assertEquals(37009621, p.getTime()); assertEquals(616827, p.getDistance(), 1); assertEquals(493462, p.getWeight(), 1);
if (prevRoute.getEdgeCount() > 0) { EdgeIteratorState incomingVirtualEdge = prevRoute.getFinalEdge(); queryGraph.unfavorVirtualEdgePair(fromQResult.getClosestNode(), incomingVirtualEdge.getEdge()); if (path.getTime() < 0) throw new RuntimeException("Time was negative " + path.getTime() + " for index " + idx + ". Please report as bug and include:" + ghRequest); debug += ", " + path.getDebugInfo(); idx++;
InstructionList wayList = p.calcInstructions(usTR); assertEquals(4, wayList.size()); assertEquals(34000, p.getDistance(), 1e-1); assertEquals(34000, sumDistances(wayList), 1e-1); assertEquals(5, gpxList.size()); assertEquals(1604120, p.getTime()); assertEquals(1604120, gpxList.get(gpxList.size() - 1).getTime());
@Test public void testTwoWeightsPerEdge() { FlagEncoder encoder = new Bike2WeightFlagEncoder(); EncodingManager em = new EncodingManager(encoder); AlgorithmOptions opts = AlgorithmOptions.start(). weighting(new FastestWeighting(encoder)).build(); GraphHopperStorage graph = createGHStorage(em, Arrays.asList(opts.getWeighting()), true); initEleGraph(graph); // force the other path GHUtility.getEdge(graph, 0, 3).setFlags(encoder.setProperties(10, false, true)); // for two weights per edge it happened that Path (and also the Weighting) read the wrong side // of the speed and read 0 => infinity weight => overflow of millis => negative millis! Path p = createAlgo(graph, opts). calcPath(0, 10); assertEquals(85124371, p.getTime()); assertEquals(425622, p.getDistance(), 1); assertEquals(85124.4, p.getWeight(), 1); }
/** * Returns the path length plus a penalty if the starting/ending edge is unfavored. */ private double penalizedPathDistance(Path path, Set<EdgeIteratorState> penalizedVirtualEdges) { double totalPenalty = 0; // Unfavored edges in the middle of the path should not be penalized because we are // only concerned about the direction at the start/end. final List<EdgeIteratorState> edges = path.calcEdges(); if (!edges.isEmpty()) { if (penalizedVirtualEdges.contains(edges.get(0))) { totalPenalty += uTurnDistancePenalty; } } if (edges.size() > 1) { if (penalizedVirtualEdges.contains(edges.get(edges.size() - 1))) { totalPenalty += uTurnDistancePenalty; } } return path.getDistance() + totalPenalty; }
createAlgo(g2, opts).calcPath(0, 7); assertEquals(IntArrayList.from(0, 2, 5, 7), p.calcNodes()); assertEquals(1064, p.getTime()); assertEquals(4.2, p.getDistance(), 1e-5);