Commit 114d2a18 authored by duminil's avatar duminil
Browse files

Buildings are extracted, header and footer are missing

parent cf59f25e
package eu.simstadt.lowlevelgmlparser; package eu.simstadt.lowlevelgmlparser;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import com.ximpleware.AutoPilot; import com.ximpleware.AutoPilot;
import com.ximpleware.NavException; import com.ximpleware.NavException;
import com.ximpleware.VTDNav; import com.ximpleware.VTDNav;
import com.ximpleware.XPathEvalException;
import com.ximpleware.XPathParseException;
public class BuildingXmlNode public class BuildingXmlNode
...@@ -12,6 +17,7 @@ ...@@ -12,6 +17,7 @@
private int buildingLength; private int buildingLength;
private VTDNav navigator; private VTDNav navigator;
private AutoPilot coordinatesFinder; private AutoPilot coordinatesFinder;
private static final GeometryFactory gf = new GeometryFactory();
public BuildingXmlNode(VTDNav navigator, int buildingOffset, int buildingLength) { public BuildingXmlNode(VTDNav navigator, int buildingOffset, int buildingLength) {
this.navigator = navigator; this.navigator = navigator;
...@@ -20,6 +26,31 @@ public BuildingXmlNode(VTDNav navigator, int buildingOffset, int buildingLength) ...@@ -20,6 +26,31 @@ public BuildingXmlNode(VTDNav navigator, int buildingOffset, int buildingLength)
this.buildingOffset = buildingOffset; this.buildingOffset = buildingOffset;
} }
public Point getCenterOfMass() throws XPathParseException, NumberFormatException, XPathEvalException, NavException {
int coordinatesCount = 0;
double xTotal = 0;
double yTotal = 0;
coordinatesFinder.selectXPath(".//posList");
while (coordinatesFinder.evalXPath() != -1) {
long offsetAndLength = navigator.getContentFragment();
int coordinatesOffset = (int) offsetAndLength;
int coordinatesLength = (int) (offsetAndLength >> 32);
String posList = navigator.toRawString(coordinatesOffset, coordinatesLength);
String[] coordinates = posList.split(" ");
for (int k = 0; k < coordinates.length; k = k + 3) {
coordinatesCount++;
xTotal += Double.valueOf(coordinates[k]);
yTotal += Double.valueOf(coordinates[k + 1]);
}
}
double x = xTotal / coordinatesCount;
double y = yTotal / coordinatesCount;
Coordinate coord = new Coordinate(x, y);
return gf.createPoint(coord);
}
public String toString() { public String toString() {
try { try {
return navigator.toRawString(buildingOffset, buildingLength); return navigator.toRawString(buildingOffset, buildingLength);
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
import com.ximpleware.XPathParseException; import com.ximpleware.XPathParseException;
public class CityGmlIterator implements Iterable<String> public class CityGmlIterator implements Iterable<BuildingXmlNode>
{ {
private static final Logger LOGGER = Logger.getLogger(CityGmlIterator.class.getName()); private static final Logger LOGGER = Logger.getLogger(CityGmlIterator.class.getName());
...@@ -34,8 +34,8 @@ public CityGmlIterator(Path citygmlPath) ...@@ -34,8 +34,8 @@ public CityGmlIterator(Path citygmlPath)
} }
@Override @Override
public Iterator<String> iterator() { public Iterator<BuildingXmlNode> iterator() {
Iterator<String> it = new Iterator<String>() { Iterator<BuildingXmlNode> it = new Iterator<BuildingXmlNode>() {
@Override @Override
public boolean hasNext() { public boolean hasNext() {
...@@ -46,7 +46,7 @@ public boolean hasNext() { ...@@ -46,7 +46,7 @@ public boolean hasNext() {
} }
@Override @Override
public String next() { public BuildingXmlNode next() {
try { try {
buildingsCount += 1; buildingsCount += 1;
if (buildingsCount % 1000 == 0) { if (buildingsCount % 1000 == 0) {
...@@ -55,8 +55,7 @@ public String next() { ...@@ -55,8 +55,7 @@ public String next() {
offsetAndLength = navigator.getElementFragment(); offsetAndLength = navigator.getElementFragment();
buildingOffset = (int) offsetAndLength; buildingOffset = (int) offsetAndLength;
buildingLength = (int) (offsetAndLength >> 32); buildingLength = (int) (offsetAndLength >> 32);
BuildingXmlNode b = new BuildingXmlNode(navigator, buildingOffset, buildingLength); return new BuildingXmlNode(navigator, buildingOffset, buildingLength);
return b.toString();
} catch (NavException ex) {} } catch (NavException ex) {}
return null; return null;
} }
......
package eu.simstadt.regionchooser; package eu.simstadt.regionchooser;
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.logging.Logger; import java.util.logging.Logger;
import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamException;
import org.xml.sax.SAXParseException; import org.xml.sax.SAXParseException;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point; import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.ParseException; import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader; import com.vividsolutions.jts.io.WKTReader;
import com.ximpleware.AutoPilot;
import com.ximpleware.NavException; import com.ximpleware.NavException;
import com.ximpleware.VTDGen;
import com.ximpleware.VTDNav;
import com.ximpleware.XPathEvalException; import com.ximpleware.XPathEvalException;
import com.ximpleware.XPathParseException; import com.ximpleware.XPathParseException;
import eu.simstadt.lowlevelgmlparser.BuildingXmlNode;
import eu.simstadt.lowlevelgmlparser.CityGmlIterator; import eu.simstadt.lowlevelgmlparser.CityGmlIterator;
...@@ -31,77 +26,29 @@ static public StringBuffer selectRegionDirectlyFromCityGML(Path citygmlPath, Str ...@@ -31,77 +26,29 @@ static public StringBuffer selectRegionDirectlyFromCityGML(Path citygmlPath, Str
throws SAXParseException, XMLStreamException, ParseException, XPathParseException, NavException, throws SAXParseException, XMLStreamException, ParseException, XPathParseException, NavException,
NumberFormatException, XPathEvalException, IOException { NumberFormatException, XPathEvalException, IOException {
CityGmlIterator citygml = new CityGmlIterator(citygmlPath);
for (String lowLevelBuildingXML : citygml) {
System.out.println(lowLevelBuildingXML.length());
}
Geometry poly = wktReader.read(wktPolygon);
final GeometryFactory gf = new GeometryFactory();
StringBuffer sb = new StringBuffer();
int foundBuildingsCount = 0;
int buildingsCount = 0; int buildingsCount = 0;
long offsetAndLength; int foundBuildingsCount = 0;
int buildingOffset = 0; StringBuffer sb = new StringBuffer();
int buildingLength = 0; Geometry poly = wktReader.read(wktPolygon);
int coordinatesOffset = 0;
int coordinatesLength = 0;
VTDGen parser = new VTDGen();
if (parser.parseFile(citygmlPath.toString(), false)) {
VTDNav navigator = parser.getNav();
AutoPilot buildingsFinder = new AutoPilot(navigator);
buildingsFinder.selectXPath("//cityObjectMember");
while ((buildingsFinder.evalXPath()) != -1) {
AutoPilot coordinatesFinder = new AutoPilot(navigator);
coordinatesFinder.selectXPath(".//posList");
offsetAndLength = navigator.getElementFragment();
buildingOffset = (int) offsetAndLength;
buildingLength = (int) (offsetAndLength >> 32);
String cityObject = navigator.toRawString(buildingOffset, buildingLength);
// Add header
if (buildingsCount == 0) {
sb.append(navigator.toRawString(0, buildingOffset));
}
buildingsCount += 1;
if (buildingsCount % 1000 == 0) {
LOGGER.info("1000 buildings parsed");
}
int coordinatesCount = 0;
double xTotal = 0;
double yTotal = 0;
while (coordinatesFinder.evalXPath() != -1) {
offsetAndLength = navigator.getContentFragment();
coordinatesOffset = (int) offsetAndLength;
coordinatesLength = (int) (offsetAndLength >> 32);
String posList = navigator.toRawString(coordinatesOffset, coordinatesLength);
String[] coordinates = posList.split(" ");
for (int k = 0; k < coordinates.length; k = k + 3) {
coordinatesCount++;
xTotal += Double.valueOf(coordinates[k]);
yTotal += Double.valueOf(coordinates[k + 1]);
}
}
double x = xTotal / coordinatesCount; CityGmlIterator citygml = new CityGmlIterator(citygmlPath);
double y = yTotal / coordinatesCount; for (BuildingXmlNode buildingXmlNode : citygml) {
Coordinate coord = new Coordinate(x, y); buildingsCount += 1;
Point point = gf.createPoint(coord); if (buildingsCount % 1000 == 0) {
if (point.within(poly)) { LOGGER.info("1000 buildings parsed");
foundBuildingsCount++; }
sb.append(cityObject); Point point = buildingXmlNode.getCenterOfMass();
} if (point.within(poly)) {
foundBuildingsCount++;
sb.append(buildingXmlNode.toString());
} }
LOGGER.info("Buildings found in selected region " + foundBuildingsCount);
int footerOffset = buildingOffset + buildingLength;
int footerLength = (int) (Files.size(citygmlPath) - footerOffset);
sb.append(navigator.toRawString(footerOffset, footerLength));
} }
LOGGER.info("Buildings found in selected region " + foundBuildingsCount);
// int footerOffset = buildingOffset + buildingLength;
// int footerLength = (int) (Files.size(citygmlPath) - footerOffset);
// sb.append(navigator.toRawString(footerOffset, footerLength));
return sb; return sb;
} }
} }
Markdown is supported
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