From 02cc64c0c8031836475156e7c5dcf051bfb906a6 Mon Sep 17 00:00:00 2001 From: Qifeng Date: Mon, 29 Nov 2021 16:18:02 +1100 Subject: [PATCH] #186 fix(outlier ws) --- .../ala/layers/DistributionController.groovy | 47 +++++++++++-------- .../ala/spatial/service/UrlMappings.groovy | 3 +- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/grails-app/controllers/au/org/ala/layers/DistributionController.groovy b/grails-app/controllers/au/org/ala/layers/DistributionController.groovy index 2ec8553f..4761b10c 100644 --- a/grails-app/controllers/au/org/ala/layers/DistributionController.groovy +++ b/grails-app/controllers/au/org/ala/layers/DistributionController.groovy @@ -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 @@ -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 } } @@ -126,7 +127,7 @@ class DistributionController { } } as JSON } else { - render(status: 404, text: 'no records for this lsid') + render text:[] as JSON } } @@ -205,10 +206,12 @@ class DistributionController { } finally { try { input.close() - } catch (err) {} + } catch (err) { + } try { response.outputStream.close() - } catch (err) {} + } catch (err) { + } } } @@ -223,6 +226,8 @@ 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 @@ -230,35 +235,37 @@ class DistributionController { * @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 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) } diff --git a/grails-app/controllers/au/org/ala/spatial/service/UrlMappings.groovy b/grails-app/controllers/au/org/ala/spatial/service/UrlMappings.groovy index 3a6fc154..22619b9b 100644 --- a/grails-app/controllers/au/org/ala/spatial/service/UrlMappings.groovy +++ b/grails-app/controllers/au/org/ala/spatial/service/UrlMappings.groovy @@ -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")