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));
}
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;
}
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;
}
Aggregations