diff --git a/src/all.js b/src/all.js index 47a9231ed7..62e738a54e 100644 --- a/src/all.js +++ b/src/all.js @@ -1,5 +1,6 @@ goog.provide('ol'); -goog.require('ol.MVCObject'); goog.require('ol.MVCArray'); +goog.require('ol.MVCObject'); +goog.require('ol.TileBounds'); goog.require('ol.TileCoord'); diff --git a/src/ol/tilebounds.js b/src/ol/tilebounds.js new file mode 100644 index 0000000000..efe576df84 --- /dev/null +++ b/src/ol/tilebounds.js @@ -0,0 +1,43 @@ +goog.provide('ol.TileBounds'); + +goog.require('goog.asserts'); +goog.require('goog.math.Box'); +goog.require('ol.TileCoord'); + + + +/** + * @constructor + * @extends {goog.math.Box} + * @param {number} top Top. + * @param {number} right Right. + * @param {number} bottom Bottom. + * @param {number} left Left. + */ +ol.TileBounds = function(top, right, bottom, left) { + + goog.base(this, top, right, bottom, left); + +}; +goog.inherits(ol.TileBounds, goog.math.Box); + + +/** + * @param {...ol.TileCoord} var_args Tile coordinates. + * @return {!ol.TileBounds} Bounding tile box. + */ +ol.TileBounds.boundingTileBounds = function(var_args) { + var tileCoord0 = arguments[0]; + var tileBounds = new ol.TileBounds(tileCoord0.y, tileCoord0.x, + tileCoord0.y, tileCoord0.x); + var i; + for (i = 1; i < arguments.length; ++i) { + var tileCoord = arguments[i]; + goog.asserts.assert(tileCoord.z == tileCoord0.z); + tileBounds.top = Math.min(tileBounds.top, tileCoord.y); + tileBounds.right = Math.max(tileBounds.right, tileCoord.x); + tileBounds.bottom = Math.max(tileBounds.bottom, tileCoord.y); + tileBounds.left = Math.min(tileBounds.left, tileCoord.x); + } + return tileBounds; +}; diff --git a/src/ol/tilebounds_test.js b/src/ol/tilebounds_test.js new file mode 100644 index 0000000000..842eb1ab34 --- /dev/null +++ b/src/ol/tilebounds_test.js @@ -0,0 +1,36 @@ +goog.require('goog.testing.jsunit'); +goog.require('ol.TileBounds'); + + +function testContainsPositive() { + var tb = new ol.TileBounds(0, 2, 2, 0); + var tc = new ol.TileCoord(3, 1, 1); + assertTrue(tb.contains(tc)); +} + + +function testContainsNegative() { + var tb = new ol.TileBounds(0, 2, 2, 0); + var tc = new ol.TileCoord(3, 1, 3); + assertFalse(tb.contains(tc)); +} + + +function testBoundingTileBounds() { + var tb = new ol.TileBounds.boundingTileBounds( + new ol.TileCoord(3, 1, 3), + new ol.TileCoord(3, 2, 0)); + assertEquals(tb.top, 0); + assertEquals(tb.right, 2); + assertEquals(tb.bottom, 3); + assertEquals(tb.left, 1); +} + + +function testBoundingTileBoundsMixedZ() { + assertThrows(function() { + var tb = new ol.TileBounds.boundingTileBounds( + new ol.TileCoord(3, 1, 3), + new ol.TileCoord(4, 2, 0)); + }); +}