Search in sources :

Example 26 with PMap

use of com.graphhopper.util.PMap in project graphhopper by graphhopper.

the class RacingBikeFlagEncoderTest method testPriority_avoidanceOfHighMaxSpeed.

@Test
public void testPriority_avoidanceOfHighMaxSpeed() {
    // here we test the priority that would be calculated if the way was accessible (even when it is not)
    // therefore we need a modified encoder that always yields access=WAY
    BikeCommonFlagEncoder encoder = new RacingBikeFlagEncoder(new PMap("block_fords=true")) {

        @Override
        public EncodingManager.Access getAccess(ReaderWay way) {
            return WAY;
        }
    };
    EncodingManager encodingManager = EncodingManager.create(encoder);
    DecimalEncodedValue avgSpeedEnc = encoder.getAverageSpeedEnc();
    ReaderWay osmWay = new ReaderWay(1);
    osmWay.setTag("highway", "tertiary");
    osmWay.setTag("maxspeed", "50");
    IntsRef intsRef = encodingManager.createEdgeFlags();
    encoder.setSpeed(false, intsRef, encoder.applyMaxSpeed(osmWay, 20));
    assertEquals(20, avgSpeedEnc.getDecimal(false, intsRef), 1e-1);
    assertPriority(encodingManager, PREFER.getValue(), osmWay);
    osmWay.setTag("maxspeed", "60");
    encoder.setSpeed(false, intsRef, encoder.applyMaxSpeed(osmWay, 20));
    assertEquals(20, avgSpeedEnc.getDecimal(false, intsRef), 1e-1);
    assertPriority(encodingManager, PREFER.getValue(), osmWay);
    osmWay.setTag("maxspeed", "80");
    encoder.setSpeed(false, intsRef, encoder.applyMaxSpeed(osmWay, 20));
    assertEquals(20, avgSpeedEnc.getDecimal(false, intsRef), 1e-1);
    assertPriority(encodingManager, PREFER.getValue(), osmWay);
    osmWay.setTag("maxspeed", "90");
    encoder.setSpeed(false, intsRef, encoder.applyMaxSpeed(osmWay, 20));
    assertEquals(20, avgSpeedEnc.getDecimal(false, intsRef), 1e-1);
    assertPriority(encodingManager, UNCHANGED.getValue(), osmWay);
    osmWay.setTag("maxspeed", "120");
    encoder.setSpeed(false, intsRef, encoder.applyMaxSpeed(osmWay, 20));
    assertEquals(20, avgSpeedEnc.getDecimal(false, intsRef), 1e-1);
    assertPriority(encodingManager, UNCHANGED.getValue(), osmWay);
    osmWay.setTag("highway", "motorway");
    encoder.setSpeed(false, intsRef, encoder.applyMaxSpeed(osmWay, 20));
    assertEquals(20, avgSpeedEnc.getDecimal(false, intsRef), 1e-1);
    assertPriority(encodingManager, AVOID.getValue(), osmWay);
    osmWay.setTag("tunnel", "yes");
    encoder.setSpeed(false, intsRef, encoder.applyMaxSpeed(osmWay, 20));
    assertEquals(20, avgSpeedEnc.getDecimal(false, intsRef), 1e-1);
    assertPriority(encodingManager, AVOID_MORE.getValue(), osmWay);
    osmWay.clearTags();
    osmWay.setTag("highway", "motorway");
    osmWay.setTag("tunnel", "yes");
    osmWay.setTag("maxspeed", "80");
    encoder.setSpeed(false, intsRef, encoder.applyMaxSpeed(osmWay, 20));
    assertEquals(20, avgSpeedEnc.getDecimal(false, intsRef), 1e-1);
    assertPriority(encodingManager, AVOID_MORE.getValue(), osmWay);
    osmWay.clearTags();
    osmWay.setTag("highway", "motorway");
    osmWay.setTag("tunnel", "yes");
    osmWay.setTag("maxspeed", "120");
    encoder.setSpeed(false, intsRef, encoder.applyMaxSpeed(osmWay, 20));
    assertEquals(20, avgSpeedEnc.getDecimal(false, intsRef), 1e-1);
    assertPriority(encodingManager, AVOID_MORE.getValue(), osmWay);
    osmWay.clearTags();
    osmWay.setTag("highway", "notdefined");
    osmWay.setTag("tunnel", "yes");
    osmWay.setTag("maxspeed", "120");
    encoder.setSpeed(false, intsRef, encoder.applyMaxSpeed(osmWay, 20));
    assertEquals(20, avgSpeedEnc.getDecimal(false, intsRef), 1e-1);
    assertPriority(encodingManager, AVOID_MORE.getValue(), osmWay);
    osmWay.clearTags();
    osmWay.setTag("highway", "notdefined");
    osmWay.setTag("maxspeed", "50");
    encoder.setSpeed(false, intsRef, encoder.applyMaxSpeed(osmWay, 20));
    assertEquals(20, avgSpeedEnc.getDecimal(false, intsRef), 1e-1);
    assertPriority(encodingManager, UNCHANGED.getValue(), osmWay);
}
Also used : DecimalEncodedValue(com.graphhopper.routing.ev.DecimalEncodedValue) PMap(com.graphhopper.util.PMap) ReaderWay(com.graphhopper.reader.ReaderWay) IntsRef(com.graphhopper.storage.IntsRef) Test(org.junit.jupiter.api.Test)

