|
|
import {Path} from './Path'; |
|
|
import * as Util from '../../core/Util'; |
|
|
import * as LineUtil from '../../geometry/LineUtil'; |
|
|
import {LatLng, toLatLng} from '../../geo/LatLng'; |
|
|
import {LatLngBounds} from '../../geo/LatLngBounds'; |
|
|
import {Bounds} from '../../geometry/Bounds'; |
|
|
import {Point} from '../../geometry/Point'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export var Polyline = Path.extend({ |
|
|
|
|
|
|
|
|
|
|
|
options: { |
|
|
|
|
|
|
|
|
|
|
|
smoothFactor: 1.0, |
|
|
|
|
|
|
|
|
|
|
|
noClip: false |
|
|
}, |
|
|
|
|
|
initialize: function (latlngs, options) { |
|
|
Util.setOptions(this, options); |
|
|
this._setLatLngs(latlngs); |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
getLatLngs: function () { |
|
|
return this._latlngs; |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
setLatLngs: function (latlngs) { |
|
|
this._setLatLngs(latlngs); |
|
|
return this.redraw(); |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
isEmpty: function () { |
|
|
return !this._latlngs.length; |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
closestLayerPoint: function (p) { |
|
|
var minDistance = Infinity, |
|
|
minPoint = null, |
|
|
closest = LineUtil._sqClosestPointOnSegment, |
|
|
p1, p2; |
|
|
|
|
|
for (var j = 0, jLen = this._parts.length; j < jLen; j++) { |
|
|
var points = this._parts[j]; |
|
|
|
|
|
for (var i = 1, len = points.length; i < len; i++) { |
|
|
p1 = points[i - 1]; |
|
|
p2 = points[i]; |
|
|
|
|
|
var sqDist = closest(p, p1, p2, true); |
|
|
|
|
|
if (sqDist < minDistance) { |
|
|
minDistance = sqDist; |
|
|
minPoint = closest(p, p1, p2); |
|
|
} |
|
|
} |
|
|
} |
|
|
if (minPoint) { |
|
|
minPoint.distance = Math.sqrt(minDistance); |
|
|
} |
|
|
return minPoint; |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
getCenter: function () { |
|
|
|
|
|
if (!this._map) { |
|
|
throw new Error('Must add layer to map before using getCenter()'); |
|
|
} |
|
|
return LineUtil.polylineCenter(this._defaultShape(), this._map.options.crs); |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
getBounds: function () { |
|
|
return this._bounds; |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
addLatLng: function (latlng, latlngs) { |
|
|
latlngs = latlngs || this._defaultShape(); |
|
|
latlng = toLatLng(latlng); |
|
|
latlngs.push(latlng); |
|
|
this._bounds.extend(latlng); |
|
|
return this.redraw(); |
|
|
}, |
|
|
|
|
|
_setLatLngs: function (latlngs) { |
|
|
this._bounds = new LatLngBounds(); |
|
|
this._latlngs = this._convertLatLngs(latlngs); |
|
|
}, |
|
|
|
|
|
_defaultShape: function () { |
|
|
return LineUtil.isFlat(this._latlngs) ? this._latlngs : this._latlngs[0]; |
|
|
}, |
|
|
|
|
|
|
|
|
_convertLatLngs: function (latlngs) { |
|
|
var result = [], |
|
|
flat = LineUtil.isFlat(latlngs); |
|
|
|
|
|
for (var i = 0, len = latlngs.length; i < len; i++) { |
|
|
if (flat) { |
|
|
result[i] = toLatLng(latlngs[i]); |
|
|
this._bounds.extend(result[i]); |
|
|
} else { |
|
|
result[i] = this._convertLatLngs(latlngs[i]); |
|
|
} |
|
|
} |
|
|
|
|
|
return result; |
|
|
}, |
|
|
|
|
|
_project: function () { |
|
|
var pxBounds = new Bounds(); |
|
|
this._rings = []; |
|
|
this._projectLatlngs(this._latlngs, this._rings, pxBounds); |
|
|
|
|
|
if (this._bounds.isValid() && pxBounds.isValid()) { |
|
|
this._rawPxBounds = pxBounds; |
|
|
this._updateBounds(); |
|
|
} |
|
|
}, |
|
|
|
|
|
_updateBounds: function () { |
|
|
var w = this._clickTolerance(), |
|
|
p = new Point(w, w); |
|
|
|
|
|
if (!this._rawPxBounds) { |
|
|
return; |
|
|
} |
|
|
|
|
|
this._pxBounds = new Bounds([ |
|
|
this._rawPxBounds.min.subtract(p), |
|
|
this._rawPxBounds.max.add(p) |
|
|
]); |
|
|
}, |
|
|
|
|
|
|
|
|
_projectLatlngs: function (latlngs, result, projectedBounds) { |
|
|
var flat = latlngs[0] instanceof LatLng, |
|
|
len = latlngs.length, |
|
|
i, ring; |
|
|
|
|
|
if (flat) { |
|
|
ring = []; |
|
|
for (i = 0; i < len; i++) { |
|
|
ring[i] = this._map.latLngToLayerPoint(latlngs[i]); |
|
|
projectedBounds.extend(ring[i]); |
|
|
} |
|
|
result.push(ring); |
|
|
} else { |
|
|
for (i = 0; i < len; i++) { |
|
|
this._projectLatlngs(latlngs[i], result, projectedBounds); |
|
|
} |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
_clipPoints: function () { |
|
|
var bounds = this._renderer._bounds; |
|
|
|
|
|
this._parts = []; |
|
|
if (!this._pxBounds || !this._pxBounds.intersects(bounds)) { |
|
|
return; |
|
|
} |
|
|
|
|
|
if (this.options.noClip) { |
|
|
this._parts = this._rings; |
|
|
return; |
|
|
} |
|
|
|
|
|
var parts = this._parts, |
|
|
i, j, k, len, len2, segment, points; |
|
|
|
|
|
for (i = 0, k = 0, len = this._rings.length; i < len; i++) { |
|
|
points = this._rings[i]; |
|
|
|
|
|
for (j = 0, len2 = points.length; j < len2 - 1; j++) { |
|
|
segment = LineUtil.clipSegment(points[j], points[j + 1], bounds, j, true); |
|
|
|
|
|
if (!segment) { continue; } |
|
|
|
|
|
parts[k] = parts[k] || []; |
|
|
parts[k].push(segment[0]); |
|
|
|
|
|
|
|
|
if ((segment[1] !== points[j + 1]) || (j === len2 - 2)) { |
|
|
parts[k].push(segment[1]); |
|
|
k++; |
|
|
} |
|
|
} |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
_simplifyPoints: function () { |
|
|
var parts = this._parts, |
|
|
tolerance = this.options.smoothFactor; |
|
|
|
|
|
for (var i = 0, len = parts.length; i < len; i++) { |
|
|
parts[i] = LineUtil.simplify(parts[i], tolerance); |
|
|
} |
|
|
}, |
|
|
|
|
|
_update: function () { |
|
|
if (!this._map) { return; } |
|
|
|
|
|
this._clipPoints(); |
|
|
this._simplifyPoints(); |
|
|
this._updatePath(); |
|
|
}, |
|
|
|
|
|
_updatePath: function () { |
|
|
this._renderer._updatePoly(this); |
|
|
}, |
|
|
|
|
|
|
|
|
_containsPoint: function (p, closed) { |
|
|
var i, j, k, len, len2, part, |
|
|
w = this._clickTolerance(); |
|
|
|
|
|
if (!this._pxBounds || !this._pxBounds.contains(p)) { return false; } |
|
|
|
|
|
|
|
|
for (i = 0, len = this._parts.length; i < len; i++) { |
|
|
part = this._parts[i]; |
|
|
|
|
|
for (j = 0, len2 = part.length, k = len2 - 1; j < len2; k = j++) { |
|
|
if (!closed && (j === 0)) { continue; } |
|
|
|
|
|
if (LineUtil.pointToSegmentDistance(p, part[k], part[j]) <= w) { |
|
|
return true; |
|
|
} |
|
|
} |
|
|
} |
|
|
return false; |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function polyline(latlngs, options) { |
|
|
return new Polyline(latlngs, options); |
|
|
} |
|
|
|
|
|
|
|
|
Polyline._flat = LineUtil._flat; |
|
|
|