Code coverage report for src/geo/LatLngBounds.js

Statements: 100% (55 / 55)      Branches: 97.67% (42 / 43)      Functions: 100% (18 / 18)      Lines: 100% (53 / 53)     

All files » src/geo/ » LatLngBounds.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        1 50   31   31 62       1     67 66   1     67 66 32 32   34 34   34 34   1 1 1   67         1         1           1           7       7       2       2       5       5       5       5       5 2   3     5       5 3 3   2     5         1   1               1       1       3   2   2         3           1 9 1   8    
/*
 * L.LatLngBounds represents a rectangular area on the map in geographical coordinates.
 */
 
L.LatLngBounds = function (southWest, northEast) { // (LatLng, LatLng) or (LatLng[])
	if (!southWest) { return; }
 
	var latlngs = northEast ? [southWest, northEast] : southWest;
 
	for (var i = 0, len = latlngs.length; i < len; i++) {
		this.extend(latlngs[i]);
	}
};
 
L.LatLngBounds.prototype = {
	// extend the bounds to contain the given point or bounds
	extend: function (obj) { // (LatLng) or (LatLngBounds)
		if (typeof obj[0] === 'number' || typeof obj[0] === 'string' || obj instanceof L.LatLng) {
			obj = L.latLng(obj);
		} else {
			obj = L.latLngBounds(obj);
		}
 
		if (obj instanceof L.LatLng) {
			if (!this._southWest && !this._northEast) {
				this._southWest = new L.LatLng(obj.lat, obj.lng);
				this._northEast = new L.LatLng(obj.lat, obj.lng);
			} else {
				this._southWest.lat = Math.min(obj.lat, this._southWest.lat);
				this._southWest.lng = Math.min(obj.lng, this._southWest.lng);
 
				this._northEast.lat = Math.max(obj.lat, this._northEast.lat);
				this._northEast.lng = Math.max(obj.lng, this._northEast.lng);
			}
		} else Eif (obj instanceof L.LatLngBounds) {
			this.extend(obj._southWest);
			this.extend(obj._northEast);
		}
		return this;
	},
 
	// extend the bounds by a percentage
	pad: function (bufferRatio) { // (Number) -> LatLngBounds
		var sw = this._southWest,
		    ne = this._northEast,
		    heightBuffer = Math.abs(sw.lat - ne.lat) * bufferRatio,
		    widthBuffer = Math.abs(sw.lng - ne.lng) * bufferRatio;
 
		return new L.LatLngBounds(
		        new L.LatLng(sw.lat - heightBuffer, sw.lng - widthBuffer),
		        new L.LatLng(ne.lat + heightBuffer, ne.lng + widthBuffer));
	},
 
	getCenter: function () { // -> LatLng
		return new L.LatLng(
		        (this._southWest.lat + this._northEast.lat) / 2,
		        (this._southWest.lng + this._northEast.lng) / 2);
	},
 
	getSouthWest: function () {
		return this._southWest;
	},
 
	getNorthEast: function () {
		return this._northEast;
	},
 
	getNorthWest: function () {
		return new L.LatLng(this.getNorth(), this.getWest());
	},
 
	getSouthEast: function () {
		return new L.LatLng(this.getSouth(), this.getEast());
	},
 
	getWest: function () {
		return this._southWest.lng;
	},
 
	getSouth: function () {
		return this._southWest.lat;
	},
 
	getEast: function () {
		return this._northEast.lng;
	},
 
	getNorth: function () {
		return this._northEast.lat;
	},
 
	contains: function (obj) { // (LatLngBounds) or (LatLng) -> Boolean
		if (typeof obj[0] === 'number' || obj instanceof L.LatLng) {
			obj = L.latLng(obj);
		} else {
			obj = L.latLngBounds(obj);
		}
 
		var sw = this._southWest,
		    ne = this._northEast,
		    sw2, ne2;
 
		if (obj instanceof L.LatLngBounds) {
			sw2 = obj.getSouthWest();
			ne2 = obj.getNorthEast();
		} else {
			sw2 = ne2 = obj;
		}
 
		return (sw2.lat >= sw.lat) && (ne2.lat <= ne.lat) &&
		       (sw2.lng >= sw.lng) && (ne2.lng <= ne.lng);
	},
 
	intersects: function (bounds) { // (LatLngBounds)
		bounds = L.latLngBounds(bounds);
 
		var sw = this._southWest,
		    ne = this._northEast,
		    sw2 = bounds.getSouthWest(),
		    ne2 = bounds.getNorthEast(),
 
		    latIntersects = (ne2.lat >= sw.lat) && (sw2.lat <= ne.lat),
		    lngIntersects = (ne2.lng >= sw.lng) && (sw2.lng <= ne.lng);
 
		return latIntersects && lngIntersects;
	},
 
	toBBoxString: function () {
		return [this.getWest(), this.getSouth(), this.getEast(), this.getNorth()].join(',');
	},
 
	equals: function (bounds) { // (LatLngBounds)
		if (!bounds) { return false; }
 
		bounds = L.latLngBounds(bounds);
 
		return this._southWest.equals(bounds.getSouthWest()) &&
		       this._northEast.equals(bounds.getNorthEast());
	},
 
	isValid: function () {
		return !!(this._southWest && this._northEast);
	}
};
 
//TODO International date line?
 
L.latLngBounds = function (a, b) { // (LatLngBounds) or (LatLng, LatLng)
	if (!a || a instanceof L.LatLngBounds) {
		return a;
	}
	return new L.LatLngBounds(a, b);
};