Search in sources :

Example 1 with GermanySpatialRule

use of com.graphhopper.routing.util.spatialrules.countries.GermanySpatialRule in project graphhopper by graphhopper.

the class SpatialRuleLookupArrayTest method testSpatialLookup.

@Test
public void testSpatialLookup() {
    SpatialRuleLookupArray lookup = new SpatialRuleLookupArray(new BBox(1, 2, 1, 2), 1, false);
    SpatialRule germanyRule = new GermanySpatialRule().addBorder(new Polygon(new double[] { 1, 1, 2, 2 }, new double[] { 1, 2, 2, 1 }));
    lookup.addRule(germanyRule);
    SpatialRule austriaRule = new AustriaSpatialRule().addBorder(new Polygon(new double[] { 5, 5, 6, 6 }, new double[] { 5, 6, 6, 5 }));
    lookup.addRule(austriaRule);
    SpatialRule rule = lookup.lookupRule(1.5, 1.5);
    assertEquals(germanyRule, rule);
    assertEquals("DEU", rule.getId());
    int id = lookup.getSpatialId(rule);
    assertTrue(id > 0);
    assertEquals(rule, lookup.getSpatialRule(id));
}
Also used : AustriaSpatialRule(com.graphhopper.routing.util.spatialrules.countries.AustriaSpatialRule) BBox(com.graphhopper.util.shapes.BBox) GermanySpatialRule(com.graphhopper.routing.util.spatialrules.countries.GermanySpatialRule) AustriaSpatialRule(com.graphhopper.routing.util.spatialrules.countries.AustriaSpatialRule) GermanySpatialRule(com.graphhopper.routing.util.spatialrules.countries.GermanySpatialRule) Test(org.junit.Test)

Example 2 with GermanySpatialRule

use of com.graphhopper.routing.util.spatialrules.countries.GermanySpatialRule in project graphhopper by graphhopper.

the class DefaultModule method buildIndex.

static SpatialRuleLookup buildIndex(Reader reader, BBox graphBBox) {
    GHJson ghJson = new GHJsonBuilder().create();
    JsonFeatureCollection jsonFeatureCollection = ghJson.fromJson(reader, JsonFeatureCollection.class);
    return new SpatialRuleLookupBuilder().build(Arrays.asList(new GermanySpatialRule(), new AustriaSpatialRule()), jsonFeatureCollection, graphBBox, 1, true);
}
Also used : AustriaSpatialRule(com.graphhopper.routing.util.spatialrules.countries.AustriaSpatialRule) GHJsonBuilder(com.graphhopper.json.GHJsonBuilder) JsonFeatureCollection(com.graphhopper.json.geo.JsonFeatureCollection) GHJson(com.graphhopper.json.GHJson) GermanySpatialRule(com.graphhopper.routing.util.spatialrules.countries.GermanySpatialRule) SpatialRuleLookupBuilder(com.graphhopper.routing.util.spatialrules.SpatialRuleLookupBuilder)

Example 3 with GermanySpatialRule

use of com.graphhopper.routing.util.spatialrules.countries.GermanySpatialRule in project graphhopper by graphhopper.

the class DataFlagEncoderTest method testSpatialId.

