Search in sources :

Example 16 with FastestWeighting

use of com.graphhopper.routing.weighting.FastestWeighting in project graphhopper by graphhopper.

the class PathTest method testWayList.

@Test
public void testWayList() {
    GraphHopperStorage g = new GraphBuilder(carManager).create();
    NodeAccess na = g.getNodeAccess();
    na.setNode(0, 0.0, 0.1);
    na.setNode(1, 1.0, 0.1);
    na.setNode(2, 2.0, 0.1);
    EdgeIteratorState edge1 = g.edge(0, 1).setDistance(1000).setFlags(encoder.setProperties(10, true, true));
    edge1.setWayGeometry(Helper.createPointList(8, 1, 9, 1));
    EdgeIteratorState edge2 = g.edge(2, 1).setDistance(2000).setFlags(encoder.setProperties(50, true, true));
    edge2.setWayGeometry(Helper.createPointList(11, 1, 10, 1));
    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();
    // 0-1-2
    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(3000.0, tmp.get("distance"));
    assertEquals(504000L, tmp.get("time"));
    assertEquals("Continue", tmp.get("text"));
    assertEquals("[0, 6]", tmp.get("interval").toString());
    tmp = res.get(1);
    assertEquals(0.0, tmp.get("distance"));
    assertEquals(0L, tmp.get("time"));
    assertEquals("Arrive at destination", tmp.get("text"));
    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);
    // force minor change for instructions
    edge2.setName("2");
    na.setNode(3, 1.0, 1.0);
    EdgeIteratorState edge3 = g.edge(1, 3).setDistance(1000).setFlags(encoder.setProperties(10, true, true));
    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();
    tmp = res.get(0);
    assertEquals(1000.0, tmp.get("distance"));
    assertEquals(360000L, tmp.get("time"));
    assertEquals("Continue", tmp.get("text"));
    assertEquals("[0, 3]", tmp.get("interval").toString());
    tmp = res.get(1);
    assertEquals(2000.0, tmp.get("distance"));
    assertEquals(144000L, tmp.get("time"));
    assertEquals("Turn sharp right onto 2", tmp.get("text"));
    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);
    // now reverse order
    path = new Path(g, new FastestWeighting(encoder));
    e1 = new SPTEntry(edge1.getEdge(), 0, 1);
    e1.parent = new SPTEntry(edge2.getEdge(), 1, 1);
    e1.parent.parent = new SPTEntry(-1, 2, 1);
    path.setSPTEntry(e1);
    path.extract();
    // 2-1-0
    assertPList(Helper.createPointList(2, 0.1, 11, 1, 10, 1, 1, 0.1, 9, 1, 8, 1, 0, 0.1), path.calcPoints());
    instr = path.calcInstructions(tr);
    res = instr.createJson();
    tmp = res.get(0);
    assertEquals(2000.0, tmp.get("distance"));
    assertEquals(144000L, tmp.get("time"));
    assertEquals("Continue onto 2", tmp.get("text"));
    assertEquals("[0, 3]", tmp.get("interval").toString());
    tmp = res.get(1);
    assertEquals(1000.0, tmp.get("distance"));
    assertEquals(360000L, tmp.get("time"));
    assertEquals("Turn sharp left", tmp.get("text"));
    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);
}
Also used : FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) AbstractGraphStorageTester.assertPList(com.graphhopper.storage.AbstractGraphStorageTester.assertPList) Test(org.junit.Test)

Example 17 with FastestWeighting

use of com.graphhopper.routing.weighting.FastestWeighting in project graphhopper by graphhopper.

the class PathTest method testFindInstruction.

