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:
+32
-11
@@ -1,5 +1,3 @@
|
||||
// FIXME cope with tile grids whose minium zoom is not zero
|
||||
|
||||
goog.provide('ol.tilegrid.TileGrid');
|
||||
|
||||
goog.require('goog.array');
|
||||
@@ -32,6 +30,12 @@ ol.DEFAULT_MAX_ZOOM = 42;
|
||||
*/
|
||||
ol.tilegrid.TileGrid = function(options) {
|
||||
|
||||
/**
|
||||
* @protected
|
||||
* @type {number}
|
||||
*/
|
||||
this.minZoom = goog.isDef(options.minZoom) ? options.minZoom : 0;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {!Array.<number>}
|
||||
@@ -42,10 +46,10 @@ ol.tilegrid.TileGrid = function(options) {
|
||||
}, true));
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @protected
|
||||
* @type {number}
|
||||
*/
|
||||
this.numResolutions_ = this.resolutions_.length;
|
||||
this.maxZoom = this.resolutions_.length - 1;
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -60,7 +64,7 @@ ol.tilegrid.TileGrid = function(options) {
|
||||
this.origins_ = null;
|
||||
if (goog.isDef(options.origins)) {
|
||||
this.origins_ = options.origins;
|
||||
goog.asserts.assert(this.origins_.length == this.resolutions_.length);
|
||||
goog.asserts.assert(this.origins_.length == this.maxZoom + 1);
|
||||
}
|
||||
goog.asserts.assert(
|
||||
(goog.isNull(this.origin_) && !goog.isNull(this.origins_)) ||
|
||||
@@ -73,7 +77,7 @@ ol.tilegrid.TileGrid = function(options) {
|
||||
this.tileSizes_ = null;
|
||||
if (goog.isDef(options.tileSizes)) {
|
||||
this.tileSizes_ = options.tileSizes;
|
||||
goog.asserts.assert(this.tileSizes_.length == this.resolutions_.length);
|
||||
goog.asserts.assert(this.tileSizes_.length == this.maxZoom + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,7 +115,7 @@ ol.tilegrid.TileGrid.prototype.forEachTileCoordParentTileRange =
|
||||
function(tileCoord, callback, opt_obj, opt_tileRange, opt_extent) {
|
||||
var tileCoordExtent = this.getTileCoordExtent(tileCoord, opt_extent);
|
||||
var z = tileCoord.z - 1;
|
||||
while (z >= 0) {
|
||||
while (z >= this.minZoom) {
|
||||
if (callback.call(opt_obj, z,
|
||||
this.getTileRangeForExtentAndZ(tileCoordExtent, z, opt_tileRange))) {
|
||||
return true;
|
||||
@@ -122,6 +126,22 @@ ol.tilegrid.TileGrid.prototype.forEachTileCoordParentTileRange =
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {number} Max zoom.
|
||||
*/
|
||||
ol.tilegrid.TileGrid.prototype.getMaxZoom = function() {
|
||||
return this.maxZoom;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {number} Min zoom.
|
||||
*/
|
||||
ol.tilegrid.TileGrid.prototype.getMinZoom = function() {
|
||||
return this.minZoom;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} z Z.
|
||||
* @return {ol.Coordinate} Origin.
|
||||
@@ -131,7 +151,7 @@ ol.tilegrid.TileGrid.prototype.getOrigin = function(z) {
|
||||
return this.origin_;
|
||||
} else {
|
||||
goog.asserts.assert(!goog.isNull(this.origins_));
|
||||
goog.asserts.assert(0 <= z && z < this.origins_.length);
|
||||
goog.asserts.assert(this.minZoom <= z && z <= this.maxZoom);
|
||||
return this.origins_[z];
|
||||
}
|
||||
};
|
||||
@@ -142,7 +162,7 @@ ol.tilegrid.TileGrid.prototype.getOrigin = function(z) {
|
||||
* @return {number} Resolution.
|
||||
*/
|
||||
ol.tilegrid.TileGrid.prototype.getResolution = function(z) {
|
||||
goog.asserts.assert(0 <= z && z < this.numResolutions_);
|
||||
goog.asserts.assert(this.minZoom <= z && z <= this.maxZoom);
|
||||
return this.resolutions_[z];
|
||||
};
|
||||
|
||||
@@ -328,7 +348,8 @@ ol.tilegrid.TileGrid.prototype.getTileCoordForCoordAndZ =
|
||||
* @return {number} Tile resolution.
|
||||
*/
|
||||
ol.tilegrid.TileGrid.prototype.getTileCoordResolution = function(tileCoord) {
|
||||
goog.asserts.assert(0 <= tileCoord.z && tileCoord.z < this.numResolutions_);
|
||||
goog.asserts.assert(
|
||||
this.minZoom <= tileCoord.z && tileCoord.z <= this.maxZoom);
|
||||
return this.resolutions_[tileCoord.z];
|
||||
};
|
||||
|
||||
@@ -342,7 +363,7 @@ ol.tilegrid.TileGrid.prototype.getTileSize = function(z) {
|
||||
return this.tileSize_;
|
||||
} else {
|
||||
goog.asserts.assert(!goog.isNull(this.tileSizes_));
|
||||
goog.asserts.assert(0 <= z && z < this.tileSizes_.length);
|
||||
goog.asserts.assert(this.minZoom <= z && z <= this.maxZoom);
|
||||
return this.tileSizes_[z];
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
Reference in New Issue
Block a user