Add ol.TileBounds.forEachTileCoord

This commit is contained in:
Tom Payne
2012-07-07 14:31:14 +02:00
committed by Tom Payne
parent 85716afa38
commit abb49a899a
3 changed files with 56 additions and 0 deletions

View File

@@ -126,6 +126,9 @@ TileCoord
clone() -> TileCoord
getHash() -> number
TileBounds
forEachTileCoord(z, function(tileCoord))
Tile
tileCoord TileCoord
url string

View File

@@ -25,6 +25,29 @@ ol.TileBounds = function(top, right, bottom, left) {
goog.inherits(ol.TileBounds, goog.math.Box);
/**
* @param {number} z Z.
* @param {function(this: T, ol.TileCoord): boolean} f Callback.
* @param {T=} opt_obj The object to be used for the value of 'this' within f.
*/
ol.TileBounds.prototype.forEachTileCoord = function(z, f, opt_obj) {
var tileCoord = new ol.TileCoord(z, 0, 0);
var x, y;
for (x = this.left; x <= this.right; ++x) {
tileCoord.x = x;
for (y = this.top; y <= this.bottom; ++y) {
tileCoord.y = y;
if (f.call(opt_obj, tileCoord)) {
return;
}
goog.asserts.assert(tileCoord.z == z);
goog.asserts.assert(tileCoord.x == x);
goog.asserts.assert(tileCoord.y == y);
}
}
};
/**
* @param {...ol.TileCoord} var_args Tile coordinates.
* @return {!ol.TileBounds} Bounding tile box.

View File

@@ -34,3 +34,33 @@ function testBoundingTileBoundsMixedZ() {
new ol.TileCoord(4, 2, 0));
});
}
function testForEachTileCoord() {
var tileBounds = new ol.TileBounds(2, 1, 3, 0);
var tileCoords = [];
tileBounds.forEachTileCoord(5, function(tileCoord) {
tileCoords.push(tileCoord.clone());
});
assertEquals(4, tileCoords.length);
assertEquals(5, tileCoords[0].z);
assertEquals(0, tileCoords[0].x);
assertEquals(2, tileCoords[0].y);
assertEquals(5, tileCoords[1].z);
assertEquals(0, tileCoords[1].x);
assertEquals(3, tileCoords[1].y);
assertEquals(5, tileCoords[2].z);
assertEquals(1, tileCoords[2].x);
assertEquals(2, tileCoords[2].y);
assertEquals(5, tileCoords[3].z);
assertEquals(1, tileCoords[3].x);
assertEquals(3, tileCoords[3].y);
}