@Test
public void testSpatialId() {
    List<SpatialRule> rules = Collections.<SpatialRule>singletonList(new GermanySpatialRule());
    final BBox bbox = new BBox(0, 1, 0, 1);
    JsonFeatureCollection jsonFeatures = new JsonFeatureCollection() {

        @Override
        public List<JsonFeature> getFeatures() {
            Geometry geometry = new GeoJsonPolygon().addPolygon(new Polygon(new double[] { 0, 0, 1, 1 }, new double[] { 0, 1, 1, 0 }));
            Map<String, Object> properties = new HashMap<>();
            properties.put("ISO_A3", "DEU");
            return Collections.singletonList(new JsonFeature("x", "Polygon", bbox, geometry, properties));
        }
    };
    SpatialRuleLookup index = new SpatialRuleLookupBuilder().build(rules, jsonFeatures, bbox, 1, false);
    DataFlagEncoder encoder = new DataFlagEncoder(new PMap().put("spatial_rules", index.size()));
    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 : GeoJsonPolygon(com.graphhopper.json.geo.GeoJsonPolygon) GermanySpatialRule(com.graphhopper.routing.util.spatialrules.countries.GermanySpatialRule) ReaderWay(com.graphhopper.reader.ReaderWay) JsonFeature(com.graphhopper.json.geo.JsonFeature) Geometry(com.graphhopper.json.geo.Geometry) Graph(com.graphhopper.storage.Graph) BBox(com.graphhopper.util.shapes.BBox) JsonFeatureCollection(com.graphhopper.json.geo.JsonFeatureCollection) GraphBuilder(com.graphhopper.storage.GraphBuilder) GeoJsonPolygon(com.graphhopper.json.geo.GeoJsonPolygon) GHPoint(com.graphhopper.util.shapes.GHPoint) GermanySpatialRule(com.graphhopper.routing.util.spatialrules.countries.GermanySpatialRule) Test(org.junit.Test)

Example 4 with GermanySpatialRule

use of com.graphhopper.routing.util.spatialrules.countries.GermanySpatialRule in project graphhopper by graphhopper.

the class SpatialRuleLookupArrayTest method testExactCountry.

@Test
public void testExactCountry() {
    SpatialRuleLookup spatialRuleLookup = new SpatialRuleLookupArray(new BBox(-180, 180, -90, 90), .1, true);
    // Taken from here: https://github.com/johan/world.geo.json/blob/master/countries/DEU.geo.json
    String germanPolygonJson = "[9.921906,54.983104],[9.93958,54.596642],[10.950112,54.363607],[10.939467,54.008693],[11.956252,54.196486],[12.51844,54.470371],[13.647467,54.075511],[14.119686,53.757029],[14.353315,53.248171],[14.074521,52.981263],[14.4376,52.62485],[14.685026,52.089947],[14.607098,51.745188],[15.016996,51.106674],[14.570718,51.002339],[14.307013,51.117268],[14.056228,50.926918],[13.338132,50.733234],[12.966837,50.484076],[12.240111,50.266338],[12.415191,49.969121],[12.521024,49.547415],[13.031329,49.307068],[13.595946,48.877172],[13.243357,48.416115],[12.884103,48.289146],[13.025851,47.637584],[12.932627,47.467646],[12.62076,47.672388],[12.141357,47.703083],[11.426414,47.523766],[10.544504,47.566399],[10.402084,47.302488],[9.896068,47.580197],[9.594226,47.525058],[8.522612,47.830828],[8.317301,47.61358],[7.466759,47.620582],[7.593676,48.333019],[8.099279,49.017784],[6.65823,49.201958],[6.18632,49.463803],[6.242751,49.902226],[6.043073,50.128052],[6.156658,50.803721],[5.988658,51.851616],[6.589397,51.852029],[6.84287,52.22844],[7.092053,53.144043],[6.90514,53.482162],[7.100425,53.693932],[7.936239,53.748296],[8.121706,53.527792],[8.800734,54.020786],[8.572118,54.395646],[8.526229,54.962744],[9.282049,54.830865],[9.921906,54.983104]";
    Polygon germanPolygon = parsePolygonString(germanPolygonJson);
    spatialRuleLookup.addRule(new GermanySpatialRule().addBorder(germanPolygon));
    // Far from the border of Germany, in Germany
    assertEquals("DEU", spatialRuleLookup.lookupRule(48.777106, 9.180769).getId());
    assertEquals("DEU", spatialRuleLookup.lookupRule(51.806281, 7.269380).getId());
    assertEquals("DEU", spatialRuleLookup.lookupRule(50.636710, 12.514561).getId());
    // Far from the border of Germany, not in Germany
    assertEquals("", spatialRuleLookup.lookupRule(48.029533, 7.250122).getId());
    assertEquals("", spatialRuleLookup.lookupRule(51.694467, 15.209218).getId());
    assertEquals("", spatialRuleLookup.lookupRule(47.283669, 11.167381).getId());
    // Close to the border of Germany, in Germany - Whereas the borders are defined by the GeoJson above and do not strictly follow the acutal border
    assertEquals("DEU", spatialRuleLookup.lookupRule(50.017714, 12.356129).getId());
    assertEquals("DEU", spatialRuleLookup.lookupRule(49.949930, 6.225853).getId());
    assertEquals("DEU", spatialRuleLookup.lookupRule(47.580866, 9.707582).getId());
    assertEquals("DEU", spatialRuleLookup.lookupRule(47.565101, 9.724267).getId());
    assertEquals("DEU", spatialRuleLookup.lookupRule(47.557166, 9.738343).getId());
    // Close to the border of Germany, not in Germany
    assertEquals("", spatialRuleLookup.lookupRule(50.025342, 12.386262).getId());
    assertEquals("", spatialRuleLookup.lookupRule(49.932900, 6.174023).getId());
    assertEquals("", spatialRuleLookup.lookupRule(47.547463, 9.741948).getId());
}
Also used : BBox(com.graphhopper.util.shapes.BBox) GermanySpatialRule(com.graphhopper.routing.util.spatialrules.countries.GermanySpatialRule) Test(org.junit.Test)

Aggregations

GermanySpatialRule (com.graphhopper.routing.util.spatialrules.countries.GermanySpatialRule)4 BBox (com.graphhopper.util.shapes.BBox)3 Test (org.junit.Test)3 JsonFeatureCollection (com.graphhopper.json.geo.JsonFeatureCollection)2 AustriaSpatialRule (com.graphhopper.routing.util.spatialrules.countries.AustriaSpatialRule)2 GHJson (com.graphhopper.json.GHJson)1 GHJsonBuilder (com.graphhopper.json.GHJsonBuilder)1 GeoJsonPolygon (com.graphhopper.json.geo.GeoJsonPolygon)1 Geometry (com.graphhopper.json.geo.Geometry)1 JsonFeature (com.graphhopper.json.geo.JsonFeature)1 ReaderWay (com.graphhopper.reader.ReaderWay)1 SpatialRuleLookupBuilder (com.graphhopper.routing.util.spatialrules.SpatialRuleLookupBuilder)1 Graph (com.graphhopper.storage.Graph)1 GraphBuilder (com.graphhopper.storage.GraphBuilder)1 GHPoint (com.graphhopper.util.shapes.GHPoint)1