-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
460 lines (432 loc) · 17.4 KB
/
index.js
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
Vorgewende
API Methods
lineString (poly, (options.maxAngle), (options.minCoordDistance), (options.debug)) - Returns array consisting of a GeoJSON lineString for each headland for the given field
Polygon (poly, (options.maxAngle), (options.minCoordDistance), (options.width))- Returns an array consisting of a GeoJSON Polygon for each headland using the working width as its width for the given field
*/
import * as turf from '@turf/turf'
import polygonClipping from 'polygon-clipping'
// import jsts from 'jsts'
const headland = {
lineStrings(poly, options = {}) {
if (!poly) throw new Error('No GeoJSON polygon passed to lineStrings function')
// define default options
options.maxAngle = options.maxAngle || 30
options.minCoordDistance = options.minCoordDistance || 10
let debugData = []
let holes = []
let lineStrings = []
let uncertainPoints = []
let curLine = []
let referencePoint = []
let lastPointIndex = 0
let lastPoint = []
let refAngle = 0
poly = turf.cleanCoords(poly)
// interpolate polygon to improve results
if (options.interpolate) poly = interpolate(poly, minCoordDistance)
poly.geometry.coordinates[0].forEach((currentCoord, coordIndex) => {
// always start a new lineString when the index is 0, or if a new featureIndex starts
if (coordIndex === 0 || !curLine) {
if (curLine.length) lineStrings.push(curLine)
curLine = [currentCoord]
lastPoint = currentCoord
lastPointIndex = coordIndex
return
}
const lastCoord = referencePoint.length ? referencePoint : lastPoint
// make sure there are no duplicates messing with the algorithm
const distToLastPoint = turf.distance(lastPoint, currentCoord) * 1000
if (currentCoord[0] === lastCoord[0] && currentCoord[1] === lastCoord[1]) {
// console.log('found duplicate');
return
} else if (distToLastPoint < 2) {
// console.log('found point closer than 2m');
return
}
// if this is the second point, calculate the angle between the first two
// points and save it as a reference point
if (curLine.length === 1) {
refAngle = turf.bearing(lastCoord, currentCoord) // angleCoords(lastCoord,currentCoord)
curLine.push(currentCoord)
lastPoint = currentCoord
lastPointIndex = coordIndex
return
}
// acquire all relevant info for the coordinate
const coord = getCoordDetails(currentCoord, lastCoord, refAngle, options.minCoordDistance, options.maxAngle)
// first case: minCoordDistance held = true, and turning angle exceeded
if (coord.aboveminCoordDistance && coord.exceedsTurnAngle) {
// console.log('First case');
// uncertain polys were already exceeding the turning angle,
// so they are part of the new headland
// only push the first point to the "old" headland as well
referencePoint = []
if (options.debug) debugData.push({
...coord,
lastPointIndex,
// uncertainPoints
type: 'Breakpoint'
})
if (uncertainPoints.length) {
lineStrings.push(curLine)
curLine = [curLine[curLine.length - 1], ...uncertainPoints, currentCoord]
refAngle = turf.bearing(uncertainPoints[0], currentCoord)
uncertainPoints = []
} else {
lineStrings.push(curLine)
refAngle = turf.bearing(lastPoint, currentCoord)
curLine = [lastPoint, currentCoord]
}
// second case, minCoordDistance held = true, but turning angle is not exceeded
} else if (coord.aboveminCoordDistance && !coord.exceedsTurnAngle) {
// console.log('Second case');
if (options.debug) debugData.push({
...coord,
lastPointIndex,
// uncertainPoints
type: 'Regular point'
})
if (uncertainPoints.length) {
curLine = curLine.concat(uncertainPoints)
uncertainPoints = []
}
referencePoint = []
refAngle = coord.angle
curLine.push(currentCoord)
// this point is uncertain, it could lead to a new headland, but might also
// belong to the current one, move on with caution
} else if (!coord.aboveminCoordDistance && coord.exceedsTurnAngle) {
// console.log('Third case');
if (options.debug) debugData.push({
...coord,
lastPointIndex,
// uncertainPoints
type: 'Below distance, above turning angle'
})
if (!referencePoint.length) {
referencePoint = lastCoord
lastPointIndex = coordIndex - 1
}
uncertainPoints.push(currentCoord)
} else {
// console.log('Foruth case');
// The minCoordDistance is not held and no danger due to not exceeded
// turning angle. We add the poin to the line string, but save the previous
// point for the next angle calculation
const actAngle = turf.bearing(lastPoint, currentCoord)
const diff = angleBetCoords(actAngle, refAngle)
const correctedDiff = coord.distance / options.minCoordDistance
// use uncertain polys to adapt the reference angle if they are not
// too sharp
if (Math.abs(diff) < 60) {
refAngle += diff * correctedDiff
if (refAngle < -180) refAngle += 360
else if (refAngle > 180) refAngle -= 360
}
if (options.debug) debugData.push({
...coord,
lastPointIndex,
type: 'Below distance, below turning angle'
})
if (!referencePoint.length) {
referencePoint = lastCoord
lastPointIndex = coordIndex - 1
}
if (uncertainPoints.length === 1) {
curLine.push(uncertainPoints[0])
curLine.push(currentCoord)
uncertainPoints = []
} else if (uncertainPoints.length) {
uncertainPoints.push(currentCoord)
} else {
curLine.push(currentCoord)
}
}
// make this the point the last point
lastPoint = currentCoord
lastPointIndex = coordIndex
})
// add the remainder of the section to the lineString
if (curLine.length && lineStrings.length) {
// check if the section is actually part of the first headland
const initAngle = turf.bearing(lineStrings[0][0], lineStrings[0][1]) //angleCoords(lineStrings[0][0],lineStrings[0][1])
const lastAngleDiff = angleBetCoords(refAngle, initAngle)
if (lastAngleDiff >= options.maxAngle || lastAngleDiff <= -options.maxAngle) {
if (options.debug) debugData.push({
angleDiff: lastAngleDiff,
refAngle: initAngle,
coord: lineStrings[0][0],
lastPointIndex,
type: 'Last point'
})
// TODO: This is missing one more point, but need to figure out which one exactly
if (uncertainPoints.length) lineStrings[0] = uncertainPoints.concat(lineStrings[0]) // [curLine[0],...uncertainPoints,lineStrings[0]] //
lineStrings.push(curLine)
} else {
if (options.debug) debugData.push({
angleDiff: lastAngleDiff,
refAngle: initAngle,
coord: lineStrings[0][0],
lastPointIndex,
type: 'lastPoint'
})
lineStrings[0] = curLine.concat(uncertainPoints, lineStrings[0])
}
} else if (curLine.length) {
lineStrings.push(curLine)
}
// in valid GeoJSON, the first geometry in a feature is the outer ring,
// on which we try to calculate the headlands. Any subsequent geometry in the feature is assumed to be
// a hole in the polgyon
// for these holes, we simply assume that the headland is wrapped around the hole
for (let i = 1; i < poly.geometry.coordinates.length; i++) {
const hole = poly.geometry.coordinates[i]
holes.push(turf.polygon([hole]))
}
const lineStringFeatures = lineStrings.map(ls => turf.lineString(ls))
return {
lineStrings: turf.featureCollection(lineStringFeatures),
holes: turf.featureCollection(holes),
debug: debugData
}
},
polygons(poly, options) {
if (!poly) throw new Error('No GeoJSON polygon passed to headlands.polygons function')
// define default settings
options.maxAngle = options.maxAngle || 30
options.minCoordDistance = options.minCoordDistance || 10
options.width = options.width || 12
// get the line strings first
// first make sure windings are correct
poly = turf.rewind(poly)
const {
lineStrings,
holes,
debug
} = this.lineStrings(poly, options)
let polygons
try {
polygons = lineStrings.features.map(ls => {
// add additional point to start
ls = extendLineString(ls, options.width)
// and to the end of the linestring
ls = extendLineString(ls, options.width, true)
// buffer the line string
let headland = turf.buffer(ls, options.width / 1000)
// extent the line string once again, and split the buffered line string
// into an inner and outer part (outer part is discarded)
headland = splitBuffer(headland, ls, options.width)
// finally we clip away any unwated parts of the polygon
headland = clipHeadland(headland, poly, options.width)
if (!headland) {
// this shouldn't happen, but does happen due to various bugs
// in this algorithm, but also in @turf and polygon-clipping
return
}
// finally we check for intersections between the headlands, and return
return headland
})
} catch (e) {
throw new Error('Failed to create headland polygons. This is likely to occur when the input polygon is too small: ' + e)
}
if (holes && holes.features && holes.features.length) {
holes.features.forEach(hole => {
let headland = turf.buffer(hole, options.width / 1000)
headland = turf.difference(headland, hole)
polygons.push(headland)
})
}
polygons = checkIntersections(polygons)
return {
polygons: turf.featureCollection(polygons),
debug
}
}
}
function checkIntersections(polys) {
const filtered = []
for (let i = 0; i < polys.length; i++) {
// if (i !== 5) continue
if (!polys[i]) continue
const curHeadland = polys[i]
try {
// create a 'unified' headland, which joins all headlands except the one
// currently evaluated (i)
const headlandsExceptCurrent = polys.filter(p => p !== curHeadland)
const unified = polygonClipping.union(...headlandsExceptCurrent.map(h => h.geometry.coordinates))
// calculate the intersection area of the the unified and the current headland
// if the intersection area is within 99% of the currentHeadlands area, we throw it out
const intersectionCoords = polygonClipping.intersection(curHeadland.geometry.coordinates,unified)
if (!intersectionCoords) {
continue
}
const intersectionPoly = turf.multiPolygon(intersectionCoords)
const intersectionArea = turf.area(intersectionPoly)
const curHeadlandArea = turf.area(curHeadland)
if (intersectionArea > curHeadlandArea * 0.99) {
continue
}
curHeadland.properties.area = curHeadlandArea
filtered.push(curHeadland)
} catch (e) {
console.log(e);
// weird ass errors thrown by Turf (actually JSTS) on some polys (???)
// in these cases just hope for the best
filtered.push(curHeadland)
}
}
return filtered
}
function getCoordDetails(currentCoord, lastCoord, refAngle, minCoordDistance, maxAngle) {
const curDistance = turf.distance(lastCoord, currentCoord) * 1000
const curAngle = turf.bearing(lastCoord, currentCoord)
const angleDiff = angleBetCoords(refAngle, curAngle)
return {
distance: curDistance,
angle: curAngle,
refAngle: refAngle,
angleDiff: angleDiff,
exceedsTurnAngle: angleDiff >= maxAngle || angleDiff <= -maxAngle ? true : false,
aboveminCoordDistance: curDistance >= minCoordDistance ? true : false,
coord: currentCoord
}
}
function splitBuffer(headlandBuffer, ls, width) {
// extent the line string again in both directions to make sure it's
// longer than the buffer polygon
ls = extendLineString(ls, width * 1000)
ls = extendLineString(ls, width * 1000, true)
let sliced
try {
sliced = polygonCut(headlandBuffer, ls)
sliced = sliced.features[0]
} catch (e) {
// this will throw a "topology exception" error in jsts for one headland
// (in most of the cases) due to a precision error.
// In this case we just use the buffered headland, from wich we have to subtract
// the outer part of the field in a subsequent step
sliced = headlandBuffer
}
return sliced
}
function clipHeadland(headland, poly, width) {
// clip the area that is outside of the original polygon
const intersectionArea = polygonClipping.intersection(headland.geometry.coordinates,poly.geometry.coordinates)
if (!intersectionArea) return headland
// now remove the any inwards bound features from the polygon
const innerPoly = turf.buffer(poly, width / 1000 * -1)
headland = polygonClipping.difference(intersectionArea, innerPoly.geometry.coordinates)
return turf.multiPolygon(headland)
}
// extend the line string by one point in each direction dpeending
// if the additional point is outside the polygon
function extendLineString(ls, width, lastPoint) {
// calculate bearing between first/last two points
const coordinates = ls.geometry.coordinates
const point1 = lastPoint ? coordinates[coordinates.length - 2] : coordinates[1]
const point2 = lastPoint ? coordinates[coordinates.length - 1] : coordinates[0]
const newPointDirection = turf.bearing(point1, point2)
const newPoint = turf.destination(point2, width / 1000, newPointDirection)
if (lastPoint) {
ls.geometry.coordinates.push(newPoint.geometry.coordinates)
} else {
ls.geometry.coordinates.unshift(newPoint.geometry.coordinates)
}
return ls
}
function interpolate(poly, minCoordDistance) {
const coordinates = []
// convert poly to line
// const line = polygonToLine(poly)
// add additional points
turf.segmentEach(poly, (currentSegment, featureIndex, multiFeatureIndex, geometryIndex, segmentIndex) => {
if (featureIndex > 1 || multiFeatureIndex > 1 || geometryIndex > 1) return
const curDistance = length(currentSegment) * 1000
// push first coordinate
if (segmentIndex === 0) {
coordinates.push(currentSegment.geometry.coordinates[0])
}
if (curDistance > minCoordDistance) {
const interpolations = Math.floor(curDistance / minCoordDistance + 1)
for (var i = 1; i <= interpolations; i++) {
const point = along(currentSegment, i * minCoordDistance / 1000)
coordinates.push(point.geometry.coordinates)
}
}
// push second coordinate
coordinates.push(currentSegment.geometry.coordinates[1])
})
return polygon([coordinates])
}
function polygonCut(polygon, line, idPrefix) {
const THICK_LINE_UNITS = 'kilometers';
const THICK_LINE_WIDTH = 0.0001;
var i, j, id, intersectPoints, lineCoords, forCut, forSelect;
var thickLineString, thickLinePolygon, clipped, polyg, intersect;
var polyCoords = [];
var cutPolyGeoms = [];
var cutFeatures = [];
var offsetLine = [];
var retVal = null;
if (typeof(idPrefix) === 'undefined') {
idPrefix = '';
}
intersectPoints = turf.lineIntersect(polygon, line);
if (intersectPoints.features.length == 0) {
return retVal;
}
var lineCoords = turf.getCoords(line);
if ((turf.booleanWithin(turf.point(lineCoords[0]), polygon) ||
(turf.booleanWithin(turf.point(lineCoords[lineCoords.length - 1]), polygon)))) {
return retVal;
}
offsetLine[0] = turf.lineOffset(line, THICK_LINE_WIDTH, {
units: THICK_LINE_UNITS
});
offsetLine[1] = turf.lineOffset(line, -THICK_LINE_WIDTH, {
units: THICK_LINE_UNITS
});
// remove NaN coords
offsetLine[0].geometry.coordinates = offsetLine[0].geometry.coordinates.filter(coord => !isNaN(coord[0]))
offsetLine[1].geometry.coordinates = offsetLine[1].geometry.coordinates.filter(coord => !isNaN(coord[0]))
for (i = 0; i <= 1; i++) {
forCut = i;
forSelect = (i + 1) % 2;
polyCoords = [];
for (j = 0; j < line.geometry.coordinates.length; j++) {
polyCoords.push(line.geometry.coordinates[j]);
}
for (j = (offsetLine[forCut].geometry.coordinates.length - 1); j >= 0; j--) {
polyCoords.push(offsetLine[forCut].geometry.coordinates[j]);
}
polyCoords.push(line.geometry.coordinates[0]);
polyCoords = polyCoords.filter(coord => !isNaN(coord[0]))
thickLineString = turf.lineString(polyCoords);
thickLinePolygon = turf.lineToPolygon(thickLineString);
clipped = turf.difference(polygon, thickLinePolygon);
cutPolyGeoms = [];
for (j = 0; j < clipped.geometry.coordinates.length; j++) {
polyg = turf.polygon(clipped.geometry.coordinates[j]);
intersect = turf.lineIntersect(polyg, offsetLine[forSelect]);
if (intersect.features.length > 0) {
cutPolyGeoms.push(polyg.geometry.coordinates);
};
};
cutPolyGeoms.forEach(function(geometry, index) {
id = idPrefix + (i + 1) + '.' + (index + 1);
cutFeatures.push(turf.polygon(geometry, {
id: id
}));
});
}
if (cutFeatures.length > 0) retVal = turf.featureCollection(cutFeatures);
return retVal;
};
function angleBetCoords(a, b) {
let difference = a - (b)
if (difference < -180) difference += 360
else if (difference > 180) difference -= 360
return difference
}
export default headland