| 1 |
|
| 2 |
(function (factory) { |
| 3 |
var L; |
| 4 |
if (typeof define === 'function' && define.amd) { |
| 5 |
|
| 6 |
define(['leaflet'], factory); |
| 7 |
} else if (typeof module !== 'undefined') { |
| 8 |
|
| 9 |
L = require('leaflet'); |
| 10 |
module.exports = factory(L); |
| 11 |
} else { |
| 12 |
|
| 13 |
if (typeof window.L === 'undefined') |
| 14 |
throw 'Leaflet must be loaded first'; |
| 15 |
factory(window.L); |
| 16 |
} |
| 17 |
}(function (L) { |
| 18 |
"use strict"; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
L.GeometryUtil = L.extend(L.GeometryUtil || {}, { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
distance: function (map, latlngA, latlngB) { |
| 35 |
return map.latLngToLayerPoint(latlngA).distanceTo(map.latLngToLayerPoint(latlngB)); |
| 36 |
}, |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
distanceSegment: function (map, latlng, latlngA, latlngB) { |
| 47 |
var p = map.latLngToLayerPoint(latlng), |
| 48 |
p1 = map.latLngToLayerPoint(latlngA), |
| 49 |
p2 = map.latLngToLayerPoint(latlngB); |
| 50 |
return L.LineUtil.pointToSegmentDistance(p, p1, p2); |
| 51 |
}, |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
readableDistance: function (distance, unit) { |
| 60 |
var isMetric = (unit !== 'imperial'), |
| 61 |
distanceStr; |
| 62 |
if (isMetric) { |
| 63 |
|
| 64 |
if (distance > 1000) { |
| 65 |
distanceStr = (distance / 1000).toFixed(2) + ' km'; |
| 66 |
} |
| 67 |
else { |
| 68 |
distanceStr = Math.ceil(distance) + ' m'; |
| 69 |
} |
| 70 |
} |
| 71 |
else { |
| 72 |
distance *= 1.09361; |
| 73 |
if (distance > 1760) { |
| 74 |
distanceStr = (distance / 1760).toFixed(2) + ' miles'; |
| 75 |
} |
| 76 |
else { |
| 77 |
distanceStr = Math.ceil(distance) + ' yd'; |
| 78 |
} |
| 79 |
} |
| 80 |
return distanceStr; |
| 81 |
}, |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
belongsSegment: function(latlng, latlngA, latlngB, tolerance) { |
| 92 |
tolerance = tolerance === undefined ? 0.2 : tolerance; |
| 93 |
var hypotenuse = latlngA.distanceTo(latlngB), |
| 94 |
delta = latlngA.distanceTo(latlng) + latlng.distanceTo(latlngB) - hypotenuse; |
| 95 |
return delta/hypotenuse < tolerance; |
| 96 |
}, |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
length: function (coords) { |
| 104 |
var accumulated = L.GeometryUtil.accumulatedLengths(coords); |
| 105 |
return accumulated.length > 0 ? accumulated[accumulated.length-1] : 0; |
| 106 |
}, |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
accumulatedLengths: function (coords) { |
| 114 |
if (typeof coords.getLatLngs == 'function') { |
| 115 |
coords = coords.getLatLngs(); |
| 116 |
} |
| 117 |
if (coords.length === 0) |
| 118 |
return []; |
| 119 |
var total = 0, |
| 120 |
lengths = [0]; |
| 121 |
for (var i = 0, n = coords.length - 1; i< n; i++) { |
| 122 |
total += coords[i].distanceTo(coords[i+1]); |
| 123 |
lengths.push(total); |
| 124 |
} |
| 125 |
return lengths; |
| 126 |
}, |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
closestOnSegment: function (map, latlng, latlngA, latlngB) { |
| 137 |
var maxzoom = map.getMaxZoom(); |
| 138 |
if (maxzoom === Infinity) |
| 139 |
maxzoom = map.getZoom(); |
| 140 |
var p = map.project(latlng, maxzoom), |
| 141 |
p1 = map.project(latlngA, maxzoom), |
| 142 |
p2 = map.project(latlngB, maxzoom), |
| 143 |
closest = L.LineUtil.closestPointOnSegment(p, p1, p2); |
| 144 |
return map.unproject(closest, maxzoom); |
| 145 |
}, |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
closest: function (map, layer, latlng, vertices) { |
| 156 |
if (typeof layer.getLatLngs != 'function') |
| 157 |
layer = L.polyline(layer); |
| 158 |
|
| 159 |
var latlngs = layer.getLatLngs().slice(0), |
| 160 |
mindist = Infinity, |
| 161 |
result = null, |
| 162 |
i, n, distance; |
| 163 |
|
| 164 |
|
| 165 |
if (vertices) { |
| 166 |
for(i = 0, n = latlngs.length; i < n; i++) { |
| 167 |
var ll = latlngs[i]; |
| 168 |
distance = L.GeometryUtil.distance(map, latlng, ll); |
| 169 |
if (distance < mindist) { |
| 170 |
mindist = distance; |
| 171 |
result = ll; |
| 172 |
result.distance = distance; |
| 173 |
} |
| 174 |
} |
| 175 |
return result; |
| 176 |
} |
| 177 |
|
| 178 |
if (layer instanceof L.Polygon) { |
| 179 |
latlngs.push(latlngs[0]); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
for (i = 0, n = latlngs.length; i < n-1; i++) { |
| 184 |
var latlngA = latlngs[i], |
| 185 |
latlngB = latlngs[i+1]; |
| 186 |
distance = L.GeometryUtil.distanceSegment(map, latlng, latlngA, latlngB); |
| 187 |
if (distance <= mindist) { |
| 188 |
mindist = distance; |
| 189 |
result = L.GeometryUtil.closestOnSegment(map, latlng, latlngA, latlngB); |
| 190 |
result.distance = distance; |
| 191 |
} |
| 192 |
} |
| 193 |
return result; |
| 194 |
}, |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
closestLayer: function (map, layers, latlng) { |
| 204 |
var mindist = Infinity, |
| 205 |
result = null, |
| 206 |
ll = null, |
| 207 |
distance = Infinity; |
| 208 |
|
| 209 |
for (var i = 0, n = layers.length; i < n; i++) { |
| 210 |
var layer = layers[i]; |
| 211 |
|
| 212 |
if (typeof layer.getLatLng == 'function') { |
| 213 |
ll = layer.getLatLng(); |
| 214 |
distance = L.GeometryUtil.distance(map, latlng, ll); |
| 215 |
} |
| 216 |
else { |
| 217 |
ll = L.GeometryUtil.closest(map, layer, latlng); |
| 218 |
if (ll) distance = ll.distance; |
| 219 |
} |
| 220 |
if (distance < mindist) { |
| 221 |
mindist = distance; |
| 222 |
result = {layer: layer, latlng: ll, distance: distance}; |
| 223 |
} |
| 224 |
} |
| 225 |
return result; |
| 226 |
}, |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
closestLayerSnap: function (map, layers, latlng, tolerance, withVertices) { |
| 239 |
tolerance = typeof tolerance == 'number' ? tolerance : Infinity; |
| 240 |
withVertices = typeof withVertices == 'boolean' ? withVertices : true; |
| 241 |
|
| 242 |
var result = L.GeometryUtil.closestLayer(map, layers, latlng); |
| 243 |
if (!result || result.distance > tolerance) |
| 244 |
return null; |
| 245 |
|
| 246 |
|
| 247 |
if (withVertices && typeof result.layer.getLatLngs == 'function') { |
| 248 |
var closest = L.GeometryUtil.closest(map, result.layer, result.latlng, true); |
| 249 |
if (closest.distance < tolerance) { |
| 250 |
result.latlng = closest; |
| 251 |
result.distance = L.GeometryUtil.distance(map, closest, latlng); |
| 252 |
} |
| 253 |
} |
| 254 |
return result; |
| 255 |
}, |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
interpolateOnPointSegment: function (pA, pB, ratio) { |
| 265 |
return L.point( |
| 266 |
(pA.x * (1 - ratio)) + (ratio * pB.x), |
| 267 |
(pA.y * (1 - ratio)) + (ratio * pB.y) |
| 268 |
); |
| 269 |
}, |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
interpolateOnLine: function (map, latLngs, ratio) { |
| 280 |
latLngs = (latLngs instanceof L.Polyline) ? latLngs.getLatLngs() : latLngs; |
| 281 |
var n = latLngs.length; |
| 282 |
if (n < 2) { |
| 283 |
return null; |
| 284 |
} |
| 285 |
|
| 286 |
if (ratio === 0) { |
| 287 |
return { |
| 288 |
latLng: latLngs[0] instanceof L.LatLng ? latLngs[0] : L.latLng(latLngs[0]), |
| 289 |
predecessor: -1 |
| 290 |
}; |
| 291 |
} |
| 292 |
if (ratio == 1) { |
| 293 |
return { |
| 294 |
latLng: latLngs[latLngs.length -1] instanceof L.LatLng ? latLngs[latLngs.length -1] : L.latLng(latLngs[latLngs.length -1]), |
| 295 |
predecessor: latLngs.length - 2 |
| 296 |
}; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
ratio = Math.max(Math.min(ratio, 1), 0); |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
var maxzoom = map.getMaxZoom(); |
| 305 |
if (maxzoom === Infinity) |
| 306 |
maxzoom = map.getZoom(); |
| 307 |
var pts = []; |
| 308 |
var lineLength = 0; |
| 309 |
for(var i = 0; i < n; i++) { |
| 310 |
pts[i] = map.project(latLngs[i], maxzoom); |
| 311 |
if(i > 0) |
| 312 |
lineLength += pts[i-1].distanceTo(pts[i]); |
| 313 |
} |
| 314 |
|
| 315 |
var ratioDist = lineLength * ratio; |
| 316 |
var a = pts[0], |
| 317 |
b = pts[1], |
| 318 |
distA = 0, |
| 319 |
distB = a.distanceTo(b); |
| 320 |
|
| 321 |
|
| 322 |
var index = 1; |
| 323 |
for (; index < n && distB < ratioDist; index++) { |
| 324 |
a = b; |
| 325 |
distA = distB; |
| 326 |
b = pts[index]; |
| 327 |
distB += a.distanceTo(b); |
| 328 |
} |
| 329 |
|
| 330 |
var segmentRatio = ((distB - distA) !== 0) ? ((ratioDist - distA) / (distB - distA)) : 0; |
| 331 |
var interpolatedPoint = L.GeometryUtil.interpolateOnPointSegment(a, b, segmentRatio); |
| 332 |
return { |
| 333 |
latLng: map.unproject(interpolatedPoint, maxzoom), |
| 334 |
predecessor: index-2 |
| 335 |
}; |
| 336 |
}, |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
locateOnLine: function (map, polyline, latlng) { |
| 348 |
var latlngs = polyline.getLatLngs(); |
| 349 |
if (latlng.equals(latlngs[0])) |
| 350 |
return 0.0; |
| 351 |
if (latlng.equals(latlngs[latlngs.length-1])) |
| 352 |
return 1.0; |
| 353 |
|
| 354 |
var point = L.GeometryUtil.closest(map, polyline, latlng, false), |
| 355 |
lengths = L.GeometryUtil.accumulatedLengths(latlngs), |
| 356 |
total_length = lengths[lengths.length-1], |
| 357 |
portion = 0, |
| 358 |
found = false; |
| 359 |
for (var i=0, n = latlngs.length-1; i < n; i++) { |
| 360 |
var l1 = latlngs[i], |
| 361 |
l2 = latlngs[i+1]; |
| 362 |
portion = lengths[i]; |
| 363 |
if (L.GeometryUtil.belongsSegment(point, l1, l2)) { |
| 364 |
portion += l1.distanceTo(point); |
| 365 |
found = true; |
| 366 |
break; |
| 367 |
} |
| 368 |
} |
| 369 |
if (!found) { |
| 370 |
throw "Could not interpolate " + latlng.toString() + " within " + polyline.toString(); |
| 371 |
} |
| 372 |
return portion / total_length; |
| 373 |
}, |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
reverse: function (polyline) { |
| 381 |
return L.polyline(polyline.getLatLngs().slice(0).reverse()); |
| 382 |
}, |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
extract: function (map, polyline, start, end) { |
| 394 |
if (start > end) { |
| 395 |
return L.GeometryUtil.extract(map, L.GeometryUtil.reverse(polyline), 1.0-start, 1.0-end); |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
start = Math.max(Math.min(start, 1), 0); |
| 400 |
end = Math.max(Math.min(end, 1), 0); |
| 401 |
|
| 402 |
var latlngs = polyline.getLatLngs(), |
| 403 |
startpoint = L.GeometryUtil.interpolateOnLine(map, polyline, start), |
| 404 |
endpoint = L.GeometryUtil.interpolateOnLine(map, polyline, end); |
| 405 |
|
| 406 |
if (start == end) { |
| 407 |
var point = L.GeometryUtil.interpolateOnLine(map, polyline, end); |
| 408 |
return [point.latLng]; |
| 409 |
} |
| 410 |
|
| 411 |
if (startpoint.predecessor == -1) |
| 412 |
startpoint.predecessor = 0; |
| 413 |
if (endpoint.predecessor == -1) |
| 414 |
endpoint.predecessor = 0; |
| 415 |
var result = latlngs.slice(startpoint.predecessor+1, endpoint.predecessor+1); |
| 416 |
result.unshift(startpoint.latLng); |
| 417 |
result.push(endpoint.latLng); |
| 418 |
return result; |
| 419 |
}, |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
isBefore: function (polyline, other) { |
| 428 |
if (!other) return false; |
| 429 |
var lla = polyline.getLatLngs(), |
| 430 |
llb = other.getLatLngs(); |
| 431 |
return (lla[lla.length-1]).equals(llb[0]); |
| 432 |
}, |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
isAfter: function (polyline, other) { |
| 441 |
if (!other) return false; |
| 442 |
var lla = polyline.getLatLngs(), |
| 443 |
llb = other.getLatLngs(); |
| 444 |
return (lla[0]).equals(llb[llb.length-1]); |
| 445 |
}, |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
startsAtExtremity: function (polyline, other) { |
| 454 |
if (!other) return false; |
| 455 |
var lla = polyline.getLatLngs(), |
| 456 |
llb = other.getLatLngs(), |
| 457 |
start = lla[0]; |
| 458 |
return start.equals(llb[0]) || start.equals(llb[llb.length-1]); |
| 459 |
}, |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
computeAngle: function(a, b) { |
| 468 |
return (Math.atan2(b.y - a.y, b.x - a.x) * 180 / Math.PI); |
| 469 |
}, |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
computeSlope: function(a, b) { |
| 478 |
var s = (b.y - a.y) / (b.x - a.x), |
| 479 |
o = a.y - (s * a.x); |
| 480 |
return {'a': s, 'b': o}; |
| 481 |
}, |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
rotatePoint: function(map, latlngPoint, angleDeg, latlngCenter) { |
| 491 |
var maxzoom = map.getMaxZoom(); |
| 492 |
if (maxzoom === Infinity) |
| 493 |
maxzoom = map.getZoom(); |
| 494 |
var angleRad = angleDeg*Math.PI/180, |
| 495 |
pPoint = map.project(latlngPoint, maxzoom), |
| 496 |
pCenter = map.project(latlngCenter, maxzoom), |
| 497 |
x2 = Math.cos(angleRad)*(pPoint.x-pCenter.x) - Math.sin(angleRad)*(pPoint.y-pCenter.y) + pCenter.x, |
| 498 |
y2 = Math.sin(angleRad)*(pPoint.x-pCenter.x) + Math.cos(angleRad)*(pPoint.y-pCenter.y) + pCenter.y; |
| 499 |
return map.unproject(new L.Point(x2,y2), maxzoom); |
| 500 |
}, |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
bearing: function(latlng1, latlng2) { |
| 510 |
var rad = Math.PI / 180, |
| 511 |
lat1 = latlng1.lat * rad, |
| 512 |
lat2 = latlng2.lat * rad, |
| 513 |
lon1 = latlng1.lng * rad, |
| 514 |
lon2 = latlng2.lng * rad, |
| 515 |
y = Math.sin(lon2 - lon1) * Math.cos(lat2), |
| 516 |
x = Math.cos(lat1) * Math.sin(lat2) - |
| 517 |
Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1); |
| 518 |
|
| 519 |
var bearing = ((Math.atan2(y, x) * 180 / Math.PI) + 360) % 360; |
| 520 |
return bearing >= 180 ? bearing-360 : bearing; |
| 521 |
}, |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
destination: function(latlng, heading, distance) { |
| 534 |
heading = (heading + 360) % 360; |
| 535 |
var rad = Math.PI / 180, |
| 536 |
radInv = 180 / Math.PI, |
| 537 |
R = 6378137, |
| 538 |
lon1 = latlng.lng * rad, |
| 539 |
lat1 = latlng.lat * rad, |
| 540 |
rheading = heading * rad, |
| 541 |
sinLat1 = Math.sin(lat1), |
| 542 |
cosLat1 = Math.cos(lat1), |
| 543 |
cosDistR = Math.cos(distance / R), |
| 544 |
sinDistR = Math.sin(distance / R), |
| 545 |
lat2 = Math.asin(sinLat1 * cosDistR + cosLat1 * |
| 546 |
sinDistR * Math.cos(rheading)), |
| 547 |
lon2 = lon1 + Math.atan2(Math.sin(rheading) * sinDistR * |
| 548 |
cosLat1, cosDistR - sinLat1 * Math.sin(lat2)); |
| 549 |
lon2 = lon2 * radInv; |
| 550 |
lon2 = lon2 > 180 ? lon2 - 360 : lon2 < -180 ? lon2 + 360 : lon2; |
| 551 |
return L.latLng([lat2 * radInv, lon2]); |
| 552 |
} |
| 553 |
}); |
| 554 |
|
| 555 |
return L.GeometryUtil; |
| 556 |
|
| 557 |
})); |