Commit cf707aa3 authored by duminil's avatar duminil
Browse files

Tests now pass with iterator.

parent 114d2a18
Showing with 36 additions and 15 deletions
+36 -15
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
public BuildingXmlNode(VTDNav navigator, int buildingOffset, int buildingLength) { public BuildingXmlNode(VTDNav navigator, int buildingOffset, int buildingLength) {
this.navigator = navigator; this.navigator = navigator;
this.coordinatesFinder = new AutoPilot(navigator); //COULD BE STATIC? this.coordinatesFinder = new AutoPilot(navigator);
this.buildingLength = buildingLength; this.buildingLength = buildingLength;
this.buildingOffset = buildingOffset; this.buildingOffset = buildingOffset;
} }
......
package eu.simstadt.lowlevelgmlparser; package eu.simstadt.lowlevelgmlparser;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
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;
...@@ -20,12 +21,21 @@ public class CityGmlIterator implements Iterable<BuildingXmlNode> ...@@ -20,12 +21,21 @@ public class CityGmlIterator implements Iterable<BuildingXmlNode>
private AutoPilot buildingsFinder; private AutoPilot buildingsFinder;
private VTDNav navigator; private VTDNav navigator;
private long offsetAndLength; private long offsetAndLength;
private int buildingsCount = 0;
private int buildingOffset = 0; private int buildingOffset = 0;
private int buildingLength = 0; private int buildingLength = 0;
private Path citygmlPath;
/*
* Simple class to parse a CityGML and extract cityObjectMember XML nodes and their coordinates. Since the
* coordinates are extracted for RegionChooser, it's okay to not be perfectly robust, but it should be fast and not
* use much memory.
*
* Based on VTD XML, it provides a Building iterator.
*
*/
public CityGmlIterator(Path citygmlPath) public CityGmlIterator(Path citygmlPath)
throws XPathParseException, NavException, NumberFormatException, XPathEvalException, IOException { throws XPathParseException, NavException, NumberFormatException, XPathEvalException, IOException {
this.citygmlPath = citygmlPath;
VTDGen parser = new VTDGen(); VTDGen parser = new VTDGen();
parser.parseFile(citygmlPath.toString(), false); parser.parseFile(citygmlPath.toString(), false);
this.navigator = parser.getNav(); this.navigator = parser.getNav();
...@@ -41,22 +51,22 @@ public Iterator<BuildingXmlNode> iterator() { ...@@ -41,22 +51,22 @@ public Iterator<BuildingXmlNode> iterator() {
public boolean hasNext() { public boolean hasNext() {
try { try {
return buildingsFinder.evalXPath() != -1; return buildingsFinder.evalXPath() != -1;
} catch (XPathEvalException | NavException ex) {} } catch (XPathEvalException | NavException ex) {
return false; LOGGER.warning("Error while parsing " + citygmlPath);
return false;
}
} }
@Override @Override
public BuildingXmlNode next() { public BuildingXmlNode next() {
try { try {
buildingsCount += 1;
if (buildingsCount % 1000 == 0) {
LOGGER.info("1000 buildings parsed");
}
offsetAndLength = navigator.getElementFragment(); offsetAndLength = navigator.getElementFragment();
buildingOffset = (int) offsetAndLength; buildingOffset = (int) offsetAndLength;
buildingLength = (int) (offsetAndLength >> 32); buildingLength = (int) (offsetAndLength >> 32);
return new BuildingXmlNode(navigator, buildingOffset, buildingLength); return new BuildingXmlNode(navigator, buildingOffset, buildingLength);
} catch (NavException ex) {} } catch (NavException ex) {
LOGGER.warning("Error while parsing " + citygmlPath);
}
return null; return null;
} }
...@@ -68,4 +78,14 @@ public void remove() { ...@@ -68,4 +78,14 @@ public void remove() {
return it; return it;
} }
public String getHeader() throws NavException {
return navigator.toRawString(0, buildingOffset);
}
public Object getFooter() throws IOException, NavException {
int footerOffset = buildingOffset + buildingLength;
int footerLength = (int) (Files.size(citygmlPath) - footerOffset);
return navigator.toRawString(footerOffset, footerLength);
}
} }
...@@ -33,21 +33,22 @@ static public StringBuffer selectRegionDirectlyFromCityGML(Path citygmlPath, Str ...@@ -33,21 +33,22 @@ static public StringBuffer selectRegionDirectlyFromCityGML(Path citygmlPath, Str
CityGmlIterator citygml = new CityGmlIterator(citygmlPath); CityGmlIterator citygml = new CityGmlIterator(citygmlPath);
for (BuildingXmlNode buildingXmlNode : citygml) { for (BuildingXmlNode buildingXmlNode : citygml) {
buildingsCount += 1; if (buildingsCount == 0) {
if (buildingsCount % 1000 == 0) { sb.append(citygml.getHeader());
LOGGER.info("1000 buildings parsed");
} }
buildingsCount += 1;
Point point = buildingXmlNode.getCenterOfMass(); Point point = buildingXmlNode.getCenterOfMass();
if (point.within(poly)) { if (point.within(poly)) {
foundBuildingsCount++; foundBuildingsCount++;
sb.append(buildingXmlNode.toString()); sb.append(buildingXmlNode.toString());
} }
if (buildingsCount % 1000 == 0) {
LOGGER.info("1000 buildings parsed");
}
} }
LOGGER.info("Buildings found in selected region " + foundBuildingsCount); LOGGER.info("Buildings found in selected region " + foundBuildingsCount);
// int footerOffset = buildingOffset + buildingLength; sb.append(citygml.getFooter());
// int footerLength = (int) (Files.size(citygmlPath) - footerOffset);
// sb.append(navigator.toRawString(footerOffset, footerLength));
return sb; return sb;
} }
......
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