@Test
public void testFindInstruction() {
    Graph g = new GraphBuilder(carManager).create();
    NodeAccess na = g.getNodeAccess();
    na.setNode(0, 0.0, 0.0);
    na.setNode(1, 5.0, 0.0);
    na.setNode(2, 5.0, 0.5);
    na.setNode(3, 10.0, 0.5);
    na.setNode(4, 7.5, 0.25);
    na.setNode(5, 5.0, 1.0);
    EdgeIteratorState edge1 = g.edge(0, 1).setDistance(1000).setFlags(encoder.setProperties(50, true, true));
    edge1.setWayGeometry(Helper.createPointList());
    edge1.setName("Street 1");
    EdgeIteratorState edge2 = g.edge(1, 2).setDistance(1000).setFlags(encoder.setProperties(50, true, true));
    edge2.setWayGeometry(Helper.createPointList());
    edge2.setName("Street 2");
    EdgeIteratorState edge3 = g.edge(2, 3).setDistance(1000).setFlags(encoder.setProperties(50, true, true));
    edge3.setWayGeometry(Helper.createPointList());
    edge3.setName("Street 3");
    EdgeIteratorState edge4 = g.edge(3, 4).setDistance(500).setFlags(encoder.setProperties(50, true, true));
    edge4.setWayGeometry(Helper.createPointList());
    edge4.setName("Street 4");
    g.edge(1, 5).setDistance(10000).setFlags(encoder.setProperties(50, true, true));
    g.edge(2, 5).setDistance(10000).setFlags(encoder.setProperties(50, true, true));
    g.edge(3, 5).setDistance(100000).setFlags(encoder.setProperties(50, true, true));
    Path path = new Path(g, new FastestWeighting(encoder));
    SPTEntry e1 = new SPTEntry(edge4.getEdge(), 4, 1);
    e1.parent = new SPTEntry(edge3.getEdge(), 3, 1);
    e1.parent.parent = new SPTEntry(edge2.getEdge(), 2, 1);
    e1.parent.parent.parent = new SPTEntry(edge1.getEdge(), 1, 1);
    e1.parent.parent.parent.parent = new SPTEntry(-1, 0, 1);
    path.setSPTEntry(e1);
    path.extract();
    InstructionList il = path.calcInstructions(tr);
    Instruction nextInstr0 = il.find(-0.001, 0.0, 1000);
    assertEquals(Instruction.CONTINUE_ON_STREET, nextInstr0.getSign());
    Instruction nextInstr1 = il.find(0.001, 0.001, 1000);
    assertEquals(Instruction.TURN_RIGHT, nextInstr1.getSign());
    Instruction nextInstr2 = il.find(5.0, 0.004, 1000);
    assertEquals(Instruction.TURN_LEFT, nextInstr2.getSign());
    Instruction nextInstr3 = il.find(9.99, 0.503, 1000);
    assertEquals(Instruction.TURN_SHARP_LEFT, nextInstr3.getSign());
    // a bit far away ...
    Instruction nextInstr4 = il.find(7.40, 0.25, 20000);
    assertEquals(Instruction.FINISH, nextInstr4.getSign());
    // too far away
    assertNull(il.find(50.8, 50.25, 1000));
}
Also used : FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Test(org.junit.Test)

Example 18 with FastestWeighting

use of com.graphhopper.routing.weighting.FastestWeighting in project graphhopper by graphhopper.

the class GraphHopperOSMTest method createSquareGraphInstance.

private GraphHopper createSquareGraphInstance(boolean withCH) {
    CarFlagEncoder carEncoder = new CarFlagEncoder();
    EncodingManager encodingManager = new EncodingManager(carEncoder);
    Weighting weighting = new FastestWeighting(carEncoder);
    GraphHopperStorage g = new GraphHopperStorage(Collections.singletonList(weighting), new RAMDirectory(), encodingManager, false, new GraphExtension.NoOpExtension()).create(20);
    // 2---3---4
    // /    |    \
    // 1----8----5
    // /    |    /
    // 0----7---6
    NodeAccess na = g.getNodeAccess();
    na.setNode(0, 0.000, 0.000);
    na.setNode(1, 0.001, 0.000);
    na.setNode(2, 0.002, 0.000);
    na.setNode(3, 0.002, 0.001);
    na.setNode(4, 0.002, 0.002);
    na.setNode(5, 0.001, 0.002);
    na.setNode(6, 0.000, 0.002);
    na.setNode(7, 0.000, 0.001);
    na.setNode(8, 0.001, 0.001);
    g.edge(0, 1, 100, true);
    g.edge(1, 2, 100, true);
    g.edge(2, 3, 100, true);
    g.edge(3, 4, 100, true);
    g.edge(4, 5, 100, true);
    g.edge(5, 6, 100, true);
    g.edge(6, 7, 100, true);
    g.edge(7, 0, 100, true);
    g.edge(1, 8, 110, true);
    g.edge(3, 8, 110, true);
    g.edge(5, 8, 110, true);
    g.edge(7, 8, 110, true);
    GraphHopper tmp = new GraphHopperOSM().setCHEnabled(withCH).setEncodingManager(encodingManager);
    tmp.getCHFactoryDecorator().setWeightingsAsStrings("fastest");
    tmp.setGraphHopperStorage(g);
    tmp.postProcessing();
    return tmp;
}
Also used : FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) AbstractWeighting(com.graphhopper.routing.weighting.AbstractWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) GraphHopper(com.graphhopper.GraphHopper)

Example 19 with FastestWeighting

use of com.graphhopper.routing.weighting.FastestWeighting in project graphhopper by graphhopper.

