Search in sources :

Example 1 with CustomArea

use of com.graphhopper.routing.util.CustomArea in project graphhopper by graphhopper.

the class AreaIndexTest method basic.

@Test
void basic() {
    GeometryFactory geometryFactory = new GeometryFactory();
    Polygon border1 = geometryFactory.createPolygon(new Coordinate[] { new Coordinate(1, 1), new Coordinate(2, 1), new Coordinate(2, 2), new Coordinate(1, 2), new Coordinate(1, 1) });
    Polygon border2 = geometryFactory.createPolygon(new Coordinate[] { new Coordinate(5, 5), new Coordinate(6, 5), new Coordinate(6, 6), new Coordinate(5, 6), new Coordinate(5, 5) });
    Polygon border3 = geometryFactory.createPolygon(new Coordinate[] { new Coordinate(9, 9), new Coordinate(10, 9), new Coordinate(10, 10), new Coordinate(9, 10), new Coordinate(9, 9) });
    AreaIndex<CustomArea> index = new AreaIndex<>(Arrays.asList(createCustomArea("1", border1), createCustomArea("2", border2), createCustomArea("3", border3), createCustomArea("4", border2, border3)));
    testQuery(index, 0, 0);
    testQuery(index, 1.5, 1.5, "1");
    testQuery(index, 1.00001, 1.00001, "1");
    testQuery(index, 1.5, 1.00001, "1");
    testQuery(index, 1.00001, 1.5, "1");
    testQuery(index, 5.5, 5.5, "2", "4");
    testQuery(index, 9.5, 9.5, "3", "4");
}
Also used : GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Coordinate(org.locationtech.jts.geom.Coordinate) Polygon(org.locationtech.jts.geom.Polygon) CustomArea(com.graphhopper.routing.util.CustomArea) AreaIndex(com.graphhopper.routing.util.AreaIndex) Test(org.junit.jupiter.api.Test)

Example 2 with CustomArea

use of com.graphhopper.routing.util.CustomArea in project graphhopper by graphhopper.

the class AreaIndexTest method testPrecision.

@Test
void testPrecision() {
    // 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);
    AreaIndex<CustomArea> index = new AreaIndex<>(Collections.singletonList(createCustomArea("germany", germanPolygon)));
    // Far from the border of Germany, in Germany
    testQuery(index, 48.777106, 9.180769, "germany");
    testQuery(index, 51.806281, 7.269380, "germany");
    testQuery(index, 50.636710, 12.514561, "germany");
    // Far from the border of Germany, not in Germany
    testQuery(index, 48.029533, 7.250122);
    testQuery(index, 51.694467, 15.209218);
    testQuery(index, 47.283669, 11.167381);
    // Close to the border of Germany, in Germany - Whereas the borders are defined by the GeoJson above and do not strictly follow the actual border
    testQuery(index, 50.017714, 12.356129, "germany");
    testQuery(index, 49.949930, 6.225853, "germany");
    testQuery(index, 47.580866, 9.707582, "germany");
    testQuery(index, 47.565101, 9.724267, "germany");
    testQuery(index, 47.557166, 9.738343, "germany");
    // Close to the border of Germany, not in Germany
    testQuery(index, 50.025342, 12.386262);
    testQuery(index, 49.932900, 6.174023);
    testQuery(index, 47.547463, 9.741948);
}
Also used : Polygon(org.locationtech.jts.geom.Polygon) CustomArea(com.graphhopper.routing.util.CustomArea) AreaIndex(com.graphhopper.routing.util.AreaIndex) Test(org.junit.jupiter.api.Test)

Example 3 with CustomArea

use of com.graphhopper.routing.util.CustomArea in project graphhopper by graphhopper.

the class AreaIndexTest method testOverlap.

@Test
public void testOverlap() {
    GeometryFactory gf = new GeometryFactory();
    // Let the two polygons overlap
    Polygon border1 = gf.createPolygon(new Coordinate[] { new Coordinate(1, 1), new Coordinate(2, 1), new Coordinate(2, 2), new Coordinate(1, 2), new Coordinate(1, 1) });
    Polygon border2 = gf.createPolygon(new Coordinate[] { new Coordinate(0.5, 1), new Coordinate(1.5, 1), new Coordinate(1.5, 2), new Coordinate(0.5, 2), new Coordinate(0.5, 1) });
    AreaIndex<CustomArea> index = new AreaIndex<>(Arrays.asList(createCustomArea("1", border1), createCustomArea("2", border2)));
    testQuery(index, 1.5, 1.25, "1", "2");
    testQuery(index, 1.5, 0.99, "2");
    testQuery(index, 1.5, 1.0001, "1", "2");
    testQuery(index, 1.5, 1.4999, "1", "2");
    testQuery(index, 1.5, 1.51, "1");
}
Also used : GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Coordinate(org.locationtech.jts.geom.Coordinate) Polygon(org.locationtech.jts.geom.Polygon) CustomArea(com.graphhopper.routing.util.CustomArea) AreaIndex(com.graphhopper.routing.util.AreaIndex) Test(org.junit.jupiter.api.Test)

