Skip to content

Commit

Permalink
Speed up map processing by using multiple thread
Browse files Browse the repository at this point in the history
  • Loading branch information
utas-raymondng committed Sep 24, 2024
1 parent 02b488e commit 4c41a2f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.math.RoundingMode;
import java.net.URL;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.Function;

public class GeometryUtils {
Expand Down Expand Up @@ -271,22 +272,51 @@ protected static List<Geometry> convertToListGeometry(Geometry multipolygon) {
* @param large - A Polygon to break into grid
* @return - A polygon the break into grid.
*/
protected static List<Geometry> breakLargeGeometryToGrid(Geometry large) {
protected static List<Geometry> breakLargeGeometryToGrid(final Geometry large) {
logger.debug("Start break down large geometry");
// Get the bounding box (extent) of the large polygon
Envelope envelope = large.getEnvelopeInternal();

// Hard code cell size, we can adjust the break grid size. 10.0 result in 3x3 grid
// cover Australia
List<Polygon> gridPolygons = createGridPolygons(envelope, getCellSize());

List<Geometry> intersectedPolygons = new ArrayList<>();
// Create an ExecutorService with a fixed thread pool size
ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

// List to store Future objects representing the results of the tasks
List<Future<Geometry>> futureResults = new ArrayList<>();

// Submit tasks to executor for each gridPolygon
for (Polygon gridPolygon : gridPolygons) {
Geometry intersection = gridPolygon.intersection(large);
if (!intersection.isEmpty()) {
intersectedPolygons.add(intersection);
Callable<Geometry> task = () -> {
Geometry intersection = gridPolygon.intersection(large);
return !intersection.isEmpty() ? intersection : null;
};
Future<Geometry> future = executorService.submit(task);
futureResults.add(future);
}

// List to store the intersected polygons
final List<Geometry> intersectedPolygons = new ArrayList<>();

// Collect the results from the futures
for (Future<Geometry> future : futureResults) {
try {
// This blocks until the result is available
Geometry result = future.get();
if (result != null) {
intersectedPolygons.add(result);
}
} catch (InterruptedException | ExecutionException e) {
// Nothing to report
}
}

// Shutdown the ExecutorService after all tasks are completed
executorService.shutdown();

logger.debug("End break down large geometry");
return intersectedPolygons;
}

Expand Down
4 changes: 4 additions & 0 deletions indexer/src/main/resources/application-edge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ management:
web:
exposure:
include: "health,info,env,beans,logfile"

logging:
level:
au.org.aodn.ardcvocabs: DEBUG

0 comments on commit 4c41a2f

Please sign in to comment.