protected void postInit(int from, int to) { if (!traversalMode.isEdgeBased()) { if (updateBestPath) { bestWeightMapOther = bestWeightMapFrom; updateBestPath(GHUtility.getEdge(graph, currFrom.adjNode, to), currFrom, to, true); } } else if (from == to) { // special case of identical start and end bestPath.sptEntry = currFrom; bestPath.edgeTo = currTo; finishedFrom = true; finishedTo = true; } }
PointList getPoints(Graph g, int base, int adj) { EdgeIteratorState edge = GHUtility.getEdge(g, base, adj); if (edge == null) throw new IllegalStateException("edge " + base + "-" + adj + " not found"); return edge.fetchWayGeometry(3); }
private void blockNode3(Graph g, TurnCostExtension tcs, TurnCostEncoder tEncoder) { // Totally block this node (all 9 turn relations) final long BLOCK = tEncoder.getTurnFlags(true, 0); tcs.addTurnInfo(getEdge(g, 2, 3).getEdge(), 3, getEdge(g, 3, 1).getEdge(), BLOCK); tcs.addTurnInfo(getEdge(g, 2, 3).getEdge(), 3, getEdge(g, 3, 4).getEdge(), BLOCK); tcs.addTurnInfo(getEdge(g, 4, 3).getEdge(), 3, getEdge(g, 3, 1).getEdge(), BLOCK); tcs.addTurnInfo(getEdge(g, 4, 3).getEdge(), 3, getEdge(g, 3, 2).getEdge(), BLOCK); tcs.addTurnInfo(getEdge(g, 6, 3).getEdge(), 3, getEdge(g, 3, 1).getEdge(), BLOCK); tcs.addTurnInfo(getEdge(g, 6, 3).getEdge(), 3, getEdge(g, 3, 4).getEdge(), BLOCK); tcs.addTurnInfo(getEdge(g, 1, 3).getEdge(), 3, getEdge(g, 3, 6).getEdge(), BLOCK); tcs.addTurnInfo(getEdge(g, 1, 3).getEdge(), 3, getEdge(g, 3, 2).getEdge(), BLOCK); tcs.addTurnInfo(getEdge(g, 1, 3).getEdge(), 3, getEdge(g, 3, 4).getEdge(), BLOCK); }
private void initTurnRestrictions(Graph g, TurnCostExtension tcs, TurnCostEncoder tEncoder) { long tflags = tEncoder.getTurnFlags(true, 0); // only forward from 2-3 to 3-4 => limit 2,3->3,6 and 2,3->3,1 tcs.addTurnInfo(getEdge(g, 2, 3).getEdge(), 3, getEdge(g, 3, 6).getEdge(), tflags); tcs.addTurnInfo(getEdge(g, 2, 3).getEdge(), 3, getEdge(g, 3, 1).getEdge(), tflags); // only right from 5-2 to 2-3 => limit 5,2->2,0 tcs.addTurnInfo(getEdge(g, 5, 2).getEdge(), 2, getEdge(g, 2, 0).getEdge(), tflags); // only right from 7-6 to 6-3 => limit 7,6->6,5 tcs.addTurnInfo(getEdge(g, 7, 6).getEdge(), 6, getEdge(g, 6, 5).getEdge(), tflags); // no 5-6 to 6-3 tcs.addTurnInfo(getEdge(g, 5, 6).getEdge(), 6, getEdge(g, 6, 3).getEdge(), tflags); // no 4-3 to 3-1 tcs.addTurnInfo(getEdge(g, 4, 3).getEdge(), 3, getEdge(g, 3, 1).getEdge(), tflags); // no 4-3 to 3-2 tcs.addTurnInfo(getEdge(g, 4, 3).getEdge(), 3, getEdge(g, 3, 2).getEdge(), tflags); // no u-turn at 6-7 tcs.addTurnInfo(getEdge(g, 6, 7).getEdge(), 7, getEdge(g, 7, 6).getEdge(), tflags); // no u-turn at 3-6 tcs.addTurnInfo(getEdge(g, 3, 6).getEdge(), 6, getEdge(g, 6, 3).getEdge(), tflags); }
@Test public void testEdgesShareOneNode() { initGraph(g); EdgeIteratorState iter = GHUtility.getEdge(g, 0, 2); QueryResult res1 = createLocationResult(0.5, 0, iter, 0, EDGE); iter = GHUtility.getEdge(g, 1, 0); QueryResult res2 = createLocationResult(1.5, 2, iter, 0, EDGE); QueryGraph queryGraph = new QueryGraph(g); queryGraph.lookup(Arrays.asList(res1, res2)); assertEquals(new GHPoint(0.5, 0), res1.getSnappedPoint()); assertEquals(new GHPoint(1.300019, 1.899962), res2.getSnappedPoint()); assertNotNull(GHUtility.getEdge(queryGraph, 0, 4)); assertNotNull(GHUtility.getEdge(queryGraph, 0, 3)); }
@Test public void testGetEdgeProps() { initGraph(g); EdgeIteratorState e1 = GHUtility.getEdge(g, 0, 2); QueryGraph queryGraph = new QueryGraph(g); QueryResult res1 = createLocationResult(0.5, 0, e1, 0, EDGE); queryGraph.lookup(Arrays.asList(res1)); // get virtual edge e1 = GHUtility.getEdge(queryGraph, res1.getClosestNode(), 0); EdgeIteratorState e2 = queryGraph.getEdgeIteratorState(e1.getEdge(), Integer.MIN_VALUE); assertEquals(e1.getEdge(), e2.getEdge()); }
@Test public void testCurvature() { Graph graph = initExampleGraph(); EdgeIteratorState edge = GHUtility.getEdge(graph, 0, 1); double bendinessOfStraightWay = getBendiness(edge, 100.0); double bendinessOfCurvyWay = getBendiness(edge, 10.0); assertTrue("The bendiness of the straight road is smaller than the one of the curvy road", bendinessOfCurvyWay < bendinessOfStraightWay); }
@Test public void testUnchangedForStepsBridgeAndTunnel() { Graph graph = initExampleGraph(); EdgeIteratorState edge = GHUtility.getEdge(graph, 0, 1); long oldFlags = edge.getFlags(); ReaderWay way = new ReaderWay(1); way.setTag("highway", "steps"); encoder.applyWayTags(way, edge); assertEquals(oldFlags, edge.getFlags()); }
@Test public void testEnabledElevation() { graph = createGHStorage(defaultGraphLoc, true); NodeAccess na = graph.getNodeAccess(); assertTrue(na.is3D()); na.setNode(0, 10, 20, -10); na.setNode(1, 11, 2, 100); assertEquals(-10, na.getEle(0), 1e-1); assertEquals(100, na.getEle(1), 1e-1); graph.edge(0, 1).setWayGeometry(Helper.createPointList3D(10, 27, 72, 11, 20, 1)); assertEquals(Helper.createPointList3D(10, 27, 72, 11, 20, 1), GHUtility.getEdge(graph, 0, 1).fetchWayGeometry(0)); assertEquals(Helper.createPointList3D(10, 20, -10, 10, 27, 72, 11, 20, 1, 11, 2, 100), GHUtility.getEdge(graph, 0, 1).fetchWayGeometry(3)); assertEquals(Helper.createPointList3D(11, 2, 100, 11, 20, 1, 10, 27, 72, 10, 20, -10), GHUtility.getEdge(graph, 1, 0).fetchWayGeometry(3)); }
@Test public void testApplyWayTags() { Graph graph = initExampleGraph(); EdgeIteratorState edge = GHUtility.getEdge(graph, 0, 1); ReaderWay way = new ReaderWay(1); encoder.applyWayTags(way, edge); long flags = edge.getFlags(); // decrease speed assertEquals(2, encoder.getSpeed(flags), 1e-1); // increase speed but use maximum speed (calculated was 24) assertEquals(18, encoder.getReverseSpeed(flags), 1e-1); }
@Test public void testRemoveNode() { FlagEncoder carEncoder = new CarFlagEncoder(); BikeFlagEncoder bikeEncoder = new BikeFlagEncoder(); EncodingManager em2 = new EncodingManager(carEncoder, bikeEncoder); GraphHopperStorage g = createSubnetworkTestStorage2(em2); PrepareRoutingSubnetworks instance = new PrepareRoutingSubnetworks(g, em2.fetchEdgeEncoders()); EdgeExplorer edgeExplorer = g.createEdgeExplorer(); assertFalse(instance.detectNodeRemovedForAllEncoders(edgeExplorer, 4)); assertFalse(instance.detectNodeRemovedForAllEncoders(edgeExplorer, 5)); assertFalse(instance.detectNodeRemovedForAllEncoders(edgeExplorer, 6)); // mark certain edges inaccessible for all encoders for (EdgeIteratorState edge : Arrays.asList(GHUtility.getEdge(g, 5, 6), GHUtility.getEdge(g, 4, 5), GHUtility.getEdge(g, 4, 6))) { for (FlagEncoder encoders : em2.fetchEdgeEncoders()) { edge.setFlags(encoders.setAccess(0, false, false)); } } assertTrue(instance.detectNodeRemovedForAllEncoders(edgeExplorer, 4)); assertTrue(instance.detectNodeRemovedForAllEncoders(edgeExplorer, 5)); assertTrue(instance.detectNodeRemovedForAllEncoders(edgeExplorer, 6)); }
@Test public void testMergeFlagsBeforeAdding() { FlagEncoder carEncoder = new CarFlagEncoder(5, 5, 3); FlagEncoder bikeEncoder = new BikeFlagEncoder(5, 5, 3); EncodingManager manager = new EncodingManager(carEncoder, bikeEncoder); GraphHopperStorage g = new GraphBuilder(manager).create(); initGraph(g); TurnCostExtension tcs = (TurnCostExtension) g.getExtension(); long carRestricted = carEncoder.getTurnFlags(true, 0); long bikeRestricted = bikeEncoder.getTurnFlags(true, 0); int edge23 = getEdge(g, 2, 3).getEdge(); int edge02 = getEdge(g, 0, 2).getEdge(); tcs.addTurnInfo(edge02, 2, edge23, carRestricted | bikeRestricted); long flags023 = tcs.getTurnCostFlags(edge02, 2, edge23); assertEquals(Double.POSITIVE_INFINITY, carEncoder.getTurnCost(flags023), 0); assertEquals(Double.POSITIVE_INFINITY, bikeEncoder.getTurnCost(flags023), 0); } }
@Test public void testunfavorVirtualEdgePair() { initHorseshoeGraph(g); EdgeIteratorState edge = GHUtility.getEdge(g, 0, 1); // query result on first vertical part of way (upward) QueryResult qr = fakeEdgeQueryResult(edge, 1.5, 0, 0); QueryGraph queryGraph = new QueryGraph(g); queryGraph.lookup(Arrays.asList(qr)); // enforce coming in north queryGraph.unfavorVirtualEdgePair(2, 1); // test penalized south VirtualEdgeIteratorState incomingEdge = (VirtualEdgeIteratorState) queryGraph.getEdgeIteratorState(1, 2); VirtualEdgeIteratorState incomingEdgeReverse = (VirtualEdgeIteratorState) queryGraph.getEdgeIteratorState(1, incomingEdge.getBaseNode()); boolean expect = true; // expect incoming and reverse incoming edge to be avoided assertEquals(expect, incomingEdge.getBool(EdgeIteratorState.K_UNFAVORED_EDGE, !expect)); assertEquals(expect, incomingEdgeReverse.getBool(EdgeIteratorState.K_UNFAVORED_EDGE, !expect)); assertEquals(new LinkedHashSet<>(Arrays.asList(incomingEdge, incomingEdgeReverse)), queryGraph.getUnfavoredVirtualEdges()); queryGraph.clearUnfavoredStatus(); expect = false; // expect incoming and reverse incoming edge not to be avoided assertEquals(expect, incomingEdge.getBool(EdgeIteratorState.K_UNFAVORED_EDGE, !expect)); assertEquals(expect, incomingEdgeReverse.getBool(EdgeIteratorState.K_UNFAVORED_EDGE, !expect)); assertEquals(new LinkedHashSet<>(), queryGraph.getUnfavoredVirtualEdges()); }
@Test public void testOneWay() { NodeAccess na = g.getNodeAccess(); na.setNode(0, 0, 0); na.setNode(1, 0, 1); g.edge(0, 1, 10, false); EdgeIteratorState edge = GHUtility.getEdge(g, 0, 1); QueryResult res1 = createLocationResult(0.1, 0.1, edge, 0, EDGE); QueryResult res2 = createLocationResult(0.1, 0.9, edge, 0, EDGE); QueryGraph queryGraph = new QueryGraph(g); queryGraph.lookup(Arrays.asList(res2, res1)); assertEquals(2, res1.getClosestNode()); assertEquals(new GHPoint(0, 0.1), res1.getSnappedPoint()); assertEquals(3, res2.getClosestNode()); assertEquals(new GHPoint(0, 0.9), res2.getSnappedPoint()); assertEquals(2, getPoints(queryGraph, 0, 2).getSize()); assertEquals(2, getPoints(queryGraph, 2, 3).getSize()); assertEquals(2, getPoints(queryGraph, 3, 1).getSize()); assertNull(GHUtility.getEdge(queryGraph, 3, 0)); assertNull(GHUtility.getEdge(queryGraph, 2, 1)); }
@Test public void testBasicTurnCosts() { GraphHopperStorage g = createStorage(createEncodingManager(false)); initGraph(g); TurnCostExtension tcs = (TurnCostExtension) g.getExtension(); Path p = createAlgo(g, AlgorithmOptions.start(). weighting(createWeighting(carEncoder, tcs, 40)). traversalMode(TraversalMode.EDGE_BASED_1DIR).build()). calcPath(5, 1); // no restriction and costs EdgeIteratorState e3_6 = getEdge(g, 5, 6); e3_6.setDistance(2); assertEquals(IntArrayList.from(new int[]{5, 2, 3, 1}), p.calcNodes()); // now introduce some turn costs long tflags = carEncoder.getTurnFlags(false, 2); tcs.addTurnInfo(getEdge(g, 5, 2).getEdge(), 2, getEdge(g, 2, 3).getEdge(), tflags); p = createAlgo(g, AlgorithmOptions.start(). weighting(createWeighting(carEncoder, tcs, 40)). traversalMode(TraversalMode.EDGE_BASED_1DIR).build()). calcPath(5, 1); assertEquals(IntArrayList.from(new int[]{5, 6, 3, 1}), p.calcNodes()); }
/** * Creates query result on edge (node1-node2) very close to node1. */ QueryResult newQR(Graph graph, int node1, int node2) { EdgeIteratorState edge = GHUtility.getEdge(graph, node1, node2); if (edge == null) throw new IllegalStateException("edge not found? " + node1 + "-" + node2); NodeAccess na = graph.getNodeAccess(); double lat = na.getLatitude(edge.getBaseNode()); double lon = na.getLongitude(edge.getBaseNode()); double latAdj = na.getLatitude(edge.getAdjNode()); double lonAdj = na.getLongitude(edge.getAdjNode()); // calculate query point near the base node but not directly on it! QueryResult res = new QueryResult(lat + (latAdj - lat) * .1, lon + (lonAdj - lon) * .1); res.setClosestNode(edge.getBaseNode()); res.setClosestEdge(edge); res.setWayIndex(0); res.setSnappedPosition(QueryResult.Position.EDGE); res.calcSnappedPoint(distCalc); return res; }
@Test public void testMultiplePreparationsDifferentView() { CarFlagEncoder tmpCarEncoder = new CarFlagEncoder(); BikeFlagEncoder tmpBikeEncoder = new BikeFlagEncoder(); EncodingManager tmpEncodingManager = new EncodingManager(tmpCarEncoder, tmpBikeEncoder); Weighting carWeighting = new FastestWeighting(tmpCarEncoder); Weighting bikeWeighting = new FastestWeighting(tmpBikeEncoder); List<Weighting> chWeightings = Arrays.asList(carWeighting, bikeWeighting); GraphHopperStorage ghStorage = new GraphHopperStorage(chWeightings, dir, tmpEncodingManager, false, new GraphExtension.NoOpExtension()).create(1000); initShortcutsGraph(ghStorage); EdgeIteratorState edge = GHUtility.getEdge(ghStorage, 9, 14); edge.setFlags(tmpBikeEncoder.setAccess(edge.getFlags(), false, false)); ghStorage.freeze(); checkPath(ghStorage, carWeighting, 7, 5, IntArrayList.from(3, 9, 14, 16, 13, 12)); // detour around blocked 9,14 checkPath(ghStorage, bikeWeighting, 9, 5, IntArrayList.from(3, 10, 14, 16, 13, 12)); }
@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); }
@Test public void testMultipleVehicles_issue548() { FastestWeighting footWeighting = new FastestWeighting(footEncoder); AlgorithmOptions footOptions = AlgorithmOptions.start(). weighting(footWeighting).build(); FastestWeighting carWeighting = new FastestWeighting(carEncoder); AlgorithmOptions carOptions = AlgorithmOptions.start(). weighting(carWeighting).build(); GraphHopperStorage ghStorage = createGHStorage(encodingManager, Arrays.asList(footOptions.getWeighting(), carOptions.getWeighting()), false); initFootVsCar(ghStorage); // normal path would be 0-4-6-7 but block 4-6 GHUtility.getEdge(ghStorage, 4, 6).setFlags(carEncoder.setProperties(20, false, false)); RoutingAlgorithm algoFoot = createFactory(ghStorage, footOptions). createAlgo(getGraph(ghStorage, footWeighting), footOptions); RoutingAlgorithm algoCar = createFactory(ghStorage, carOptions). createAlgo(getGraph(ghStorage, carWeighting), carOptions); Path p1 = algoCar.calcPath(0, 7); assertEquals(IntArrayList.from(new int[]{0, 1, 5, 6, 7}), p1.calcNodes()); assertEquals(p1.toString(), 26000, p1.getDistance(), 1e-6); }
@Test public void testGetWeightIfAdvancedEncoder() { FlagEncoder customEncoder = new Bike2WeightFlagEncoder(); EncodingManager em = new EncodingManager(customEncoder); FastestWeighting weighting = new FastestWeighting(customEncoder); GraphHopperStorage ghStorage = new GraphBuilder(em).setCHGraph(weighting).create(); ghStorage.edge(0, 2); ghStorage.freeze(); CHGraphImpl lg = (CHGraphImpl) ghStorage.getGraph(CHGraph.class, weighting); CHEdgeIteratorState sc1 = lg.shortcut(0, 1); long flags = customEncoder.setProperties(10, false, true); sc1.setFlags(flags); sc1.setWeight(100.123); assertEquals(100.123, lg.getEdgeIteratorState(sc1.getEdge(), sc1.getAdjNode()).getWeight(), 1e-3); assertEquals(100.123, lg.getEdgeIteratorState(sc1.getEdge(), sc1.getBaseNode()).getWeight(), 1e-3); assertEquals(100.123, ((CHEdgeIteratorState) GHUtility.getEdge(lg, sc1.getBaseNode(), sc1.getAdjNode())).getWeight(), 1e-3); assertEquals(100.123, ((CHEdgeIteratorState) GHUtility.getEdge(lg, sc1.getAdjNode(), sc1.getBaseNode())).getWeight(), 1e-3); sc1 = lg.shortcut(1, 0); assertTrue(sc1.isShortcut()); sc1.setFlags(PrepareEncoder.getScDirMask()); sc1.setWeight(1.011011); assertEquals(1.011011, sc1.getWeight(), 1e-3); }