Clean up directory structure
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
goog.provide('ol.Tile');
|
||||
|
||||
goog.require('goog.array');
|
||||
goog.require('goog.events');
|
||||
goog.require('goog.events.EventTarget');
|
||||
goog.require('goog.events.EventType');
|
||||
goog.require('ol.TileCoord');
|
||||
|
||||
|
||||
/**
|
||||
* @enum {number}
|
||||
*/
|
||||
ol.TileLoadState = {
|
||||
IDLE: 0,
|
||||
LOADING: 1,
|
||||
LOADED: 2,
|
||||
ERROR: 3
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {goog.events.EventTarget}
|
||||
* @param {ol.TileCoord} tileCoord Tile coordinate.
|
||||
* @param {string} src Source.
|
||||
* @param {string=} opt_crossOrigin Cross origin.
|
||||
*/
|
||||
ol.Tile = function(tileCoord, src, opt_crossOrigin) {
|
||||
|
||||
goog.base(this);
|
||||
|
||||
/**
|
||||
* @type {ol.TileCoord}
|
||||
*/
|
||||
this.tileCoord = tileCoord;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {string}
|
||||
*/
|
||||
this.src_ = src;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.TileLoadState}
|
||||
*/
|
||||
this.state_ = ol.TileLoadState.IDLE;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Image}
|
||||
*/
|
||||
this.image_ = new Image();
|
||||
if (goog.isDef(opt_crossOrigin)) {
|
||||
this.image_.crossOrigin = opt_crossOrigin;
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Object.<number, Image>}
|
||||
*/
|
||||
this.imageByContext_ = {};
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Array.<number>}
|
||||
*/
|
||||
this.imageListenerKeys_ = null;
|
||||
|
||||
};
|
||||
goog.inherits(ol.Tile, goog.events.EventTarget);
|
||||
|
||||
|
||||
/**
|
||||
* @protected
|
||||
*/
|
||||
ol.Tile.prototype.dispatchChangeEvent = function() {
|
||||
this.dispatchEvent(goog.events.EventType.CHANGE);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {Object=} opt_context Object.
|
||||
* @return {Image} Image.
|
||||
*/
|
||||
ol.Tile.prototype.getImage = function(opt_context) {
|
||||
if (goog.isDef(opt_context)) {
|
||||
var image;
|
||||
var key = goog.getUid(opt_context);
|
||||
if (key in this.imageByContext_) {
|
||||
return this.imageByContext_[key];
|
||||
} else if (goog.object.isEmpty(this.imageByContext_)) {
|
||||
image = this.image_;
|
||||
} else {
|
||||
image = /** @type {Image} */ this.image_.cloneNode(false);
|
||||
}
|
||||
this.imageByContext_[key] = image;
|
||||
return image;
|
||||
} else {
|
||||
return this.image_;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
ol.Tile.prototype.handleImageError_ = function() {
|
||||
this.state_ = ol.TileLoadState.ERROR;
|
||||
this.unlistenImage_();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
ol.Tile.prototype.handleImageLoad_ = function() {
|
||||
this.state_ = ol.TileLoadState.LOADED;
|
||||
this.unlistenImage_();
|
||||
this.dispatchChangeEvent();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {boolean} Is loaded.
|
||||
*/
|
||||
ol.Tile.prototype.isLoaded = function() {
|
||||
return this.state_ == ol.TileLoadState.LOADED;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
*/
|
||||
ol.Tile.prototype.load = function() {
|
||||
if (this.state_ == ol.TileLoadState.IDLE) {
|
||||
this.state_ = ol.TileLoadState.LOADING;
|
||||
goog.asserts.assert(goog.isNull(this.imageListenerKeys_));
|
||||
this.imageListenerKeys_ = [
|
||||
goog.events.listenOnce(this.image_, goog.events.EventType.ERROR,
|
||||
this.handleImageError_, false, this),
|
||||
goog.events.listenOnce(this.image_, goog.events.EventType.LOAD,
|
||||
this.handleImageLoad_, false, this)
|
||||
];
|
||||
this.image_.src = this.src_;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
ol.Tile.prototype.unlistenImage_ = function() {
|
||||
goog.asserts.assert(!goog.isNull(this.imageListenerKeys_));
|
||||
goog.array.forEach(this.imageListenerKeys_, goog.events.unlistenByKey);
|
||||
this.imageListenerKeys_ = null;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
goog.provide('ol.TileBounds');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('ol.Rectangle');
|
||||
goog.require('ol.TileCoord');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.Rectangle}
|
||||
* @param {number} minX Minimum X.
|
||||
* @param {number} minY Minimum Y.
|
||||
* @param {number} maxX Maximum X.
|
||||
* @param {number} maxY Maximum Y.
|
||||
*/
|
||||
ol.TileBounds = function(minX, minY, maxX, maxY) {
|
||||
goog.base(this, minX, minY, maxX, maxY);
|
||||
};
|
||||
goog.inherits(ol.TileBounds, ol.Rectangle);
|
||||
|
||||
|
||||
/**
|
||||
* @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.x, tileCoord0.y,
|
||||
tileCoord0.x, tileCoord0.y);
|
||||
var i;
|
||||
for (i = 1; i < arguments.length; ++i) {
|
||||
var tileCoord = arguments[i];
|
||||
goog.asserts.assert(tileCoord.z == tileCoord0.z);
|
||||
tileBounds.minX = Math.min(tileBounds.minX, tileCoord.x);
|
||||
tileBounds.minY = Math.min(tileBounds.minY, tileCoord.y);
|
||||
tileBounds.maxX = Math.max(tileBounds.maxX, tileCoord.x);
|
||||
tileBounds.maxY = Math.max(tileBounds.maxY, tileCoord.y);
|
||||
}
|
||||
return tileBounds;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {ol.TileBounds} Clone.
|
||||
*/
|
||||
ol.TileBounds.prototype.clone = function() {
|
||||
return new ol.TileBounds(this.minX, this.minY, this.maxX, this.maxY);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.TileBounds} tileBounds Tile bounds.
|
||||
* @return {boolean} Contains.
|
||||
*/
|
||||
ol.TileBounds.prototype.contains = function(tileBounds) {
|
||||
return this.minX <= tileBounds.minX && tileBounds.maxX <= this.maxX &&
|
||||
this.minY <= tileBounds.minY && tileBounds.minY <= this.minY;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.TileBounds} tileBounds Tile bounds.
|
||||
* @return {boolean} Equals.
|
||||
*/
|
||||
ol.TileBounds.prototype.equals = function(tileBounds) {
|
||||
return this.minX == tileBounds.minX && tileBounds.maxX == this.maxX &&
|
||||
this.minY == tileBounds.minY && tileBounds.minY == this.minY;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} z Z.
|
||||
* @param {function(this: T, ol.TileCoord)} f Callback.
|
||||
* @param {T=} opt_obj The object to be used for the value of 'this' within f.
|
||||
* @template T
|
||||
*/
|
||||
ol.TileBounds.prototype.forEachTileCoord = function(z, f, opt_obj) {
|
||||
var tileCoord = new ol.TileCoord(z, 0, 0);
|
||||
var x, y;
|
||||
for (x = this.minX; x <= this.maxX; ++x) {
|
||||
tileCoord.x = x;
|
||||
for (y = this.minY; y <= this.maxY; ++y) {
|
||||
tileCoord.y = y;
|
||||
f.call(opt_obj, tileCoord);
|
||||
goog.asserts.assert(tileCoord.z == z);
|
||||
goog.asserts.assert(tileCoord.x == x);
|
||||
goog.asserts.assert(tileCoord.y == y);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @override
|
||||
* @return {number} Height.
|
||||
*/
|
||||
ol.TileBounds.prototype.getHeight = function() {
|
||||
return this.maxY - this.minY + 1;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @override
|
||||
* @return {number} Width.
|
||||
*/
|
||||
ol.TileBounds.prototype.getWidth = function() {
|
||||
return this.maxX - this.minX + 1;
|
||||
};
|
||||
@@ -0,0 +1,86 @@
|
||||
goog.require('goog.testing.jsunit');
|
||||
goog.require('ol.TileBounds');
|
||||
|
||||
|
||||
function testClone() {
|
||||
var tileBounds = new ol.TileBounds(1, 2, 3, 4);
|
||||
var clonedTileBounds = tileBounds.clone();
|
||||
assertTrue(clonedTileBounds instanceof ol.TileBounds);
|
||||
assertFalse(clonedTileBounds === tileBounds);
|
||||
assertEquals(tileBounds.minX, clonedTileBounds.minX);
|
||||
assertEquals(tileBounds.minY, clonedTileBounds.minY);
|
||||
assertEquals(tileBounds.maxX, clonedTileBounds.maxX);
|
||||
assertEquals(tileBounds.maxY, clonedTileBounds.maxY);
|
||||
}
|
||||
|
||||
|
||||
function testContainsPositive() {
|
||||
var tileBounds = new ol.TileBounds(0, 0, 2, 2);
|
||||
var tileCoord = new ol.TileCoord(3, 1, 1);
|
||||
assertTrue(tileBounds.contains(tileCoord));
|
||||
}
|
||||
|
||||
|
||||
function testContainsNegative() {
|
||||
var tileBounds = new ol.TileBounds(0, 0, 2, 2);
|
||||
var tileCoord = new ol.TileCoord(3, 1, 3);
|
||||
assertFalse(tileBounds.contains(tileCoord));
|
||||
}
|
||||
|
||||
|
||||
function testBoundingTileBounds() {
|
||||
var tileBounds = new ol.TileBounds.boundingTileBounds(
|
||||
new ol.TileCoord(3, 1, 3),
|
||||
new ol.TileCoord(3, 2, 0));
|
||||
assertEquals(1, tileBounds.minX);
|
||||
assertEquals(0, tileBounds.minY);
|
||||
assertEquals(2, tileBounds.maxX);
|
||||
assertEquals(3, tileBounds.maxY);
|
||||
}
|
||||
|
||||
|
||||
function testBoundingTileBoundsMixedZ() {
|
||||
assertThrows(function() {
|
||||
var tileBounds = new ol.TileBounds.boundingTileBounds(
|
||||
new ol.TileCoord(3, 1, 3),
|
||||
new ol.TileCoord(4, 2, 0));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function testForEachTileCoord() {
|
||||
|
||||
var tileBounds = new ol.TileBounds(0, 2, 1, 3);
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testSize() {
|
||||
var tileBounds = new ol.TileBounds(0, 1, 2, 4);
|
||||
var size = tileBounds.getSize();
|
||||
assertEquals(3, size.width);
|
||||
assertEquals(4, size.height);
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
goog.provide('ol.TileCoord');
|
||||
|
||||
goog.require('goog.array');
|
||||
goog.require('ol.Coordinate');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.Coordinate}
|
||||
* @param {number} z Z.
|
||||
* @param {number} x X.
|
||||
* @param {number} y Y.
|
||||
*/
|
||||
ol.TileCoord = function(z, x, y) {
|
||||
|
||||
goog.base(this, x, y);
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
*/
|
||||
this.z = z;
|
||||
|
||||
};
|
||||
goog.inherits(ol.TileCoord, ol.Coordinate);
|
||||
|
||||
|
||||
/**
|
||||
* @return {ol.TileCoord} Clone.
|
||||
*/
|
||||
ol.TileCoord.prototype.clone = function() {
|
||||
return new ol.TileCoord(this.z, this.x, this.y);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {number} Hash.
|
||||
*/
|
||||
ol.TileCoord.prototype.hash = function() {
|
||||
return (this.x << this.z) + this.y;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {string} String.
|
||||
*/
|
||||
ol.TileCoord.prototype.toString = function() {
|
||||
return [this.z, this.x, this.y].join('/');
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {string} str String.
|
||||
* @return {ol.TileCoord} Tile coord.
|
||||
*/
|
||||
ol.TileCoord.fromString = function(str) {
|
||||
var v = str.split('/');
|
||||
v = goog.array.map(v, function(e, i, a) {
|
||||
return parseInt(e, 10);
|
||||
});
|
||||
return new ol.TileCoord(v[0], v[1], v[2]);
|
||||
};
|
||||
@@ -0,0 +1,26 @@
|
||||
goog.require('goog.testing.jsunit');
|
||||
goog.require('ol.TileCoord');
|
||||
|
||||
|
||||
function testConstructorOrderZXY() {
|
||||
var tc1 = new ol.TileCoord(1, 2, 3);
|
||||
assertEquals(1, tc1.z);
|
||||
assertEquals(2, tc1.x);
|
||||
assertEquals(3, tc1.y);
|
||||
}
|
||||
|
||||
|
||||
function testHashX() {
|
||||
var tc1 = new ol.TileCoord(3, 2, 1);
|
||||
var tc2 = new ol.TileCoord(3, 1, 1);
|
||||
assertTrue(tc1.hash() != tc2.hash());
|
||||
}
|
||||
|
||||
|
||||
function testFromString() {
|
||||
var str = '1/2/3';
|
||||
var tc = ol.TileCoord.fromString(str);
|
||||
assertEquals(1, tc.z);
|
||||
assertEquals(2, tc.x);
|
||||
assertEquals(3, tc.y);
|
||||
}
|
||||
@@ -0,0 +1,319 @@
|
||||
goog.provide('ol.TileGrid');
|
||||
|
||||
goog.require('goog.array');
|
||||
goog.require('goog.asserts');
|
||||
goog.require('ol.Coordinate');
|
||||
goog.require('ol.Extent');
|
||||
goog.require('ol.PixelBounds');
|
||||
goog.require('ol.Size');
|
||||
goog.require('ol.TileBounds');
|
||||
goog.require('ol.TileCoord');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @param {!Array.<number>} resolutions Resolutions.
|
||||
* @param {ol.Extent} extent Extent.
|
||||
* @param {ol.Coordinate|!Array.<ol.Coordinate>} origin Origin.
|
||||
* @param {ol.Size=} opt_tileSize Tile size.
|
||||
*/
|
||||
ol.TileGrid = function(resolutions, extent, origin, opt_tileSize) {
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Array.<number>}
|
||||
*/
|
||||
this.resolutions_ = resolutions;
|
||||
goog.asserts.assert(goog.array.isSorted(resolutions, function(a, b) {
|
||||
return -goog.array.defaultCompare(a, b);
|
||||
}, true));
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {number}
|
||||
*/
|
||||
this.numResolutions_ = this.resolutions_.length;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.Extent}
|
||||
*/
|
||||
this.extent_ = extent;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.Coordinate}
|
||||
*/
|
||||
this.origin_ = null;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Array.<ol.Coordinate>}
|
||||
*/
|
||||
this.origins_ = null;
|
||||
|
||||
if (origin instanceof ol.Coordinate) {
|
||||
this.origin_ = origin;
|
||||
} else if (goog.isArray(origin)) {
|
||||
goog.asserts.assert(origin.length == this.numResolutions_);
|
||||
this.origins_ = origin;
|
||||
} else {
|
||||
goog.asserts.assert(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.Size}
|
||||
*/
|
||||
this.tileSize_ = goog.isDef(opt_tileSize) ?
|
||||
opt_tileSize : new ol.Size(256, 256);
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.TileCoord} tileCoord Tile coordinate.
|
||||
* @param {function(number, ol.TileBounds): boolean} callback Callback.
|
||||
*/
|
||||
ol.TileGrid.prototype.forEachTileCoordParent = function(tileCoord, callback) {
|
||||
var tileCoordExtent = this.getTileCoordExtent(tileCoord);
|
||||
var z = tileCoord.z - 1;
|
||||
while (z >= 0) {
|
||||
if (callback(z, this.getTileBoundsForExtentAndZ(tileCoordExtent, z))) {
|
||||
return;
|
||||
}
|
||||
--z;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {ol.Extent} Extent.
|
||||
*/
|
||||
ol.TileGrid.prototype.getExtent = function() {
|
||||
return this.extent_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Extent} extent Extent.
|
||||
* @param {number} z Z.
|
||||
* @return {ol.TileBounds} Tile bounds.
|
||||
*/
|
||||
ol.TileGrid.prototype.getTileBoundsForExtentAndZ = function(extent, z) {
|
||||
var resolution = this.getResolution(z);
|
||||
return this.getTileBoundsForExtentAndResolution(extent, resolution);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Extent} extent Extent.
|
||||
* @param {number} resolution Resolution.
|
||||
* @return {ol.TileBounds} Tile bounds.
|
||||
*/
|
||||
ol.TileGrid.prototype.getTileBoundsForExtentAndResolution = function(
|
||||
extent, resolution) {
|
||||
var min = this.getTileCoordForCoordAndResolution(
|
||||
new ol.Coordinate(extent.minX, extent.minY), resolution);
|
||||
var max = this.getTileCoordForCoordAndResolution(
|
||||
new ol.Coordinate(extent.maxX, extent.maxY), resolution);
|
||||
return new ol.TileBounds(min.x, min.y, max.x, max.y);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} z Z.
|
||||
* @return {ol.Coordinate} Origin.
|
||||
*/
|
||||
ol.TileGrid.prototype.getOrigin = function(z) {
|
||||
if (!goog.isNull(this.origin_)) {
|
||||
return this.origin_;
|
||||
} else {
|
||||
goog.asserts.assert(!goog.isNull(this.origins_));
|
||||
goog.asserts.assert(0 <= z && z < this.origins_.length);
|
||||
return this.origins_[z];
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} z Z.
|
||||
* @return {number} Resolution.
|
||||
*/
|
||||
ol.TileGrid.prototype.getResolution = function(z) {
|
||||
goog.asserts.assert(0 <= z && z < this.numResolutions_);
|
||||
return this.resolutions_[z];
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {Array.<number>} Resolutions.
|
||||
*/
|
||||
ol.TileGrid.prototype.getResolutions = function() {
|
||||
return this.resolutions_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Coordinate} coordinate Coordinate.
|
||||
* @param {number} z Z.
|
||||
* @return {ol.TileCoord} Tile coordinate.
|
||||
*/
|
||||
ol.TileGrid.prototype.getTileCoordForCoordAndZ = function(coordinate, z) {
|
||||
var resolution = this.getResolution(z);
|
||||
return this.getTileCoordForCoordAndResolution(coordinate, resolution);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Coordinate} coordinate Coordinate.
|
||||
* @param {number} resolution Resolution.
|
||||
* @return {ol.TileCoord} Tile coordinate.
|
||||
*/
|
||||
ol.TileGrid.prototype.getTileCoordForCoordAndResolution = function(
|
||||
coordinate, resolution) {
|
||||
var z = this.getZForResolution(resolution);
|
||||
var scale = resolution / this.getResolution(z);
|
||||
var origin = this.getOrigin(z);
|
||||
|
||||
var offsetFromOrigin = new ol.Coordinate(
|
||||
Math.floor((coordinate.x - origin.x) / resolution),
|
||||
Math.floor((coordinate.y - origin.y) / resolution));
|
||||
|
||||
var tileSize = this.getTileSize();
|
||||
tileSize = new ol.Size(tileSize.width / scale,
|
||||
tileSize.height / scale);
|
||||
|
||||
var x, y;
|
||||
x = Math.floor(offsetFromOrigin.x / tileSize.width);
|
||||
y = Math.floor(offsetFromOrigin.y / tileSize.height);
|
||||
|
||||
var tileCoord = new ol.TileCoord(z, x, y);
|
||||
var tileCoordPixelBounds = this.getPixelBoundsForTileCoordAndResolution(
|
||||
tileCoord, resolution);
|
||||
|
||||
// adjust x to allow for stretched tiles
|
||||
if (offsetFromOrigin.x < tileCoordPixelBounds.minX) {
|
||||
tileCoord.x -= 1;
|
||||
} else if (offsetFromOrigin.x >= tileCoordPixelBounds.maxX) {
|
||||
tileCoord.x += 1;
|
||||
}
|
||||
// adjust y to allow for stretched tiles
|
||||
if (offsetFromOrigin.y < tileCoordPixelBounds.minY) {
|
||||
tileCoord.y -= 1;
|
||||
} else if (offsetFromOrigin.y >= tileCoordPixelBounds.maxY) {
|
||||
tileCoord.y += 1;
|
||||
}
|
||||
|
||||
return tileCoord;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.TileCoord} tileCoord Tile coordinate.
|
||||
* @return {ol.Coordinate} Tile center.
|
||||
*/
|
||||
ol.TileGrid.prototype.getTileCoordCenter = function(tileCoord) {
|
||||
var origin = this.getOrigin(tileCoord.z);
|
||||
var resolution = this.getResolution(tileCoord.z);
|
||||
var tileSize = this.tileSize_;
|
||||
var x = origin.x + (tileCoord.x + 0.5) * tileSize.width * resolution;
|
||||
var y = origin.y + (tileCoord.y + 0.5) * tileSize.height * resolution;
|
||||
return new ol.Coordinate(x, y);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} z Z.
|
||||
* @param {ol.TileBounds} tileBounds Tile bounds.
|
||||
* @return {ol.Extent} Extent.
|
||||
*/
|
||||
ol.TileGrid.prototype.getTileBoundsExtent = function(z, tileBounds) {
|
||||
var origin = this.getOrigin(z);
|
||||
var resolution = this.getResolution(z);
|
||||
var tileSize = this.tileSize_;
|
||||
var minX = origin.x + tileBounds.minX * tileSize.width * resolution;
|
||||
var minY = origin.y + tileBounds.minY * tileSize.height * resolution;
|
||||
var maxX = origin.x + (tileBounds.maxX + 1) * tileSize.width * resolution;
|
||||
var maxY = origin.y + (tileBounds.maxY + 1) * tileSize.height * resolution;
|
||||
return new ol.Extent(minX, minY, maxX, maxY);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.TileCoord} tileCoord Tile coordinate.
|
||||
* @return {ol.Extent} Extent.
|
||||
*/
|
||||
ol.TileGrid.prototype.getTileCoordExtent = function(tileCoord) {
|
||||
var origin = this.getOrigin(tileCoord.z);
|
||||
var resolution = this.getResolution(tileCoord.z);
|
||||
var tileSize = this.tileSize_;
|
||||
var minX = origin.x + tileCoord.x * tileSize.width * resolution;
|
||||
var minY = origin.y + tileCoord.y * tileSize.height * resolution;
|
||||
var maxX = minX + tileSize.width * resolution;
|
||||
var maxY = minY + tileSize.height * resolution;
|
||||
return new ol.Extent(minX, minY, maxX, maxY);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.TileCoord} tileCoord Tile coordinate.
|
||||
* @param {number} resolution Resolution.
|
||||
* @return {ol.PixelBounds} Pixel bounds.
|
||||
*/
|
||||
ol.TileGrid.prototype.getPixelBoundsForTileCoordAndResolution = function(
|
||||
tileCoord, resolution) {
|
||||
var scale = resolution / this.getResolution(tileCoord.z);
|
||||
var tileSize = this.getTileSize();
|
||||
tileSize = new ol.Size(tileSize.width / scale,
|
||||
tileSize.height / scale);
|
||||
var minX, maxX, minY, maxY;
|
||||
minX = Math.round(tileCoord.x * tileSize.width);
|
||||
maxX = Math.round((tileCoord.x + 1) * tileSize.width);
|
||||
minY = Math.round(tileCoord.y * tileSize.height);
|
||||
maxY = Math.round((tileCoord.y + 1) * tileSize.height);
|
||||
return new ol.PixelBounds(minX, minY, maxX, maxY);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.TileCoord} tileCoord Tile coordinate.
|
||||
* @return {number} Tile resolution.
|
||||
*/
|
||||
ol.TileGrid.prototype.getTileCoordResolution = function(tileCoord) {
|
||||
goog.asserts.assert(0 <= tileCoord.z && tileCoord.z < this.numResolutions_);
|
||||
return this.resolutions_[tileCoord.z];
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {ol.Size} Tile size.
|
||||
*/
|
||||
ol.TileGrid.prototype.getTileSize = function() {
|
||||
return this.tileSize_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} resolution Resolution.
|
||||
* @return {number} Z.
|
||||
*/
|
||||
ol.TileGrid.prototype.getZForResolution = function(resolution) {
|
||||
var z;
|
||||
for (z = 0; z < this.numResolutions_; ++z) {
|
||||
if (this.resolutions_[z] == resolution) {
|
||||
return z;
|
||||
} else if (this.resolutions_[z] < resolution) {
|
||||
if (z === 0) {
|
||||
return z;
|
||||
} else if (resolution - this.resolutions_[z] <=
|
||||
this.resolutions_[z - 1] - resolution) {
|
||||
return z;
|
||||
} else {
|
||||
return z - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return this.numResolutions_ - 1;
|
||||
};
|
||||
@@ -0,0 +1,465 @@
|
||||
goog.require('goog.testing.jsunit');
|
||||
goog.require('ol.Coordinate');
|
||||
goog.require('ol.Extent');
|
||||
goog.require('ol.Size');
|
||||
goog.require('ol.TileCoord');
|
||||
goog.require('ol.TileGrid');
|
||||
|
||||
|
||||
var extent;
|
||||
var resolutions;
|
||||
var origin;
|
||||
var origins;
|
||||
var tileSize;
|
||||
|
||||
|
||||
function setUp() {
|
||||
resolutions = [1000, 500, 250, 100];
|
||||
extent = new ol.Extent(0, 0, 100000, 100000);
|
||||
origin = new ol.Coordinate(0, 0);
|
||||
origins = [];
|
||||
tileSize = new ol.Size(100, 100);
|
||||
}
|
||||
|
||||
|
||||
function testCreateValid() {
|
||||
assertNotThrows(function() {
|
||||
return new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function testCreateDuplicateResolutions() {
|
||||
var resolutions = [100, 50, 50, 25, 10];
|
||||
assertThrows(function() {
|
||||
return new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function testCreateOutOfOrderResolutions() {
|
||||
var resolutions = [100, 25, 50, 10];
|
||||
assertThrows(function() {
|
||||
return new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function testCreateOrigins() {
|
||||
var resolutions = [100, 50, 25, 10];
|
||||
var origins = [origin, origin, origin, origin];
|
||||
assertNotThrows(function() {
|
||||
return new ol.TileGrid(resolutions, extent, origins, tileSize);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function testCreateTooFewOrigins() {
|
||||
var resolutions = [100, 50, 25, 10];
|
||||
var origins = [origin, origin, origin];
|
||||
assertThrows(function() {
|
||||
return new ol.TileGrid(resolutions, extent, origins, tileSize);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function testCreateTooManyOrigins() {
|
||||
var resolutions = [100, 50, 25, 10];
|
||||
var origins = [origin, origin, origin, origin, origin];
|
||||
assertThrows(function() {
|
||||
return new ol.TileGrid(resolutions, extent, origins, tileSize);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function testGetTileCoord() {
|
||||
|
||||
origin = new ol.Coordinate(0, 0);
|
||||
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
var tileCoord;
|
||||
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndZ(
|
||||
new ol.Coordinate(0, 0), 3);
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(0, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndZ(
|
||||
new ol.Coordinate(0, 100000), 3);
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(0, tileCoord.x);
|
||||
assertEquals(10, tileCoord.y);
|
||||
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndZ(
|
||||
new ol.Coordinate(100000, 0), 3);
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(10, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndZ(
|
||||
new ol.Coordinate(100000, 100000), 3);
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(10, tileCoord.x);
|
||||
assertEquals(10, tileCoord.y);
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testGetTileCoordYSouth() {
|
||||
|
||||
origin = new ol.Coordinate(0, 100000);
|
||||
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
var tileCoord;
|
||||
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndZ(
|
||||
new ol.Coordinate(0, 0), 3);
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(0, tileCoord.x);
|
||||
assertEquals(-10, tileCoord.y);
|
||||
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndZ(
|
||||
new ol.Coordinate(0, 100000), 3);
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(0, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndZ(
|
||||
new ol.Coordinate(100000, 0), 3);
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(10, tileCoord.x);
|
||||
assertEquals(-10, tileCoord.y);
|
||||
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndZ(
|
||||
new ol.Coordinate(100000, 100000), 3);
|
||||
assertEquals(3, tileCoord.z);
|
||||
assertEquals(10, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
}
|
||||
|
||||
function testGetTileCoordForCoordAndResolution() {
|
||||
|
||||
var tileSize = new ol.Size(256, 256);
|
||||
var tileGrid = new ol.TileGrid([10], extent, origin, tileSize);
|
||||
|
||||
var coordinate;
|
||||
var tileCoord;
|
||||
|
||||
// gets the first tile at the origin
|
||||
coordinate = new ol.Coordinate(0, 0);
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndResolution(
|
||||
coordinate, 10);
|
||||
assertEquals(0, tileCoord.z);
|
||||
assertEquals(0, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
// gets one tile northwest of the origin
|
||||
coordinate = new ol.Coordinate(-1280, 1280);
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndResolution(
|
||||
coordinate, 10);
|
||||
assertEquals(0, tileCoord.z);
|
||||
assertEquals(-1, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
// gets one tile northeast of the origin
|
||||
coordinate = new ol.Coordinate(1280, 1280);
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndResolution(
|
||||
coordinate, 10);
|
||||
assertEquals(0, tileCoord.z);
|
||||
assertEquals(0, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
// gets one tile southeast of the origin
|
||||
coordinate = new ol.Coordinate(1280, -1280);
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndResolution(
|
||||
coordinate, 10);
|
||||
assertEquals(0, tileCoord.z);
|
||||
assertEquals(0, tileCoord.x);
|
||||
assertEquals(-1, tileCoord.y);
|
||||
|
||||
// gets one tile southwest of the origin
|
||||
coordinate = new ol.Coordinate(-1280, -1280);
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndResolution(
|
||||
coordinate, 10);
|
||||
assertEquals(0, tileCoord.z);
|
||||
assertEquals(-1, tileCoord.x);
|
||||
assertEquals(-1, tileCoord.y);
|
||||
|
||||
// gets the tile to the east when on the edge
|
||||
coordinate = new ol.Coordinate(2560, -1280);
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndResolution(
|
||||
coordinate, 10);
|
||||
assertEquals(0, tileCoord.z);
|
||||
assertEquals(1, tileCoord.x);
|
||||
assertEquals(-1, tileCoord.y);
|
||||
|
||||
// gets the tile to the north when on the edge
|
||||
coordinate = new ol.Coordinate(1280, -2560);
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndResolution(
|
||||
coordinate, 10);
|
||||
assertEquals(0, tileCoord.z);
|
||||
assertEquals(0, tileCoord.x);
|
||||
assertEquals(-1, tileCoord.y);
|
||||
|
||||
// pixels are top aligned to the origin
|
||||
coordinate = new ol.Coordinate(1280, -2559.999);
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndResolution(
|
||||
coordinate, 10);
|
||||
assertEquals(0, tileCoord.z);
|
||||
assertEquals(0, tileCoord.x);
|
||||
assertEquals(-1, tileCoord.y);
|
||||
|
||||
// pixels are left aligned to the origin
|
||||
coordinate = new ol.Coordinate(2559.999, -1280);
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndResolution(
|
||||
coordinate, 10);
|
||||
assertEquals(0, tileCoord.z);
|
||||
assertEquals(0, tileCoord.x);
|
||||
assertEquals(-1, tileCoord.y);
|
||||
}
|
||||
|
||||
function testGetTileCoordForCoordAndResolutionFractional() {
|
||||
|
||||
var tileSize = new ol.Size(256, 256);
|
||||
var tileGrid = new ol.TileGrid([1 / 3], extent, origin, tileSize);
|
||||
|
||||
var coordinate;
|
||||
var tileCoord;
|
||||
|
||||
// These tests render at a resolution of 1. Because the layer's
|
||||
// closest resolution is 1/3, the images are scaled by 1/3.
|
||||
// In this scenario, every third tile will be one pixel wider when
|
||||
// rendered (0,0 is normal; 1,0 is wider; 0,1 is taller; etc.)
|
||||
|
||||
// gets the first tile at the origin
|
||||
coordinate = new ol.Coordinate(0, 0);
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndResolution(
|
||||
coordinate, 1);
|
||||
assertEquals(0, tileCoord.z);
|
||||
assertEquals(0, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
// gets the 1,0 tile at 256/3,0
|
||||
coordinate = new ol.Coordinate(256 / 3, 0);
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndResolution(
|
||||
coordinate, 1);
|
||||
assertEquals(0, tileCoord.z);
|
||||
assertEquals(1, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
// still gets the 1,0 tile at 512/3,0 - wider tile
|
||||
coordinate = new ol.Coordinate(512 / 3, 0);
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndResolution(
|
||||
coordinate, 1);
|
||||
assertEquals(0, tileCoord.z);
|
||||
assertEquals(1, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
// gets the 2,0 tile at 513/3,0
|
||||
coordinate = new ol.Coordinate(513 / 3, 0);
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndResolution(
|
||||
coordinate, 1);
|
||||
assertEquals(0, tileCoord.z);
|
||||
assertEquals(2, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
// gets the 3,0 tile at 768/3,0
|
||||
coordinate = new ol.Coordinate(768 / 3, 0);
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndResolution(
|
||||
coordinate, 1);
|
||||
assertEquals(0, tileCoord.z);
|
||||
assertEquals(3, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
// gets the 4,0 tile at 1024/3,0
|
||||
coordinate = new ol.Coordinate(1024 / 3, 0);
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndResolution(
|
||||
coordinate, 1);
|
||||
assertEquals(0, tileCoord.z);
|
||||
assertEquals(4, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
// still gets the 4,0 tile at 1280/3,0 - wider tile
|
||||
coordinate = new ol.Coordinate(1280 / 3, 0);
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndResolution(
|
||||
coordinate, 1);
|
||||
assertEquals(0, tileCoord.z);
|
||||
assertEquals(4, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
// gets the 5,0 tile at 1281/3,0
|
||||
coordinate = new ol.Coordinate(1281 / 3, 0);
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndResolution(
|
||||
coordinate, 1);
|
||||
assertEquals(0, tileCoord.z);
|
||||
assertEquals(5, tileCoord.x);
|
||||
assertEquals(0, tileCoord.y);
|
||||
|
||||
// gets the 0,1 tile at 0,-256/3
|
||||
coordinate = new ol.Coordinate(0, -256 / 3);
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndResolution(
|
||||
coordinate, 1);
|
||||
assertEquals(0, tileCoord.z);
|
||||
assertEquals(0, tileCoord.x);
|
||||
assertEquals(-2, tileCoord.y);
|
||||
|
||||
// still gets the 0,1 tile at 0,-512/3 - taller tile
|
||||
coordinate = new ol.Coordinate(0, -512 / 3);
|
||||
tileCoord = tileGrid.getTileCoordForCoordAndResolution(
|
||||
coordinate, 1);
|
||||
assertEquals(0, tileCoord.z);
|
||||
assertEquals(0, tileCoord.x);
|
||||
assertEquals(-2, tileCoord.y);
|
||||
}
|
||||
|
||||
|
||||
function testGetTileCoordCenter() {
|
||||
|
||||
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
var center;
|
||||
|
||||
center = tileGrid.getTileCoordCenter(new ol.TileCoord(0, 0, 0));
|
||||
assertEquals(50000, center.x);
|
||||
assertEquals(50000, center.y);
|
||||
|
||||
center = tileGrid.getTileCoordCenter(new ol.TileCoord(3, 0, 0));
|
||||
assertEquals(5000, center.x);
|
||||
assertEquals(5000, center.y);
|
||||
|
||||
center = tileGrid.getTileCoordCenter(new ol.TileCoord(3, 9, 9));
|
||||
assertEquals(95000, center.x);
|
||||
assertEquals(95000, center.y);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function testGetTileCoordExtent() {
|
||||
|
||||
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
var tileCoordExtent;
|
||||
|
||||
tileCoordExtent = tileGrid.getTileCoordExtent(new ol.TileCoord(0, 0, 0));
|
||||
assertEquals(0, tileCoordExtent.minX);
|
||||
assertEquals(0, tileCoordExtent.minY);
|
||||
assertEquals(100000, tileCoordExtent.maxX);
|
||||
assertEquals(100000, tileCoordExtent.maxY);
|
||||
|
||||
tileCoordExtent = tileGrid.getTileCoordExtent(new ol.TileCoord(3, 9, 0));
|
||||
assertEquals(90000, tileCoordExtent.minX);
|
||||
assertEquals(0, tileCoordExtent.minY);
|
||||
assertEquals(100000, tileCoordExtent.maxX);
|
||||
assertEquals(10000, tileCoordExtent.maxY);
|
||||
|
||||
tileCoordExtent = tileGrid.getTileCoordExtent(new ol.TileCoord(3, 0, 9));
|
||||
assertEquals(0, tileCoordExtent.minX);
|
||||
assertEquals(90000, tileCoordExtent.minY);
|
||||
assertEquals(10000, tileCoordExtent.maxX);
|
||||
assertEquals(100000, tileCoordExtent.maxY);
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testGetExtentTileBounds() {
|
||||
|
||||
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
var e = new ol.Extent(45000, 5000, 55000, 15000);
|
||||
var tileBounds;
|
||||
|
||||
tileBounds = tileGrid.getTileBoundsForExtentAndZ(e, 0);
|
||||
assertEquals(0, tileBounds.minY);
|
||||
assertEquals(0, tileBounds.minX);
|
||||
assertEquals(0, tileBounds.maxX);
|
||||
assertEquals(0, tileBounds.maxY);
|
||||
|
||||
tileBounds = tileGrid.getTileBoundsForExtentAndZ(e, 1);
|
||||
assertEquals(0, tileBounds.minX);
|
||||
assertEquals(0, tileBounds.minY);
|
||||
assertEquals(1, tileBounds.maxX);
|
||||
assertEquals(0, tileBounds.maxY);
|
||||
|
||||
tileBounds = tileGrid.getTileBoundsForExtentAndZ(e, 2);
|
||||
assertEquals(1, tileBounds.minX);
|
||||
assertEquals(0, tileBounds.minY);
|
||||
assertEquals(2, tileBounds.maxX);
|
||||
assertEquals(0, tileBounds.maxY);
|
||||
|
||||
tileBounds = tileGrid.getTileBoundsForExtentAndZ(e, 3);
|
||||
assertEquals(4, tileBounds.minX);
|
||||
assertEquals(0, tileBounds.minY);
|
||||
assertEquals(5, tileBounds.maxX);
|
||||
assertEquals(1, tileBounds.maxY);
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testForEachTileCoordParent() {
|
||||
|
||||
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
var zs = [], tileBoundss = [];
|
||||
|
||||
tileGrid.forEachTileCoordParent(
|
||||
new ol.TileCoord(3, 7, 3),
|
||||
function(z, tileBounds) {
|
||||
zs.push(z);
|
||||
tileBoundss.push(tileBounds);
|
||||
return false;
|
||||
});
|
||||
|
||||
assertEquals(3, zs.length);
|
||||
assertEquals(3, tileBoundss.length);
|
||||
|
||||
assertEquals(2, zs[0]);
|
||||
assertEquals(2, tileBoundss[0].minX);
|
||||
assertEquals(1, tileBoundss[0].minY);
|
||||
assertEquals(3, tileBoundss[0].maxX);
|
||||
assertEquals(1, tileBoundss[0].maxY);
|
||||
|
||||
assertEquals(1, zs[1]);
|
||||
assertEquals(1, tileBoundss[1].minX);
|
||||
assertEquals(0, tileBoundss[1].minY);
|
||||
assertEquals(1, tileBoundss[1].maxX);
|
||||
assertEquals(0, tileBoundss[1].maxY);
|
||||
|
||||
assertEquals(0, zs[2]);
|
||||
assertEquals(0, tileBoundss[2].minX);
|
||||
assertEquals(0, tileBoundss[2].minY);
|
||||
assertEquals(0, tileBoundss[2].maxX);
|
||||
assertEquals(0, tileBoundss[2].maxY);
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testGetZForResolutionExact() {
|
||||
|
||||
var tileGrid =
|
||||
new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
|
||||
assertEquals(0, tileGrid.getZForResolution(1000));
|
||||
assertEquals(1, tileGrid.getZForResolution(500));
|
||||
assertEquals(2, tileGrid.getZForResolution(250));
|
||||
assertEquals(3, tileGrid.getZForResolution(100));
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testGetZForResolutionApproximate() {
|
||||
|
||||
var tileGrid =
|
||||
new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
|
||||
assertEquals(0, tileGrid.getZForResolution(2000));
|
||||
assertEquals(0, tileGrid.getZForResolution(1000));
|
||||
assertEquals(0, tileGrid.getZForResolution(900));
|
||||
assertEquals(1, tileGrid.getZForResolution(750));
|
||||
assertEquals(1, tileGrid.getZForResolution(625));
|
||||
assertEquals(1, tileGrid.getZForResolution(500));
|
||||
assertEquals(1, tileGrid.getZForResolution(475));
|
||||
assertEquals(2, tileGrid.getZForResolution(375));
|
||||
assertEquals(2, tileGrid.getZForResolution(250));
|
||||
assertEquals(2, tileGrid.getZForResolution(200));
|
||||
assertEquals(3, tileGrid.getZForResolution(125));
|
||||
assertEquals(3, tileGrid.getZForResolution(100));
|
||||
assertEquals(3, tileGrid.getZForResolution(50));
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
goog.provide('ol.tilegrid.createXYZ');
|
||||
|
||||
goog.require('ol.Coordinate');
|
||||
goog.require('ol.Projection');
|
||||
goog.require('ol.Size');
|
||||
goog.require('ol.TileGrid');
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} maxZoom Maximum zoom.
|
||||
* @return {ol.TileGrid} Tile grid.
|
||||
*/
|
||||
ol.tilegrid.createXYZ = function(maxZoom) {
|
||||
|
||||
var resolutions = new Array(maxZoom + 1);
|
||||
var z;
|
||||
for (z = 0; z <= maxZoom; ++z) {
|
||||
resolutions[z] = ol.Projection.EPSG_3857_HALF_SIZE / (128 << z);
|
||||
}
|
||||
|
||||
var extent = ol.Projection.EPSG_3857_EXTENT;
|
||||
var origin = new ol.Coordinate(
|
||||
-ol.Projection.EPSG_3857_HALF_SIZE, ol.Projection.EPSG_3857_HALF_SIZE);
|
||||
var tileSize = new ol.Size(256, 256);
|
||||
|
||||
return new ol.TileGrid(resolutions, extent, origin, tileSize);
|
||||
|
||||
};
|
||||
@@ -0,0 +1,26 @@
|
||||
goog.provide('ol.TileLayer');
|
||||
|
||||
goog.require('ol.Layer');
|
||||
goog.require('ol.TileStore');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.Layer}
|
||||
* @param {ol.TileStore} tileStore Tile store.
|
||||
* @param {Object.<string, *>=} opt_values Values.
|
||||
*/
|
||||
ol.TileLayer = function(tileStore, opt_values) {
|
||||
goog.base(this, tileStore, opt_values);
|
||||
};
|
||||
goog.inherits(ol.TileLayer, ol.Layer);
|
||||
|
||||
|
||||
/**
|
||||
* @override
|
||||
* @return {ol.TileStore} Store.
|
||||
*/
|
||||
ol.TileLayer.prototype.getStore = function() {
|
||||
return /** @type {ol.TileStore} */ goog.base(this, 'getStore');
|
||||
};
|
||||
@@ -0,0 +1,27 @@
|
||||
goog.provide('ol.tilelayer.createOpenStreetMap');
|
||||
|
||||
goog.require('ol.Layer');
|
||||
goog.require('ol.TileLayer');
|
||||
goog.require('ol.tilestore.createXYZ');
|
||||
|
||||
|
||||
/**
|
||||
* @param {Object.<string, *>=} opt_values Values.
|
||||
* @return {ol.Layer} Layer.
|
||||
*/
|
||||
ol.tilelayer.createOpenStreetMap = function(opt_values) {
|
||||
var store = ol.tilestore.createXYZ(
|
||||
18,
|
||||
[
|
||||
'http://a.tile.openstreetmap.org/{z}/{x}/{y}.png',
|
||||
'http://b.tile.openstreetmap.org/{z}/{x}/{y}.png',
|
||||
'http://c.tile.openstreetmap.org/{z}/{x}/{y}.png'
|
||||
],
|
||||
'© ' +
|
||||
'<a href="http://www.openstreetmap.org">OpenStreetMap</a> ' +
|
||||
'contributors, ' +
|
||||
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC BY-SA</a>',
|
||||
''
|
||||
);
|
||||
return new ol.TileLayer(store, opt_values);
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
goog.provide('ol.tilelayer.createXYZ');
|
||||
|
||||
goog.require('ol.Layer');
|
||||
goog.require('ol.TileLayer');
|
||||
goog.require('ol.tilestore.createXYZ');
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} maxZoom Maximum zoom.
|
||||
* @param {Array.<string>} templates Templates.
|
||||
* @param {string=} opt_attribution Attribution.
|
||||
* @param {string=} opt_crossOrigin Cross origin.
|
||||
* @param {Object.<string, *>=} opt_values Values.
|
||||
* @return {ol.Layer} Layer.
|
||||
*/
|
||||
ol.tilelayer.createXYZ =
|
||||
function(maxZoom, templates, opt_attribution, opt_crossOrigin, opt_values) {
|
||||
var store = ol.tilestore.createXYZ(
|
||||
maxZoom, templates, opt_attribution, opt_crossOrigin);
|
||||
return new ol.TileLayer(store, opt_values);
|
||||
};
|
||||
@@ -0,0 +1,100 @@
|
||||
goog.provide('ol.TileStore');
|
||||
|
||||
goog.require('ol.Store');
|
||||
goog.require('ol.Tile');
|
||||
goog.require('ol.TileCoord');
|
||||
goog.require('ol.TileGrid');
|
||||
goog.require('ol.TileUrlFunctionType');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.Store}
|
||||
* @param {ol.Projection} projection Projection.
|
||||
* @param {ol.TileGrid} tileGrid Tile grid.
|
||||
* @param {ol.TileUrlFunctionType} tileUrlFunction Tile URL.
|
||||
* @param {ol.Extent=} opt_extent Extent.
|
||||
* @param {string=} opt_attribution Attribution.
|
||||
* @param {string=} opt_crossOrigin Cross origin.
|
||||
*/
|
||||
ol.TileStore = function(projection, tileGrid, tileUrlFunction, opt_extent,
|
||||
opt_attribution, opt_crossOrigin) {
|
||||
|
||||
goog.base(this, projection, opt_extent, opt_attribution);
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.TileGrid}
|
||||
*/
|
||||
this.tileGrid_ = tileGrid;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.TileUrlFunctionType}
|
||||
*/
|
||||
this.tileUrlFunction_ = tileUrlFunction;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {string|undefined}
|
||||
*/
|
||||
this.crossOrigin_ = opt_crossOrigin;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Object.<string, ol.Tile>}
|
||||
* FIXME will need to expire elements from this cache
|
||||
* FIXME see elemoine's work with goog.structs.LinkedMap
|
||||
*/
|
||||
this.tileCache_ = {};
|
||||
|
||||
};
|
||||
goog.inherits(ol.TileStore, ol.Store);
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.TileStore.prototype.getResolutions = function() {
|
||||
return this.tileGrid_.getResolutions();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.TileCoord} tileCoord Tile coordinate.
|
||||
* @return {ol.Tile} Tile.
|
||||
*/
|
||||
ol.TileStore.prototype.getTile = function(tileCoord) {
|
||||
var key = tileCoord.toString();
|
||||
if (goog.object.containsKey(this.tileCache_, key)) {
|
||||
return this.tileCache_[key];
|
||||
} else {
|
||||
var tileUrl = this.getTileCoordUrl(tileCoord);
|
||||
var tile;
|
||||
if (goog.isDef(tileUrl)) {
|
||||
tile = new ol.Tile(tileCoord, tileUrl, this.crossOrigin_);
|
||||
} else {
|
||||
tile = null;
|
||||
}
|
||||
this.tileCache_[key] = tile;
|
||||
return tile;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.TileCoord} tileCoord Tile coordinate.
|
||||
* @return {string} Tile coord URL.
|
||||
*/
|
||||
ol.TileStore.prototype.getTileCoordUrl = function(tileCoord) {
|
||||
return this.tileUrlFunction_(tileCoord);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {ol.TileGrid} Tile grid.
|
||||
*/
|
||||
ol.TileStore.prototype.getTileGrid = function() {
|
||||
return this.tileGrid_;
|
||||
};
|
||||
@@ -0,0 +1,75 @@
|
||||
goog.require('goog.testing.jsunit');
|
||||
goog.require('ol.Coordinate');
|
||||
goog.require('ol.TileCoord');
|
||||
goog.require('ol.tilestore.createXYZ');
|
||||
|
||||
|
||||
function testXYZ() {
|
||||
|
||||
var tileStore = ol.tilestore.createXYZ(6, ['{z}/{x}/{y}']);
|
||||
var tileGrid = tileStore.getTileGrid();
|
||||
|
||||
var coordinate = new ol.Coordinate(829330.2064098881, 5933916.615134273);
|
||||
var tileUrl;
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(
|
||||
tileGrid.getTileCoordForCoordAndZ(coordinate, 0));
|
||||
assertEquals('0/0/0', tileUrl);
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(
|
||||
tileGrid.getTileCoordForCoordAndZ(coordinate, 1));
|
||||
assertEquals('1/1/0', tileUrl);
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(
|
||||
tileGrid.getTileCoordForCoordAndZ(coordinate, 2));
|
||||
assertEquals('2/2/1', tileUrl);
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(
|
||||
tileGrid.getTileCoordForCoordAndZ(coordinate, 3));
|
||||
assertEquals('3/4/2', tileUrl);
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(
|
||||
tileGrid.getTileCoordForCoordAndZ(coordinate, 4));
|
||||
assertEquals('4/8/5', tileUrl);
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(
|
||||
tileGrid.getTileCoordForCoordAndZ(coordinate, 5));
|
||||
assertEquals('5/16/11', tileUrl);
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(
|
||||
tileGrid.getTileCoordForCoordAndZ(coordinate, 6));
|
||||
assertEquals('6/33/22', tileUrl);
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testXYZWrapX() {
|
||||
|
||||
var tileStore = ol.tilestore.createXYZ(6, ['{z}/{x}/{y}']);
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(new ol.TileCoord(6, -31, -23));
|
||||
assertEquals('6/33/22', tileUrl);
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(new ol.TileCoord(6, 33, -23));
|
||||
assertEquals('6/33/22', tileUrl);
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(new ol.TileCoord(6, 97, -23));
|
||||
assertEquals('6/33/22', tileUrl);
|
||||
|
||||
}
|
||||
|
||||
|
||||
function testXYZCropY() {
|
||||
|
||||
var tileStore = ol.tilestore.createXYZ(6, ['{z}/{x}/{y}']);
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(new ol.TileCoord(6, 33, -87));
|
||||
assertUndefined(tileUrl);
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(new ol.TileCoord(6, 33, -23));
|
||||
assertEquals('6/33/22', tileUrl);
|
||||
|
||||
tileUrl = tileStore.getTileCoordUrl(new ol.TileCoord(6, 33, 41));
|
||||
assertUndefined(tileUrl);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
goog.provide('ol.tilestore.createXYZ');
|
||||
|
||||
goog.require('goog.math');
|
||||
goog.require('ol.Projection');
|
||||
goog.require('ol.TileCoord');
|
||||
goog.require('ol.TileGrid');
|
||||
goog.require('ol.TileStore');
|
||||
goog.require('ol.TileUrlFunction');
|
||||
goog.require('ol.tilegrid.createXYZ');
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} maxZoom Maximum zoom.
|
||||
* @param {Array.<string>} templates Templates.
|
||||
* @param {string=} opt_attribution Attribution.
|
||||
* @param {string=} opt_crossOrigin Cross origin.
|
||||
* @return {ol.TileStore} Tile store.
|
||||
*/
|
||||
ol.tilestore.createXYZ =
|
||||
function(maxZoom, templates, opt_attribution, opt_crossOrigin) {
|
||||
|
||||
var projection = ol.Projection.getFromCode('EPSG:3857');
|
||||
var tileGrid = ol.tilegrid.createXYZ(maxZoom);
|
||||
var tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform(
|
||||
function(tileCoord) {
|
||||
var n = 1 << tileCoord.z;
|
||||
var y = -tileCoord.y - 1;
|
||||
if (y < 0 || n <= y) {
|
||||
return null;
|
||||
} else {
|
||||
var x = goog.math.modulo(tileCoord.x, n);
|
||||
return new ol.TileCoord(tileCoord.z, x, y);
|
||||
}
|
||||
},
|
||||
ol.TileUrlFunction.createFromTemplates(templates));
|
||||
var extent = projection.getExtent();
|
||||
|
||||
return new ol.TileStore(projection, tileGrid, tileUrlFunction, extent,
|
||||
opt_attribution, opt_crossOrigin);
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
goog.provide('ol.TileUrlFunction');
|
||||
goog.provide('ol.TileUrlFunctionType');
|
||||
|
||||
goog.require('goog.math');
|
||||
goog.require('ol.TileCoord');
|
||||
|
||||
|
||||
/**
|
||||
* @typedef {function(ol.TileCoord): string|undefined}
|
||||
*/
|
||||
ol.TileUrlFunctionType;
|
||||
|
||||
|
||||
/**
|
||||
* @param {string} template Template.
|
||||
* @return {ol.TileUrlFunctionType} Tile URL function.
|
||||
*/
|
||||
ol.TileUrlFunction.createFromTemplate = function(template) {
|
||||
return function(tileCoord) {
|
||||
if (goog.isNull(tileCoord)) {
|
||||
return undefined;
|
||||
} else {
|
||||
return template.replace(/\{z\}/, tileCoord.z)
|
||||
.replace(/\{x\}/, tileCoord.x)
|
||||
.replace(/\{y\}/, tileCoord.y);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {Array.<ol.TileUrlFunctionType>} tileUrlFunctions Tile URL Functions.
|
||||
* @return {ol.TileUrlFunctionType} Tile URL function.
|
||||
*/
|
||||
ol.TileUrlFunction.createFromTileUrlFunctions = function(tileUrlFunctions) {
|
||||
return function(tileCoord) {
|
||||
if (goog.isNull(tileCoord)) {
|
||||
return undefined;
|
||||
} else {
|
||||
var index = goog.math.modulo(tileCoord.hash(), tileUrlFunctions.length);
|
||||
return tileUrlFunctions[index](tileCoord);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {Array.<string>} templates Templates.
|
||||
* @return {ol.TileUrlFunctionType} Tile URL function.
|
||||
*/
|
||||
ol.TileUrlFunction.createFromTemplates = function(templates) {
|
||||
return ol.TileUrlFunction.createFromTileUrlFunctions(
|
||||
goog.array.map(templates, ol.TileUrlFunction.createFromTemplate));
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {function(ol.TileCoord): ol.TileCoord} transform Transform.
|
||||
* @param {ol.TileUrlFunctionType} tileUrlFunction Tile URL function.
|
||||
* @return {ol.TileUrlFunctionType} Tile URL function.
|
||||
*/
|
||||
ol.TileUrlFunction.withTileCoordTransform =
|
||||
function(transform, tileUrlFunction) {
|
||||
return function(tileCoord) {
|
||||
if (goog.isNull(tileCoord)) {
|
||||
return undefined;
|
||||
} else {
|
||||
return tileUrlFunction(transform(tileCoord));
|
||||
}
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,33 @@
|
||||
goog.require('goog.testing.jsunit');
|
||||
goog.require('ol.TileCoord');
|
||||
goog.require('ol.TileUrlFunction');
|
||||
|
||||
|
||||
function testCreateFromTemplate() {
|
||||
var tileUrl = ol.TileUrlFunction.createFromTemplate('{z}/{x}/{y}');
|
||||
assertEquals('3/2/1', tileUrl(new ol.TileCoord(3, 2, 1)));
|
||||
assertUndefined(tileUrl(null));
|
||||
}
|
||||
|
||||
|
||||
function testWithTileCoordTransform() {
|
||||
var tileUrl = ol.TileUrlFunction.withTileCoordTransform(
|
||||
function(tileCoord) {
|
||||
return new ol.TileCoord(tileCoord.z, tileCoord.x, -tileCoord.y);
|
||||
},
|
||||
ol.TileUrlFunction.createFromTemplate('{z}/{x}/{y}'));
|
||||
assertEquals('3/2/1', tileUrl(new ol.TileCoord(3, 2, -1)));
|
||||
assertUndefined(tileUrl(null));
|
||||
}
|
||||
|
||||
|
||||
function testCreateFromTileUrlFunctions() {
|
||||
var tileUrl = ol.TileUrlFunction.createFromTileUrlFunctions([
|
||||
ol.TileUrlFunction.createFromTemplate('a'),
|
||||
ol.TileUrlFunction.createFromTemplate('b')
|
||||
]);
|
||||
var tileUrl1 = tileUrl(new ol.TileCoord(1, 0, 0));
|
||||
var tileUrl2 = tileUrl(new ol.TileCoord(1, 0, 1));
|
||||
assertTrue(tileUrl1 != tileUrl2);
|
||||
assertUndefined(tileUrl(null));
|
||||
}
|
||||
Reference in New Issue
Block a user