forked from ProminentEdge/leaflet-measure-path
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet-measure-path.js
453 lines (371 loc) · 15.1 KB
/
leaflet-measure-path.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
!(function() {
'use strict';
L.Marker.Measurement = L[L.Layer ? 'Layer' : 'Class'].extend({
options: {
pane: 'markerPane'
},
initialize: function(latlng, measurement, title, rotation, options) {
L.setOptions(this, options);
this._latlng = latlng;
this._measurement = measurement;
this._title = title;
this._rotation = rotation;
},
addTo: function(map) {
map.addLayer(this);
return this;
},
onAdd: function(map) {
this._map = map;
var pane = this.getPane ? this.getPane() : map.getPanes().markerPane;
var el = this._element = L.DomUtil.create('div', 'leaflet-zoom-animated leaflet-measure-path-measurement', pane);
var inner = L.DomUtil.create('div', '', el);
inner.title = this._title;
inner.innerHTML = this._measurement;
map.on('zoomanim', this._animateZoom, this);
this._setPosition();
},
onRemove: function(map) {
map.off('zoomanim', this._animateZoom, this);
var pane = this.getPane ? this.getPane() : map.getPanes().markerPane;
pane.removeChild(this._element);
this._map = null;
},
_setPosition: function() {
L.DomUtil.setPosition(this._element, this._map.latLngToLayerPoint(this._latlng));
this._element.style.transform += ' rotate(' + this._rotation + 'rad)';
},
_animateZoom: function(opt) {
var pos = this._map._latLngToNewLayerPoint(this._latlng, opt.zoom, opt.center).round();
L.DomUtil.setPosition(this._element, pos);
this._element.style.transform += ' rotate(' + this._rotation + 'rad)';
}
});
L.marker.measurement = function(latLng, measurement, title, rotation, options) {
return new L.Marker.Measurement(latLng, measurement, title, rotation, options);
};
var formatDistance = function(d) {
var unit,
feet;
if (this._measurementOptions.imperial) {
feet = d / 0.3048;
if (feet > 3000) {
d = d / 1609.344;
unit = 'mi';
} else {
d = feet;
unit = 'ft';
}
} else {
if (d > 1000) {
d = d / 1000;
unit = 'km';
} else {
unit = 'm';
}
}
if (d < 100) {
return d.toFixed(1) + ' ' + unit;
} else {
return Math.round(d) + ' ' + unit;
}
}
var formatArea = function(a) {
var unit,
sqfeet;
if (this._measurementOptions.imperial) {
if (a > 404.685642) {
a = a / 4046.85642;
unit = 'ac';
} else {
a = a / 0.09290304;
unit = 'ft²';
}
} else if (this._measurementOptions.ha) {
if (a > 1000000000) {
a = a / 1000000000;
unit = 'km²';
} else if (a > 10000) {
a = a / 10000;
unit = 'ha';
} else {
unit = 'm²';
}
} else {
if (a > 1000000) {
a = a / 1000000;
unit = 'km²';
} else {
unit = 'm²';
}
}
if (a < 100) {
return a.toFixed(1) + ' ' + unit;
} else {
return Math.round(a) + ' ' + unit;
}
}
var RADIUS = 6378137;
// ringArea function copied from geojson-area
// (https://github.com/mapbox/geojson-area)
// This function is distributed under a separate license,
// see LICENSE.md.
var ringArea = function ringArea(coords) {
var rad = function rad(_) {
return _ * Math.PI / 180;
};
var p1, p2, p3, lowerIndex, middleIndex, upperIndex,
area = 0,
coordsLength = coords.length;
if (coordsLength > 2) {
for (var i = 0; i < coordsLength; i++) {
if (i === coordsLength - 2) {// i = N-2
lowerIndex = coordsLength - 2;
middleIndex = coordsLength -1;
upperIndex = 0;
} else if (i === coordsLength - 1) {// i = N-1
lowerIndex = coordsLength - 1;
middleIndex = 0;
upperIndex = 1;
} else { // i = 0 to N-3
lowerIndex = i;
middleIndex = i+1;
upperIndex = i+2;
}
p1 = coords[lowerIndex];
p2 = coords[middleIndex];
p3 = coords[upperIndex];
area += ( rad(p3.lng) - rad(p1.lng) ) * Math.sin( rad(p2.lat));
}
area = area * RADIUS * RADIUS / 2;
}
return Math.abs(area);
};
/**
* Handles the init hook for polylines and circles.
* Implements the showOnHover functionality if called for.
*/
var addInitHook = function() {
var showOnHover = this.options.measurementOptions && this.options.measurementOptions.showOnHover;
if (this.options.showMeasurements && !showOnHover) {
this.showMeasurements();
}
if (this.options.showMeasurements && showOnHover) {
this.on('mouseover', function() {
this.showMeasurements();
});
this.on('mouseout', function() {
this.hideMeasurements();
});
}
};
var circleArea = function circleArea(d) {
var rho = d / RADIUS;
return 2 * Math.PI * RADIUS * RADIUS * (1 - Math.cos(rho));
};
var override = function(method, fn, hookAfter) {
if (!hookAfter) {
return function() {
var originalReturnValue = method.apply(this, arguments);
var args = Array.prototype.slice.call(arguments)
args.push(originalReturnValue);
return fn.apply(this, args);
}
} else {
return function() {
fn.apply(this, arguments);
return method.apply(this, arguments);
}
}
};
L.Polyline.include({
showMeasurements: function(options) {
if (!this._map || this._measurementLayer) return this;
this._measurementOptions = L.extend({
showOnHover: (options && options.showOnHover) || false,
minPixelDistance: 30,
showDistances: true,
showArea: true,
showTotalDistance: true,
lang: {
totalLength: 'Total length',
totalArea: 'Total area',
segmentLength: 'Segment length'
}
}, options || {});
this._measurementLayer = L.layerGroup().addTo(this._map);
this.updateMeasurements();
this._map.on('zoomend', this.updateMeasurements, this);
return this;
},
hideMeasurements: function() {
if (!this._map) return this;
this._map.off('zoomend', this.updateMeasurements, this);
if (!this._measurementLayer) return this;
this._map.removeLayer(this._measurementLayer);
this._measurementLayer = null;
return this;
},
onAdd: override(L.Polyline.prototype.onAdd, function(originalReturnValue) {
var showOnHover = this.options.measurementOptions && this.options.measurementOptions.showOnHover;
if (this.options.showMeasurements && !showOnHover) {
this.showMeasurements(this.options.measurementOptions);
}
return originalReturnValue;
}),
onRemove: override(L.Polyline.prototype.onRemove, function(originalReturnValue) {
this.hideMeasurements();
return originalReturnValue;
}, true),
setLatLngs: override(L.Polyline.prototype.setLatLngs, function(originalReturnValue) {
this.updateMeasurements();
return originalReturnValue;
}),
spliceLatLngs: override(L.Polyline.prototype.spliceLatLngs, function(originalReturnValue) {
this.updateMeasurements();
return originalReturnValue;
}),
formatDistance: formatDistance,
formatArea: formatArea,
updateMeasurements: function() {
if (!this._measurementLayer) return this;
this._totalArea = 0;
var latLngs = this.getLatLngs(),
isPolygon = this instanceof L.Polygon,
options = this._measurementOptions,
totalDist = 0,
multi = false,
formatter,
ll1,
ll2,
p1,
p2,
pixelDist,
dist;
this._measurementLayer.clearLayers();
let _this = this;
let proccess = function(theLatLngs){
if (theLatLngs && theLatLngs.length && L.Util.isArray(theLatLngs[0])) {
// Outer ring is stored as an array in the first element,
// use that instead.
multi = true;
theLatLngs.forEach(function(ll){
proccess(ll);
});
} else {
totalDist = 0;
if (_this._measurementOptions.showDistances && theLatLngs.length > 1) {
formatter = _this._measurementOptions.formatDistance || L.bind(_this.formatDistance, _this);
for (var i = 1, len = theLatLngs.length; (isPolygon && i <= len) || i < len; i++) {
ll1 = theLatLngs[i - 1];
ll2 = theLatLngs[i % len];
dist = ll1.distanceTo(ll2);
totalDist += dist;
p1 = _this._map.latLngToLayerPoint(ll1);
p2 = _this._map.latLngToLayerPoint(ll2);
pixelDist = p1.distanceTo(p2);
if (pixelDist >= options.minPixelDistance) {
L.marker.measurement(
_this._map.layerPointToLatLng([(p1.x + p2.x) / 2, (p1.y + p2.y) / 2]),
formatter(dist), options.lang.segmentLength, _this._getRotation(ll1, ll2), options)
.addTo(_this._measurementLayer);
}
}
// Show total length for polylines
if (!multi && !isPolygon && _this._measurementOptions.showTotalDistance) {
L.marker.measurement(ll2, formatter(totalDist), options.lang.totalLength, 0, options)
.addTo(_this._measurementLayer);
}
}
}
}
proccess(latLngs);
latLngs = _this.getLatLngs();
// Show total area for polygons, use turf if available, if not, multi-polygons are not supported
if(isPolygon && options.showArea && (latLngs.length > 2 || multi)) {
if(typeof turf === "object" && typeof turf.area === "function"){
formatter = options.formatArea || L.bind(_this.formatArea, _this);
L.marker.measurement(_this.getBounds().getCenter(),
formatter(turf.area(_this.toGeoJSON())), options.lang.totalArea, 0, options)
.addTo(_this._measurementLayer);
} else if(!multi){
formatter = options.formatArea || L.bind(_this.formatArea, _this);
var area = ringArea(latLngs);
L.marker.measurement(_this.getBounds().getCenter(),
formatter(area), options.lang.totalArea, 0, options)
.addTo(_this._measurementLayer);
}
}
return this;
},
_getRotation: function(ll1, ll2) {
var p1 = this._map.project(ll1),
p2 = this._map.project(ll2);
return Math.atan((p2.y - p1.y) / (p2.x - p1.x));
}
});
L.Polyline.addInitHook(function() {
addInitHook.call(this);
});
L.Circle.include({
showMeasurements: function(options) {
if (!this._map || this._measurementLayer) return this;
this._measurementOptions = L.extend({
showOnHover: false,
showArea: true,
lang: {
totalArea: 'Total area',
}
}, options || {});
this._measurementLayer = L.layerGroup().addTo(this._map);
this.updateMeasurements();
this._map.on('zoomend', this.updateMeasurements, this);
return this;
},
hideMeasurements: function() {
if (!this._map) return this;
this._map.on('zoomend', this.updateMeasurements, this);
if (!this._measurementLayer) return this;
this._map.removeLayer(this._measurementLayer);
this._measurementLayer = null;
return this;
},
onAdd: override(L.Circle.prototype.onAdd, function(originalReturnValue) {
var showOnHover = this.options.measurementOptions && this.options.measurementOptions.showOnHover;
if (this.options.showMeasurements && !showOnHover) {
this.showMeasurements(this.options.measurementOptions);
}
return originalReturnValue;
}),
onRemove: override(L.Circle.prototype.onRemove, function(originalReturnValue) {
this.hideMeasurements();
return originalReturnValue;
}, true),
setLatLng: override(L.Circle.prototype.setLatLng, function(originalReturnValue) {
this.updateMeasurements();
return originalReturnValue;
}),
setRadius: override(L.Circle.prototype.setRadius, function(originalReturnValue) {
this.updateMeasurements();
return originalReturnValue;
}),
formatArea: formatArea,
updateMeasurements: function() {
if (!this._measurementLayer) return;
var latLng = this.getLatLng(),
options = this._measurementOptions,
formatter = options.formatArea || L.bind(this.formatArea, this);
this._measurementLayer.clearLayers();
if (options.showArea) {
formatter = options.formatArea || L.bind(this.formatArea, this);
var area = circleArea(this.getRadius());
L.marker.measurement(latLng,
formatter(area), options.lang.totalArea, 0, options)
.addTo(this._measurementLayer);
}
}
})
L.Circle.addInitHook(function() {
addInitHook.call(this);
});
})();