From 99a083430ad5eae634fc019264438fcb241466b5 Mon Sep 17 00:00:00 2001 From: Eric Duminil <eric.duminil@gmail.com> Date: Fri, 7 Oct 2022 12:43:14 +0200 Subject: [PATCH] Refactor --- .../regionchooser/RegionChooserBrowser.java | 17 +--------- .../regionchooser/RegionChooserUtils.java | 31 ++++++++++++++----- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/main/java/eu/simstadt/regionchooser/RegionChooserBrowser.java b/src/main/java/eu/simstadt/regionchooser/RegionChooserBrowser.java index 31edc82..1c323d7 100644 --- a/src/main/java/eu/simstadt/regionchooser/RegionChooserBrowser.java +++ b/src/main/java/eu/simstadt/regionchooser/RegionChooserBrowser.java @@ -1,9 +1,7 @@ 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"); diff --git a/src/main/java/eu/simstadt/regionchooser/RegionChooserUtils.java b/src/main/java/eu/simstadt/regionchooser/RegionChooserUtils.java index 4bc3876..5c9f85f 100644 --- a/src/main/java/eu/simstadt/regionchooser/RegionChooserUtils.java +++ b/src/main/java/eu/simstadt/regionchooser/RegionChooserUtils.java @@ -1,5 +1,6 @@ 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); + } + } + } + } } -- GitLab