diff --git a/src/main/java/eu/simstadt/regionchooser/RegionExtractor.java b/src/main/java/eu/simstadt/regionchooser/RegionExtractor.java index 40291196f9dae6e1658f683fb2e91a782edd4abf..bb940d4c1b6355e73e18b63ce22ae3fd8248b99d 100644 --- a/src/main/java/eu/simstadt/regionchooser/RegionExtractor.java +++ b/src/main/java/eu/simstadt/regionchooser/RegionExtractor.java @@ -54,7 +54,7 @@ static int selectRegionDirectlyFromCityGML(String wktPolygon, String srsName, Wr int cityObjectsCount = 0; int foundBuildingsCount = 0; int foundVegetationCount = 0; - Geometry poly = WKT_READER.read(wktPolygon); + Geometry polygon = WKT_READER.read(wktPolygon); CityGmlIterator citygml = null; for (int i = 0; i < citygmlPaths.length; i++) { @@ -64,13 +64,11 @@ static int selectRegionDirectlyFromCityGML(String wktPolygon, String srsName, Wr citygml = new CityGmlIterator(citygmlPath); for (CityObjectMember cityObjectNode : citygml) { if (cityObjectsCount == 0) { - sb.append(replaceEnvelopeInHeader(citygml.getHeader(), poly.getEnvelopeInternal(), srsName)); + sb.append(replaceEnvelopeInHeader(citygml.getHeader(), polygon.getEnvelopeInternal(), srsName)); } cityObjectsCount += 1; if (cityObjectNode.hasCoordinates()) { - Coordinate coord = new Coordinate(cityObjectNode.x, cityObjectNode.y); - Point point = GEOMETRY_FACTORY.createPoint(coord); - if (point.within(poly)) { + if (isBoundingBoxTouching(cityObjectNode, polygon)) { if (cityObjectNode.isBuilding()) { foundBuildingsCount++; } else { @@ -102,6 +100,21 @@ static int selectRegionDirectlyFromCityGML(String wktPolygon, String srsName, Wr return foundBuildingsCount; } + /** + * Returns true if bounding box of cityObjectNode intersects the polygon. + */ + private static boolean isBoundingBoxTouching(CityObjectMember cityObjectNode, Geometry polygon) { + return point(cityObjectNode.xMax, cityObjectNode.yMax).within(polygon) || + point(cityObjectNode.xMax, cityObjectNode.yMin).within(polygon) || + point(cityObjectNode.xMin, cityObjectNode.yMin).within(polygon) || + point(cityObjectNode.xMin, cityObjectNode.yMax).within(polygon); + } + + private static Point point(Double x, Double y) { + Coordinate coord = new Coordinate(x, y); + return GEOMETRY_FACTORY.createPoint(coord); + } + /** * Some Citygml files include an envelope (bounding box), defined at the very beginning of the file. If the extracted * region comes from a huge file (e.g. from NYC), it might inherit this header with a huge envelope. Some methods