This commit is contained in:
ahocevar
2011-09-30 17:31:45 -06:00
2 changed files with 50 additions and 18 deletions
+11 -17
View File
@@ -372,27 +372,21 @@ OpenLayers.Bounds = OpenLayers.Class({
* {Boolean} The passed-in lonlat is within this bounds. * {Boolean} The passed-in lonlat is within this bounds.
*/ */
containsLonLat: function(ll, options) { containsLonLat: function(ll, options) {
options = options || {};
if (typeof options === "boolean") { if (typeof options === "boolean") {
options.inclusive = options; options = {inclusive: options};
} }
options = options || {};
var contains = this.contains(ll.lon, ll.lat, options.inclusive), var contains = this.contains(ll.lon, ll.lat, options.inclusive),
worldBounds = options.worldBounds; worldBounds = options.worldBounds;
if (worldBounds && !contains) { if (worldBounds && !contains) {
var worldWidth = worldBounds.getWidth(); var worldWidth = worldBounds.getWidth();
ll = ll.clone(); ll = ll.clone();
while(!contains && ll.lon > worldBounds.right) { var worldCenterX = (worldBounds.left + worldBounds.right) / 2;
ll.lon -= worldWidth; var worldsAway = Math.round((ll.lon - worldCenterX) / worldWidth);
contains = worldBounds.containsLonLat( ll.lon -= (worldsAway * worldWidth);
ll, {inclusive: options.inclusive} contains = this.containsLonLat(
); ll, {inclusive: options.inclusive}
} );
while(!contains && ll.lon < worldBounds.left) {
ll.lon += worldWidth;
contains = worldBounds.containsLonLat(
ll, {inclusive: options.inclusive}
);
}
} }
return contains; return contains;
}, },
@@ -472,10 +466,10 @@ OpenLayers.Bounds = OpenLayers.Class({
* {Boolean} The passed-in bounds object intersects this bounds. * {Boolean} The passed-in bounds object intersects this bounds.
*/ */
intersectsBounds:function(bounds, options) { intersectsBounds:function(bounds, options) {
options = options || {}; if (typeof options === "boolean") {
if (typeof options === "boolean") { options = {inclusive: options};
options = {inclusive: true};
} }
options = options || {};
if (options.worldBounds) { if (options.worldBounds) {
var self = this.wrapDateLine(options.worldBounds); var self = this.wrapDateLine(options.worldBounds);
bounds = bounds.wrapDateLine(options.worldBounds); bounds = bounds.wrapDateLine(options.worldBounds);
+39 -1
View File
@@ -125,6 +125,43 @@
} }
function test_containsLonLat_wraped(t) {
var worldBounds = new OpenLayers.Bounds(-180, -90, 180, 90);
var cases = [{
ll: [0, 0], bbox: [-10, -10, 10, 10], contained: true
}, {
ll: [20, 0], bbox: [-10, -10, 10, 10], contained: false
}, {
ll: [360, 0], bbox: [-10, -10, 10, 10], contained: true
}, {
ll: [380, 0], bbox: [-10, -10, 10, 10], contained: false
}, {
ll: [725, 5], bbox: [-10, -10, 10, 10], contained: true
}, {
ll: [-355, -5], bbox: [-10, -10, 10, 10], contained: true
}, {
ll: [-715, 5], bbox: [-10, -10, 10, 10], contained: true
}, {
ll: [-735, 5], bbox: [-10, -10, 10, 10], contained: false
}, {
ll: [-180 * 50, 5], bbox: [-10, -10, 10, 10], contained: true
}];
var len = cases.length;
t.plan(len);
var c, bounds, loc;
for (var i=0; i<len; ++i) {
c = cases[i];
loc = new OpenLayers.LonLat(c.ll[0], c.ll[1]);
bounds = new OpenLayers.Bounds.fromArray(c.bbox);
t.eq(bounds.containsLonLat(loc, {worldBounds: worldBounds}), c.contained, "case " + i);
}
}
function test_Bounds_fromString(t) { function test_Bounds_fromString(t) {
t.plan( 12 ); t.plan( 12 );
bounds = OpenLayers.Bounds.fromString("1,2,3,4"); bounds = OpenLayers.Bounds.fromString("1,2,3,4");
@@ -555,7 +592,8 @@
//straddling right //straddling right
testBounds = simpleBounds.add(10,0); testBounds = simpleBounds.add(10,0);
wrappedBounds = testBounds.wrapDateLine(maxExtent); wrappedBounds = testBounds.wrapDateLine(maxExtent);
t.ok(wrappedBounds.equals(testBounds), "wrapping a bounds that straddles the right of maxextent does nothing"); desiredBounds = testBounds.add(-maxExtent.getWidth(), 0)
t.ok(wrappedBounds.equals(desiredBounds), "wrapping a bounds that straddles the right of maxextent moves extent to left side of the world");
//right leftTolerance //right leftTolerance
testBounds = simpleBounds.add(14,0); testBounds = simpleBounds.add(14,0);