From 2752d3204607120e9826c214ada2730177ea0ca6 Mon Sep 17 00:00:00 2001 From: QBai <31238531+qifeng-bai@users.noreply.github.com> Date: Tue, 30 Jan 2024 09:48:27 +1100 Subject: [PATCH] #230 fix (wkt parsing and possible dead loop in grid cutting --- build.gradle | 3 ++- .../au/org/ala/spatial/intersect/SimpleRegion.groovy | 10 ++++++---- .../org/ala/spatial/intersect/SimpleShapeFile.groovy | 5 ++++- .../au/org/ala/spatial/process/Classification.groovy | 5 +++-- .../au/org/ala/spatial/process/SlaveProcess.groovy | 10 ++++++---- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/build.gradle b/build.gradle index d6b7f62..20bfdc2 100644 --- a/build.gradle +++ b/build.gradle @@ -28,7 +28,7 @@ ext { drivers = ["firefox", "chrome", "chromeHeadless"] } -version "2.1.1-SNAPSHOT" +version "2.1.2-SNAPSHOT" group "au.org.ala" apply plugin: "eclipse" @@ -107,6 +107,7 @@ dependencies { implementation "org.hibernate:hibernate-spatial" implementation "org.grails.plugins:gsp" implementation "io.micronaut:micronaut-inject-groovy" + console "org.grails:grails-console" profile "org.grails.profiles:web" runtimeOnly "com.h2database:h2" diff --git a/src/main/groovy/au/org/ala/spatial/intersect/SimpleRegion.groovy b/src/main/groovy/au/org/ala/spatial/intersect/SimpleRegion.groovy index 01bcb8d..22ccb71 100644 --- a/src/main/groovy/au/org/ala/spatial/intersect/SimpleRegion.groovy +++ b/src/main/groovy/au/org/ala/spatial/intersect/SimpleRegion.groovy @@ -198,6 +198,7 @@ class SimpleRegion implements Serializable { for (int i = 0; i < points.size(); i++) { pointsArray[i] = points.get(i) } + log.debug("Calculating BBox of a polygon") simpleregion.setPolygon(pointsArray) return simpleregion } @@ -327,7 +328,6 @@ class SimpleRegion implements Serializable { points[i] = points_[i] } } - /* bounding box setup */ bounding_box = new double[2][2] bounding_box[0][0] = points[0] @@ -963,7 +963,7 @@ class SimpleRegion implements Serializable { if (three_state_map == null) { three_state_map = new byte[height][width] } - + log.debug("Calculating overlapped grids over polygons") double divx = (longitude2 - longitude1) / width double divy = (latitude2 - latitude1) / height @@ -1018,7 +1018,8 @@ class SimpleRegion implements Serializable { } } else { //sloped line endlat = dy2 - for (double k = (y + 1) * divy + latitude1; k < endlat; k += (int)(divy)) { + int kStep = Math.max(divy, 1) + for (double k = (y + 1) * divy + latitude1; k < endlat; k += kStep) { //move in yDirection to get x xcross = (k - intercept) / slope icross = (int) ((xcross - longitude1) / divx) @@ -1140,7 +1141,8 @@ class SimpleRegion implements Serializable { } } else { //sloped line endlat = dy2 - for (double k = (y + 1) * divy + latitude1; k < endlat; k += (int)(divy)) { + int kStep = Math.max(divy, 1) + for (double k = (y + 1) * divy + latitude1; k < endlat; k += kStep) { //move in yDirection to get x xcross = (k - intercept) / slope icross = (int) ((xcross - longitude1) / divx) diff --git a/src/main/groovy/au/org/ala/spatial/intersect/SimpleShapeFile.groovy b/src/main/groovy/au/org/ala/spatial/intersect/SimpleShapeFile.groovy index 05f1d32..815c22e 100644 --- a/src/main/groovy/au/org/ala/spatial/intersect/SimpleShapeFile.groovy +++ b/src/main/groovy/au/org/ala/spatial/intersect/SimpleShapeFile.groovy @@ -29,6 +29,9 @@ import java.util.concurrent.CountDownLatch import java.util.concurrent.LinkedBlockingQueue import java.util.stream.Collectors +import com.vividsolutions.jts.geom.*; +import com.vividsolutions.jts.io.ParseException; +import com.vividsolutions.jts.io.WKTReader; /** * SimpleShapeFile is a representation of a Shape File for * intersections with points @@ -215,7 +218,7 @@ class SimpleShapeFile implements Serializable { } else if (pointsString.startsWith("MULTIPOLYGON")) { regions.addAll(parseMultipolygon(pointsString.substring(bracket3rd + 1, pointsString.length() - 3))) } else if (pointsString.startsWith("POLYGON")) { - regions.add(parsePolygon(pointsString.substring(bracket2nd + 1, pointsString.length() - 2))) + regions.add(parsePolygon(pointsString.substring(bracket2nd + 1, pointsString.length() - 2))) } if (regions.size() == 0) { diff --git a/src/main/groovy/au/org/ala/spatial/process/Classification.groovy b/src/main/groovy/au/org/ala/spatial/process/Classification.groovy index 54e58a6..c2596d4 100644 --- a/src/main/groovy/au/org/ala/spatial/process/Classification.groovy +++ b/src/main/groovy/au/org/ala/spatial/process/Classification.groovy @@ -27,7 +27,7 @@ import org.apache.commons.io.FileUtils class Classification extends SlaveProcess { void start() { - taskLog("Classification is about to start") + taskLog("Starting Classification") //list of layers List layers = getInput('layer').toString().split(',') def envnameslist = new String[layers.size()] @@ -49,9 +49,10 @@ class Classification extends SlaveProcess { def makeShapefile = getInput('shp') new File(getTaskPath()).mkdirs() - + taskLog("Cutting Grids") def cutDataPath = cutGrid(envnameslist, resolution as String, regionEnvelope.region, regionEnvelope.envelope, null) + taskLog("Running aloc process") String[] cmd = ["java", "-Xmx" + String.valueOf(spatialConfig.aloc.xmx), "-jar", spatialConfig.data.dir + '/modelling/aloc/aloc.jar', cutDataPath, String.valueOf(groups), String.valueOf(spatialConfig.aloc.threads), getTaskPath()] diff --git a/src/main/groovy/au/org/ala/spatial/process/SlaveProcess.groovy b/src/main/groovy/au/org/ala/spatial/process/SlaveProcess.groovy index 9322b39..0702142 100644 --- a/src/main/groovy/au/org/ala/spatial/process/SlaveProcess.groovy +++ b/src/main/groovy/au/org/ala/spatial/process/SlaveProcess.groovy @@ -572,6 +572,7 @@ class SlaveProcess { h = (int) Math.ceil((extents[1][1] - extents[0][1]) / res) w = (int) Math.ceil((extents[1][0] - extents[0][0]) / res) + log.debug("Calculating mask") mask = getRegionMask(extents, w, h, region) } else if (envelopes != null) { h = (int) Math.ceil((extents[1][1] - extents[0][1]) / res) @@ -646,7 +647,7 @@ class SlaveProcess { String standardLayersDir = spatialConfig.data.dir + '/standard_layer/' File file = new File(standardLayersDir + resolution + '/' + layer + '.grd') - log.debug("Get grid from: " + file.path) + log.debug("loading grid from: " + file.path) //move up a resolution when the file does not exist at the target resolution try { while (!file.exists()) { @@ -677,12 +678,13 @@ class SlaveProcess { } String layerPath = standardLayersDir + File.separator + resolution + File.separator + layer + taskLog("Loading " + layerPath) if (new File(layerPath + ".grd").exists()) { return layerPath } else { - taskLog("Fatal error: Cannot calcuate grid due to missing the layer file: " + layerPath) - log.error("Fatal error: Cannot calcuate grid due to missing the layer file: " + layerPath) + taskLog("Fatal error: Cannot calculate grid due to missing the layer file: " + layerPath) + log.error("Fatal error: Cannot calculate grid due to missing the layer file: " + layerPath) return null } } @@ -938,7 +940,7 @@ class SlaveProcess { } } } - + log.debug("Writing grids to " + dir + layer) grid.writeGrid(dir + layer, dfiltered, extents[0][0], extents[0][1],