-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathmeasure-area-mode.ts
62 lines (51 loc) · 1.7 KB
/
measure-area-mode.ts
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
import turfArea from '@turf/area';
import turfCentroid from '@turf/centroid';
import { ClickEvent, Tooltip, ModeProps } from '../types';
import { FeatureCollection } from '../geojson-types';
import { DrawPolygonMode } from './draw-polygon-mode';
const DEFAULT_TOOLTIPS = [];
export class MeasureAreaMode extends DrawPolygonMode {
handleClick(event: ClickEvent, props: ModeProps<FeatureCollection>) {
const propsWithoutEdit = {
...props,
onEdit: () => {},
};
super.handleClick(event, propsWithoutEdit);
}
handleKeyUp(event: KeyboardEvent, props: ModeProps<FeatureCollection>): void {
const propsWithoutEdit = {
...props,
onEdit: () => {},
};
super.handleKeyUp(event, propsWithoutEdit);
}
getTooltips(props: ModeProps<FeatureCollection>): Tooltip[] {
const tentativeGuide = this.getTentativeGuide(props);
if (tentativeGuide && tentativeGuide.geometry.type === 'Polygon') {
const { modeConfig } = props;
const { formatTooltip, measurementCallback } = modeConfig || {};
const units = 'sq. m';
const centroid = turfCentroid(tentativeGuide);
const area = turfArea(tentativeGuide);
let text: string;
if (formatTooltip) {
text = formatTooltip(area);
} else {
// By default, round to 2 decimal places and append units
// @ts-expect-error are isn't string
text = `${parseFloat(area).toFixed(2)} ${units}`;
}
if (measurementCallback) {
measurementCallback(area);
}
return [
{
// @ts-expect-error turf types diff
position: centroid.geometry.coordinates,
text,
},
];
}
return DEFAULT_TOOLTIPS;
}
}