Example 27 with PMap

use of com.graphhopper.util.PMap in project graphhopper by graphhopper.

the class SpatialRuleLookupHelper method buildAndInjectSpatialRuleIntoGH.

public static void buildAndInjectSpatialRuleIntoGH(GraphHopper graphHopper, CmdArgs args) {
    String spatialRuleLocation = args.get("spatial_rules.location", "");
    if (!spatialRuleLocation.isEmpty()) {
        try {
            final BBox maxBounds = BBox.parseBBoxString(args.get("spatial_rules.max_bbox", "-180, 180, -90, 90"));
            final InputStreamReader reader = new InputStreamReader(new FileInputStream(spatialRuleLocation), UTF_CS);
            final SpatialRuleLookup index = SpatialRuleLookupBuilder.buildIndex(new GHJsonFactory().create().fromJson(reader, JsonFeatureCollection.class), "ISO_A3", new CountriesSpatialRuleFactory(), .1, maxBounds);
            logger.info("Set spatial rule lookup with " + index.size() + " rules");
            final FlagEncoderFactory oldFEF = graphHopper.getFlagEncoderFactory();
            graphHopper.setFlagEncoderFactory(new FlagEncoderFactory() {

                @Override
                public FlagEncoder createFlagEncoder(String name, PMap configuration) {
                    if (name.equals(GENERIC)) {
                        return new DataFlagEncoder(configuration).setSpatialRuleLookup(index);
                    }
                    return oldFEF.createFlagEncoder(name, configuration);
                }
            });
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) DataFlagEncoder(com.graphhopper.routing.util.DataFlagEncoder) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) PMap(com.graphhopper.util.PMap) SpatialRuleLookup(com.graphhopper.routing.util.spatialrules.SpatialRuleLookup) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) DataFlagEncoder(com.graphhopper.routing.util.DataFlagEncoder) BBox(com.graphhopper.util.shapes.BBox) GHJsonFactory(com.graphhopper.json.GHJsonFactory) JsonFeatureCollection(com.graphhopper.json.geo.JsonFeatureCollection) CountriesSpatialRuleFactory(com.graphhopper.routing.util.spatialrules.CountriesSpatialRuleFactory) FlagEncoderFactory(com.graphhopper.routing.util.FlagEncoderFactory)

Example 28 with PMap

use of com.graphhopper.util.PMap in project graphhopper by graphhopper.

the class DataFlagEncoderTest method testSpatialId.

