From 2ad61a3f5240e7d4c0af9baa7397f0fc84498148 Mon Sep 17 00:00:00 2001 From: Eric Duminil <eric.duminil@gmail.com> Date: Mon, 18 Dec 2023 13:37:02 +0100 Subject: [PATCH] Compares bounding box and polygon Not just center of mass Tests are broken. RegionChooser now extracts more buildings for a given region --- .../regionchooser/RegionExtractor.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/java/eu/simstadt/regionchooser/RegionExtractor.java b/src/main/java/eu/simstadt/regionchooser/RegionExtractor.java index 4029119..bb940d4 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 -- GitLab