Implement 'overlaps' spatial operator on bounds and use it on gridlayers

This commit is contained in:
Iván Sánchez Ortega 2015-06-02 12:35:31 +02:00
parent e70b84218c
commit c0384bbe21
4 changed files with 42 additions and 2 deletions

View File

@ -143,6 +143,19 @@ describe('LatLngBounds', function () {
expect(a.intersects([[16, 20], [50, 60]])).to.eql(true);
expect(a.contains([[40, 50], [50, 60]])).to.eql(false);
});
it('returns true if just touches the boundary of the given bounds', function () {
expect(a.intersects([[25, 40], [55, 50]])).to.eql(true);
});
});
describe('#overlaps', function () {
it('returns true if overlaps the given bounds', function () {
expect(a.overlaps([[16, 20], [50, 60]])).to.eql(true);
});
it('returns false if just touches the boundary of the given bounds', function () {
expect(a.overlaps([[25, 40], [55, 50]])).to.eql(false);
});
});
});

View File

@ -119,7 +119,7 @@ L.LatLngBounds.prototype = {
(sw2.lng >= sw.lng) && (ne2.lng <= ne.lng);
},
intersects: function (bounds) { // (LatLngBounds)
intersects: function (bounds) { // (LatLngBounds) -> Boolean
bounds = L.latLngBounds(bounds);
var sw = this._southWest,
@ -133,6 +133,20 @@ L.LatLngBounds.prototype = {
return latIntersects && lngIntersects;
},
overlaps: function (bounds) { // (LatLngBounds) -> Boolean
bounds = L.latLngBounds(bounds);
var sw = this._southWest,
ne = this._northEast,
sw2 = bounds.getSouthWest(),
ne2 = bounds.getNorthEast(),
latOverlaps = (ne2.lat > sw.lat) && (sw2.lat < ne.lat),
lngOverlaps = (ne2.lng > sw.lng) && (sw2.lng < ne.lng);
return latOverlaps && lngOverlaps;
},
toBBoxString: function () {
return [this.getWest(), this.getSouth(), this.getEast(), this.getNorth()].join(',');
},

View File

@ -82,6 +82,19 @@ L.Bounds.prototype = {
return xIntersects && yIntersects;
},
overlaps: function (bounds) { // (Bounds) -> Boolean
bounds = L.bounds(bounds);
var min = this.min,
max = this.max,
min2 = bounds.min,
max2 = bounds.max,
xOverlaps = (max2.x > min.x) && (min2.x < max.x),
yOverlaps = (max2.y > min.y) && (min2.y < max.y);
return xOverlaps && yOverlaps;
},
isValid: function () {
return !!(this.min && this.max);
}

View File

@ -462,7 +462,7 @@ L.GridLayer = L.Layer.extend({
// don't load tile if it doesn't intersect the bounds in options
var tileBounds = this._tileCoordsToBounds(coords);
return L.latLngBounds(this.options.bounds).intersects(tileBounds);
return L.latLngBounds(this.options.bounds).overlaps(tileBounds);
},
_keyToBounds: function (key) {