Skip to content

Commit

Permalink
#230 fix (wkt parsing and possible dead loop in grid cutting
Browse files Browse the repository at this point in the history
  • Loading branch information
qifeng-bai committed Jan 29, 2024
1 parent f71f2e9 commit 2752d32
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
10 changes: 6 additions & 4 deletions src/main/groovy/au/org/ala/spatial/intersect/SimpleRegion.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> layers = getInput('layer').toString().split(',')
def envnameslist = new String[layers.size()]
Expand All @@ -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()]
Expand Down
10 changes: 6 additions & 4 deletions src/main/groovy/au/org/ala/spatial/process/SlaveProcess.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -938,7 +940,7 @@ class SlaveProcess {
}
}
}

log.debug("Writing grids to " + dir + layer)
grid.writeGrid(dir + layer, dfiltered,
extents[0][0],
extents[0][1],
Expand Down

0 comments on commit 2752d32

Please sign in to comment.