Code coverage report for src/layer/vector/Polyline.js

Statements: 70.27% (52 / 74)      Branches: 53.85% (14 / 26)      Functions: 75% (12 / 16)      Lines: 71.23% (52 / 73)     

All files » src/layer/vector/ » Polyline.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        1   7   7                     2   2 4         2 2   2               3 3                 2 2 2 2       3   3 3 3 6 6 6 6 4 4       3 2   3                               12   12 28     28   12       1       2   2 4 4     4   2       2       2 2 2                                                       2     2 2         2   2 2   2       1      
/*
 * L.Polyline is used to display polylines on a map.
 */
 
L.Polyline = L.Path.extend({
	initialize: function (latlngs, options) {
		L.Path.prototype.initialize.call(this, options);
 
		this._latlngs = this._convertLatLngs(latlngs);
	},
 
	options: {
		// how much to simplify the polyline on each zoom level
		// more = better performance and smoother look, less = more accurate
		smoothFactor: 1.0,
		noClip: false
	},
 
	projectLatlngs: function () {
		this._originalPoints = [];
 
		for (var i = 0, len = this._latlngs.length; i < len; i++) {
			this._originalPoints[i] = this._map.latLngToLayerPoint(this._latlngs[i]);
		}
	},
 
	getPathString: function () {
		for (var i = 0, len = this._parts.length, str = ''; i < len; i++) {
			str += this._getPathPartStr(this._parts[i]);
		}
		return str;
	},
 
	getLatLngs: function () {
		return this._latlngs;
	},
 
	setLatLngs: function (latlngs) {
		this._latlngs = this._convertLatLngs(latlngs);
		return this.redraw();
	},
 
	addLatLng: function (latlng) {
		this._latlngs.push(L.latLng(latlng));
		return this.redraw();
	},
 
	spliceLatLngs: function () { // (Number index, Number howMany)
		var removed = [].splice.apply(this._latlngs, arguments);
		this._convertLatLngs(this._latlngs, true);
		this.redraw();
		return removed;
	},
 
	closestLayerPoint: function (p) {
		var minDistance = Infinity, parts = this._parts, p1, p2, minPoint = null;
 
		for (var j = 0, jLen = parts.length; j < jLen; j++) {
			var points = parts[j];
			for (var i = 1, len = points.length; i < len; i++) {
				p1 = points[i - 1];
				p2 = points[i];
				var sqDist = L.LineUtil._sqClosestPointOnSegment(p, p1, p2, true);
				if (sqDist < minDistance) {
					minDistance = sqDist;
					minPoint = L.LineUtil._sqClosestPointOnSegment(p, p1, p2);
				}
			}
		}
		if (minPoint) {
			minPoint.distance = Math.sqrt(minDistance);
		}
		return minPoint;
	},
 
	getBounds: function () {
		var bounds = new L.LatLngBounds(),
		    latLngs = this.getLatLngs(),
		    i, len;
 
		for (i = 0, len = latLngs.length; i < len; i++) {
			bounds.extend(latLngs[i]);
		}
 
		return bounds;
	},
 
	_convertLatLngs: function (latlngs, overwrite) {
		var i, len, target = overwrite ? latlngs : [];
 
		for (i = 0, len = latlngs.length; i < len; i++) {
			Iif (L.Util.isArray(latlngs[i]) && typeof latlngs[i][0] !== 'number') {
				return;
			}
			target[i] = L.latLng(latlngs[i]);
		}
		return target;
	},
 
	_initEvents: function () {
		L.Path.prototype._initEvents.call(this);
	},
 
	_getPathPartStr: function (points) {
		var round = L.Path.VML;
 
		for (var j = 0, len2 = points.length, str = '', p; j < len2; j++) {
			p = points[j];
			Iif (round) {
				p._round();
			}
			str += (j ? 'L' : 'M') + p.x + ' ' + p.y;
		}
		return str;
	},
 
	_clipPoints: function () {
		var points = this._originalPoints,
		    len = points.length,
		    i, k, segment;
 
		Eif (this.options.noClip) {
			this._parts = [points];
			return;
		}
 
		this._parts = [];
 
		var parts = this._parts,
		    vp = this._map._pathViewport,
		    lu = L.LineUtil;
 
		for (i = 0, k = 0; i < len - 1; i++) {
			segment = lu.clipSegment(points[i], points[i + 1], vp, i);
			if (!segment) {
				continue;
			}
 
			parts[k] = parts[k] || [];
			parts[k].push(segment[0]);
 
			// if segment goes out of screen, or it's the last one, it's the end of the line part
			if ((segment[1] !== points[i + 1]) || (i === len - 2)) {
				parts[k].push(segment[1]);
				k++;
			}
		}
	},
 
	// simplify each clipped part of the polyline
	_simplifyPoints: function () {
		var parts = this._parts,
		    lu = L.LineUtil;
 
		for (var i = 0, len = parts.length; i < len; i++) {
			parts[i] = lu.simplify(parts[i], this.options.smoothFactor);
		}
	},
 
	_updatePath: function () {
		Iif (!this._map) { return; }
 
		this._clipPoints();
		this._simplifyPoints();
 
		L.Path.prototype._updatePath.call(this);
	}
});
 
L.polyline = function (latlngs, options) {
	return new L.Polyline(latlngs, options);
};