Skip to content

Commit

Permalink
Refactor to move method out of GeometryUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
utas-raymondng committed Nov 21, 2024
1 parent 873e607 commit 0f4a084
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
32 changes: 32 additions & 0 deletions indexer/src/main/java/au/org/aodn/esindexer/utils/FileUtils.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0f4a084

Please sign in to comment.