Commit 809a82ae authored by duminil's avatar duminil
Browse files

RegionChooser: Test with xmin, xmax, ymin and ymax for buildings.

parent 68891bca
...@@ -5,7 +5,9 @@ ...@@ -5,7 +5,9 @@
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;
...@@ -21,6 +23,7 @@ ...@@ -21,6 +23,7 @@
static private WKTReader wktReader = new WKTReader(); static private WKTReader wktReader = new WKTReader();
private static final Logger LOGGER = Logger.getLogger(RegionExtractor.class.getName()); private static final Logger LOGGER = Logger.getLogger(RegionExtractor.class.getName());
private static final GeometryFactory gf = new GeometryFactory();
static public StringBuffer selectRegionDirectlyFromCityGML(Path citygmlPath, String wktPolygon, String srsName) static public StringBuffer selectRegionDirectlyFromCityGML(Path citygmlPath, String wktPolygon, String srsName)
throws SAXParseException, XMLStreamException, ParseException, XPathParseException, NavException, throws SAXParseException, XMLStreamException, ParseException, XPathParseException, NavException,
...@@ -37,7 +40,8 @@ static public StringBuffer selectRegionDirectlyFromCityGML(Path citygmlPath, Str ...@@ -37,7 +40,8 @@ static public StringBuffer selectRegionDirectlyFromCityGML(Path citygmlPath, Str
sb.append(citygml.getHeader()); sb.append(citygml.getHeader());
} }
buildingsCount += 1; buildingsCount += 1;
Point point = buildingXmlNode.getCenterOfMass(); Coordinate coord = new Coordinate(buildingXmlNode.x, buildingXmlNode.y);
Point point = gf.createPoint(coord); //NOTE: Should it be in buildingXmlNode?
if (point.within(poly)) { if (point.within(poly)) {
foundBuildingsCount++; foundBuildingsCount++;
sb.append(buildingXmlNode.toString()); sb.append(buildingXmlNode.toString());
......
package eu.simstadt.regionchooser.citygml_parser; package eu.simstadt.regionchooser.citygml_parser;
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;
...@@ -17,19 +14,32 @@ ...@@ -17,19 +14,32 @@
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 Double x;
public Double xMin;
public Double xMax;
public Double yMin;
public Double yMax;
public Double y;
public BuildingXmlNode(VTDNav navigator, int buildingOffset, int buildingLength) { public BuildingXmlNode(VTDNav navigator, int buildingOffset, int buildingLength)
throws NumberFormatException, XPathParseException, XPathEvalException, NavException {
this.navigator = navigator; this.navigator = navigator;
this.coordinatesFinder = new AutoPilot(navigator); this.coordinatesFinder = new AutoPilot(navigator);
this.buildingLength = buildingLength; this.buildingLength = buildingLength;
this.buildingOffset = buildingOffset; this.buildingOffset = buildingOffset;
extractCoordinates(); //NOTE: Should it be done lazily? Is there any reason to extract a BuildingXmlNode without coordinates?
} }
public Point getCenterOfMass() throws XPathParseException, NumberFormatException, XPathEvalException, NavException { private void extractCoordinates()
throws XPathParseException, NumberFormatException, XPathEvalException, NavException {
int coordinatesCount = 0; int coordinatesCount = 0;
double xTotal = 0; double xTotal = 0;
double yTotal = 0; double yTotal = 0;
double x, y;
double xMin = Double.MAX_VALUE;
double xMax = Double.MIN_VALUE;
double yMin = Double.MAX_VALUE;
double yMax = Double.MIN_VALUE;
coordinatesFinder.selectXPath(".//posList|.//pos"); coordinatesFinder.selectXPath(".//posList|.//pos");
while (coordinatesFinder.evalXPath() != -1) { while (coordinatesFinder.evalXPath() != -1) {
...@@ -40,15 +50,30 @@ public Point getCenterOfMass() throws XPathParseException, NumberFormatException ...@@ -40,15 +50,30 @@ public Point getCenterOfMass() throws XPathParseException, NumberFormatException
String[] coordinates = posList.trim().split("\\s+"); String[] coordinates = posList.trim().split("\\s+");
for (int k = 0; k < coordinates.length; k = k + 3) { for (int k = 0; k < coordinates.length; k = k + 3) {
coordinatesCount++; coordinatesCount++;
xTotal += Double.valueOf(coordinates[k]); x = Double.valueOf(coordinates[k]);
yTotal += Double.valueOf(coordinates[k + 1]); y = Double.valueOf(coordinates[k + 1]);
if (x < xMin) {
xMin = x;
} }
if (y < yMin) {
yMin = y;
} }
if (x > xMax) {
double x = xTotal / coordinatesCount; xMax = x;
double y = yTotal / coordinatesCount; }
Coordinate coord = new Coordinate(x, y); if (y > yMax) {
return gf.createPoint(coord); yMax = y;
}
xTotal += x;
yTotal += y;
}
}
this.xMin = xMin;
this.xMax = xMax;
this.yMin = yMin;
this.yMax = yMax;
this.x = xTotal / coordinatesCount;
this.y = yTotal / coordinatesCount;
} }
public String toString() { public String toString() {
...@@ -58,5 +83,4 @@ public String toString() { ...@@ -58,5 +83,4 @@ public String toString() {
return null; return null;
} }
} }
} }
...@@ -63,7 +63,7 @@ public BuildingXmlNode next() { ...@@ -63,7 +63,7 @@ public BuildingXmlNode next() {
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 | NumberFormatException | XPathParseException | XPathEvalException ex) {
LOGGER.warning("Error while parsing " + citygmlPath); LOGGER.warning("Error while parsing " + citygmlPath);
} }
return null; return null;
......
package eu.simstadt.regionchooser.test; package eu.simstadt.regionchooser.test;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
...@@ -18,10 +19,16 @@ private void testNoNanInCoordinates(Path citygmlPath) ...@@ -18,10 +19,16 @@ private void testNoNanInCoordinates(Path citygmlPath)
throws NumberFormatException, XPathParseException, NavException, XPathEvalException, IOException { throws NumberFormatException, XPathParseException, NavException, XPathEvalException, IOException {
CityGmlIterator buildingXmlNodes = new CityGmlIterator(citygmlPath); CityGmlIterator buildingXmlNodes = new CityGmlIterator(citygmlPath);
for (BuildingXmlNode buildingXmlNode : buildingXmlNodes) { for (BuildingXmlNode buildingXmlNode : buildingXmlNodes) {
double x = buildingXmlNode.getCenterOfMass().getX(); assertFalse("Coordinate should be a double", Double.isNaN(buildingXmlNode.x));
double y = buildingXmlNode.getCenterOfMass().getY(); assertFalse("Coordinate should be a double", Double.isNaN(buildingXmlNode.y));
assertFalse("Coordinate should be a double", Double.isNaN(x)); assertFalse("Coordinate should be a double", Double.isNaN(buildingXmlNode.xMax));
assertFalse("Coordinate should be a double", Double.isNaN(y)); assertFalse("Coordinate should be a double", Double.isNaN(buildingXmlNode.yMax));
assertFalse("Coordinate should be a double", Double.isNaN(buildingXmlNode.xMin));
assertFalse("Coordinate should be a double", Double.isNaN(buildingXmlNode.yMin));
assertTrue("Coordinates Min/Max should be plausible", buildingXmlNode.xMax > buildingXmlNode.x);
assertTrue("Coordinates Min/Max should be plausible", buildingXmlNode.yMax > buildingXmlNode.y);
assertTrue("Coordinates Min/Max should be plausible", buildingXmlNode.xMin < buildingXmlNode.x);
assertTrue("Coordinates Min/Max should be plausible", buildingXmlNode.yMin < buildingXmlNode.y);
} }
} }
......
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