Skip to content

Commit

Permalink
Don't double parse spatial pids #929
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisala committed Jun 13, 2024
1 parent c31dfb3 commit 7790a79
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions grails-app/services/au/org/ala/ecodata/SpatialService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import grails.core.GrailsApplication
import grails.plugin.cache.Cacheable
import groovy.json.JsonParserType
import groovy.json.JsonSlurper
import org.geotools.geojson.geom.GeometryJSON
import org.locationtech.jts.geom.Coordinate
import org.locationtech.jts.geom.Geometry
import org.locationtech.jts.geom.GeometryFactory

import static ParatooService.deepCopy
/**
* The SpatialService is responsible for:
Expand Down Expand Up @@ -140,13 +144,14 @@ class SpatialService {
if (boundaryPid) {
// Get geoJSON of the object stored in spatial portal
long start = System.currentTimeMillis()
def boundaryGeoJson = getGeoJsonForPidToMap(boundaryPid)
// def boundaryGeoJson = getGeoJsonForPidToMap(boundaryPid)
// long end = System.currentTimeMillis()
// log.debug("Time taken to get geojson for pid $boundaryPid: ${end-start}ms")
//
// start = end
// Geometry boundaryGeometry = GeometryUtils.geoJsonMapToGeometry(boundaryGeoJson)
Geometry boundaryGeometry = getGeoJsonForPidToGeometry(boundaryPid)
long end = System.currentTimeMillis()
log.debug("Time taken to get geojson for pid $boundaryPid: ${end-start}ms")

start = end
Geometry boundaryGeometry = GeometryUtils.geoJsonMapToGeometry(boundaryGeoJson)
end = System.currentTimeMillis()
log.debug("Time taken to convert geojson to geometry for pid $boundaryPid: ${end-start}ms")

if (boundaryGeometry.isValid()) {
Expand Down Expand Up @@ -235,6 +240,25 @@ class SpatialService {
getGeoJsonForPid(pid)
}

@Cacheable(value = "spatialGeoJsonPidObjectGeometry", key={pid})
Geometry getGeoJsonForPidToGeometry(String pid) {
log.debug("Cache miss for getGeoJsonForPidToMap($pid)")
log.debug("Cache miss for getGeoJsonForPid($pid)")
String url = grailsApplication.config.getProperty('spatial.baseUrl')+"/ws/shapes/geojson/$pid"
String jsonText = webService.get(url)

Geometry geometry = null
try {
geometry = new GeometryJSON().read(jsonText)
log.info("*************************************Successfully created geometry for pid $pid")
}
catch (Exception e) {
log.error("Error reading geometry for pid $pid")
geometry = new GeometryFactory().createPoint(new Coordinate(0, 0))
}
geometry
}

/**
* Get GeoJSON of a spatial object.
* @param pid
Expand Down

0 comments on commit 7790a79

Please sign in to comment.