forked from hotosm/tasking-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetTaskSizes.js
More file actions
230 lines (212 loc) · 7.51 KB
/
setTaskSizes.js
File metadata and controls
230 lines (212 loc) · 7.51 KB
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
import { useEffect, useLayoutEffect, useState, useCallback } from 'react';
import area from '@turf/area';
import transformScale from '@turf/transform-scale';
import { featureCollection } from '@turf/helpers';
import { FormattedMessage } from 'react-intl';
import messages from './messages';
import { splitTaskGrid, makeGrid } from '../../utils/taskGrid';
import { CustomButton } from '../button';
import {
UndoIcon,
MappedIcon,
CircleIcon,
FourCellsGridIcon,
NineCellsGridIcon,
} from '../svgIcons';
import { getAllFeatures, removeFeaturesById } from '../../utils/terrawDraw';
export default function SetTaskSizes({ metadata, mapObj, updateMetadata }) {
const [splitMode, setSplitMode] = useState(null);
const splitHandler = useCallback(
(event) => {
const taskGrid = mapObj.map.getSource('grid')._data;
if (metadata.tempTaskGrid === null) {
updateMetadata({ ...metadata, tempTaskGrid: taskGrid });
}
// Make the geom smaller to avoid borders.
const geom = transformScale(event.features[0].geometry, 0.5);
const newTaskGrid = splitTaskGrid(taskGrid, geom);
updateMetadata({
...metadata,
taskGrid: featureCollection(newTaskGrid),
tasksNumber: featureCollection(newTaskGrid).features.length,
});
},
[updateMetadata, metadata, mapObj.map],
);
useEffect(() => {
if (splitMode === 'click') {
mapObj.map.on('mouseenter', 'grid', (event) => {
mapObj.map.getCanvas().style.cursor = 'pointer';
});
mapObj.map.on('mouseleave', 'grid', (event) => {
mapObj.map.getCanvas().style.cursor = '';
});
mapObj.map.on('click', 'grid', splitHandler);
} else {
mapObj.map.on('mouseenter', 'grid', (event) => {
mapObj.map.getCanvas().style.cursor = '';
});
mapObj.map.off('click', 'grid', splitHandler);
}
}, [mapObj, splitHandler, splitMode]);
const splitDrawing = useCallback(() => {
const drawInstance = mapObj.draw.getTerraDrawInstance();
if (!drawInstance) return;
if (splitMode === 'draw') {
setSplitMode(null);
drawInstance.setMode('select');
return;
}
setSplitMode('draw');
drawInstance.setMode('polygon');
drawInstance.on('finish', (id) => {
const allFeatures = getAllFeatures(drawInstance);
const previousFeatureIds = allFeatures.reduce(
(prev, curr) => (curr.id !== id ? [...prev, curr.id] : prev),
[],
);
const newFeature = allFeatures.filter((f) => f.id === id);
if (previousFeatureIds.length > 0) {
removeFeaturesById(drawInstance, previousFeatureIds);
}
if (newFeature.length > 0) {
const geom = newFeature[0].geometry;
const taskGrid = mapObj.map.getSource('grid')._data;
if (metadata.tempTaskGrid === null) {
updateMetadata({ ...metadata, tempTaskGrid: taskGrid });
}
const newTaskGrid = splitTaskGrid(taskGrid, geom);
updateMetadata({
...metadata,
taskGrid: featureCollection(newTaskGrid),
tasksNumber: featureCollection(newTaskGrid).features.length,
});
}
removeFeaturesById(drawInstance, [id]);
drawInstance.setMode('select');
setSplitMode(null);
});
}, [mapObj.draw, mapObj.map, splitMode, metadata, updateMetadata]);
const resetGrid = () => {
updateMetadata({ ...metadata, taskGrid: metadata.tempTaskGrid });
};
const smallerSize = useCallback(() => {
const zoomLevel = metadata.zoomLevel + 1;
const squareGrid = makeGrid(metadata.geom, zoomLevel);
updateMetadata({
...metadata,
zoomLevel: zoomLevel,
tempTaskGrid: squareGrid,
taskGrid: squareGrid,
tasksNumber: squareGrid.features.length,
});
}, [metadata, updateMetadata]);
const largerSize = useCallback(() => {
const zoomLevel = metadata.zoomLevel - 1;
const squareGrid = makeGrid(metadata.geom, zoomLevel);
if (zoomLevel > 0) {
updateMetadata({
...metadata,
zoomLevel: zoomLevel,
tempTaskGrid: squareGrid,
taskGrid: squareGrid,
tasksNumber: squareGrid.features.length,
});
}
}, [metadata, updateMetadata]);
useLayoutEffect(() => {
if (mapObj.map.getSource('grid') !== undefined) {
mapObj.map.getSource('grid').setData(metadata.taskGrid);
} else {
mapObj.map.addSource('grid', {
type: 'geojson',
data: { type: 'FeatureCollection', features: metadata.taskGrid },
});
}
return () => {
// remove the split on click function when leaving the page
mapObj.map.off('click', 'grid', splitHandler);
};
}, [metadata, mapObj, smallerSize, largerSize, splitHandler]);
return (
<>
<h3 className="f3 ttu fw6 mt2 mb3 barlow-condensed blue-dark">
<FormattedMessage {...messages.step2} />
</h3>
<div>
<div>
<p>
<FormattedMessage {...messages.taskSizes} />
</p>
<div role="group">
<CustomButton
className="bg-white blue-dark ba b--grey-light ph3 pv2 mr2"
onClick={smallerSize}
icon={<NineCellsGridIcon className="h1 w1 v-mid" />}
>
<FormattedMessage {...messages.smaller} />
</CustomButton>
<CustomButton
className="bg-white blue-dark ba b--grey-light ph3 pv2"
onClick={largerSize}
icon={<FourCellsGridIcon className="h1 w1 v-mid" />}
>
<FormattedMessage {...messages.larger} />
</CustomButton>
</div>
</div>
<div className="pt3 pb1">
<p>
<FormattedMessage {...messages.splitTaskDescription} />
</p>
<div role="group">
<CustomButton
className={`bg-white ph3 pv2 mr2 ba ${
splitMode === 'click' ? 'red b--red' : 'blue-dark b--grey-light'
}`}
onClick={() => setSplitMode(splitMode === 'click' ? null : 'click')}
icon={<CircleIcon className="v-mid" style={{ width: '0.5rem' }} />}
>
<FormattedMessage {...messages.splitByClicking} />
</CustomButton>
<CustomButton
className={`bg-white ph3 pv2 mr2 ba ${
splitMode === 'draw' ? 'red b--red' : 'blue-dark b--grey-light'
}`}
onClick={splitDrawing}
icon={<MappedIcon className="h1 w1 v-mid" />}
>
<FormattedMessage {...messages.splitByDrawing} />
</CustomButton>
<CustomButton
className="bg-white blue-dark ba b--grey-light ph3 pv2"
onClick={resetGrid}
icon={<UndoIcon className="w1 h1 v-mid" />}
>
<FormattedMessage {...messages.reset} />
</CustomButton>
</div>
</div>
<p className="f6 blue-grey lh-title mt3 mb2">
<FormattedMessage
{...messages.taskNumberMessage}
values={{ n: <strong>{metadata.tasksNumber || 0}</strong> }}
/>
</p>
<p className="f6 blue-grey lh-title mt1">
{metadata.taskGrid && metadata.taskGrid.features && (
<FormattedMessage
{...messages.taskAreaMessage}
values={{
area: (
<strong>{(area(metadata.taskGrid.features[0]) / 1e6).toFixed(2) || 0}</strong>
),
sq: <sup>2</sup>,
}}
/>
)}
</p>
</div>
</>
);
}