-
Notifications
You must be signed in to change notification settings - Fork 0
/
arraymode.js
94 lines (86 loc) · 3 KB
/
arraymode.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
import * as turf from '@turf/turf';
const clear = (target, featureIds) => {
featureIds.forEach((id) => target.deleteFeature(id));
};
const translatePoint = (point, distance, angle) => turf.destination(point, distance, angle)
.geometry.coordinates;
const makeRectangleAround = (target, centerPoint, width, height, bearing) => {
const topLeft = translatePoint(translatePoint(centerPoint, width / 2, bearing - 180),
height / 2, bearing - 90);
const bottomLeft = translatePoint(translatePoint(centerPoint, width / 2, bearing - 180),
height / 2, bearing + 90);
const bottomRight = translatePoint(translatePoint(centerPoint, width / 2, bearing),
height / 2, bearing + 90);
const topRight = translatePoint(translatePoint(centerPoint, width / 2, bearing),
height / 2, bearing - 90);
const rectCoordinates = [topLeft, bottomLeft, bottomRight, topRight, topLeft];
const resultingFeature = target.newFeature({
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: rectCoordinates,
},
});
target.addFeature(resultingFeature);
return resultingFeature;
};
const createArrayFromTo = (target, start, end, { width, height, gap }) => {
const totalLength = turf.length(turf.lineString([start, end]));
const bearing = turf.bearing(start, end);
const numPoints = totalLength / (width + gap);
const numDrawLoop = numPoints - 1;
const featureIds = [];
for (let i = 0; i < numDrawLoop; i += 1) {
const { coordinates } = turf.destination(start,
(i * totalLength) / numDrawLoop,
bearing).geometry;
featureIds.push(makeRectangleAround(target, coordinates,
width,
height,
bearing).id);
}
return featureIds;
};
// measurings of created rectangles (in kilometers)
// TODO: make configurable through UI
const gap = 0.00066;
const width = 0.0019;
const height = 0.004;
export default {
onSetup() {
this.updateUIClasses({ mouse: 'add' });
this.setActionableState({
trash: true,
});
return {
state: 'chooseFirst',
firstPoint: null,
secondPoint: null,
featureIds: [],
};
},
onMouseMove(state, e) {
if (state.state === 'chooseSecond') {
state.secondPoint = [e.lngLat.lng, e.lngLat.lat];
clear(this, state.featureIds);
state.featureIds = createArrayFromTo(this, state.firstPoint, state.secondPoint, { width, height, gap });
}
},
onClick(state, e) {
if (state.state === 'chooseFirst') {
state.state = 'chooseSecond';
state.firstPoint = [e.lngLat.lng, e.lngLat.lat];
} else if (state.state === 'chooseSecond') {
state.secondPoint = [e.lngLat.lng, e.lngLat.lat];
clear(this, state.featureIds);
state.featureIds = createArrayFromTo(this, state.firstPoint, state.secondPoint, { width, height, gap });
this.map.fire('draw.create', {
features: state.featureIds.map((id) => this.getFeature(id).toGeoJSON()),
});
this.changeMode('simple_select');
}
},
toDisplayFeatures(state, geojson, display) {
display(geojson);
},
};