Allow extents to restrict tile ranges requested from the server
The addition of full extent tile ranges also allows us to simplify wrapX handling for tile layers. By limiting wrapX to true and false as possible values, we can remove a lot of guessing logic.
This commit is contained in:
+98
-55
@@ -4,6 +4,7 @@ goog.require('goog.array');
|
||||
goog.require('goog.asserts');
|
||||
goog.require('goog.functions');
|
||||
goog.require('goog.math');
|
||||
goog.require('goog.object');
|
||||
goog.require('ol');
|
||||
goog.require('ol.Coordinate');
|
||||
goog.require('ol.TileCoord');
|
||||
@@ -69,6 +70,10 @@ ol.tilegrid.TileGrid = function(options) {
|
||||
goog.asserts.assert(this.origins_.length == this.resolutions_.length,
|
||||
'number of origins and resolutions must be equal');
|
||||
}
|
||||
if (goog.isNull(this.origins_) && goog.isNull(this.origin_) &&
|
||||
goog.isDef(options.extent)) {
|
||||
this.origin_ = ol.extent.getBottomLeft(options.extent);
|
||||
}
|
||||
goog.asserts.assert(
|
||||
(goog.isNull(this.origin_) && !goog.isNull(this.origins_)) ||
|
||||
(!goog.isNull(this.origin_) && goog.isNull(this.origins_)),
|
||||
@@ -97,6 +102,51 @@ ol.tilegrid.TileGrid = function(options) {
|
||||
(!goog.isNull(this.tileSize_) && goog.isNull(this.tileSizes_)),
|
||||
'either tileSize or tileSizes must be configured, never both');
|
||||
|
||||
var extent = options.extent;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.Extent}
|
||||
*/
|
||||
this.extent_ = goog.isDef(extent) ? extent : null;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Array.<ol.TileRange>}
|
||||
*/
|
||||
this.fullTileRanges_ = null;
|
||||
|
||||
if (goog.isDef(options.sizes)) {
|
||||
goog.asserts.assert(options.sizes.length == this.resolutions_.length,
|
||||
'number of sizes and resolutions must be equal');
|
||||
this.fullTileRanges_ = goog.array.map(options.sizes, function(size, z) {
|
||||
goog.asserts.assert(size[0] > 0, 'width must be > 0');
|
||||
goog.asserts.assert(size[1] !== 0, 'height must not be 0');
|
||||
var tileRange = new ol.TileRange(0, size[0] - 1, 0, size[1] - 1);
|
||||
if (tileRange.maxY < tileRange.minY) {
|
||||
tileRange.minY = size[1];
|
||||
tileRange.maxY = -1;
|
||||
}
|
||||
if (this.minZoom <= z && z <= this.maxZoom && goog.isDef(extent)) {
|
||||
goog.asserts.assert(tileRange.containsTileRange(
|
||||
this.getTileRangeForExtentAndZ(extent, z)),
|
||||
'extent tile range must not exceed tilegrid width and height');
|
||||
}
|
||||
return tileRange;
|
||||
}, this);
|
||||
} else if (goog.isDef(extent)) {
|
||||
var extentWidth = ol.extent.getWidth(extent);
|
||||
var extentHeight = ol.extent.getHeight(extent);
|
||||
var fullTileRanges = new Array(this.resolutions_.length);
|
||||
var tileSize;
|
||||
for (var z = 0, zz = fullTileRanges.length; z < zz; ++z) {
|
||||
tileSize = ol.size.toSize(this.getTileSize(z), this.tmpSize_);
|
||||
fullTileRanges[z] = new ol.TileRange(
|
||||
0, Math.ceil(extentWidth / tileSize[0] / this.resolutions_[z]) - 1,
|
||||
0, Math.ceil(extentHeight / tileSize[1] / this.resolutions_[z]) - 1);
|
||||
}
|
||||
this.fullTileRanges_ = fullTileRanges;
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -104,17 +154,6 @@ ol.tilegrid.TileGrid = function(options) {
|
||||
*/
|
||||
this.tmpSize_ = [0, 0];
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Array.<number>}
|
||||
*/
|
||||
this.widths_ = null;
|
||||
if (goog.isDef(options.widths)) {
|
||||
this.widths_ = options.widths;
|
||||
goog.asserts.assert(this.widths_.length == this.resolutions_.length,
|
||||
'number of widths and resolutions must be equal');
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -161,6 +200,15 @@ ol.tilegrid.TileGrid.prototype.forEachTileCoordParentTileRange =
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Get the extent for this tile grid, if it was configured.
|
||||
* @return {ol.Extent} Extent.
|
||||
*/
|
||||
ol.tilegrid.TileGrid.prototype.getExtent = function() {
|
||||
return this.extent_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Get the maximum zoom level for the grid.
|
||||
* @return {number} Max zoom.
|
||||
@@ -412,25 +460,6 @@ ol.tilegrid.TileGrid.prototype.getTileCoordResolution = function(tileCoord) {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} z Zoom level.
|
||||
* @param {ol.proj.Projection} projection Projection.
|
||||
* @param {ol.TileRange=} opt_tileRange Tile range.
|
||||
* @return {ol.TileRange} Tile range.
|
||||
*/
|
||||
ol.tilegrid.TileGrid.prototype.getTileRange =
|
||||
function(z, projection, opt_tileRange) {
|
||||
var projectionExtentTileRange = this.getTileRangeForExtentAndZ(
|
||||
ol.tilegrid.extentFromProjection(projection), z);
|
||||
var width = this.getWidth(z);
|
||||
if (!goog.isDef(width)) {
|
||||
width = projectionExtentTileRange.getWidth();
|
||||
}
|
||||
return ol.TileRange.createOrUpdate(
|
||||
0, width - 1, 0, projectionExtentTileRange.getHeight(), opt_tileRange);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Get the tile size for a zoom level. The type of the return value matches the
|
||||
* `tileSize` or `tileSizes` that the tile grid was configured with. To always
|
||||
@@ -455,15 +484,16 @@ ol.tilegrid.TileGrid.prototype.getTileSize = function(z) {
|
||||
|
||||
/**
|
||||
* @param {number} z Zoom level.
|
||||
* @return {number|undefined} Width for the specified zoom level or `undefined`
|
||||
* if unknown.
|
||||
* @return {ol.TileRange} Extent tile range for the specified zoom level.
|
||||
*/
|
||||
ol.tilegrid.TileGrid.prototype.getWidth = function(z) {
|
||||
if (!goog.isNull(this.widths_)) {
|
||||
ol.tilegrid.TileGrid.prototype.getFullTileRange = function(z) {
|
||||
if (goog.isNull(this.fullTileRanges_)) {
|
||||
return null;
|
||||
} else {
|
||||
goog.asserts.assert(this.minZoom <= z && z <= this.maxZoom,
|
||||
'z is not in allowed range (%s <= %s <= %s',
|
||||
this.minZoom, z, this.maxZoom);
|
||||
return this.widths_[z];
|
||||
return this.fullTileRanges_[z];
|
||||
}
|
||||
};
|
||||
|
||||
@@ -478,26 +508,6 @@ ol.tilegrid.TileGrid.prototype.getZForResolution = function(resolution) {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} z Zoom level.
|
||||
* @param {ol.proj.Projection} projection Projection.
|
||||
* @return {boolean} Whether the tile grid is defined for the whole globe when
|
||||
* used with the provided `projection` at zoom level `z`.
|
||||
*/
|
||||
ol.tilegrid.TileGrid.prototype.isGlobal = function(z, projection) {
|
||||
var width = this.getWidth(z);
|
||||
if (goog.isDef(width)) {
|
||||
var projTileGrid = ol.tilegrid.getForProjection(projection);
|
||||
var projExtent = projection.getExtent();
|
||||
return ol.size.toSize(this.getTileSize(z), this.tmpSize_)[0] * width ==
|
||||
projTileGrid.getTileSize(z) *
|
||||
projTileGrid.getTileRangeForExtentAndZ(projExtent, z).getWidth();
|
||||
} else {
|
||||
return projection.isGlobal();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.proj.Projection} projection Projection.
|
||||
* @return {ol.tilegrid.TileGrid} Default tile grid for the passed projection.
|
||||
@@ -611,3 +621,36 @@ ol.tilegrid.extentFromProjection = function(projection) {
|
||||
}
|
||||
return extent;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.tilegrid.TileGrid} tileGrid Tile grid.
|
||||
* @return {function(ol.TileCoord, ol.proj.Projection, ol.TileCoord=):
|
||||
* ol.TileCoord} Tile coordinate transform.
|
||||
*/
|
||||
ol.tilegrid.createOriginTopLeftTileCoordTransform = function(tileGrid) {
|
||||
goog.asserts.assert(!goog.isNull(tileGrid), 'tileGrid required');
|
||||
return (
|
||||
/**
|
||||
* @param {ol.TileCoord} tileCoord Tile coordinate.
|
||||
* @param {ol.proj.Projection} projection Projection.
|
||||
* @param {ol.TileCoord=} opt_tileCoord Destination tile coordinate.
|
||||
* @return {ol.TileCoord} Tile coordinate.
|
||||
*/
|
||||
function(tileCoord, projection, opt_tileCoord) {
|
||||
if (goog.isNull(tileCoord)) {
|
||||
return null;
|
||||
}
|
||||
var z = tileCoord[0];
|
||||
var fullTileRange = tileGrid.getFullTileRange(z);
|
||||
var height;
|
||||
if (goog.isNull(fullTileRange) || fullTileRange.minY < 0) {
|
||||
height = 0;
|
||||
} else {
|
||||
height = fullTileRange.getHeight();
|
||||
}
|
||||
return ol.tilecoord.createOrUpdate(
|
||||
z, tileCoord[1], height - tileCoord[2] - 1, opt_tileCoord);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
@@ -32,12 +32,13 @@ ol.tilegrid.WMTS = function(options) {
|
||||
// FIXME: should the matrixIds become optionnal?
|
||||
|
||||
goog.base(this, {
|
||||
extent: options.extent,
|
||||
origin: options.origin,
|
||||
origins: options.origins,
|
||||
resolutions: options.resolutions,
|
||||
tileSize: options.tileSize,
|
||||
tileSizes: options.tileSizes,
|
||||
widths: options.widths
|
||||
sizes: options.sizes
|
||||
});
|
||||
|
||||
};
|
||||
@@ -69,11 +70,13 @@ ol.tilegrid.WMTS.prototype.getMatrixIds = function() {
|
||||
* Create a tile grid from a WMTS capabilities matrix set.
|
||||
* @param {Object} matrixSet An object representing a matrixSet in the
|
||||
* capabilities document.
|
||||
* @param {ol.Extent=} opt_extent An optional extent to restrict the tile
|
||||
* ranges the server provides.
|
||||
* @return {ol.tilegrid.WMTS} WMTS tileGrid instance.
|
||||
* @api
|
||||
*/
|
||||
ol.tilegrid.WMTS.createFromCapabilitiesMatrixSet =
|
||||
function(matrixSet) {
|
||||
function(matrixSet, opt_extent) {
|
||||
|
||||
/** @type {!Array.<number>} */
|
||||
var resolutions = [];
|
||||
@@ -83,8 +86,8 @@ ol.tilegrid.WMTS.createFromCapabilitiesMatrixSet =
|
||||
var origins = [];
|
||||
/** @type {!Array.<ol.Size>} */
|
||||
var tileSizes = [];
|
||||
/** @type {!Array.<number>} */
|
||||
var widths = [];
|
||||
/** @type {!Array.<ol.Size>} */
|
||||
var sizes = [];
|
||||
|
||||
var supportedCRSPropName = 'SupportedCRS';
|
||||
var matrixIdsPropName = 'TileMatrix';
|
||||
@@ -108,26 +111,30 @@ ol.tilegrid.WMTS.createFromCapabilitiesMatrixSet =
|
||||
goog.array.forEach(matrixSet[matrixIdsPropName],
|
||||
function(elt, index, array) {
|
||||
matrixIds.push(elt[identifierPropName]);
|
||||
var resolution = elt[scaleDenominatorPropName] * 0.28E-3 /
|
||||
metersPerUnit;
|
||||
var tileWidth = elt[tileWidthPropName];
|
||||
var tileHeight = elt[tileHeightPropName];
|
||||
var matrixHeight = elt['MatrixHeight'];
|
||||
if (switchOriginXY) {
|
||||
origins.push([elt[topLeftCornerPropName][1],
|
||||
elt[topLeftCornerPropName][0]]);
|
||||
} else {
|
||||
origins.push(elt[topLeftCornerPropName]);
|
||||
}
|
||||
resolutions.push(elt[scaleDenominatorPropName] * 0.28E-3 /
|
||||
metersPerUnit);
|
||||
var tileWidth = elt[tileWidthPropName];
|
||||
var tileHeight = elt[tileHeightPropName];
|
||||
resolutions.push(resolution);
|
||||
tileSizes.push(tileWidth == tileHeight ?
|
||||
tileWidth : [tileWidth, tileHeight]);
|
||||
widths.push(elt['MatrixWidth']);
|
||||
// top-left origin, so height is negative
|
||||
sizes.push([elt['MatrixWidth'], -matrixHeight]);
|
||||
});
|
||||
|
||||
return new ol.tilegrid.WMTS({
|
||||
extent: opt_extent,
|
||||
origins: origins,
|
||||
resolutions: resolutions,
|
||||
matrixIds: matrixIds,
|
||||
tileSizes: tileSizes,
|
||||
widths: widths
|
||||
sizes: sizes
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user