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

View File

@@ -3,11 +3,9 @@ goog.provide('ol.source.BingMaps');
goog.require('goog.Uri');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.math');
goog.require('goog.net.Jsonp');
goog.require('ol.Attribution');
goog.require('ol.Size');
goog.require('ol.TileCoord');
goog.require('ol.TileRange');
goog.require('ol.TileUrlFunction');
goog.require('ol.extent');
@@ -72,30 +70,17 @@ ol.source.BingMaps.prototype.handleImageryMetadataResponse =
goog.asserts.assert(resourceSet.resources.length == 1);
var resource = resourceSet.resources[0];
var zoomMin = resource.zoomMin;
var zoomMax = resource.zoomMax;
var tileSize = new ol.Size(resource.imageWidth, resource.imageHeight);
var tileGrid = new ol.tilegrid.XYZ({
maxZoom: zoomMax,
minZoom: resource.zoomMin,
maxZoom: resource.zoomMax,
tileSize: tileSize
});
this.tileGrid = tileGrid;
var culture = this.culture_;
this.tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform(
function(tileCoord) {
if (tileCoord.z < zoomMin || zoomMax < tileCoord.z) {
return null;
}
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);
}
},
tileGrid.createTileCoordTransform(),
ol.TileUrlFunction.createFromTileUrlFunctions(
goog.array.map(
resource.imageUrlSubdomains,

View File

@@ -1,6 +1,5 @@
// FIXME add some error checking
// FIXME check order of async callbacks
// FIXME use minzoom when supported
/**
* @see http://mapbox.com/developers/api/
@@ -10,10 +9,8 @@ goog.provide('ol.source.TileJSON');
goog.provide('ol.tilejson');
goog.require('goog.asserts');
goog.require('goog.math');
goog.require('goog.net.jsloader');
goog.require('ol.Attribution');
goog.require('ol.TileCoord');
goog.require('ol.TileRange');
goog.require('ol.TileUrlFunction');
goog.require('ol.extent');
@@ -77,17 +74,14 @@ ol.source.TileJSON.prototype.handleTileJSONResponse = function() {
var epsg4326Projection = ol.projection.get('EPSG:4326');
var epsg4326Extent, extent;
var extent;
if (goog.isDef(tileJSON.bounds)) {
var bounds = tileJSON.bounds;
epsg4326Extent = [bounds[0], bounds[2], bounds[1], bounds[3]];
var epsg4326Extent = [bounds[0], bounds[2], bounds[1], bounds[3]];
var transform = ol.projection.getTransformFromProjections(
epsg4326Projection, this.getProjection());
extent = ol.extent.transform(epsg4326Extent, transform);
this.setExtent(extent);
} else {
epsg4326Extent = null;
extent = null;
}
var scheme = goog.isDef(tileJSON.scheme) || 'xyz';
@@ -95,38 +89,22 @@ ol.source.TileJSON.prototype.handleTileJSONResponse = function() {
goog.asserts.assert(tileJSON.scheme == 'xyz');
}
var minZoom = tileJSON.minzoom || 0;
goog.asserts.assert(minZoom === 0); // FIXME
var maxZoom = tileJSON.maxzoom || 22;
var tileGrid = new ol.tilegrid.XYZ({
maxZoom: maxZoom
maxZoom: maxZoom,
minZoom: minZoom
});
this.tileGrid = tileGrid;
this.tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform(
function(tileCoord) {
if (tileCoord.z < minZoom || maxZoom < tileCoord.z) {
return null;
}
var n = 1 << tileCoord.z;
var y = -tileCoord.y - 1;
if (y < 0 || n <= y) {
return null;
}
var x = goog.math.modulo(tileCoord.x, n);
if (!goog.isNull(extent)) {
var tileExtent = tileGrid.getTileCoordExtent(
new ol.TileCoord(tileCoord.z, x, tileCoord.y));
if (!ol.extent.intersects(tileExtent, extent)) {
return null;
}
}
return new ol.TileCoord(tileCoord.z, x, y);
},
tileGrid.createTileCoordTransform({
extent: extent
}),
ol.TileUrlFunction.createFromTemplates(tileJSON.tiles));
if (goog.isDef(tileJSON.attribution)) {
var attributionExtent = goog.isNull(extent) ?
epsg4326Projection.getExtent() : extent;
var attributionExtent = goog.isDef(extent) ?
extent : epsg4326Projection.getExtent();
/** @type {Object.<string, Array.<ol.TileRange>>} */
var tileRanges = {};
var z, zKey;

View File

@@ -1,15 +1,10 @@
// FIXME add minZoom support
goog.provide('ol.source.XYZ');
goog.provide('ol.source.XYZOptions');
goog.require('goog.math');
goog.require('ol.Attribution');
goog.require('ol.Projection');
goog.require('ol.TileCoord');
goog.require('ol.TileUrlFunction');
goog.require('ol.TileUrlFunctionType');
goog.require('ol.extent');
goog.require('ol.projection');
goog.require('ol.source.ImageTileSource');
goog.require('ol.tilegrid.XYZ');
@@ -21,6 +16,7 @@ goog.require('ol.tilegrid.XYZ');
* extent: (ol.Extent|undefined),
* logo: (string|undefined),
* maxZoom: number,
* minZoom: (number|undefined),
* projection: (ol.Projection|undefined),
* tileUrlFunction: (ol.TileUrlFunctionType|undefined),
* url: (string|undefined),
@@ -54,55 +50,16 @@ ol.source.XYZ = function(options) {
}
var tileGrid = new ol.tilegrid.XYZ({
maxZoom: options.maxZoom
maxZoom: options.maxZoom,
minZoom: options.minZoom
});
// FIXME factor out common code
if (goog.isDef(options.extent)) {
var tileCoordTransform = tileGrid.createTileCoordTransform({
extent: options.extent
});
var extent = options.extent;
var tmpExtent = ol.extent.createEmptyExtent();
var tmpTileCoord = new ol.TileCoord(0, 0, 0);
tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform(
function(tileCoord) {
if (options.maxZoom < tileCoord.z) {
return null;
}
var n = 1 << tileCoord.z;
var y = -tileCoord.y - 1;
if (y < 0 || n <= y) {
return null;
}
var x = goog.math.modulo(tileCoord.x, n);
tmpTileCoord.z = tileCoord.z;
tmpTileCoord.x = x;
tmpTileCoord.y = tileCoord.y;
var tileExtent = tileGrid.getTileCoordExtent(tmpTileCoord, tmpExtent);
if (!ol.extent.intersects(tileExtent, extent)) {
return null;
}
return new ol.TileCoord(tileCoord.z, x, y);
},
tileUrlFunction);
} else {
tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform(
function(tileCoord) {
if (options.maxZoom < tileCoord.z) {
return null;
}
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);
}
},
tileUrlFunction);
}
tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform(
tileCoordTransform, tileUrlFunction);
goog.base(this, {
attributions: options.attributions,