Skip to content

Commit

Permalink
Merge pull request #1020 from AtlasOfLivingAustralia/feature/issue3292
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisala authored Sep 24, 2024
2 parents 678e6d2 + 8bcc854 commit 88d2bfe
Showing 1 changed file with 41 additions and 23 deletions.
64 changes: 41 additions & 23 deletions grails-app/services/au/org/ala/ecodata/SpatialService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ class SpatialService {
}
else {
def response = getPidFidIntersection(url, fid, pid)
if (response instanceof List) {
result[fid] = response
}
result[fid] = response
}
}

Expand All @@ -161,8 +159,18 @@ class SpatialService {
}

@Cacheable(value = "spatialPidFidIntersection")
def getPidFidIntersection(String url, String fid, String pid) {
webService.getJson(url + fid + "/" + pid)
List getPidFidIntersection(String url, String fid, String pid) {
def intersections = webService.getJson(url + fid + "/" + pid)
if (intersections instanceof List) {
List intersectionsList = intersections.collect { obj ->
new HashMap(obj)
}
return intersectionsList
}
else {
log.info("No intersection for pid $pid and fid $fid")
return []
}
}

private List filterOutObjectsInBoundary(Map response, Map mainObjectGeoJson) {
Expand All @@ -180,8 +188,12 @@ class SpatialService {
private List filterOutObjectsInBoundary(Map response, Geometry mainGeometry) {
List checkForBoundaryIntersectionInLayers = metadataService.getGeographicConfig().checkForBoundaryIntersectionInLayers
if (!mainGeometry.isValid()) {
log.info("Main geometry invalid. Cannot check intersection is near boundary.")
return [response, [:]]
// fix invalid geometry
mainGeometry = mainGeometry.buffer(0)
if (!mainGeometry.isValid()) {
log.info("Main geometry invalid. Cannot check intersection is near boundary.")
return [response, [:]]
}
}
Map filteredResponse = [:]
Map intersectionAreaByFacets = [:].withDefault { [:] }
Expand All @@ -200,22 +212,27 @@ class SpatialService {
long end = System.currentTimeMillis()
log.debug("Time taken to convert geojson to geometry for pid $boundaryPid: ${end - start}ms")

if (boundaryGeometry.isValid()) {
// check if intersection should be ignored
start = end
if (isValidGeometryIntersection(mainGeometry, boundaryGeometry)) {
filteredResponse[fid].add(obj)
def (intersectionAreaOfMainGeometry, area) = getIntersectionProportionAndArea(mainGeometry, boundaryGeometry)
intersectionAreaByFacets[fid][obj.name] = area
} else {
log.debug("Filtered out ${obj.fieldname}(${fid}) - ${obj.name}")
if (!boundaryGeometry.isValid()) {
// fix invalid geometry
boundaryGeometry = boundaryGeometry.buffer(0)
if (!boundaryGeometry.isValid()) {
log.debug("Cannot check object $boundaryPid($fid) is near main geomerty")
return
}
}

end = System.currentTimeMillis()
log.debug("Time taken to check intersection for pid $boundaryPid: ${end - start}ms")
// check if intersection should be ignored
start = end
if (isValidGeometryIntersection(mainGeometry, boundaryGeometry)) {
filteredResponse[fid].add(obj)
def (intersectionAreaOfMainGeometry, area) = getIntersectionProportionAndArea(mainGeometry, boundaryGeometry)
intersectionAreaByFacets[fid][obj.name] = area
} else {
log.debug("Cannot check object $boundaryPid($fid) is near main geomerty")
log.debug("Filtered out ${obj.fieldname}(${fid}) - ${obj.name}")
}

end = System.currentTimeMillis()
log.debug("Time taken to check intersection for pid $boundaryPid: ${end - start}ms")
}
}
} else {
Expand Down Expand Up @@ -334,14 +351,15 @@ class SpatialService {
@Cacheable(value="spatialGeoJsonPid", key= {pid})
Map getGeoJsonForPid (String pid) {
log.debug("Cache miss for getGeoJsonForPid($pid)")
String url = grailsApplication.config.getProperty('spatial.baseUrl')+"/ws/shapes/geojson/$pid"
Map resp = webService.getJson(url)
// spatial returning invalid polygons when using geojson endpoint
String url = grailsApplication.config.getProperty('spatial.baseUrl')+"/ws/shapes/wkt/$pid"
def resp = webService.get(url)

if (resp.error) {
if (resp instanceof Map) {
return null
}

deepCopy(resp)
GeometryUtils.wktToGeoJson(resp)
}

/**
Expand Down

0 comments on commit 88d2bfe

Please sign in to comment.