diff --git a/indexer/src/main/java/au/org/aodn/esindexer/utils/FileUtils.java b/indexer/src/main/java/au/org/aodn/esindexer/utils/FileUtils.java new file mode 100644 index 00000000..556cccb4 --- /dev/null +++ b/indexer/src/main/java/au/org/aodn/esindexer/utils/FileUtils.java @@ -0,0 +1,32 @@ +package au.org.aodn.esindexer.utils; + +import org.springframework.core.io.ClassPathResource; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; + +public class FileUtils { + public static File saveResourceToTemp(String resourceName, String filename) { + String tempDir = System.getProperty("java.io.tmpdir"); + ClassPathResource resource = new ClassPathResource(resourceName); + + File tempFile = new File(tempDir, filename); + try(InputStream input = resource.getInputStream()) { + tempFile.deleteOnExit(); // Ensure the file is deleted when the JVM exits + + // Write the InputStream to the temporary file + try (FileOutputStream outputStream = new FileOutputStream(tempFile)) { + byte[] buffer = new byte[1024]; + int bytesRead; + while ((bytesRead = input.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + } + } catch (IOException e) { + throw new RuntimeException(e); + } + return tempFile; + } +} diff --git a/indexer/src/main/java/au/org/aodn/esindexer/utils/GeometryUtils.java b/indexer/src/main/java/au/org/aodn/esindexer/utils/GeometryUtils.java index b45bc9e0..f3ed1243 100644 --- a/indexer/src/main/java/au/org/aodn/esindexer/utils/GeometryUtils.java +++ b/indexer/src/main/java/au/org/aodn/esindexer/utils/GeometryUtils.java @@ -18,7 +18,6 @@ import org.locationtech.jts.operation.union.UnaryUnionOp; import org.locationtech.jts.simplify.DouglasPeuckerSimplifier; import org.opengis.feature.simple.SimpleFeature; -import org.springframework.core.io.ClassPathResource; import java.io.*; import java.net.URL; @@ -57,8 +56,8 @@ public enum PointOrientation { public static void init() { try { // shp file depends on shx, so need to have shx appear in temp folder. - saveResourceToTemp("land/ne_10m_land.shx", "shapefile.shx"); - File tempFile = saveResourceToTemp("land/ne_10m_land.shp", "shapefile.shp"); + FileUtils.saveResourceToTemp("land/ne_10m_land.shx", "shapefile.shx"); + File tempFile = FileUtils.saveResourceToTemp("land/ne_10m_land.shp", "shapefile.shp"); // Load the shapefile from the temporary file using ShapefileDataStore URL tempFileUrl = tempFile.toURI().toURL(); @@ -98,28 +97,6 @@ public static void init() { throw new RuntimeException(ioe); } } - - protected static File saveResourceToTemp(String resourceName, String filename) { - String tempDir = System.getProperty("java.io.tmpdir"); - ClassPathResource resource = new ClassPathResource(resourceName); - - File tempFile = new File(tempDir, filename); - try(InputStream input = resource.getInputStream()) { - tempFile.deleteOnExit(); // Ensure the file is deleted when the JVM exits - - // Write the InputStream to the temporary file - try (FileOutputStream outputStream = new FileOutputStream(tempFile)) { - byte[] buffer = new byte[1024]; - int bytesRead; - while ((bytesRead = input.read(buffer)) != -1) { - outputStream.write(buffer, 0, bytesRead); - } - } - } catch (IOException e) { - throw new RuntimeException(e); - } - return tempFile; - } /** * @param polygons - Assume to be EPSG:4326, as GeoJson always use this encoding. * @return