the class LMAlgoFactoryDecoratorTest method addWeighting.

@Test
public void addWeighting() {
    LMAlgoFactoryDecorator dec = new LMAlgoFactoryDecorator().setEnabled(true);
    dec.addWeighting("fastest");
    assertEquals(Arrays.asList("fastest"), dec.getWeightingsAsStrings());
    // special parameters like the maximum weight
    dec = new LMAlgoFactoryDecorator().setEnabled(true);
    dec.addWeighting("fastest|maximum=65000");
    dec.addWeighting("shortest|maximum=20000");
    assertEquals(Arrays.asList("fastest", "shortest"), dec.getWeightingsAsStrings());
    FlagEncoder car = new CarFlagEncoder();
    EncodingManager em = new EncodingManager(car);
    dec.addWeighting(new FastestWeighting(car)).addWeighting(new ShortestWeighting(car));
    dec.createPreparations(new GraphHopperStorage(new RAMDirectory(), em, false, new GraphExtension.NoOpExtension()), null);
    assertEquals(1, dec.getPreparations().get(0).getLandmarkStorage().getFactor(), .1);
    assertEquals(0.3, dec.getPreparations().get(1).getLandmarkStorage().getFactor(), .1);
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) RAMDirectory(com.graphhopper.storage.RAMDirectory) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Test(org.junit.Test)

Example 20 with FastestWeighting

use of com.graphhopper.routing.weighting.FastestWeighting in project graphhopper by graphhopper.

the class LandmarkStorageTest method testWithBorderBlocking.

@Test
public void testWithBorderBlocking() {
    AbstractRoutingAlgorithmTester.initBiGraph(ghStorage);
    LandmarkStorage storage = new LandmarkStorage(ghStorage, new RAMDirectory(), new FastestWeighting(encoder), 2);
    final SpatialRule ruleRight = new DefaultSpatialRule() {

        @Override
        public String getId() {
            return "right";
        }
    };
    final SpatialRule ruleLeft = new DefaultSpatialRule() {

        @Override
        public String getId() {
            return "left";
        }
    };
    final SpatialRuleLookup lookup = new SpatialRuleLookup() {

        @Override
        public SpatialRule lookupRule(double lat, double lon) {
            if (lon > 0.00105)
                return ruleRight;
            return ruleLeft;
        }

        @Override
        public SpatialRule lookupRule(GHPoint point) {
            return lookupRule(point.lat, point.lon);
        }

        @Override
        public int getSpatialId(SpatialRule rule) {
            throw new IllegalStateException();
        }

        @Override
        public int size() {
            return 2;
        }

        @Override
        public BBox getBounds() {
            return new BBox(-180, 180, -90, 90);
        }
    };
    storage.setSpatialRuleLookup(lookup);
    storage.setMinimumNodes(2);
    storage.createLandmarks();
    assertEquals(3, storage.getSubnetworksWithLandmarks());
}
Also used : BBox(com.graphhopper.util.shapes.BBox) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) SpatialRule(com.graphhopper.routing.util.spatialrules.SpatialRule) DefaultSpatialRule(com.graphhopper.routing.util.spatialrules.DefaultSpatialRule) DefaultSpatialRule(com.graphhopper.routing.util.spatialrules.DefaultSpatialRule) SpatialRuleLookup(com.graphhopper.routing.util.spatialrules.SpatialRuleLookup) GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.Test)

Aggregations

FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)35 Test (org.junit.Test)33 Weighting (com.graphhopper.routing.weighting.Weighting)15 ShortestWeighting (com.graphhopper.routing.weighting.ShortestWeighting)6 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)5 QueryResult (com.graphhopper.storage.index.QueryResult)5 CarFlagEncoder (com.graphhopper.routing.util.CarFlagEncoder)4 EncodingManager (com.graphhopper.routing.util.EncodingManager)4 Graph (com.graphhopper.storage.Graph)4 GHPoint (com.graphhopper.util.shapes.GHPoint)4 RAMDirectory (com.graphhopper.storage.RAMDirectory)3 LocationIndex (com.graphhopper.storage.index.LocationIndex)3 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)3 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)3 GHRequest (com.graphhopper.GHRequest)2 GHResponse (com.graphhopper.GHResponse)2 BikeFlagEncoder (com.graphhopper.routing.util.BikeFlagEncoder)2 DefaultEdgeFilter (com.graphhopper.routing.util.DefaultEdgeFilter)2 EdgeFilter (com.graphhopper.routing.util.EdgeFilter)2 AbstractWeighting (com.graphhopper.routing.weighting.AbstractWeighting)2