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 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.logging.Logger;
......@@ -36,10 +37,17 @@ public class CityGmlIterator implements Iterable<CityObjectMember>
* 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;
if (!Files.isRegularFile(citygmlPath)) {
throw new NoSuchFileException(citygmlPath.toString());
}
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.buildingsFinder = new AutoPilot(navigator);
buildingsFinder.selectXPath(CityObjectMember.XPATH_PATTERN);
......
......@@ -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 °².
* @throws XPathParseException
*/
public static double calculateArea(Path citygmlPath) throws XPathParseException {
public static double calculateArea(Path citygmlPath) throws XPathParseException, IOException {
Polygon originalConvexHull = getConvexHull(citygmlPath);
return originalConvexHull.getArea();
}
private static Polygon getConvexHull(Path citygmlPath) throws XPathParseException {
private static Polygon getConvexHull(Path citygmlPath) throws XPathParseException, IOException {
GeometryFactory geometryFactory = new GeometryFactory();
ArrayList<Coordinate> allPoints = new ArrayList<>();
CityGmlIterator citygml = new CityGmlIterator(citygmlPath);
......
......@@ -2,7 +2,10 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
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 java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.jupiter.api.Test;
......@@ -15,7 +18,7 @@
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 void testNoNanInCoordinates(Path citygmlPath) throws XPathParseException {
private void testNoNanInCoordinates(Path citygmlPath) throws XPathParseException, IOException {
CityGmlIterator buildingXmlNodes = new CityGmlIterator(citygmlPath);
for (CityObjectMember cityObjectNode : buildingXmlNodes) {
assertTrue(cityObjectNode.hasCoordinates(), "Building and vegetations should have coordinates");
......@@ -34,7 +37,7 @@ private void testNoNanInCoordinates(Path citygmlPath) throws XPathParseException
}
@Test
public void testExtractCoordsFromStuttgart() throws XPathParseException {
public void testExtractCoordsFromStuttgart() throws XPathParseException, IOException {
Path repo = Paths.get(REGION_CHOOSER_TESTDATA, "Stuttgart.proj");
Path citygmlPath = repo.resolve("Stuttgart_LOD0_LOD1_buildings_and_trees.gml");
testNoNanInCoordinates(citygmlPath);
......@@ -51,28 +54,28 @@ public void testExtractCoordsFromStuttgart() throws XPathParseException {
}
@Test
public void testExtractCoordsFromGruenbuehl() throws XPathParseException {
public void testExtractCoordsFromGruenbuehl() throws XPathParseException, IOException {
Path repo = Paths.get(REGION_CHOOSER_TESTDATA, "Gruenbuehl.proj");
Path citygmlPath = repo.resolve("20140218_Gruenbuehl_LOD2_1building.gml");
testNoNanInCoordinates(citygmlPath);
}
@Test
public void testExtractCoordsFromMunich() throws XPathParseException {
public void testExtractCoordsFromMunich() throws XPathParseException, IOException {
Path repo = Paths.get(REGION_CHOOSER_TESTDATA, "Muenchen.proj");
Path citygmlPath = repo.resolve("Munich_v_1_0_0.gml");
testNoNanInCoordinates(citygmlPath);
}
@Test
public void testExtractCoordsFromNYC() throws XPathParseException {
public void testExtractCoordsFromNYC() throws XPathParseException, IOException {
Path repo = Paths.get(REGION_CHOOSER_TESTDATA, "NewYork.proj");
Path citygmlPath = repo.resolve("ManhattanSmall.gml");
testNoNanInCoordinates(citygmlPath);
}
@Test
public void testExtractNoCoordsFromEmptyBuilding() throws XPathParseException {
public void testExtractNoCoordsFromEmptyBuilding() throws XPathParseException, IOException {
Path repo = Paths.get(REGION_CHOOSER_TESTDATA, "Stuttgart.proj");
Path citygmlPath = repo.resolve("Stöckach_empty_buildings.gml");
CityGmlIterator buildingXmlNodes = new CityGmlIterator(citygmlPath);
......@@ -85,4 +88,11 @@ public void testExtractNoCoordsFromEmptyBuilding() throws XPathParseException {
}
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