Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various #267

Merged
merged 4 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions grails-app/controllers/au/org/ala/spatial/ShapesController.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,9 @@ class ShapesController {
WKTReader wktReader = new WKTReader()
Geometry geom = wktReader.read(wkt.toString())

// union all the polygons in a MULTIPOLYGON, if any
geom = geom.union()

// Use CCW for exterior rings. Normalizing will use the JTS default (CW). Reverse makes it CCW.
Geometry validGeom = GeomMakeValid.makeValid(geom)
validGeom.normalize()
Expand All @@ -1059,27 +1062,8 @@ class ShapesController {
return filename.replaceAll("[^a-zA-Z0-9\\(\\)\\[\\]\\-]", "_")
}

private static cleanObjectId(String id) {
private static String cleanObjectId(String id) {
String.valueOf(Long.valueOf(id))
}

// requestBody schemas
// class UploadWkt {
// String wkt
// String name
// String description
// String user_id
// }
//
// class UploadGeoJSON {
// String name
// String description
// String user_id
// Map geojson
// }
//
// class UploadFeatures {
// List<String> featureIndex
// }
}

52 changes: 10 additions & 42 deletions grails-app/controllers/au/org/ala/spatial/WorkflowController.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package au.org.ala.spatial

import au.ala.org.ws.security.RequireApiKey
import au.org.ala.web.AuthService
import grails.converters.JSON

Expand All @@ -41,6 +42,7 @@ class WorkflowController {
* @param id
* @return
*/
@RequireApiKey
def save(Long id) {
String user_id = authService.getUserId()
def data = request.JSON
Expand All @@ -49,57 +51,25 @@ class WorkflowController {
def description = data['description']?.toString()
def metadata = data['metadata']?.toString()

def header

String errorMsg
if ("true".equalsIgnoreCase(data['doi']?.toString())) {
// test for minimum data for a DOI
errorMsg = getErrorForDoi(data)

if (!errorMsg) {
// analysis_id is used to hold the minted value. This makes it readonly.
header = userDataService.put(user_id, RECORD_TYPE, description, metadata, isPublic, null)
if (header?.ud_header_id) {
userDataService.update(header.ud_header_id, user_id, RECORD_TYPE, description, metadata, isPublic, header.ud_header_id.toString())
}
}
} else {
header = userDataService.put(user_id, RECORD_TYPE, description, metadata, isPublic, null)
}

header = mapping(header)
def header = mapping(userDataService.put(user_id, RECORD_TYPE, description, metadata, isPublic, null))

def result
if (errorMsg) {
result = false
def map = [successful: result, message: errorMsg]
render map as JSON
} else if (header) {
if (header) {
result = true
def map = [successful: result, data: header]
render map as JSON
} else {
result = false
errorMsg = "Failed to save workflow"
def map = [successful: result, message: errorMsg]
def map = [successful: result, message: "Failed to save workflow"]
render map as JSON
}
}

def doi(String id) {
List list = userDataService.searchDescAndTypeOr(null, RECORD_TYPE, null, null, id, 0, 1)

if (list.size() > 0) {
show(list.get(0).ud_header_id)
} else {
render status: HttpURLConnection.HTTP_NOT_FOUND
}
}

@RequireApiKey
def show(Long id) {
String user_id = authService.getUserId()

def item = userDataService.get(id)
def item = UDHeader.get(id)

if (!item) {
render status: 404
Expand Down Expand Up @@ -128,10 +98,11 @@ class WorkflowController {
}
}

@RequireApiKey
def delete(Long id) {
String user_id = authService.getUserId()

def metadata = userDataService.get(id)
def metadata = UDHeader.get(id)

// check authorisation and that it is not minted (no analysis_id)
if ((metadata.user_id == user_id ||
Expand All @@ -148,6 +119,7 @@ class WorkflowController {
}
}

@RequireApiKey
def search() {
String user_id = authService.getUserId()

Expand All @@ -161,10 +133,6 @@ class WorkflowController {
render formattedList as JSON
}

private def getErrorForDoi(data) {
return ""
}

private def mapping(header) {
return [id : header.ud_header_id, mintId: header.analysis_id, name: header.description,
userId : header.user_id, isPrivate: !PUBLIC.equalsIgnoreCase(header.data_path),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,10 @@ class TabulationService {
//is it grid as contextual layer?
IntersectionFile f = layerService.getIntersectionFile(fid)

if (f == null) {
return []
}

if (f.getType().equalsIgnoreCase("c")) {
if (wkt.length() > 0) {
String sql
Expand Down
91 changes: 71 additions & 20 deletions grails-app/services/au/org/ala/spatial/UserDataService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,85 @@ package au.org.ala.spatial
* A small cache for data resource attribution.
* This should be revisited if this cache grows or needs regular refreshing.
*/
//@CompileStatic
class UserDataService {

void put(String s1, String s2, String s3, String s4, String s5, String o) {







UDHeader put(String user_id, String record_type, String description, String metadata, String data_path, String analysis_id) {
Date upload_dt = new Date(System.currentTimeMillis())
UDHeader udHeader = new UDHeader(
user_id: user_id,
record_type: record_type,
description: description,
metadata: metadata,
data_path: data_path,
analysis_id: analysis_id,
upload_dt: upload_dt
)

UDHeader.withTransaction {
if (!udHeader.save(flush: true)) {
udHeader.errors.allErrors.each { error ->
log.error("Error saving UDHeader: ${error}")
}
return null
}
}

return udHeader
}


void update(Object o, String s1, String s2, String s3, String s4, String s5, String s6) {




UDHeader update(Long ud_header_id, String user_id, String record_type, String description, String metadata, String data_path, String analysis_id) {
UDHeader.withTransaction {
UDHeader udHeader = UDHeader.get(ud_header_id)
if (udHeader != null) {
udHeader.user_id = user_id
udHeader.record_type = record_type
udHeader.description = description
udHeader.metadata = metadata
udHeader.data_path = data_path
udHeader.analysis_id = analysis_id
udHeader.upload_dt = new Date(System.currentTimeMillis())

if (!udHeader.save(flush: true)) {
udHeader.errors.allErrors.each { error ->
log.error("Error saving UDHeader: ${error}")
}
return null
}
}

return udHeader
}
}

Object searchDescAndTypeOr(String o1, String s1, String o2, String o3, String s2, int integer1, int integer2) {}
boolean delete(Long ud_header_id) {
UDHeader.withTransaction {
UDHeader.get(ud_header_id).delete()
}

Object get(long aLong) {
null
return true
}

void delete(long aLong) {

List<UDHeader> searchDescAndTypeOr(String desc, String record_type, String user_id, String data_path, String analysis_id, int start, int limit) {
def criteria = UDHeader.createCriteria()
def results = criteria.list(max: limit, offset: start) {
if (desc) {
ilike('description', desc)
}
if (record_type) {
eq('record_type', record_type)
}
or {
if (user_id) {
eq('user_id', user_id)
}
if (data_path) {
eq('data_path', data_path)
}
if (analysis_id) {
eq('analysis_id', analysis_id)
}
}
}
return results
}
}
12 changes: 11 additions & 1 deletion src/main/groovy/au/org/ala/spatial/StreamGobbler.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,17 @@ class StreamGobbler extends Thread {
String line
while ((line = br.readLine()) != null) {
if (taskWrapper != null) {
taskWrapper.task.history.put(System.currentTimeMillis() as String, logPrefix + ": " + line)
// surpress some JDK logging
if (!line.startsWith('INFO:') && !line.startsWith('NOTE:') && !line.contains('java.')) {
String str = logPrefix + ": " + line

// trim str to max 250 characters
if (str.length() > 250) {
str = str.substring(0, 250)
}

taskWrapper.task.history.put(System.currentTimeMillis() as String, str)
}
}
if (stringBuffer != null) {
stringBuffer.append(line).append('\n');
Expand Down
2 changes: 2 additions & 0 deletions src/main/groovy/au/org/ala/spatial/Util.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ class Util {
s.q.addAll(json.fq)
}
s.wkt = json.wkt

return s;
}

static String[] getDistributionsOrChecklists(List<Distributions> ja) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ class Classification extends SlaveProcess {
taskLog("Cutting Grids")
def cutDataPath = cutGrid(envnameslist, resolution as String, regionEnvelope.region, regionEnvelope.envelope, null)

File tmpDir = new File(getTaskPath() + "/tmp/")

taskLog("Running aloc process")
String[] cmd = ["java", "-Xmx" + String.valueOf(spatialConfig.aloc.xmx),
"-Djava.util.prefs.userRoot=" + getTaskPath() + "/tmp/",
"-jar", spatialConfig.data.dir + '/modelling/aloc/aloc.jar',
cutDataPath, String.valueOf(groups), String.valueOf(spatialConfig.aloc.threads), getTaskPath()]

Expand Down Expand Up @@ -130,5 +133,7 @@ class Classification extends SlaveProcess {
Util.replaceTextInFile(getTaskPath() + "classification.html", replaceMap)
addOutput("metadata", "classification.html", true)
}

tmpDir.delete()
}
}
4 changes: 4 additions & 0 deletions src/main/groovy/au/org/ala/spatial/process/Maxent.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Maxent extends SlaveProcess {
}
taskLog("Run Maxent model")
def cmd = ["java", "-mx" + String.valueOf(spatialConfig.maxent.mx),
"-Djava.util.prefs.userRoot=" + getTaskPath() + "/tmp/",
"-jar", spatialConfig.data.dir + '/modelling/maxent/maxent.jar',
"-e", cutDataPath, "-s", speciesPath.get(0), "-a", "tooltips=false",
"nowarnings", "noprefixes", "-z",
Expand Down Expand Up @@ -212,6 +213,9 @@ class Maxent extends SlaveProcess {
FileUtils.moveFile(new File(getTaskPath() + "_species.gri"), target)
addOutput("layers", "/layer/" + taskWrapper.id + "_species.gri")
}

File tmpDir = new File(getTaskPath() + "/tmp/")
FileUtils.deleteDirectory(tmpDir)
}

static def writeMaxentsld(filename) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ class SlaveProcess {
runCmd(cmd.toArray(new String[cmd.size()]), false, spatialConfig.admin.timeout)

} catch (Exception e) {
e.printStackTrace()
log.error("Failed to convert asc to grd: " + e.getMessage(), e)
}
}

Expand Down