Search in sources :

Example 36 with BBox

use of com.graphhopper.util.shapes.BBox in project graphhopper by graphhopper.

the class SpatialRuleLookupBuilderTest method testIntersection.

@Test
public void testIntersection() {
    /*
            We are creating a BBox smaller than Germany. We have the German Spatial rule activated by default.
            So the BBox should not contain a Point lying somewhere close in Germany.
         */
    Reader reader = new InputStreamReader(SpatialRuleLookupBuilderTest.class.getResourceAsStream("countries.geo.json"));
    SpatialRuleLookup spatialRuleLookup = DefaultModule.buildIndex(reader, new BBox(9, 10, 51, 52));
    assertFalse("BBox seems to be incorrectly contracted", spatialRuleLookup.getBounds().contains(49.9, 8.9));
}
Also used : BBox(com.graphhopper.util.shapes.BBox) Test(org.junit.Test)

Example 37 with BBox

use of com.graphhopper.util.shapes.BBox in project graphhopper by graphhopper.

the class LinearKeyAlgo method setBounds.

@Override
public LinearKeyAlgo setBounds(double minLonInit, double maxLonInit, double minLatInit, double maxLatInit) {
    bounds = new BBox(minLonInit, maxLonInit, minLatInit, maxLatInit);
    latDelta = (bounds.maxLat - bounds.minLat) / latUnits;
    lonDelta = (bounds.maxLon - bounds.minLon) / lonUnits;
    return this;
}
Also used : BBox(com.graphhopper.util.shapes.BBox)

Example 38 with BBox

use of com.graphhopper.util.shapes.BBox in project graphhopper by graphhopper.

the class SpatialRuleLookupBuilder method build.

/**
     * This method connects the rules with the jsonFeatureCollection via their ISO_A3 property and the rules its
     * getId method.
     *
     * @param jsonProperty the key that should be used to fetch the ID that is passed to SpatialRuleFactory#createSpatialRule
     * @return the index or null if the specified bounds does not intersect with the calculated ones from the rules.
     */
public SpatialRuleLookup build(String jsonProperty, SpatialRuleFactory ruleFactory, JsonFeatureCollection jsonFeatureCollection, BBox bounds, double resolution, boolean exact) {
    // TODO filter out polyons that don't intersect with the given BBox, will be implicitly done later anyway
    BBox polygonBounds = BBox.createInverse(false);
    List<SpatialRule> rules = new ArrayList<>();
    Set<String> ids = new HashSet<>();
    int unknownCounter = 0;
    for (JsonFeature jsonFeature : jsonFeatureCollection.getFeatures()) {
        Geometry geometry = jsonFeature.getGeometry();
        if (!geometry.isPolygon())
            continue;
        List<Polygon> borders = geometry.asPolygon().getPolygons();
        String id = (String) jsonFeature.getProperty(jsonProperty);
        if (id == null || id.isEmpty()) {
            id = "_unknown_id_" + unknownCounter;
            unknownCounter++;
        }
        if (ids.contains(id))
            throw new RuntimeException("The id " + id + " was already used. Either leave the json property '" + jsonProperty + "' empty or use an unique id.");
        ids.add(id);
        SpatialRule spatialRule = ruleFactory.createSpatialRule(id, borders);
        if (spatialRule == SpatialRule.EMPTY)
            continue;
        rules.add(spatialRule);
        for (Polygon polygon : borders) {
            polygonBounds.update(polygon.getMinLat(), polygon.getMinLon());
            polygonBounds.update(polygon.getMaxLat(), polygon.getMaxLon());
        }
    }
    if (rules.isEmpty())
        return null;
    if (!polygonBounds.isValid()) {
        throw new IllegalStateException("No associated polygons found in JsonFeatureCollection for rules " + rules);
    }
    // Only create a SpatialRuleLookup if there are rules defined in the given bounds
    BBox calculatedBounds = polygonBounds.calculateIntersection(bounds);
    if (calculatedBounds == null)
        return null;
    SpatialRuleLookup spatialRuleLookup = new SpatialRuleLookupArray(calculatedBounds, resolution, exact);
    for (SpatialRule spatialRule : rules) {
        spatialRuleLookup.addRule(spatialRule);
    }
    return spatialRuleLookup;
}
Also used : DefaultSpatialRule(com.graphhopper.routing.util.spatialrules.countries.DefaultSpatialRule) JsonFeature(com.graphhopper.json.geo.JsonFeature) Geometry(com.graphhopper.json.geo.Geometry) BBox(com.graphhopper.util.shapes.BBox)

Aggregations

BBox (com.graphhopper.util.shapes.BBox)38 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)11 GHPoint (com.graphhopper.util.shapes.GHPoint)7 GHJsonFactory (com.graphhopper.json.GHJsonFactory)4 JsonFeatureCollection (com.graphhopper.json.geo.JsonFeatureCollection)4 JsonFeature (com.graphhopper.json.geo.JsonFeature)3 Graph (com.graphhopper.storage.Graph)3 IntArrayList (com.carrotsearch.hppc.IntArrayList)2 IntHashSet (com.carrotsearch.hppc.IntHashSet)2 EdgeFilter (com.graphhopper.routing.util.EdgeFilter)2 SpatialRuleLookup (com.graphhopper.routing.util.spatialrules.SpatialRuleLookup)2 GraphBuilder (com.graphhopper.storage.GraphBuilder)2 PMap (com.graphhopper.util.PMap)2 PointList (com.graphhopper.util.PointList)2 Circle (com.graphhopper.util.shapes.Circle)2 Map (java.util.Map)2 IntIntHashMap (com.carrotsearch.hppc.IntIntHashMap)1 IntLongHashMap (com.carrotsearch.hppc.IntLongHashMap)1 GTFSFeed (com.conveyal.gtfs.GTFSFeed)1