Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- added fauna plot to site
- site with features are now of compound type
- plot update will now create new site
  • Loading branch information
temi committed May 15, 2024
1 parent 8933698 commit dc6099e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 24 deletions.
10 changes: 10 additions & 0 deletions grails-app/domain/au/org/ala/ecodata/Site.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,14 @@ class Site {
status != Status.DELETED
}.find()
}

static List<Site> findAllByExternalId(ExternalId.IdType idType, String externalId, Map params) {
where {
externalIds {
idType == idType
externalId == externalId
}
status != Status.DELETED
}.list(params)
}
}
22 changes: 16 additions & 6 deletions grails-app/services/au/org/ala/ecodata/ParatooService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,10 @@ class ParatooService {
List features = geoJson?.features ?: []
geoJson.remove('features')
siteProps.features = features
siteProps.type = Site.TYPE_SURVEY_AREA
if (features)
siteProps.type = Site.TYPE_COMPOUND
else
siteProps.type = Site.TYPE_SURVEY_AREA
siteProps.publicationStatus = PublicationStatus.PUBLISHED
siteProps.projects = [project.projectId]
String externalId = geoJson.properties?.externalId
Expand All @@ -598,18 +601,19 @@ class ParatooService {
Site site
// create new site for every non-plot submission
if (config.usesPlotLayout) {
site = Site.findByExternalId(ExternalId.IdType.MONITOR_PLOT_GUID, externalId)
if (site?.features) {
siteProps.features?.addAll(site.features)
}
List sites = Site.findAllByExternalId(ExternalId.IdType.MONITOR_PLOT_GUID, externalId, [sort: "lastUpdated", order: "desc"])
if (sites)
site = sites.first()
}

Map result
if (!site) {
// If the plot layout has been updated, create a new site
if (!site || isUpdatedPlotLayout(site, observation, config)) {
result = siteService.create(siteProps)
} else {
result = [siteId: site.siteId]
}

if (result.error) {
// Don't treat this as a fatal error for the purposes of responding to the paratoo request
log.error("Error creating a site for survey " + collection.orgMintedUUID + ", project " + project.projectId + ": " + result.error)
Expand All @@ -619,6 +623,12 @@ class ParatooService {
[siteId:siteId, name:siteProps?.name]
}

private static boolean isUpdatedPlotLayout (Site site, Map observation, ParatooProtocolConfig config) {
Date localSiteUpdated = site.lastUpdated
Date plotLayoutUpdated = config.getPlotLayoutUpdatedAt(observation)
plotLayoutUpdated?.after(localSiteUpdated) ?: false
}

private Map syncParatooProtocols(List<Map> protocols) {
Map result = [errors: [], messages: []]
List guids = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import org.locationtech.jts.geom.Geometry
@Slf4j
@JsonIgnoreProperties(['metaClass', 'errors', 'expandoMetaClass'])
class ParatooProtocolConfig {

static final String FAUNA_PLOT = 'Fauna plot'
static final String CORE_PLOT = 'Core monitoring plot'
String name
String apiEndpoint
boolean usesPlotLayout = true
Expand All @@ -26,7 +27,9 @@ class ParatooProtocolConfig {
String plotVisitPath = 'plot_visit'
String plotLayoutPath = "${plotVisitPath}.plot_layout"
String plotLayoutIdPath = "${plotLayoutPath}.id"
String plotLayoutUpdatedAtPath = "${plotLayoutPath}.updatedAt"
String plotLayoutPointsPath = "${plotLayoutPath}.plot_points"
String faunaPlotPointPath = "${plotLayoutPath}.fauna_plot_point"
String plotSelectionPath = "${plotLayoutPath}.plot_selection"
String plotLayoutDimensionLabelPath = "${plotLayoutPath}.plot_dimensions.label"
String plotLayoutTypeLabelPath = "${plotLayoutPath}.plot_type.label"
Expand Down Expand Up @@ -79,6 +82,16 @@ class ParatooProtocolConfig {
return removeMilliseconds(date)
}

Date getPlotLayoutUpdatedAt(Map surveyData) {
def date = getProperty(surveyData, plotLayoutUpdatedAtPath)
if (!date) {
date = getPropertyFromSurvey(surveyData, plotLayoutUpdatedAtPath)
}

date = getFirst(date)
date ? DateUtil.parseWithMilliseconds(date) : null
}

Map getSurveyId(Map surveyData) {
if(surveyIdPath == null || surveyData == null) {
return null
Expand Down Expand Up @@ -203,7 +216,11 @@ class ParatooProtocolConfig {
geoJson = extractSiteDataFromPlotVisit(output)
// get list of all features associated with observation
if (geoJson && form && output) {
geoJson.features = extractFeatures(output, form)
List features = extractFeatures(output, form)
if (features) {
features.addAll(geoJson.features?:[])
geoJson = createConvexHullGeoJSON(features, geoJson.properties.name, geoJson.properties.externalId)
}
}
}
else if (geometryPath) {
Expand All @@ -212,20 +229,10 @@ class ParatooProtocolConfig {
else if (form && output) {
List features = extractFeatures(output, form)
if (features) {
List featureGeometries = features.collect { it.geometry }
Geometry geometry = GeometryUtils.getFeatureCollectionConvexHull(featureGeometries)
String startDateInString = getStartDate(output)
startDateInString = DateUtil.convertUTCDateToStringInTimeZone(startDateInString, clientTimeZone?:TimeZone.default)
String name = "${form.name} site - ${startDateInString}"
geoJson = [
type: 'Feature',
geometry: GeometryUtils.geometryToGeoJsonMap(geometry),
properties: [
name: name,
description: "${name} (convex hull of all features)",
],
features: features
]
geoJson = createConvexHullGeoJSON(features, name)
}
}

Expand Down Expand Up @@ -313,6 +320,8 @@ class ParatooProtocolConfig {
log.warn("No plot_layout found in survey at path ${plotLayoutIdPath}")
return null
}
else
plotLayoutId = plotLayoutId.toString()
List plotLayoutPoints = getProperty(surveyData, plotLayoutPointsPath)
Map plotSelection = getProperty(surveyData, plotSelectionPath)
Map plotSelectionGeoJson = plotSelectionToGeoJson(plotSelection)
Expand All @@ -322,16 +331,33 @@ class ParatooProtocolConfig {

String name = plotSelectionGeoJson.properties.name + ' - ' + plotLayoutTypeLabel + ' (' + plotLayoutDimensionLabel + ')'

Map plotGeoJson = createFeatureFromGeoJSON(plotLayoutPoints, name, plotLayoutId, plotSelectionGeoJson?.properties?.notes)

//Map faunaPlotGeoJson = toGeometry(plotLayout.fauna_plot_point)
Map plotGeoJson = createFeatureFromGeoJSON(plotLayoutPoints, name, plotLayoutId, "${CORE_PLOT} ${plotSelectionGeoJson?.properties?.notes?:""}")
List faunaPlotPoints = getProperty(surveyData, faunaPlotPointPath)
Map faunaPlotGeoJson = createFeatureFromGeoJSON(faunaPlotPoints, name, plotLayoutId, "${FAUNA_PLOT} ${plotSelectionGeoJson?.properties?.notes?:""}")

// TODO maybe turn this into a feature with properties to distinguish the fauna plot?
// Or a multi-polygon?
if (faunaPlotGeoJson) {
List features = [plotGeoJson, faunaPlotGeoJson]
plotGeoJson = createConvexHullGeoJSON(features, name, plotLayoutId)
}

plotGeoJson
}

static Map createConvexHullGeoJSON (List features, String name, String externalId = "") {
List featureGeometries = features.collect { it.geometry }
Geometry geometry = GeometryUtils.getFeatureCollectionConvexHull(featureGeometries)
[
type: 'Feature',
geometry: GeometryUtils.geometryToGeoJsonMap(geometry),
properties: [
name: name,
externalId: externalId,
description: "${name} (convex hull of all features)",
],
features: features
]
}

static Map createFeatureFromGeoJSON(List plotLayoutPoints, String name, def plotLayoutId, String notes = "") {
Map plotGeometry = toGeometry(plotLayoutPoints)
createFeatureObject(plotGeometry, name, plotLayoutId, notes)
Expand Down

0 comments on commit dc6099e

Please sign in to comment.