Example 4 with CustomArea

use of com.graphhopper.routing.util.CustomArea in project graphhopper by graphhopper.

the class OSMReader method setArtificialWayTags.

/**
 * This method is called during the second pass of {@link WaySegmentParser} and provides an entry point to enrich
 * the given OSM way with additional tags before it is passed on to the tag parsers.
 */
protected void setArtificialWayTags(PointList pointList, ReaderWay way) {
    // Estimate length of ways containing a route tag e.g. for ferry speed calculation
    double firstLat = pointList.getLat(0), firstLon = pointList.getLon(0);
    double lastLat = pointList.getLat(pointList.size() - 1), lastLon = pointList.getLon(pointList.size() - 1);
    // we have to remove existing artificial tags, because we modify the way even though there can be multiple edges
    // per way. sooner or later we should separate the artificial ('edge') tags from the way, see discussion here:
    // https://github.com/graphhopper/graphhopper/pull/2457#discussion_r751155404
    way.removeTag("estimated_distance");
    double estimatedDist = distCalc.calcDist(firstLat, firstLon, lastLat, lastLon);
    way.setTag("estimated_distance", estimatedDist);
    way.removeTag("duration:seconds");
    if (way.getTag("duration") != null) {
        try {
            long dur = OSMReaderUtility.parseDuration(way.getTag("duration"));
            // Provide the duration value in seconds in an artificial graphhopper specific tag:
            way.setTag("duration:seconds", Long.toString(dur));
        } catch (Exception ex) {
            LOGGER.warn("Parsing error in way with OSMID=" + way.getId() + " : " + ex.getMessage());
        }
    }
    way.removeTag("country");
    way.removeTag("country_rule");
    way.removeTag("custom_areas");
    List<CustomArea> customAreas;
    if (areaIndex != null) {
        double middleLat;
        double middleLon;
        if (pointList.size() > 2) {
            middleLat = pointList.getLat(pointList.size() / 2);
            middleLon = pointList.getLon(pointList.size() / 2);
        } else {
            middleLat = (firstLat + lastLat) / 2;
            middleLon = (firstLon + lastLon) / 2;
        }
        customAreas = areaIndex.query(middleLat, middleLon);
    } else {
        customAreas = emptyList();
    }
    // special handling for countries: since they are built-in with GraphHopper they are always fed to the EncodingManager
    Country country = Country.MISSING;
    for (CustomArea customArea : customAreas) {
        Object countryCode = customArea.getProperties().get("ISO3166-1:alpha3");
        if (countryCode == null)
            continue;
        if (country != Country.MISSING)
            LOGGER.warn("Multiple countries found for way {}: {}, {}", way.getId(), country, countryCode);
        country = Country.valueOf(countryCode.toString());
    }
    way.setTag("country", country);
    if (countryRuleFactory != null) {
        CountryRule countryRule = countryRuleFactory.getCountryRule(country);
        if (countryRule != null)
            way.setTag("country_rule", countryRule);
    }
    // also add all custom areas as artificial tag
    way.setTag("custom_areas", customAreas);
}
Also used : CountryRule(com.graphhopper.routing.util.countryrules.CountryRule) Country(com.graphhopper.routing.ev.Country) CustomArea(com.graphhopper.routing.util.CustomArea) IOException(java.io.IOException)

Aggregations

CustomArea (com.graphhopper.routing.util.CustomArea)4 AreaIndex (com.graphhopper.routing.util.AreaIndex)3 Test (org.junit.jupiter.api.Test)3 Polygon (org.locationtech.jts.geom.Polygon)3 Coordinate (org.locationtech.jts.geom.Coordinate)2 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)2 Country (com.graphhopper.routing.ev.Country)1 CountryRule (com.graphhopper.routing.util.countryrules.CountryRule)1 IOException (java.io.IOException)1