Commit 628c7622 authored by Eric Duminil's avatar Eric Duminil
Browse files

Checking if files are present before parsing them

parent c8d16daa
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Iterator; import java.util.Iterator;
import java.util.logging.Logger; import java.util.logging.Logger;
...@@ -36,10 +37,17 @@ public class CityGmlIterator implements Iterable<CityObjectMember> ...@@ -36,10 +37,17 @@ public class CityGmlIterator implements Iterable<CityObjectMember>
* Based on VTD XML, it provides a Building iterator. * Based on VTD XML, it provides a Building iterator.
* *
*/ */
public CityGmlIterator(Path citygmlPath) throws XPathParseException { public CityGmlIterator(Path citygmlPath) throws XPathParseException, IOException {
this.citygmlPath = citygmlPath; this.citygmlPath = citygmlPath;
if (!Files.isRegularFile(citygmlPath)) {
throw new NoSuchFileException(citygmlPath.toString());
}
VTDGen parser = new VTDGen(); VTDGen parser = new VTDGen();
parser.parseFile(citygmlPath.toString(), false); // VTDGen.parseFile() swallows IOExceptions internally and returns false instead of throwing.
boolean parsed = parser.parseFile(citygmlPath.toString(), false);
if (!parsed) {
throw new IOException("Failed to parse " + citygmlPath);
}
this.navigator = parser.getNav(); this.navigator = parser.getNav();
this.buildingsFinder = new AutoPilot(navigator); this.buildingsFinder = new AutoPilot(navigator);
buildingsFinder.selectXPath(CityObjectMember.XPATH_PATTERN); buildingsFinder.selectXPath(CityObjectMember.XPATH_PATTERN);
......
...@@ -66,12 +66,12 @@ public static Geometry calculateFromCityGML(Path citygmlPath) throws XPathParseE ...@@ -66,12 +66,12 @@ public static Geometry calculateFromCityGML(Path citygmlPath) throws XPathParseE
* @return area of convex hull, in the same unit as original file. Hopefully m², but possibly ft² or even °². * @return area of convex hull, in the same unit as original file. Hopefully m², but possibly ft² or even °².
* @throws XPathParseException * @throws XPathParseException
*/ */
public static double calculateArea(Path citygmlPath) throws XPathParseException { public static double calculateArea(Path citygmlPath) throws XPathParseException, IOException {
Polygon originalConvexHull = getConvexHull(citygmlPath); Polygon originalConvexHull = getConvexHull(citygmlPath);
return originalConvexHull.getArea(); return originalConvexHull.getArea();
} }
private static Polygon getConvexHull(Path citygmlPath) throws XPathParseException { private static Polygon getConvexHull(Path citygmlPath) throws XPathParseException, IOException {
GeometryFactory geometryFactory = new GeometryFactory(); GeometryFactory geometryFactory = new GeometryFactory();
ArrayList<Coordinate> allPoints = new ArrayList<>(); ArrayList<Coordinate> allPoints = new ArrayList<>();
CityGmlIterator citygml = new CityGmlIterator(citygmlPath); CityGmlIterator citygml = new CityGmlIterator(citygmlPath);
......
...@@ -2,7 +2,10 @@ ...@@ -2,7 +2,10 @@
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
...@@ -15,7 +18,7 @@ ...@@ -15,7 +18,7 @@
private static final String COORDINATES_SHOULD_BE_PLAUSIBLE = "Min/Max Coordinates should be plausible"; private static final String COORDINATES_SHOULD_BE_PLAUSIBLE = "Min/Max Coordinates should be plausible";
private static final String COORDINATE_SHOULD_BE_A_DOUBLE = "Coordinate should be a double"; private static final String COORDINATE_SHOULD_BE_A_DOUBLE = "Coordinate should be a double";
private void testNoNanInCoordinates(Path citygmlPath) throws XPathParseException { private void testNoNanInCoordinates(Path citygmlPath) throws XPathParseException, IOException {
CityGmlIterator buildingXmlNodes = new CityGmlIterator(citygmlPath); CityGmlIterator buildingXmlNodes = new CityGmlIterator(citygmlPath);
for (CityObjectMember cityObjectNode : buildingXmlNodes) { for (CityObjectMember cityObjectNode : buildingXmlNodes) {
assertTrue(cityObjectNode.hasCoordinates(), "Building and vegetations should have coordinates"); assertTrue(cityObjectNode.hasCoordinates(), "Building and vegetations should have coordinates");
...@@ -34,7 +37,7 @@ private void testNoNanInCoordinates(Path citygmlPath) throws XPathParseException ...@@ -34,7 +37,7 @@ private void testNoNanInCoordinates(Path citygmlPath) throws XPathParseException
} }
@Test @Test
public void testExtractCoordsFromStuttgart() throws XPathParseException { public void testExtractCoordsFromStuttgart() throws XPathParseException, IOException {
Path repo = Paths.get(REGION_CHOOSER_TESTDATA, "Stuttgart.proj"); Path repo = Paths.get(REGION_CHOOSER_TESTDATA, "Stuttgart.proj");
Path citygmlPath = repo.resolve("Stuttgart_LOD0_LOD1_buildings_and_trees.gml"); Path citygmlPath = repo.resolve("Stuttgart_LOD0_LOD1_buildings_and_trees.gml");
testNoNanInCoordinates(citygmlPath); testNoNanInCoordinates(citygmlPath);
...@@ -51,28 +54,28 @@ public void testExtractCoordsFromStuttgart() throws XPathParseException { ...@@ -51,28 +54,28 @@ public void testExtractCoordsFromStuttgart() throws XPathParseException {
} }
@Test @Test
public void testExtractCoordsFromGruenbuehl() throws XPathParseException { public void testExtractCoordsFromGruenbuehl() throws XPathParseException, IOException {
Path repo = Paths.get(REGION_CHOOSER_TESTDATA, "Gruenbuehl.proj"); Path repo = Paths.get(REGION_CHOOSER_TESTDATA, "Gruenbuehl.proj");
Path citygmlPath = repo.resolve("20140218_Gruenbuehl_LOD2_1building.gml"); Path citygmlPath = repo.resolve("20140218_Gruenbuehl_LOD2_1building.gml");
testNoNanInCoordinates(citygmlPath); testNoNanInCoordinates(citygmlPath);
} }
@Test @Test
public void testExtractCoordsFromMunich() throws XPathParseException { public void testExtractCoordsFromMunich() throws XPathParseException, IOException {
Path repo = Paths.get(REGION_CHOOSER_TESTDATA, "Muenchen.proj"); Path repo = Paths.get(REGION_CHOOSER_TESTDATA, "Muenchen.proj");
Path citygmlPath = repo.resolve("Munich_v_1_0_0.gml"); Path citygmlPath = repo.resolve("Munich_v_1_0_0.gml");
testNoNanInCoordinates(citygmlPath); testNoNanInCoordinates(citygmlPath);
} }
@Test @Test
public void testExtractCoordsFromNYC() throws XPathParseException { public void testExtractCoordsFromNYC() throws XPathParseException, IOException {
Path repo = Paths.get(REGION_CHOOSER_TESTDATA, "NewYork.proj"); Path repo = Paths.get(REGION_CHOOSER_TESTDATA, "NewYork.proj");
Path citygmlPath = repo.resolve("ManhattanSmall.gml"); Path citygmlPath = repo.resolve("ManhattanSmall.gml");
testNoNanInCoordinates(citygmlPath); testNoNanInCoordinates(citygmlPath);
} }
@Test @Test
public void testExtractNoCoordsFromEmptyBuilding() throws XPathParseException { public void testExtractNoCoordsFromEmptyBuilding() throws XPathParseException, IOException {
Path repo = Paths.get(REGION_CHOOSER_TESTDATA, "Stuttgart.proj"); Path repo = Paths.get(REGION_CHOOSER_TESTDATA, "Stuttgart.proj");
Path citygmlPath = repo.resolve("Stöckach_empty_buildings.gml"); Path citygmlPath = repo.resolve("Stöckach_empty_buildings.gml");
CityGmlIterator buildingXmlNodes = new CityGmlIterator(citygmlPath); CityGmlIterator buildingXmlNodes = new CityGmlIterator(citygmlPath);
...@@ -85,4 +88,11 @@ public void testExtractNoCoordsFromEmptyBuilding() throws XPathParseException { ...@@ -85,4 +88,11 @@ public void testExtractNoCoordsFromEmptyBuilding() throws XPathParseException {
} }
assertEquals(3, counter, "3 buildings should have been analyzed"); assertEquals(3, counter, "3 buildings should have been analyzed");
} }
@Test
public void testCityGmlIteratorThrowsForMissingFile() {
Path citygmlPath = Paths.get(REGION_CHOOSER_TESTDATA, "Stuttgart.proj", "does_not_exist.gml");
assertThrows(NoSuchFileException.class, () -> new CityGmlIterator(citygmlPath),
"A missing CityGML file should raise a clear NoSuchFileException, not an opaque VTD-XML error");
}
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment