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

@@ -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;