Refactor tile functions

* Add support for minZoom in XYZ tile grids and tile sources
* Factor out common tile coordinate transforms
This commit is contained in:
Tom Payne
2013-04-29 17:15:33 +02:00
parent 2447f09128
commit 6976fd96e3
8 changed files with 133 additions and 131 deletions
+70 -12
View File
@@ -1,7 +1,10 @@
goog.provide('ol.tilegrid.XYZ');
goog.require('goog.math');
goog.require('ol.Size');
goog.require('ol.TileCoord');
goog.require('ol.TileRange');
goog.require('ol.extent');
goog.require('ol.projection');
goog.require('ol.projection.EPSG3857');
goog.require('ol.tilegrid.TileGrid');
@@ -15,20 +18,15 @@ goog.require('ol.tilegrid.TileGrid');
*/
ol.tilegrid.XYZ = function(options) {
/**
* @private
* @type {number}
*/
this.maxZoom_ = options.maxZoom;
var resolutions = new Array(this.maxZoom_ + 1);
var resolutions = new Array(options.maxZoom + 1);
var z;
var size = 2 * ol.projection.EPSG3857.HALF_SIZE / ol.DEFAULT_TILE_SIZE;
for (z = 0; z <= this.maxZoom_; ++z) {
for (z = 0; z <= options.maxZoom; ++z) {
resolutions[z] = size / Math.pow(2, z);
}
goog.base(this, {
minZoom: options.minZoom,
origin: [-ol.projection.EPSG3857.HALF_SIZE,
ol.projection.EPSG3857.HALF_SIZE],
resolutions: resolutions,
@@ -39,15 +37,75 @@ ol.tilegrid.XYZ = function(options) {
goog.inherits(ol.tilegrid.XYZ, ol.tilegrid.TileGrid);
/**
* @param {{wrapX: (boolean|undefined),
* extent: (ol.Extent|undefined)}=} opt_options Options.
* @return {function(ol.TileCoord, ol.Projection, ol.TileCoord=): ol.TileCoord}
* Tile coordinate transform.
*/
ol.tilegrid.XYZ.prototype.createTileCoordTransform = function(opt_options) {
var options = goog.isDef(opt_options) ? opt_options : {};
var tileGrid = this;
var minZ = this.minZoom;
var maxZ = this.maxZoom;
var wrapX = goog.isDef(options.wrapX) ? options.wrapX : true;
var extent = options.extent;
var tmpExtent = ol.extent.createEmptyExtent();
var tmpTileCoord = new ol.TileCoord(0, 0, 0);
return (
/**
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @param {ol.Projection} projection Projection.
* @param {ol.TileCoord=} opt_tileCoord Destination tile coordinate.
* @return {ol.TileCoord} Tile coordinate.
*/
function(tileCoord, projection, opt_tileCoord) {
var z = tileCoord.z;
if (z < minZ || maxZ < z) {
return null;
}
var n = Math.pow(2, z);
var x = tileCoord.x;
if (wrapX) {
x = goog.math.modulo(x, n);
} else if (x < 0 || n <= x) {
return null;
}
var y = tileCoord.y;
if (y < -n || -1 < y) {
return null;
}
if (goog.isDef(extent)) {
tmpTileCoord.z = z;
tmpTileCoord.x = x;
tmpTileCoord.y = y;
var tileExtent =
tileGrid.getTileCoordExtent(tmpTileCoord, tmpExtent);
if (!ol.extent.intersects(extent, tileExtent)) {
return null;
}
}
if (goog.isDef(opt_tileCoord)) {
opt_tileCoord.z = z;
opt_tileCoord.x = x;
opt_tileCoord.y = -y - 1;
return opt_tileCoord;
} else {
return new ol.TileCoord(z, x, -y - 1);
}
});
};
/**
* @inheritDoc
*/
ol.tilegrid.XYZ.prototype.getTileCoordChildTileRange =
function(tileCoord, opt_tileRange) {
if (tileCoord.z < this.maxZoom_) {
if (tileCoord.z < this.maxZoom) {
return ol.TileRange.createOrUpdate(
tileCoord.x << 1, tileCoord.x + 1 << 1,
tileCoord.y << 1, tileCoord.y + 1 << 1,
2 * tileCoord.x, 2 * (tileCoord.x + 1),
2 * tileCoord.y, 2 * (tileCoord.y + 1),
opt_tileRange);
} else {
return null;
@@ -63,7 +121,7 @@ ol.tilegrid.XYZ.prototype.forEachTileCoordParentTileRange =
var tileRange = ol.TileRange.createOrUpdate(
0, tileCoord.x, 0, tileCoord.y, opt_tileRange);
var z;
for (z = tileCoord.z - 1; z >= 0; --z) {
for (z = tileCoord.z - 1; z >= this.minZoom; --z) {
tileRange.minX = tileRange.maxX >>= 1;
tileRange.minY = tileRange.maxY >>= 1;
if (callback.call(opt_obj, z, tileRange)) {