-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathAooEoo.groovy
341 lines (286 loc) · 13.6 KB
/
AooEoo.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* Copyright (C) 2016 Atlas of Living Australia
* All Rights Reserved.
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*/
package au.org.ala.spatial.process
import au.org.ala.spatial.dto.AreaInput
import au.org.ala.spatial.dto.SpeciesInput
import au.org.ala.spatial.util.SpatialConversionUtils
import au.org.ala.spatial.util.SpatialUtils
import grails.converters.JSON
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import org.geotools.kml.KML
import org.geotools.kml.KMLConfiguration
import org.geotools.xsd.Encoder
import org.locationtech.jts.geom.Geometry
import org.locationtech.jts.geom.GeometryFactory
import org.locationtech.jts.geom.LineSegment
import org.locationtech.jts.io.WKTReader
import org.locationtech.jts.triangulate.DelaunayTriangulationBuilder
import java.awt.geom.Point2D
@Slf4j
@CompileStatic
class AooEoo extends SlaveProcess {
void start() {
//grid size
def gridSize = getInput('resolution').toDouble()
//area to restrict
List<AreaInput> area = JSON.parse(getInput('area').toString()).collect { it as AreaInput } as List<AreaInput>
//number of target species
SpeciesInput species = JSON.parse(getInput('species').toString()) as SpeciesInput
// concave hull coverage parameter
def alpha = getInput('coverage').toDouble()
def radius = getInput('radius').toDouble()
def speciesArea = getSpeciesArea(species, area)
// eoo
taskLog("fetching unique coordinates")
def points = facet("lat_long", speciesArea)
if (points.size() < 3) {
throw new Exception("Fewer than 3 occurrences are found in the defined area.")
}
StringBuilder eWkt = new StringBuilder()
taskLog("building GEOMOETRYCOLLECTION of POINTs")
processPoints(points, eWkt)
taskLog("counting occurrences")
def occurrenceCount = occurrenceCount(speciesArea)
// aoo
taskLog("processing lat_long")
Set<Point2D> aooPoints = aooProcess(points, gridSize)
double aoo = gridSize * gridSize * aooPoints.size() * 10000
taskLog("building WKT grid")
String aooWkt = aooWkt(aooPoints, gridSize)
//radius for point circles size
taskLog("building WKT circles")
def circleWkt = circleRadiusProcess(aooPoints, radius)
double circleArea = SpatialUtils.calculateArea(circleWkt) / 1000000.0
double eoo
WKTReader reader = new WKTReader()
String metadata
try {
taskLog("calculating convex hull")
Geometry g = reader.read(eWkt.toString())
Geometry convexHull = g.convexHull()
String wkt = convexHull.toText().replace(" (", "(").replace(", ", ",")
eoo = SpatialUtils.calculateArea(wkt) / 1000000.0
//aoo area
taskLog("calculating WKT grid union")
Geometry a = reader.read(aooWkt)
Geometry aUnion = a.union()
String aWkt = aUnion.toText().replace(" (", "(").replace(", ", ",")
//concave hull
taskLog("calculating concave hull")
Geometry concaveHull = buildConcaveHull(g, alpha)
Double alphaHull = null
String concaveWkt = null
if (concaveHull == null) {
taskLog("Unable to produce alpha hull")
} else {
concaveWkt = concaveHull.toText().replace(" (", "(").replace(", ", ",")
alphaHull = SpatialUtils.calculateArea(concaveWkt) / 1000000.0
}
taskLog("generating output files")
if (eoo > 0) {
def filename = "Extent of occurrence.wkt"
new File(getTaskPath() + filename).write(wkt)
def values = [file : "Extent of occurrence.wkt",
name : "Extent of occurrence (area): " + species.name,
description: "Created by AOO and EOO Tool"]
addOutput("areas", (values as JSON).toString(), true)
addOutput("files", filename, true)
filename = "Area of occupancy.wkt"
new File(getTaskPath() + filename).write(aWkt)
values = [file : "Area of occupancy.wkt", name: "Area of occupancy (area): " + species.name,
description: "Created by AOO and EOO Tool"]
addOutput("areas", (values as JSON).toString(), true)
addOutput("files", filename, true)
if (alphaHull != null) {
filename = "Alpha Hull.wkt"
new File(getTaskPath() + filename).write(concaveWkt)
values = [file : "Alpha Hull.wkt", name: "Alpha Hull: " + species.name,
description: "Created by AOO and EOO Tool"]
addOutput("areas", (values as JSON).toString(), true)
addOutput("files", filename, true)
}
filename = "Point Radius.wkt"
new File(getTaskPath() + filename).write(circleWkt)
values = [file : "Point Radius.wkt", name: "Point Radius: " + species.name,
description: "Created by AOO and EOO Tool"]
addOutput("areas", (values as JSON).toString(), true)
addOutput("files", filename, true)
metadata = '<html><body>' +
'<div class="aooeoo">' +
'<div>The Sensitive Data Service may have changed the location of taxa that have a sensitive status.' +
' It is wise to first map the taxa and examine each record, then filter these records to create the ' +
'desired subset, then run the tool on the new filtered taxa layer.</div><br />' +
'<table >' +
'<tr><td>Number of records used for the calculations</td><td>' + occurrenceCount + "</td></tr>" +
'<tr><td>Species</td><td>' + species.name + '</td></tr>' +
"<tr><td>Area of Occupancy (AOO: ${gridSize} degree grid)</td><td>" + String.format(Locale.US, '%.0f', aoo) + ' sq km</td></tr>' +
'<tr><td>Area of Occupancy (Points with radius: ' + String.format(Locale.US, "%.0f", radius) + 'm)</td><td>' + String.format(Locale.US, '%.0f', circleArea) + ' sq km</td></tr>' +
'<tr><td>Extent of Occurrence (EOO: Minimum convex hull)</td><td>' + (String.format(Locale.US, '%.0f', eoo)) + ' sq km</td></tr>' +
((alphaHull != null) ? "<tr><td>Alpha Hull (Alpha: ${alpha})</td><td>" + String.format(Locale.US, '%.0f', alphaHull) + ' sq km</td></tr>' : "") +
'</table></body></html>' +
'</div>'
new File(getTaskPath() + 'Calculated AOO and EOO.html').write(metadata)
addOutput('metadata', 'Calculated AOO and EOO.html', true)
//export areas in kml format
filename = 'Extent of occurrence.kml'
writeWktAsKmlToFile(new File(getTaskPath() + filename), wkt)
addOutput('files', filename, true)
filename = 'Area of occupancy.kml'
writeWktAsKmlToFile(new File(getTaskPath() + filename), aWkt)
addOutput('files', filename, true)
if (alphaHull != null) {
filename = 'Alpha Hull.kml'
writeWktAsKmlToFile(new File(getTaskPath() + filename), concaveWkt)
addOutput('files', filename, true)
}
filename = 'Point Radius.kml'
writeWktAsKmlToFile(new File(getTaskPath() + filename), circleWkt)
addOutput('files', filename, true)
} else {
taskLog('Error: extent of occurrences is 0')
log.error 'Extent of occurrences is 0.'
}
} catch (err) {
taskLog('Error: failed to calculate AOO and EOO')
log.error 'failed to calculate aoo eoo ' + taskWrapper.id, err
throw new Exception('AooEoo:' + taskWrapper.id + ' failed!', err)
}
}
static void writeWktAsKmlToFile(File file, String wkt) {
String header = '<?xml version=\"1.0\" encoding=\"UTF-8\"?><kml xmlns=\"http://earth.google.com/kml/2.2\"><Document> <name></name> <description></description> <Style id=\"style1\"> <LineStyle> <color>40000000</color> <width>3</width> </LineStyle> <PolyStyle> <color>73FF0000</color> <fill>1</fill> <outline>1</outline> </PolyStyle> </Style> <Placemark> <name></name> <description></description> <styleUrl>#style1</styleUrl>'
Encoder encoder = new Encoder(new KMLConfiguration())
encoder.setIndenting(true)
encoder.setOmitXMLDeclaration(true) //Omit duplication xml declaration.
WKTReader reader = new WKTReader()
String kml = encoder.encodeAsString(reader.read(wkt), KML.Geometry)
String footer = '</Placemark></Document></kml>'
file.write(header + kml + footer)
}
static int processPoints(String [] points, StringBuilder sb) {
int pointCount = 0
sb.append('GEOMETRYCOLLECTION(')
points.each { point ->
try {
//point=latitude,longitude
String[] ll = point.replace('\"', '').split(',')
String s = String.format(Locale.US, 'POINT(%.6f %.6f)', Double.parseDouble(ll[1]), Double.parseDouble(ll[0]))
if (pointCount > 0) {
sb.append(',')
}
sb.append(s)
pointCount++
} catch (Exception ignored) {
}
}
sb.append(')')
return pointCount
}
private static Set<Point2D> aooProcess(String [] points, double gridSize) {
Set<Point2D> set = new HashSet<Point2D>()
points.each { point ->
try {
//key=latitude,longitude
String[] ll = point.replace('\"', '').split(',')
Point2D pt = new Point2D.Float(round(Double.parseDouble(ll[1]), gridSize),
round(Double.parseDouble(ll[0]), gridSize))
set.add(pt)
} catch (Exception ignored) {
}
}
set
}
// sq km
private static String circleRadiusProcess(Set<Point2D> points, double radius) {
Geometry geom = null
points.each { point ->
WKTReader wkt = new WKTReader()
Geometry circle = wkt.read(SpatialConversionUtils.createCircleJs(point.x, point.y, radius))
if (geom == null) {
geom = circle
} else {
geom = geom.union(circle)
}
}
geom.toString()
}
static float round(double d, double by) {
long l = (long) (d / by)
return (float) (l * by + (l < 0 ? -by : 0))
}
static String aooWkt(Set<Point2D> pointSet, Double gridsize) {
int pointCount = 0
StringBuilder sb = new StringBuilder()
sb.append('MULTIPOLYGON(')
pointSet.each { point ->
//key=latitude,longitude
float x = (float) point.x
float y = (float) point.y
String s = String.format(Locale.US, '((%.6f %.6f,%.6f %.6f,%.6f %.6f,%.6f %.6f,%.6f %.6f))',
x, y,
x, y + gridsize,
x + gridsize, y + gridsize,
x + gridsize, y,
x, y)
if (pointCount > 0) {
sb.append(',')
}
sb.append(s)
pointCount++
}
sb.append(')')
return sb.toString()
}
/**
* Alpha hull
* 1. Connect all points with Delaunay Triangulation
* 2. Calculate mean edge length
* 3. Remove all triangles that have an edge with length > mean length * alpha
* 4. Return geometry
*
* @param geometry
* @param alpha
* @return
*/
static Geometry buildConcaveHull(Geometry geometry, Double alpha) {
DelaunayTriangulationBuilder triangulation = new DelaunayTriangulationBuilder()
triangulation.setSites(geometry)
Geometry triangles = triangulation.getTriangles(new GeometryFactory())
Geometry edges = triangulation.getEdges(new GeometryFactory())
//get mean edge length
int sum = 0
for (int i = 0; i < edges.numGeometries; i++) {
sum += (int) edges.getGeometryN(i).length
}
double meanByAlpha = sum / (edges.numGeometries * alpha)
//remove triangles with at least one edge length > meanByAlpha
Geometry union = null
for (int i = 0; i < triangles.numGeometries; i++) {
Geometry triangle = triangles.getGeometryN(i)
boolean valid = true
for (int j = 1; j < 3 && valid; j++) {
if (new LineSegment(triangle.coordinates[j], triangle.coordinates[j - 1]).length > meanByAlpha) {
valid = false
}
}
if (valid) {
if (union == null) union = triangle
else union = union.union(triangle)
}
}
//return geometry
return union
}
}