Skip to content

Commit

Permalink
#186 fix(outlier ws)
Browse files Browse the repository at this point in the history
  • Loading branch information
qifeng-bai committed Nov 29, 2021
1 parent 345dbd1 commit 02cc64c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import au.org.ala.layers.dto.Distribution
import au.org.ala.layers.dto.MapDTO
import au.org.ala.spatial.util.AttributionCache
import grails.converters.JSON
import groovy.json.JsonSlurper
import org.json.simple.JSONObject
import org.json.simple.parser.JSONParser
import org.json.simple.parser.ParseException
Expand Down Expand Up @@ -108,10 +109,10 @@ class DistributionController {
if (distributions != null && !distributions.isEmpty()) {
distributionsService.addImageUrl(distributions.get(0))
render distributions.get(0).toMap().findAll {
i -> i.value != null && "class" != i.key
i -> i.value != null && "class" != i.key
} as JSON
} else {
render(status: 404, text: 'no records for this lsid')
render text:[] as JSON
}
}

Expand All @@ -126,7 +127,7 @@ class DistributionController {
}
} as JSON
} else {
render(status: 404, text: 'no records for this lsid')
render text:[] as JSON
}
}

Expand Down Expand Up @@ -205,10 +206,12 @@ class DistributionController {
} finally {
try {
input.close()
} catch (err) {}
} catch (err) {
}
try {
response.outputStream.close()
} catch (err) {}
} catch (err) {
}
}
}

Expand All @@ -223,42 +226,46 @@ class DistributionController {
* point's decimal latitude (with key "decimalLatitude") and
* decimal longitude (with key "decimalLongitude"). The decimal
* latitude and longitude values must be numbers.
*
* ALSO, the points can be in POST BODY
* @param response the http response
* @return A map containing the distance outside the expert distribution for
* each point which falls outside the area defined by the
* distribution. Keys are point ids, values are the distances
* @throws Exception
*/
def outliers(String lsid) {
log.info("Calculating EDL of " + lsid)
JSONObject pointsMap
def pointsJson = params.get('pointsJson', null)

if (pointsJson == null) {
render(status: 404, text: 'missing parameter pointsJson')
pointsMap = request.getJSON()
} else {
pointsMap = (JSONObject) new JSONParser().parse(pointsJson as String)
}

try {
JSONObject pointsMap = (JSONObject) new JSONParser().parse(pointsJson as String)

if (pointsMap == null) {
render(status: 400, text: 'missing parameter pointsJson / no points via post body')
}
//Check if it has EDL
List<Distribution> distributions = distributionDao.getDistributionByLSID([lsid] as String[], Distribution.EXPERT_DISTRIBUTION, true)
if (distributions.size() > 0) {
try {
Map outlierDistances = distributionDao.identifyOutlierPointsForDistribution(lsid, pointsMap,
Distribution.EXPERT_DISTRIBUTION)
render outlierDistances as JSON
} catch (IllegalArgumentException ex) {
} catch (ParseException | ClassCastException ex) {
log.error 'failed to get outliers', ex
render(status: 400, text: 'No expert distribution for species associated with supplied lsid')
return null
render(status: 400, text: 'Invalid JSON for point information')
return
}
} catch (ParseException ex) {
log.error 'failed to get outliers', ex
render(status: 400, text: 'Invalid JSON for point information')
return null
} catch (ClassCastException ex) {
log.error 'failed to get outliers', ex
render(status: 400, text: 'Invalid format for point information')
return null
} else {
render(status: 400, text: 'No expert distribution for species associated with supplied lsid')
}
}


def overviewMapPng(String geomIdx) {
map(geomIdx)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ class UrlMappings {
"/distribution/map/$lsid**?"(controller: "distribution", action: "lsidMapFirst")
"/distribution/map/png/$geomIdx(.$format)?"(controller: "distribution", action: "overviewMapPng")
"/distribution/map/seed(.$format)?"(controller: "distribution", action: "overviewMapSeed")
"/distribution/outliers/$outliers(.$format)?"(controller: "distribution", action: "outliers")
"/distribution/outliers/$lsid?"(controller: "distribution", action: "outliers")
"/distribution/map/lsid/$lsid**?"(controller: "distribution", action: "overviewMapPngLsid")
"/distribution/map/spcode/$spcode(.$format)?"(controller: "distribution", action: "overviewMapPngSpcode")
"/distribution/map/name/$name(.$format)?"(controller: "distribution", action: "overviewMapPngName")
"/distribution/identifyOutlierPointsForDistribution"(controller: "distribution", action: "identifyOutlierPointsForDistribution")
"/distribution/map/lsids/$lsid**?"(controller: "distribution", action: "lsidMaps")
"/distribution/$id(.$format)?"(controller: "distribution", action: "show")
"/distributions(.$format)?"(controller: "distribution", action: "index")
Expand Down

0 comments on commit 02cc64c

Please sign in to comment.