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

Refactor

parent fd1b29d4
Showing with 24 additions and 24 deletions
+24 -24
package eu.simstadt.regionchooser; package eu.simstadt.regionchooser;
import java.io.BufferedWriter;
import java.io.File; import java.io.File;
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.nio.file.Paths; import java.nio.file.Paths;
import java.util.logging.Logger; import java.util.logging.Logger;
...@@ -32,7 +30,6 @@ public class RegionChooserBrowser extends Region ...@@ -32,7 +30,6 @@ public class RegionChooserBrowser extends Region
{ {
private static final Logger LOGGER = Logger.getLogger(RegionChooserBrowser.class.getName()); private static final Logger LOGGER = Logger.getLogger(RegionChooserBrowser.class.getName());
private static final String PREF_RECENT_REPOSITORY = "RECENT_REPOSITORY"; 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`. * 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 ...@@ -88,21 +85,9 @@ public void downloadRegionFromCityGMLs(String wktPolygon, String project, String
File buildingIdsFile = selectSaveFileWithDialog(project, File buildingIdsFile = selectSaveFileWithDialog(project,
csvCitygmls.replace(";", "_").replace(".gml", ""), "selected_region"); 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() { public void selectRepository() {
Preferences userPrefs = Preferences.userRoot().node("/eu/simstadt/desktop"); Preferences userPrefs = Preferences.userRoot().node("/eu/simstadt/desktop");
......
package eu.simstadt.regionchooser; package eu.simstadt.regionchooser;
import java.io.BufferedWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
...@@ -23,6 +24,7 @@ ...@@ -23,6 +24,7 @@
private static final Pattern srsNamePattern = Pattern.compile("(?i)(?<=srsName=[\"'])[^\"']+(?=[\"'])"); private static final Pattern srsNamePattern = Pattern.compile("(?i)(?<=srsName=[\"'])[^\"']+(?=[\"'])");
private static final int CITYGML_HEADER_LENGTH = 50; private static final int CITYGML_HEADER_LENGTH = 50;
private static final String EPSG = "EPSG:"; private static final String EPSG = "EPSG:";
private static final int BUFFER = 1024;
private RegionChooserUtils() { private RegionChooserUtils() {
// only static use // only static use
...@@ -32,14 +34,14 @@ private RegionChooserUtils() { ...@@ -32,14 +34,14 @@ private RegionChooserUtils() {
* The srsName (The name by which this reference system is identified) inside the CityGML file can have multiple * 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 * 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. * 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" * 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 * or "ETRS89_UTM32" as srsName, so those are also included. It isn't guaranteed that those formats are correctly
* parsed, though. * parsed, though.
* *
* The EPSG ids and parameters are defined in resources ('nad/epsg') inside proj4j-0.1.0.jar. Some EPSG ids are * 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 * missing though, e.g. 7415
* *
* @param srsName * @param srsName
* @return CoordinateReferenceSystem * @return CoordinateReferenceSystem
*/ */
...@@ -85,9 +87,9 @@ private static CoordinateReferenceSystem crsFromSrsName(String srsName) { ...@@ -85,9 +87,9 @@ private static CoordinateReferenceSystem crsFromSrsName(String srsName) {
/** /**
* Converts a jts.geom.Polygon from one CoordinateReferenceSystem to another. * Converts a jts.geom.Polygon from one CoordinateReferenceSystem to another.
* *
* NOTE: It would be easier with org.geotools.referencing.CRS instead of Proj4J * NOTE: It would be easier with org.geotools.referencing.CRS instead of Proj4J
* *
* @param polygonInOriginalCRS * @param polygonInOriginalCRS
* @param originalCRS * @param originalCRS
* @param newCRS * @param newCRS
...@@ -108,11 +110,11 @@ public static Polygon changePolygonCRS(Polygon polygonInOriginalCRS, CoordinateR ...@@ -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 * 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 * 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. * GeoCoordinatesAccessor. This solution can be convenient for Web services, RegionChooser or HullExtractor.
* *
* @param citygmlPath * @param citygmlPath
* @return * @return
* @throws IOException * @throws IOException
...@@ -132,7 +134,7 @@ public static CoordinateReferenceSystem crsFromCityGMLHeader(Path citygmlPath) t ...@@ -132,7 +134,7 @@ public static CoordinateReferenceSystem crsFromCityGMLHeader(Path citygmlPath) t
/** /**
* Finds every CityGML in every .proj folder in a repository. * Finds every CityGML in every .proj folder in a repository.
* *
* @param repository * @param repository
* @return a stream of CityGML Paths * @return a stream of CityGML Paths
* @throws IOException * @throws IOException
...@@ -146,4 +148,17 @@ public static Stream<Path> everyCityGML(Path repository) throws IOException { ...@@ -146,4 +148,17 @@ public static Stream<Path> everyCityGML(Path repository) throws IOException {
p.toString().toLowerCase().endsWith(".gml")); 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);
}
}
}
}
} }
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