@Test
public void testSpatialId() {
    final GermanySpatialRule germany = new GermanySpatialRule();
    germany.setBorders(Collections.singletonList(new Polygon(new double[] { 0, 0, 1, 1 }, new double[] { 0, 1, 1, 0 })));
    SpatialRuleLookup index = new SpatialRuleLookup() {

        @Override
        public SpatialRule lookupRule(double lat, double lon) {
            for (Polygon polygon : germany.getBorders()) {
                if (polygon.contains(lat, lon)) {
                    return germany;
                }
            }
            return SpatialRule.EMPTY;
        }

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

        @Override
        public int getSpatialId(SpatialRule rule) {
            if (germany.equals(rule)) {
                return 1;
            } else {
                return 0;
            }
        }

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

        @Override
        public BBox getBounds() {
            return new BBox(-180, 180, -90, 90);
        }
    };
    DataFlagEncoder encoder = new DataFlagEncoder(new PMap());
    encoder.setSpatialRuleLookup(index);
    EncodingManager em = new EncodingManager(encoder);
    ReaderWay way = new ReaderWay(27l);
    way.setTag("highway", "track");
    way.setTag("estimated_center", new GHPoint(0.005, 0.005));
    ReaderWay way2 = new ReaderWay(28l);
    way2.setTag("highway", "track");
    way2.setTag("estimated_center", new GHPoint(-0.005, -0.005));
    ReaderWay livingStreet = new ReaderWay(29l);
    livingStreet.setTag("highway", "living_street");
    livingStreet.setTag("estimated_center", new GHPoint(0.005, 0.005));
    ReaderWay livingStreet2 = new ReaderWay(30l);
    livingStreet2.setTag("highway", "living_street");
    livingStreet2.setTag("estimated_center", new GHPoint(-0.005, -0.005));
    Graph graph = new GraphBuilder(em).create();
    EdgeIteratorState e1 = graph.edge(0, 1, 1, true);
    EdgeIteratorState e2 = graph.edge(0, 2, 1, true);
    EdgeIteratorState e3 = graph.edge(0, 3, 1, true);
    EdgeIteratorState e4 = graph.edge(0, 4, 1, true);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 0, 0.00, 0.00);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 1, 0.01, 0.01);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 2, -0.01, -0.01);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 3, 0.01, 0.01);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 4, -0.01, -0.01);
    e1.setFlags(encoder.handleWayTags(way, 1, 0));
    e2.setFlags(encoder.handleWayTags(way2, 1, 0));
    e3.setFlags(encoder.handleWayTags(livingStreet, 1, 0));
    e4.setFlags(encoder.handleWayTags(livingStreet2, 1, 0));
    assertEquals(index.getSpatialId(new GermanySpatialRule()), encoder.getSpatialId(e1.getFlags()));
    assertEquals(index.getSpatialId(SpatialRule.EMPTY), encoder.getSpatialId(e2.getFlags()));
    assertEquals(AccessValue.EVENTUALLY_ACCESSIBLE, encoder.getAccessValue(e1.getFlags()));
    assertEquals(AccessValue.ACCESSIBLE, encoder.getAccessValue(e2.getFlags()));
    assertEquals(5, encoder.getMaxspeed(e3, -1, false), .1);
    assertEquals(-1, encoder.getMaxspeed(e4, -1, false), .1);
}
Also used : PMap(com.graphhopper.util.PMap) GermanySpatialRule(com.graphhopper.routing.util.spatialrules.countries.GermanySpatialRule) ReaderWay(com.graphhopper.reader.ReaderWay) Graph(com.graphhopper.storage.Graph) BBox(com.graphhopper.util.shapes.BBox) GraphBuilder(com.graphhopper.storage.GraphBuilder) GHPoint(com.graphhopper.util.shapes.GHPoint) GermanySpatialRule(com.graphhopper.routing.util.spatialrules.countries.GermanySpatialRule) Test(org.junit.Test)

Example 29 with PMap

use of com.graphhopper.util.PMap in project graphhopper by graphhopper.

the class PrepareRoutingSubnetworksTest method createEncodingManager.

private static EncodingManager createEncodingManager(String flagEncodersStr) {
    EncodingManager.Builder builder = new EncodingManager.Builder();
    for (String encoderStr : flagEncodersStr.split(",")) {
        encoderStr = encoderStr.trim();
        FlagEncoder encoder = new DefaultFlagEncoderFactory().createFlagEncoder(encoderStr.split("\\|")[0], new PMap(encoderStr));
        builder.add(encoder);
        builder.add(Subnetwork.create(encoder.toString()));
    }
    return builder.build();
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) DefaultFlagEncoderFactory(com.graphhopper.routing.util.DefaultFlagEncoderFactory) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) GraphBuilder(com.graphhopper.storage.GraphBuilder) PMap(com.graphhopper.util.PMap)

Example 30 with PMap

use of com.graphhopper.util.PMap in project graphhopper by graphhopper.

the class ShortFastestWeightingTest method testTooSmall.

@Test
public void testTooSmall() {
    try {
        new ShortFastestWeighting(encoder, new PMap("short_fastest.distance_factor=0|short_fastest.time_factor=0"), TurnCostProvider.NO_TURN_COST_PROVIDER);
        fail();
    } catch (Exception ex) {
    }
}
Also used : PMap(com.graphhopper.util.PMap) Test(org.junit.jupiter.api.Test)

Aggregations

PMap (com.graphhopper.util.PMap)50 Test (org.junit.jupiter.api.Test)27 LMProfile (com.graphhopper.config.LMProfile)12 Profile (com.graphhopper.config.Profile)12 FastestWeighting (com.graphhopper.routing.weighting.FastestWeighting)12 CHProfile (com.graphhopper.config.CHProfile)10 ArrayList (java.util.ArrayList)10 GraphHopper (com.graphhopper.GraphHopper)6 MapMatching (com.graphhopper.matching.MapMatching)5 MatchResult (com.graphhopper.matching.MatchResult)5 CHProfileSelectorTest (com.graphhopper.routing.ch.CHProfileSelectorTest)5 CHRoutingAlgorithmFactory (com.graphhopper.routing.ch.CHRoutingAlgorithmFactory)5 LMProfileSelectorTest (com.graphhopper.routing.lm.LMProfileSelectorTest)5 QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)5 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)5 Gpx (com.graphhopper.jackson.Gpx)4 ReaderWay (com.graphhopper.reader.ReaderWay)4 ProfileResolver (com.graphhopper.routing.ProfileResolver)4 Weighting (com.graphhopper.routing.weighting.Weighting)4 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)4