-
Notifications
You must be signed in to change notification settings - Fork 69
/
leaflet.snap.js
669 lines (557 loc) · 22.2 KB
/
leaflet.snap.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/* globals L:true */
L.Snap = {};
L.Snap.isDifferentLayer = function (marker, layer) {
var i;
var n;
var markerId = L.stamp(marker);
if (layer.hasOwnProperty('_snapIgnore')) {
return false;
}
if (layer.hasOwnProperty('_topOwner') && marker.hasOwnProperty('_topOwner')) {
return layer._topOwner !== marker._topOwner;
}
if (layer instanceof L.Marker) {
return markerId !== L.stamp(layer);
}
if (layer.editing && layer.editing._enabled) {
if (layer.editing._verticesHandlers) {
var points = layer.editing._verticesHandlers[0]._markerGroup.getLayers();
for(i = 0, n = points.length; i < n; i++) {
if (L.stamp(points[i]) == markerId) {
return false;
}
}
}
else if (layer.editing._resizeMarkers) {
for(i = 0; i < layer.editing._resizeMarkers.length; i++) {
var resizeMarker = layer.editing._resizeMarkers[i];
if (L.stamp(resizeMarker) == markerId) {
return false;
}
}
if (layer.editing._moveMarker) {
return markerId !== L.stamp(layer.editing._moveMarker);
}
return true;
}
}
return true;
};
L.Snap.processGuide = function (latlng, marker, guide, snaplist, buffer) {
// Guide is a layer group and has no L.LayerIndexMixin (from Leaflet.LayerIndex)
if ((guide._layers !== undefined) && (typeof guide.searchBuffer !== 'function')) {
for (var id in guide._layers) {
if (guide._layers.hasOwnProperty(id)) {
L.Snap.processGuide(latlng, marker, guide._layers[id], snaplist, buffer);
}
}
}
// Search snaplist around mouse
else if (typeof guide.searchBuffer === 'function') {
var nearlayers = guide.searchBuffer(latlng, buffer);
snaplist = snaplist.concat(nearlayers.filter(function(layer) {
return L.Snap.isDifferentLayer(layer);
}));
}
// Make sure the marker doesn't snap to itself or an associated polyline layer
else if (L.Snap.isDifferentLayer(marker, guide)) {
snaplist.push(guide);
}
};
L.Snap.findClosestLayerSnap = function (map, layers, latlng, tolerance, withVertices) {
var closest = L.GeometryUtil.nClosestLayers(map, layers, latlng, 6);
// code to correct prefer snap to shapes (and their vertices, if withVertices is true) to gridlines and guidelines, and then guidelines to gridlines
var withinTolerance = [];
var pointsWithinTolerance = [];
var shapesWithinTolerance = [];
var guidesWithinTolerance = [];
for (var c=0; c<closest.length; c++) {
var layerInfo = closest[c];
if (layerInfo.distance < tolerance) {
withinTolerance.push(layerInfo);
if (layerInfo.layer.hasOwnProperty('_latlng')) {
pointsWithinTolerance.push(layerInfo);
}
else if ((! layerInfo.layer.hasOwnProperty('_gridlineGroup')) && (! layerInfo.layer.hasOwnProperty('_guidelineGroup'))) {
shapesWithinTolerance.push(layerInfo);
}
else if (layerInfo.layer.hasOwnProperty('_guidelineGroup')) {
guidesWithinTolerance.push(layerInfo);
}
}
}
if (withinTolerance.length === 0) {
return null;
}
var intInfo;
var returnLayer = withinTolerance[0].layer;
var returnLatLng = withinTolerance[0].latlng;
if (pointsWithinTolerance.length > 0) {
var pointInfo = pointsWithinTolerance[0];
returnLayer = pointInfo.layer;
returnLatLng = pointInfo.latlng;
}
else if (shapesWithinTolerance.length > 0) {
var shapeInfo = shapesWithinTolerance[0];
returnLayer = shapeInfo.layer;
returnLatLng = shapeInfo.latlng;
// this is code from L.GeometryUtil.closestSnap that will find
// the closest vertex of this layer to the point
if (withVertices && (typeof shapeInfo.layer.getLatLngs == 'function')) {
var vertexLatLng = L.GeometryUtil.closest(map, shapeInfo.layer, shapeInfo.latlng, true);
if (vertexLatLng) {
var d = L.GeometryUtil.distance(map, latlng, vertexLatLng);
if (d < tolerance) {
returnLatLng = new L.LatLng(vertexLatLng.lat, vertexLatLng.lng);
}
}
}
}
else if (guidesWithinTolerance.length > 0) {
var guideInfo = guidesWithinTolerance[0];
var guideType = guideInfo.layer._guidelineGroup;
for (var i=0; i<withinTolerance.length; i++) {
if (withinTolerance[i].layer._gridlineGroup != guideType) {
intInfo = L.Snap.findGuideIntersection('guide', map, latlng, [guideInfo, withinTolerance[i]]);
if (intInfo.distance < tolerance) {
returnLatLng = intInfo.intersection;
break;
}
}
}
}
else {
if (withinTolerance.length == 2) {
intInfo = L.Snap.findGuideIntersection('grid', map, latlng, withinTolerance);
if (intInfo.distance < tolerance) {
returnLatLng = intInfo.intersection;
}
}
}
return {
'layer' : returnLayer,
'latlng': returnLatLng
};
};
// Compatibility method to normalize Poly* objects
// between 0.7.x and 1.0+
// pulled from code from L.Edit.Poly in Leaflet.Draw
L.Snap.defaultShape = function (latlngs) {
if (!L.Polyline._flat) { return latlngs; }
return L.Polyline._flat(latlngs) ? latlngs : latlngs[0];
};
// try to prefer the corner of guidelines, or the the intersection of gridlines, if we're within the tolerance of two
L.Snap.findGuideIntersection = function (gType, map, latlng, guides) {
var nsi = (guides[0].layer['_' + gType + 'lineGroup'] == 'NS') ? 1 : 0;
var wei = (guides[0].layer['_' + gType + 'lineGroup'] == 'NS') ? 0 : 1;
var ns = L.Snap.defaultShape(guides[nsi].layer._latlngs)[0];
var we = L.Snap.defaultShape(guides[wei].layer._latlngs)[0];
var intersection = new L.LatLng(ns.lat, we.lng);
var distance = L.GeometryUtil.distance(map, intersection, latlng);
return {
'intersection': intersection,
'distance': distance
};
};
L.Snap.updateSnap = function (marker, layer, latlng) {
if (! marker.hasOwnProperty('_latlng')) {
return;
}
if (layer && latlng) {
// don't call setLatLng so that we don't fire an unnecessary 'move' event
marker._latlng = L.latLng(latlng);
marker.update();
if (marker.snap != layer) {
marker.snap = layer;
if (marker._icon) {
L.DomUtil.addClass(marker._icon, 'marker-snapped');
}
marker.fire('snap', {layer:layer, latlng: latlng});
}
}
else {
if (marker.snap) {
if (marker._icon) {
L.DomUtil.removeClass(marker._icon, 'marker-snapped');
}
marker.fire('unsnap', {layer: marker.snap});
}
delete marker.snap;
}
};
L.Snap.snapMarker = function (e, guides, map, options, buffer) {
var marker = e.target;
var latlng = e.target._latlng || e.latlng;
if (! latlng) {
return;
}
var snaplist = [];
for (var i=0, n = guides.length; i < n; i++) {
var guide = guides[i];
// don't snap to vertices of a poly object for poly move
if (marker.hasOwnProperty('_owner') && (guide._leaflet_id == marker._owner)) {
continue;
}
L.Snap.processGuide(latlng, marker, guide, snaplist, buffer);
}
if (snaplist.length === 0) {
return;
}
var closest = L.Snap.findClosestLayerSnap(map, snaplist, latlng, options.snapDistance, options.snapVertices);
closest = closest || {layer: null, latlng: null};
L.Snap.updateSnap(marker, closest.layer, closest.latlng);
if (e.latlng && closest.latlng) {
e.latlng = closest.latlng;
}
return closest;
};
L.Handler.MarkerSnap = L.Handler.extend({
options: {
snapDistance: 15, // in pixels
snapVertices: true
},
initialize: function (map, marker, options) {
L.Handler.prototype.initialize.call(this, map);
this._markers = [];
this._guides = [];
if (arguments.length == 2) {
if (!(marker instanceof L.Class)) {
options = marker;
marker = null;
}
}
L.Util.setOptions(this, options || {});
if (marker) {
// new markers should be draggable !
if (!marker.dragging) marker.dragging = new L.Handler.MarkerDrag(marker);
marker.dragging.enable();
this.watchMarker(marker);
}
// Convert snap distance in pixels into buffer in degres, for searching around mouse
// It changes at each zoom change.
function computeBuffer() {
this._buffer = map.layerPointToLatLng(new L.Point(0,0)).lat -
map.layerPointToLatLng(new L.Point(this.options.snapDistance, 0)).lat;
}
map.on('zoomend', computeBuffer, this);
map.whenReady(computeBuffer, this);
computeBuffer.call(this);
},
enable: function () {
this.disable();
for (var i=0; i<this._markers.length; i++) {
this.watchMarker(this._markers[i]);
}
},
disable: function () {
for (var i=0; i<this._markers.length; i++) {
this.unwatchMarker(this._markers[i]);
}
},
watchMarker: function (marker) {
if (this._markers.indexOf(marker) == -1)
this._markers.push(marker);
marker.on('move', this._snapMarker, this);
this._map.on('touchmove', this._snapMarker, this);
},
unwatchMarker: function (marker) {
marker.off('move', this._snapMarker, this);
this._map.off('touchmove', this._snapMarker, this);
delete marker.snap;
},
addGuideLayer: function (layer) {
for (var i=0, n=this._guides.length; i<n; i++)
if (L.stamp(layer) == L.stamp(this._guides[i]))
return;
this._guides.push(layer);
},
_snapMarker: function(e) {
var closest = L.Snap.snapMarker(e, this._guides, this._map, this.options, this._buffer);
if (e.originalEvent && e.originalEvent.clientX && closest.layer && closest.latlng) {
var snapTouchPoint = this._map.project(closest.latlng, this._map.getZoom());
e.originalEvent.clientX = snapTouchPoint.x;
e.originalEvent.clientY = snapTouchPoint.y;
e.originalEvent.snapped = true;
}
}
});
L.Handler.PolylineSnap = L.Edit.Poly.extend({
initialize: function (map, poly, options) {
var that = this;
L.Edit.Poly.prototype.initialize.call(this, poly, options);
this._snapper = new L.Handler.MarkerSnap(map, options);
poly.on('remove', function() {
that.disable();
});
},
addGuideLayer: function (layer) {
this._snapper.addGuideLayer(layer);
},
_createMoveMarker: function (latlng, icon) {
var marker = L.Edit.Poly.prototype._createMoveMarker.call(this, latlng, icon);
this._poly.snapediting._snapper.watchMarker(marker);
return marker;
},
_initHandlers: function () {
this._verticesHandlers = [];
for (var i = 0; i < this.latlngs.length; i++) {
this._verticesHandlers.push(new L.Edit.PolyVerticesEditSnap(this._poly, this.latlngs[i], this.options));
}
}
});
L.Edit.PolyVerticesEditSnap = L.Edit.PolyVerticesEdit.extend({
_createMarker: function (latlng, index) {
var marker = L.Edit.PolyVerticesEdit.prototype._createMarker.call(this, latlng, index);
// Treat middle markers differently
var isMiddle = ((index === null) || (typeof(index) == 'undefined'));
if (isMiddle) {
// Snap middle markers, only once they were touched
marker.on('dragstart', function () {
this._poly.snapediting._snapper.watchMarker(marker);
}, this);
}
else {
this._poly.snapediting._snapper.watchMarker(marker);
}
return marker;
}
});
L.Handler.RectangleSnap = L.Edit.Rectangle.extend({
initialize: function (map, shape, options) {
L.Edit.Rectangle.prototype.initialize.call(this, shape, options);
this._snapper = new L.Handler.MarkerSnap(map, options);
},
_createMarker: function (latlng, icon) {
var marker = L.Edit.Rectangle.prototype._createMarker.call(this, latlng, icon);
this._shape.snapediting._snapper.watchMarker(marker);
return marker;
},
addGuideLayer: function (layer) {
this._snapper.addGuideLayer(layer);
},
});
L.Handler.CircleSnap = L.Edit.Circle.extend({
initialize: function (map, shape, options) {
L.Edit.Circle.prototype.initialize.call(this, shape, options);
this._snapper = new L.Handler.MarkerSnap(map, options);
},
_createMarker: function (latlng, icon) {
var marker = L.Edit.Circle.prototype._createMarker.call(this, latlng, icon);
this._shape.snapediting._snapper.watchMarker(marker);
return marker;
},
addGuideLayer: function (layer) {
this._snapper.addGuideLayer(layer);
},
});
L.EditToolbar.SnapEdit = L.EditToolbar.Edit.extend({
snapOptions: {
snapDistance: 15, // in pixels
snapVertices: true
},
initialize: function(map, options) {
L.EditToolbar.Edit.prototype.initialize.call(this, map, options);
if (options.snapOptions) {
L.Util.extend(this.snapOptions, options.snapOptions);
}
if (Array.isArray(this.snapOptions.guideLayers)) {
this._guideLayers = this.snapOptions.guideLayers;
} else if (options.guideLayers instanceof L.LayerGroup) {
this._guideLayers = this.snapOptions.guideLayers.getLayers();
} else {
this._guideLayers = [];
}
},
addGuideLayer: function(layer) {
var index = this._guideLayers.findIndex(function(guideLayer) {
return L.stamp(layer) == L.stamp(guideLayer);
});
if (index == -1) {
this._guideLayers.push(layer);
this._featureGroup.eachLayer(function(layer) {
if (layer.snapediting) {
layer.snapediting._guides.push(layer);
}
});
}
},
removeGuideLayer: function(layer) {
var index = this._guideLayers.findIndex(function(guideLayer) {
return L.stamp(layer) == L.stamp(guideLayer);
});
if (index !== -1) {
this._guideLayers.splice(index, 1);
this._featureGroup.eachLayer(function(layer) {
if (layer.snapediting) { layer.snapediting._guides.splice(index, 1); }
});
}
},
clearGuideLayers: function() {
this._guideLayers = [];
this._featureGroup.eachLayer(function(layer) {
if (layer.snapediting) { layer.snapediting._guides = []; }
});
},
// essentially, the idea here is that we're gonna find the currently instantiated L.Edit handler, figure out its type,
// get rid of it, and then replace it with a snapedit instead
_enableLayerEdit: function(e) {
L.EditToolbar.Edit.prototype._enableLayerEdit.call(this, e);
var layer = e.layer || e.target || e;
if (!layer.snapediting) {
if (layer.hasOwnProperty('_mRadius')) {
if (layer.editing) {
layer.editing._markerGroup.clearLayers();
delete layer.editing;
}
layer.editing = layer.snapediting = new L.Handler.CircleSnap(layer._map, layer, this.snapOptions);
}
else if (layer.getLatLng) {
layer.snapediting = new L.Handler.MarkerSnap(layer._map, layer, this.snapOptions);
}
else {
if (layer.editing) {
if (layer.editing.hasOwnProperty('_shape')) {
layer.editing._markerGroup.clearLayers();
if (layer.editing._shape instanceof L.Rectangle) {
delete layer.editing;
layer.editing = layer.snapediting = new L.Handler.RectangleSnap(layer._map, layer, this.snapOptions);
}
else if (layer.editing._shape instanceof L.FeatureGroup) {
delete layer.editing;
layer.editing = layer.snapediting = new L.Handler.FeatureGroupSnap(layer._map, layer, this.snapOptions);
}
else {
delete layer.editing;
layer.editing = layer.snapediting = new L.Handler.CircleSnap(layer._map, layer, this.snapOptions);
}
}
else {
layer.editing._markerGroup.clearLayers();
layer.editing._verticesHandlers[0]._markerGroup.clearLayers();
delete layer.editing;
layer.editing = layer.snapediting = new L.Handler.PolylineSnap(layer._map, layer, this.snapOptions);
}
}
else {
layer.editing = layer.snapediting = new L.Handler.PolylineSnap(layer._map, layer, this.snapOptions);
}
}
for (var i = 0, n = this._guideLayers.length; i < n; i++) {
layer.snapediting.addGuideLayer(this._guideLayers[i]);
}
}
layer.snapediting.enable();
}
});
L.EditToolbar.prototype.getEditHandler = function (map, featureGroup) {
return new L.EditToolbar.SnapEdit(map, {
snapOptions: this.options.snapOptions,
featureGroup: featureGroup,
selectedPathOptions: this.options.edit.selectedPathOptions,
poly: this.options.poly
});
};
L.Draw.Feature.SnapMixin = {
_snap_initialize: function () {
this.on('enabled', this._snap_on_enabled, this);
this.on('disabled', this._snap_on_disabled, this);
},
_snap_on_enabled: function () {
if (!this.options.guideLayers) {
return;
}
if (! this._mouseMarker) {
this._map.on('layeradd', this._snap_on_enabled, this);
return;
}
else {
this._map.off('layeradd', this._snap_on_enabled, this);
}
if (!this._snapper) {
this._snapper = new L.Handler.MarkerSnap(this._map);
if (this.options.snapDistance) {
this._snapper.options.snapDistance = this.options.snapDistance;
}
if (this.options.snapVertices) {
this._snapper.options.snapVertices = this.options.snapVertices;
}
}
for (var i=0, n=this.options.guideLayers.length; i<n; i++) {
this._snapper.addGuideLayer(this.options.guideLayers[i]);
}
var marker = this._mouseMarker;
this._snapper.watchMarker(marker);
// Show marker when (snap for user feedback)
var icon = marker.options.icon;
marker.on('snap', function (e) {
marker.setIcon(this.options.icon);
marker.setOpacity(1);
}, this);
marker.on('unsnap', function (e) {
marker.setIcon(icon);
marker.setOpacity(0);
}, this);
marker.on('click', this._snap_on_click, this);
this._map.on('mousedown', this._snap_on_click, this);
this._map.on('touchstart', this._snap_on_click, this);
},
_snap_on_click: function (e) {
if (this._errorShown) {
return;
}
// for touch
if (this._markers) {
var markerCount = this._markers.length;
var marker = this._markers[markerCount - 1];
if (marker && this._mouseMarker.snap) {
L.DomUtil.addClass(marker._icon, 'marker-snapped');
}
}
// for shapes
if (this._startLatLng) {
var closest = this._manuallyCorrectClick(this._startLatLng);
if (closest.latlng) {
this._mouseMarker.setLatLng(closest.latlng);
this._startLatLng = closest.latlng;
}
}
// for poly vertices
if (this._mouseDownOrigin) {
var z = this._map.getZoom();
var mdOrigin = this._map.unproject(this._mouseDownOrigin, z);
var closestMDO = this._manuallyCorrectClick(mdOrigin);
if (closestMDO.latlng) {
this._mouseMarker.setLatLng(closestMDO.latlng);
this._mouseDownOrigin = this._map.project(closestMDO.latlng, z);
}
if (e.originalEvent) {
var oeOrigin = this._map.unproject([e.originalEvent.clientX, e.originalEvent.clientY], z);
var closestOE = this._manuallyCorrectClick(oeOrigin);
if (closestOE.latlng) {
e.originalEvent = this._map.project(closestOE.latlng, z);
}
}
}
},
_manuallyCorrectClick: function (originalLatLng) {
var ex = {
'target': this._mouseMarker,
'latlng': originalLatLng
};
if (! this._mouseMarker) {
return {
'latlng': null
};
}
var buffer = 0;
if (this.hasOwnProperty('_snapper') && this._snapper.hasOwnProperty('_buffer')) {
buffer = this._snapper._buffer;
}
return L.Snap.snapMarker(ex, this.options.guideLayers || [], this._map, this.options, buffer);
},
_snap_on_disabled: function () {
delete this._snapper;
},
};
L.Draw.Feature.include(L.Draw.Feature.SnapMixin);
L.Draw.Feature.addInitHook('_snap_initialize');