Commit 4e46e9df authored by duminil's avatar duminil
Browse files

RegionChooser: Separate class for Region Extractor

parent ddf15a0f
......@@ -6,21 +6,15 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import eu.simstadt.nf4j.ExportJobFromJavaFXRegionChooser;
......@@ -121,7 +115,7 @@ public void extractZIPtoGML(String zipFilename) throws IOException {
public void downloadRegionFromCityGML(String wktPolygon, String project, String citygml) throws IOException,
ParseException {
StringBuffer sb = selectRegionDirectlyFromCityGML(citygmlPath(project, citygml), wktPolygon);
StringBuffer sb = RegionExtractor.selectRegionDirectlyFromCityGML(citygmlPath(project, citygml), wktPolygon);
File buildingIdsFile = selectSaveFileWithDialog(project, citygml, "selected_region");
if (buildingIdsFile != null) {
......@@ -131,49 +125,6 @@ public void downloadRegionFromCityGML(String wktPolygon, String project, String
}
}
public StringBuffer selectRegionDirectlyFromCityGML(Path citygmlPath, String wktPolygon) throws IOException,
ParseException {
//TODO: Write directly to file
// Instant start = Instant.now();
Geometry poly = wktReader.read(wktPolygon);
final GeometryFactory gf = new GeometryFactory();
String s = new String(Files.readAllBytes(citygmlPath), Charset.defaultCharset());
Pattern cityObjectPattern = Pattern
.compile("(?s)<(core:)?cityObjectMember>.*?<\\/(core:)?cityObjectMember>\\s*");
Pattern gsk3CoordinatesPattern = Pattern
.compile("(?<![\\d\\.])(3\\d\\d\\d\\d\\d\\d[\\.\\d]*) (5\\d\\d\\d\\d\\d\\d[\\.\\d]*)");
Matcher cityObjectMatcher = cityObjectPattern.matcher(s);
StringBuffer sb = new StringBuffer();
int i = 0;
while (cityObjectMatcher.find()) {
cityObjectMatcher.appendReplacement(sb, "");
String cityObject = cityObjectMatcher.group();
Matcher gsk3CoordinatesMatcher = gsk3CoordinatesPattern.matcher(cityObject);
int coordinatesCount = 0;
double xTotal = 0;
double yTotal = 0;
while (gsk3CoordinatesMatcher.find()) {
coordinatesCount++;
xTotal += Double.valueOf(gsk3CoordinatesMatcher.group(1));
yTotal += Double.valueOf(gsk3CoordinatesMatcher.group(2));
}
double x = xTotal / coordinatesCount;
double y = yTotal / coordinatesCount;
Coordinate coord = new Coordinate(x, y);
Point point = gf.createPoint(coord);
if (point.within(poly)) {
i++;
sb.append(cityObject);
}
}
System.out.println("Buildings found in selected region " + i);
cityObjectMatcher.appendTail(sb);
// System.out.println(Duration.between(start, Instant.now()));
return sb;
}
private File selectSaveFileWithDialog(String project, String citygml, String suffix) {
Stage mainStage = (Stage) Browser.this.getScene().getWindow();
FileChooser fileChooser = new FileChooser();
......
package eu.simstadt.regionchooser;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
public class RegionExtractor
{
static private WKTReader wktReader = new WKTReader();
static public StringBuffer selectRegionDirectlyFromCityGML(Path citygmlPath, String wktPolygon)
throws IOException, ParseException {
//TODO: Write directly to file
// Instant start = Instant.now();
Geometry poly = wktReader.read(wktPolygon);
final GeometryFactory gf = new GeometryFactory();
String s = new String(Files.readAllBytes(citygmlPath), Charset.defaultCharset());
Pattern cityObjectPattern = Pattern
.compile("(?s)<(core:)?cityObjectMember>.*?<\\/(core:)?cityObjectMember>\\s*");
Pattern gsk3CoordinatesPattern = Pattern
.compile("(?<![\\d\\.])(3\\d\\d\\d\\d\\d\\d[\\.\\d]*) (5\\d\\d\\d\\d\\d\\d[\\.\\d]*)");
Matcher cityObjectMatcher = cityObjectPattern.matcher(s);
StringBuffer sb = new StringBuffer();
int i = 0;
while (cityObjectMatcher.find()) {
cityObjectMatcher.appendReplacement(sb, "");
String cityObject = cityObjectMatcher.group();
Matcher gsk3CoordinatesMatcher = gsk3CoordinatesPattern.matcher(cityObject);
int coordinatesCount = 0;
double xTotal = 0;
double yTotal = 0;
while (gsk3CoordinatesMatcher.find()) {
coordinatesCount++;
xTotal += Double.valueOf(gsk3CoordinatesMatcher.group(1));
yTotal += Double.valueOf(gsk3CoordinatesMatcher.group(2));
}
double x = xTotal / coordinatesCount;
double y = yTotal / coordinatesCount;
Coordinate coord = new Coordinate(x, y);
Point point = gf.createPoint(coord);
if (point.within(poly)) {
i++;
sb.append(cityObject);
}
}
System.out.println("Buildings found in selected region " + i);
cityObjectMatcher.appendTail(sb);
// System.out.println(Duration.between(start, Instant.now()));
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