-
Notifications
You must be signed in to change notification settings - Fork 166
/
measure-distance-mode.js
137 lines (120 loc) · 3.73 KB
/
measure-distance-mode.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
// @flow
import turfDistance from '@turf/distance';
import { _memoize } from '@deck.gl/core';
import type {
ClickEvent,
PointerMoveEvent,
Tooltip,
ModeProps,
GuideFeatureCollection,
EditHandleFeature
} from '../types.js';
import type { FeatureCollection, Position } from '../geojson-types.js';
import { GeoJsonEditMode } from './geojson-edit-mode.js';
const DEFAULT_TOOLTIPS = [];
export class MeasureDistanceMode extends GeoJsonEditMode {
startingPoint: ?$ReadOnly<EditHandleFeature> = null;
endingPoint: ?$ReadOnly<EditHandleFeature> = null;
endingPointLocked = false;
_setEndingPoint(mapCoords: Position) {
this.endingPoint = {
type: 'Feature',
properties: {
guideType: 'editHandle',
editHandleType: 'existing',
featureIndex: -1,
positionIndexes: []
},
geometry: {
type: 'Point',
coordinates: mapCoords
}
};
}
_getTooltips = _memoize(({ modeConfig, startingPoint, endingPoint }) => {
let tooltips = DEFAULT_TOOLTIPS;
if (startingPoint && endingPoint) {
const { formatTooltip, turfOptions, measurementCallback } = modeConfig || {};
const units = (turfOptions && turfOptions.units) || 'kilometers';
const distance = turfDistance(startingPoint, endingPoint, turfOptions);
let text;
if (formatTooltip) {
text = formatTooltip(distance);
} else {
// By default, round to 2 decimal places and append units
text = `${parseFloat(distance).toFixed(2)} ${units}`;
}
if (measurementCallback) {
measurementCallback(distance);
}
tooltips = [
{
position: endingPoint.geometry.coordinates,
text
}
];
}
return tooltips;
});
handleClick(event: ClickEvent, props: ModeProps<FeatureCollection>): void {
if (!this.startingPoint || this.endingPointLocked) {
this.startingPoint = {
type: 'Feature',
properties: {
guideType: 'editHandle',
editHandleType: 'existing',
featureIndex: -1,
positionIndexes: []
},
geometry: {
type: 'Point',
coordinates: event.mapCoords
}
};
this.endingPoint = null;
this.endingPointLocked = false;
} else if (this.startingPoint) {
this._setEndingPoint(event.mapCoords);
this.endingPointLocked = true;
}
}
// Called when the pointer moved, regardless of whether the pointer is down, up, and whether something was picked
handlePointerMove(event: PointerMoveEvent, props: ModeProps<FeatureCollection>): void {
if (this.startingPoint && !this.endingPointLocked) {
this._setEndingPoint(event.mapCoords);
}
props.onUpdateCursor('cell');
}
// Return features that can be used as a guide for editing the data
getGuides(props: ModeProps<FeatureCollection>): GuideFeatureCollection {
const guides: GuideFeatureCollection = { type: 'FeatureCollection', features: [] };
const { features } = guides;
if (this.startingPoint) {
features.push(this.startingPoint);
}
if (this.endingPoint) {
features.push(this.endingPoint);
}
if (this.startingPoint && this.endingPoint) {
features.push({
type: 'Feature',
properties: { guideType: 'tentative' },
geometry: {
type: 'LineString',
coordinates: [
this.startingPoint.geometry.coordinates,
this.endingPoint.geometry.coordinates
]
}
});
}
return guides;
}
getTooltips(props: ModeProps<FeatureCollection>): Tooltip[] {
return this._getTooltips({
modeConfig: props.modeConfig,
startingPoint: this.startingPoint,
endingPoint: this.endingPoint
});
}
}