diff --git a/src/main/java/eu/simstadt/regionchooser/RegionExtractor.java b/src/main/java/eu/simstadt/regionchooser/RegionExtractor.java index 5ed801fd7378d84ac7f626d2eefd3a65aac7a30d..192513cdb3071ae79b6ae999fad762fcac9009ff 100644 --- a/src/main/java/eu/simstadt/regionchooser/RegionExtractor.java +++ b/src/main/java/eu/simstadt/regionchooser/RegionExtractor.java @@ -53,7 +53,7 @@ static int selectRegionDirectlyFromCityGML(String wktPolygon, String srsName, Wr int cityObjectsCount = 0; int foundBuildingsCount = 0; - int foundVegetationCount = 0; + int otherObjectsCount = 0; Geometry polygon = WKT_READER.read(wktPolygon); CityGmlIterator citygml = null; @@ -68,11 +68,14 @@ static int selectRegionDirectlyFromCityGML(String wktPolygon, String srsName, Wr } cityObjectsCount += 1; if (cityObjectNode.hasCoordinates()) { - if (isBoundingBoxTouching(cityObjectNode, polygon)) { - if (cityObjectNode.isBuilding()) { + boolean isBuilding = cityObjectNode.isBuilding(); + boolean shouldBeIncluded = isBuilding ? isBuildingCenterInPolygon(cityObjectNode, polygon) + : isBoundingBoxTouching(cityObjectNode, polygon); + if (shouldBeIncluded) { + if (isBuilding) { foundBuildingsCount++; } else { - foundVegetationCount++; + otherObjectsCount++; } sb.append(cityObjectNode.toString()); } @@ -92,8 +95,8 @@ static int selectRegionDirectlyFromCityGML(String wktPolygon, String srsName, Wr } LOGGER.info("Buildings found in selected region : " + foundBuildingsCount); - if (foundVegetationCount > 0) { - LOGGER.info("Vegetation found in selected region : " + foundVegetationCount); + if (otherObjectsCount > 0) { + LOGGER.info("Other objects found in selected region : " + otherObjectsCount); } //NOTE: This could be a problem if header starts with <core:CityModel> and footer ends with </CityModel> sb.append(citygml.getFooter()); @@ -105,13 +108,17 @@ static int selectRegionDirectlyFromCityGML(String wktPolygon, String srsName, Wr * which might be desirable for roads or landuse. */ private static boolean isBoundingBoxTouching(CityObjectMember cityObjectNode, Geometry polygon) { - return point(cityObjectNode.x, cityObjectNode.y).within(polygon) || - point(cityObjectNode.xMax, cityObjectNode.yMax).within(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 boolean isBuildingCenterInPolygon(CityObjectMember cityObjectNode, Geometry polygon) { + return point(cityObjectNode.x, cityObjectNode.y).within(polygon); + } + private static Point point(Double x, Double y) { Coordinate coord = new Coordinate(x, y); return GEOMETRY_FACTORY.createPoint(coord); diff --git a/src/test/java/eu/simstadt/regionchooser/RegionExtractorTests.java b/src/test/java/eu/simstadt/regionchooser/RegionExtractorTests.java index 4654b3e290da47fbfe503efbd07b1d6235c0ddd1..5b4a2bfd46d1e1284dbdd6e41d7ded9c3c3799d9 100644 --- a/src/test/java/eu/simstadt/regionchooser/RegionExtractorTests.java +++ b/src/test/java/eu/simstadt/regionchooser/RegionExtractorTests.java @@ -178,7 +178,7 @@ void testExtractBuildingsAndTrees() throws Throwable { assertEquals(4, count); assertEquals(4, countRegexMatches(gmlWithSomeBuildingAndTrees, "<bldg:Building gml:id")); assertEquals(1, countRegexMatches(gmlWithSomeBuildingAndTrees, "<veg:PlantCover gml:id")); - assertEquals(3, countRegexMatches(gmlWithSomeBuildingAndTrees, "<veg:SolitaryVegetationObject gml:id")); + assertEquals(7, countRegexMatches(gmlWithSomeBuildingAndTrees, "<veg:SolitaryVegetationObject gml:id")); assertFalse(gmlWithSomeBuildingAndTrees.contains("DEBY_LOD2_605230"), "Building in another corner should not be included"); @@ -190,6 +190,8 @@ void testExtractBuildingsAndTrees() throws Throwable { assertTrue(gmlWithSomeBuildingAndTrees.contains("DEBY_LOD2_605227"), "Building in corner should be included"); } + //TODO: Check Roads and LandUse too. + @Test void testExtractBuildingsAndRemoveOld() throws Throwable { String wktPolygon = "POLYGON((293229.6831819388 5623753.072371232,293681.22751166753 5623744.274551504,293668.8482257676 5623469.512992135,293197.09954629745 5623504.821467172,293229.6831819388 5623753.072371232))";