-
Notifications
You must be signed in to change notification settings - Fork 88
/
Rotate.js
394 lines (348 loc) · 10.7 KB
/
Rotate.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// The MIT License (MIT)
//
// Copyright (c) 2016-2023 Camptocamp SA
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import {getDefaultModifyStyleFunction} from 'ngeo/interaction/common';
import ngeoCustomEvent from 'ngeo/CustomEvent';
import {getUid as olUtilGetUid} from 'ol/util';
import * as olExtent from 'ol/extent';
import olFeature from 'ol/Feature';
import {listen, unlistenByKey} from 'ol/events';
import olInteractionPointer from 'ol/interaction/Pointer';
import olGeomPoint from 'ol/geom/Point';
import olGeomLineString from 'ol/geom/LineString';
import olGeomPolygon from 'ol/geom/Polygon';
import olLayerVector from 'ol/layer/Vector';
import olSourceVector from 'ol/source/Vector';
import {CollectionEvent} from 'ol/Collection';
/**
* @typedef {Object} RotateEventItem
* @property {olFeature<import('ol/geom/Geometry').default>} feature
*/
/**
* @typedef {import('ngeo/CustomEvent').default<RotateEventItem>} RotateEvent
*/
/**
* Interaction to rotate features.
*
* @hidden
*/
export default class extends olInteractionPointer {
/**
* @param {import('ol/interaction/Modify').Options} options Options.
* @fires import('ngeo/interaction/ModifyCircleEvent').default
*/
constructor(options) {
super();
console.assert(options.features);
this.handleDownEvent = this.handleDown_;
this.handleDragEvent = this.handleDrag_;
this.handleUpEvent = this.handleUp_;
/**
* @type {import('ol/events').EventsKey[]}
* @private
*/
this.listenerKeys_ = [];
/**
* @type {boolean}
* @private
*/
this.modified_ = false;
/**
* @type {?import('ol/events').EventsKey}
* @private
*/
this.keyPressListenerKey_ = null;
/**
* Indicate whether the interaction is currently changing a feature's
* coordinates.
*
* @type {boolean}
* @private
*/
this.changingFeature_ = false;
/**
* @type {number}
* @private
*/
this.pixelTolerance_ = options.pixelTolerance !== undefined ? options.pixelTolerance : 10;
/**
* @type {import('ol/Collection').default<olFeature<import('ol/geom/Geometry').default>>}
* @private
*/
this.features_ = options.features;
/**
* The feature currently modified.
*
* @type {?olFeature<import('ol/geom/Geometry').default>}
* @private
*/
this.feature_ = null;
/**
* @type {?import('ol/pixel').Pixel}
* @private
*/
this.coordinate_ = null;
/**
* @type {?import('ol/coordinate').Coordinate}
* @private
*/
this.centerCoordinate_ = null;
/**
* Draw overlay where sketch features are drawn.
*
* @type {import('ol/layer/Vector').default<import('ol/source/Vector').default<import('ol/geom/Geometry').default>>}
* @private
*/
this.overlay_ = new olLayerVector({
className: 'canvas2d',
source: new olSourceVector({
useSpatialIndex: false,
wrapX: !!options.wrapX,
}),
style: options.style || getDefaultModifyStyleFunction(),
updateWhileAnimating: true,
updateWhileInteracting: true,
});
/**
* @type {Object<string, olFeature<import('ol/geom/Geometry').default>>}
* @private
*/
this.centerFeatures_ = {};
}
/**
* Activate or deactivate the interaction.
*
* @param {boolean} active Active.
* @override
*/
setActive(active) {
if (!this.features_) {
return;
}
if (this.keyPressListenerKey_) {
unlistenByKey(this.keyPressListenerKey_);
this.keyPressListenerKey_ = null;
}
olInteractionPointer.prototype.setActive.call(this, active);
if (active) {
this.keyPressListenerKey_ = listen(document, 'keyup', this.handleKeyUp_, this);
this.features_.forEach((feature) => this.addFeature_(feature));
this.listenerKeys_.push(
listen(this.features_, 'add', this.handleFeatureAdd_, this),
listen(this.features_, 'remove', this.handleFeatureRemove_, this)
);
} else {
this.listenerKeys_.forEach(unlistenByKey);
this.listenerKeys_.length = 0;
this.features_.forEach((feature) => this.removeFeature_(feature));
}
}
/**
* @param {olFeature<import('ol/geom/Geometry').default>} feature Feature.
* @private
*/
addFeature_(feature) {
const geometry = feature.getGeometry();
if (!geometry) {
throw new Error('Missing geometry');
}
feature.set('angle', 0);
// Add the center icon to the overlay
const uid = olUtilGetUid(feature);
const point = new olGeomPoint(this.getCenterCoordinate_(geometry));
const centerFeature = new olFeature(point);
this.centerFeatures_[uid] = centerFeature;
/** @type {olSourceVector<import('ol/geom/Geometry').default>} */ (this.overlay_.getSource()).addFeature(
centerFeature
);
}
/**
* @param {import('ol/MapBrowserEvent').default<unknown>} evt MapBrowserEvent
* @private
*/
willModifyFeatures_(evt) {
if (!this.modified_) {
this.modified_ = true;
const event = new ngeoCustomEvent('modifystart', {features: this.features_});
this.dispatchEvent(event);
}
}
/**
* @param {olFeature<import('ol/geom/Geometry').default>} feature Feature.
* @private
*/
removeFeature_(feature) {
this.feature_ = null;
if (feature) {
const uid = olUtilGetUid(feature);
if (this.centerFeatures_[uid]) {
const source = this.overlay_.getSource();
if (!(source instanceof olSourceVector)) {
throw new Error('Wrong source type');
}
source.removeFeature(this.centerFeatures_[uid]);
delete this.centerFeatures_[uid];
}
}
}
/**
* @param {import('ol/Map').default} map The map that the
* overlay is part of.
*/
setMap(map) {
this.overlay_.setMap(map);
olInteractionPointer.prototype.setMap.call(this, map);
}
/**
* @param {Event|import('ol/events/Event').default} evt Event.
* @private
*/
handleFeatureAdd_(evt) {
if (evt instanceof CollectionEvent) {
const feature = evt.element;
console.assert(feature instanceof olFeature, 'feature should be an ol.Feature');
this.addFeature_(feature);
}
}
/**
* @param {Event|import('ol/events/Event').default} evt Event.
* @private
*/
handleFeatureRemove_(evt) {
if (evt instanceof CollectionEvent) {
/**
* @type {olFeature<import('ol/geom/Geometry').default>}
*/
const feature = evt.element;
this.removeFeature_(feature);
}
}
/**
* @param {import('ol/MapBrowserEvent').default<unknown>} evt MapBrowserEvent.
* @returns {boolean} Start drag sequence?
* @private
*/
handleDown_(evt) {
const map = evt.map;
let feature = map.forEachFeatureAtPixel(evt.pixel, (feature, layer) => feature, undefined);
if (feature) {
let found = false;
this.features_.forEach((f) => {
if (olUtilGetUid(f) == olUtilGetUid(feature)) {
found = true;
}
});
if (!found) {
feature = undefined;
}
}
if (feature) {
this.coordinate_ = evt.coordinate;
this.feature_ = /** @type {olFeature<import('ol/geom/Geometry').default>} */ (feature);
const geometry = this.feature_.getGeometry();
if (geometry !== undefined) {
this.centerCoordinate_ = this.getCenterCoordinate_(geometry);
}
return true;
}
return false;
}
/**
* @param {import('ol/geom/Geometry').default} geometry Geometry.
* @returns {import('ol/coordinate').Coordinate} The center coordinate of the geometry.
* @private
*/
getCenterCoordinate_(geometry) {
let center;
if (geometry instanceof olGeomLineString) {
center = geometry.getFlatMidpoint();
} else if (geometry instanceof olGeomPolygon) {
center = geometry.getFlatInteriorPoint();
} else {
const extent = geometry.getExtent();
center = olExtent.getCenter(extent);
}
return center;
}
/**
* @param {import('ol/MapBrowserEvent').default<unknown>} evt MapBrowserEvent.
* @private
*/
handleDrag_(evt) {
this.willModifyFeatures_(evt);
if (!this.feature_) {
throw new Error('Missing feature');
}
if (!this.coordinate_) {
throw new Error('Missing coordinate');
}
if (!this.centerCoordinate_) {
throw new Error('Missing centerCoordinate');
}
const geometry = this.feature_.getGeometry();
if (!geometry) {
throw new Error('Missing geometry');
}
const oldX = this.coordinate_[0];
const oldY = this.coordinate_[1];
const centerX = this.centerCoordinate_[0];
const centerY = this.centerCoordinate_[1];
const dx1 = oldX - centerX;
const dy1 = oldY - centerY;
const dx0 = evt.coordinate[0] - centerX;
const dy0 = evt.coordinate[1] - centerY;
this.coordinate_[0] = evt.coordinate[0];
this.coordinate_[1] = evt.coordinate[1];
const a0 = Math.atan2(dy0, dx0);
const a1 = Math.atan2(dy1, dx1);
const angle = a1 - a0;
geometry.rotate(-angle, [centerX, centerY]);
}
/**
* @param {import('ol/MapBrowserEvent').default<unknown>} evt MapBrowserEvent.
* @returns {boolean} Stop drag sequence?
* @private
*/
handleUp_(evt) {
if (this.modified_) {
if (!this.feature_) {
throw new Error('Missing feature');
}
/** @type {RotateEvent} */
const event = new ngeoCustomEvent('rotateend', {feature: this.feature_});
this.dispatchEvent(event);
this.modified_ = false;
this.setActive(false);
}
return false;
}
/**
* Deactivate this interaction if the ESC key is pressed.
*
* @param {Event|import('ol/events/Event').default} evt Event.
* @private
*/
handleKeyUp_(evt) {
// 27 == ESC key
if (evt instanceof KeyboardEvent && evt.keyCode === 27) {
this.setActive(false);
}
}
}