-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathdraw-line-string-mode.ts
200 lines (181 loc) · 5.55 KB
/
draw-line-string-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
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
import distance from '@turf/distance';
import memoize from '../memoize';
import { LineString, FeatureCollection, Position } from '../geojson-types';
import {
ClickEvent,
PointerMoveEvent,
ModeProps,
GuideFeatureCollection,
GuideFeature,
Tooltip,
} from '../types';
import { getPickedEditHandle } from '../utils';
import { GeoJsonEditMode } from './geojson-edit-mode';
export class DrawLineStringMode extends GeoJsonEditMode {
// declaration of variables for the calculation of the distance of linestring
dist: number | null | undefined = 0;
position: Position;
elems: Position[];
handleClick(event: ClickEvent, props: ModeProps<FeatureCollection>) {
const { picks } = event;
const clickedEditHandle = getPickedEditHandle(picks);
let positionAdded = false;
if (!clickedEditHandle) {
// Don't add another point right next to an existing one
this.addClickSequence(event);
positionAdded = true;
}
const clickSequence = this.getClickSequence();
// check if the pointer is on editable state calculate the distance of new point
if (!clickedEditHandle) {
this.calculateInfoDraw(clickSequence);
}
if (
clickSequence.length > 1 &&
clickedEditHandle &&
Array.isArray(clickedEditHandle.properties.positionIndexes) &&
clickedEditHandle.properties.positionIndexes[0] === clickSequence.length - 1
) {
// They clicked the last point (or double-clicked), so add the LineString
// reset distance to new calculate
this.dist = 0;
const lineStringToAdd: LineString = {
type: 'LineString',
coordinates: [...clickSequence],
};
this.resetClickSequence();
const editAction = this.getAddFeatureAction(lineStringToAdd, props.data);
if (editAction) {
props.onEdit(editAction);
}
} else if (positionAdded) {
// new tentative point
props.onEdit({
// data is the same
updatedData: props.data,
editType: 'addTentativePosition',
editContext: {
position: event.mapCoords,
},
});
}
}
handleKeyUp(event: KeyboardEvent, props: ModeProps<FeatureCollection>) {
const { key } = event;
if (key === 'Enter') {
const clickSequence = this.getClickSequence();
if (clickSequence.length > 1) {
const lineStringToAdd: LineString = {
type: 'LineString',
coordinates: [...clickSequence],
};
this.resetClickSequence();
const editAction = this.getAddFeatureAction(lineStringToAdd, props.data);
if (editAction) {
props.onEdit(editAction);
}
}
} else if (key === 'Escape') {
this.resetClickSequence();
props.onEdit({
// Because the new drawing feature is dropped, so the data will keep as the same.
updatedData: props.data,
editType: 'cancelFeature',
editContext: {},
});
}
}
getGuides(props: ModeProps<FeatureCollection>): GuideFeatureCollection {
const { lastPointerMoveEvent } = props;
const clickSequence = this.getClickSequence();
const lastCoords = lastPointerMoveEvent ? [lastPointerMoveEvent.mapCoords] : [];
const guides: GuideFeatureCollection = {
type: 'FeatureCollection',
features: [],
};
let tentativeFeature;
if (clickSequence.length > 0) {
tentativeFeature = {
type: 'Feature',
properties: {
guideType: 'tentative',
},
geometry: {
type: 'LineString',
coordinates: [...clickSequence, ...lastCoords],
},
};
}
if (tentativeFeature) {
guides.features.push(tentativeFeature);
}
const editHandles: GuideFeature[] = clickSequence.map((clickedCoord, index) => ({
type: 'Feature',
properties: {
guideType: 'editHandle',
editHandleType: 'existing',
featureIndex: -1,
positionIndexes: [index],
},
geometry: {
type: 'Point',
coordinates: clickedCoord,
},
}));
guides.features.push(...editHandles);
return guides;
}
handlePointerMove(event: PointerMoveEvent, props: ModeProps<FeatureCollection>) {
props.onUpdateCursor('cell');
}
/**
* define the default function to display the tooltip for
* nebula geometry mode type
* @param props properties of geometry nebula mode
*/
getTooltips(props: ModeProps<FeatureCollection>): Tooltip[] {
return this._getTooltips({
modeConfig: props.modeConfig,
dist: this.dist,
});
}
// utility function
calculateInfoDraw(clickSequence) {
// check if the selected points are at least 2
if (clickSequence.length > 1) {
// setting the last point
this.position = clickSequence[clickSequence.length - 1];
// calculate the new distance by adding the
// distance of the new drawn linestring
this.dist += distance(
clickSequence[clickSequence.length - 2],
clickSequence[clickSequence.length - 1]
);
}
}
/**
* redefine the tooltip of geometry
* @param modeConfig
* @param dist
*/
_getTooltips = memoize(({ modeConfig, dist }) => {
let tooltips = [];
const { formatTooltip } = modeConfig || {};
let text;
if (dist) {
if (formatTooltip) {
text = formatTooltip(dist);
} else {
// By default, round to 2 decimal places and append units
text = `Distance: ${parseFloat(dist).toFixed(2)} kilometers`;
}
tooltips = [
{
position: this.position,
text,
},
];
}
return tooltips;
});
}