Commit 99a08343 authored by Eric Duminil's avatar Eric Duminil
Browse files

Refactor

parent fd1b29d4
package eu.simstadt.regionchooser;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Logger;
......@@ -32,7 +30,6 @@ public class RegionChooserBrowser extends Region
{
private static final Logger LOGGER = Logger.getLogger(RegionChooserBrowser.class.getName());
private static final String PREF_RECENT_REPOSITORY = "RECENT_REPOSITORY";
private static final int BUFFER = 1024;
/**
* JavaFX Backend for RegionChooser. Inside simstadt_openlayers.js frontend, this class is available as `fxapp`.
......@@ -88,21 +85,9 @@ public void downloadRegionFromCityGMLs(String wktPolygon, String project, String
File buildingIdsFile = selectSaveFileWithDialog(project,
csvCitygmls.replace(";", "_").replace(".gml", ""), "selected_region");
writeStringBuilderToFile(sb, buildingIdsFile.toPath());
RegionChooserUtils.writeStringBuilderToFile(sb, buildingIdsFile.toPath());
}
private void writeStringBuilderToFile(StringBuilder sb, Path outputFile) throws IOException {
if (outputFile != null) {
try (BufferedWriter writer = Files.newBufferedWriter(outputFile)) {
char[] chars = new char[BUFFER];
for (int aPosStart = 0; aPosStart < sb.length(); aPosStart += BUFFER) {
int chunk = Math.min(BUFFER, sb.length() - aPosStart);
sb.getChars(aPosStart, aPosStart + chunk, chars, 0);
writer.write(chars, 0, chunk);
}
}
}
}
public void selectRepository() {
Preferences userPrefs = Preferences.userRoot().node("/eu/simstadt/desktop");
......
package eu.simstadt.regionchooser;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
......@@ -23,6 +24,7 @@
private static final Pattern srsNamePattern = Pattern.compile("(?i)(?<=srsName=[\"'])[^\"']+(?=[\"'])");
private static final int CITYGML_HEADER_LENGTH = 50;
private static final String EPSG = "EPSG:";
private static final int BUFFER = 1024;
private RegionChooserUtils() {
// only static use
......@@ -32,14 +34,14 @@ private RegionChooserUtils() {
* The srsName (The name by which this reference system is identified) inside the CityGML file can have multiple
* formats. This method tries to parse the string and detect the corresponding reference system. If it is found, it
* returns a proj4j.CoordinateReferenceSystem. It throws an IllegalArgumentException otherwise.
*
*
* This method should be able to parse any EPSG id : e.g. "EPSG:1234". German Citygmls might also have "DE_DHDN_3GK3"
* or "ETRS89_UTM32" as srsName, so those are also included. It isn't guaranteed that those formats are correctly
* parsed, though.
*
*
* The EPSG ids and parameters are defined in resources ('nad/epsg') inside proj4j-0.1.0.jar. Some EPSG ids are
* missing though, e.g. 7415
*
*
* @param srsName
* @return CoordinateReferenceSystem
*/
......@@ -85,9 +87,9 @@ private static CoordinateReferenceSystem crsFromSrsName(String srsName) {
/**
* Converts a jts.geom.Polygon from one CoordinateReferenceSystem to another.
*
*
* NOTE: It would be easier with org.geotools.referencing.CRS instead of Proj4J
*
*
* @param polygonInOriginalCRS
* @param originalCRS
* @param newCRS
......@@ -108,11 +110,11 @@ public static Polygon changePolygonCRS(Polygon polygonInOriginalCRS, CoordinateR
}
/**
*
*
* Fast scan of the 50 first lines of a Citygml file to look for srsName. It might not be as reliable as parsing the
* whole CityGML, but it should be much faster and use much less memory. For a more reliable solution, use
* GeoCoordinatesAccessor. This solution can be convenient for Web services, RegionChooser or HullExtractor.
*
*
* @param citygmlPath
* @return
* @throws IOException
......@@ -132,7 +134,7 @@ public static CoordinateReferenceSystem crsFromCityGMLHeader(Path citygmlPath) t
/**
* Finds every CityGML in every .proj folder in a repository.
*
*
* @param repository
* @return a stream of CityGML Paths
* @throws IOException
......@@ -146,4 +148,17 @@ public static Stream<Path> everyCityGML(Path repository) throws IOException {
p.toString().toLowerCase().endsWith(".gml"));
}
public static void writeStringBuilderToFile(StringBuilder sb, Path outputFile) throws IOException {
if (outputFile != null) {
try (BufferedWriter writer = Files.newBufferedWriter(outputFile)) {
char[] chars = new char[BUFFER];
for (int aPosStart = 0; aPosStart < sb.length(); aPosStart += BUFFER) {
int chunk = Math.min(BUFFER, sb.length() - aPosStart);
sb.getChars(aPosStart, aPosStart + chunk, chars, 0);
writer.write(chars, 0, chunk);
}
}
}
}
}
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