Use per-projection tilecaches and tilegrids (persistent instances)

This commit is contained in:
Petr Sloup
2015-06-05 10:29:55 +02:00
parent bc74273208
commit 3f567b0bf0
+38 -14
View File
@@ -10,6 +10,7 @@ goog.require('ol.TileLoadFunctionType');
goog.require('ol.TileState'); goog.require('ol.TileState');
goog.require('ol.TileUrlFunction'); goog.require('ol.TileUrlFunction');
goog.require('ol.TileUrlFunctionType'); goog.require('ol.TileUrlFunctionType');
goog.require('ol.proj');
goog.require('ol.reproj.Tile'); goog.require('ol.reproj.Tile');
goog.require('ol.source.Tile'); goog.require('ol.source.Tile');
goog.require('ol.source.TileEvent'); goog.require('ol.source.TileEvent');
@@ -73,15 +74,15 @@ ol.source.TileImage = function(options) {
/** /**
* @protected * @protected
* @type {ol.TileCache} * @type {Object.<string, ol.TileCache>}
*/ */
this.reprojectedTileCache = new ol.TileCache(); this.tileCacheForProjection = {};
/** /**
* @protected * @protected
* @type {ol.proj.Projection} * @type {Object.<string, ol.tilegrid.TileGrid>}
*/ */
this.lastProj = null; this.tileGridForProjection = {};
}; };
goog.inherits(ol.source.TileImage, ol.source.Tile); goog.inherits(ol.source.TileImage, ol.source.Tile);
@@ -99,10 +100,34 @@ ol.source.TileImage.defaultTileLoadFunction = function(imageTile, src) {
* @inheritDoc * @inheritDoc
*/ */
ol.source.TileImage.prototype.getTileGridForProjection = function(projection) { ol.source.TileImage.prototype.getTileGridForProjection = function(projection) {
if (projection == this.getProjection() && !goog.isNull(this.tileGrid)) { if (!goog.isNull(this.tileGrid) &&
ol.proj.equivalent(this.getProjection(), projection)) {
return this.tileGrid; return this.tileGrid;
} else { } else {
return ol.tilegrid.getForProjection(projection); var projKey = goog.getUid(projection).toString();
if (!(projKey in this.tileGridForProjection)) {
this.tileGridForProjection[projKey] =
ol.tilegrid.getForProjection(projection);
}
return this.tileGridForProjection[projKey];
}
};
/**
* @param {ol.proj.Projection} projection Projection.
* @return {ol.TileCache} Tile cache.
* @protected
*/
ol.source.TileImage.prototype.getTileCacheForProjection = function(projection) {
if (ol.proj.equivalent(this.getProjection(), projection)) {
return this.tileCache;
} else {
var projKey = goog.getUid(projection).toString();
if (!(projKey in this.tileCacheForProjection)) {
this.tileCacheForProjection[projKey] = new ol.TileCache();
}
return this.tileCacheForProjection[projKey];
} }
}; };
@@ -112,22 +137,19 @@ ol.source.TileImage.prototype.getTileGridForProjection = function(projection) {
*/ */
ol.source.TileImage.prototype.getTile = ol.source.TileImage.prototype.getTile =
function(z, x, y, pixelRatio, projection) { function(z, x, y, pixelRatio, projection) {
if (goog.isNull(this.getProjection()) || this.getProjection() == projection) { if (!goog.isDefAndNotNull(this.getProjection()) ||
!goog.isDefAndNotNull(projection) ||
ol.proj.equivalent(this.getProjection(), projection)) {
return this.getTileInternal(z, x, y, pixelRatio, projection); return this.getTileInternal(z, x, y, pixelRatio, projection);
} else { } else {
if (this.lastProj != projection) { var cache = this.getTileCacheForProjection(projection);
this.reprojectedTileCache.clear();
this.lastProj = projection;
}
var cache = this.reprojectedTileCache; //TODO: per-projection cache
var tileCoordKey = this.getKeyZXY(z, x, y); var tileCoordKey = this.getKeyZXY(z, x, y);
if (cache.containsKey(tileCoordKey)) { if (cache.containsKey(tileCoordKey)) {
return /** @type {!ol.Tile} */(cache.get(tileCoordKey)); return /** @type {!ol.Tile} */(cache.get(tileCoordKey));
} else { } else {
var sourceProjection = this.getProjection(); var sourceProjection = this.getProjection();
var sourceTileGrid = this.getTileGridForProjection(sourceProjection); var sourceTileGrid = this.getTileGridForProjection(sourceProjection);
var targetTileGrid = ol.tilegrid.getForProjection(projection); var targetTileGrid = this.getTileGridForProjection(projection);
//TODO: init cache / tilegrid if needed
var tile = new ol.reproj.Tile( var tile = new ol.reproj.Tile(
sourceProjection, sourceTileGrid, sourceProjection, sourceTileGrid,
projection, targetTileGrid, projection, targetTileGrid,
@@ -229,6 +251,7 @@ ol.source.TileImage.prototype.handleTileChange_ = function(event) {
*/ */
ol.source.TileImage.prototype.setTileLoadFunction = function(tileLoadFunction) { ol.source.TileImage.prototype.setTileLoadFunction = function(tileLoadFunction) {
this.tileCache.clear(); this.tileCache.clear();
this.tileCacheForProjection = {};
this.tileLoadFunction = tileLoadFunction; this.tileLoadFunction = tileLoadFunction;
this.changed(); this.changed();
}; };
@@ -244,6 +267,7 @@ ol.source.TileImage.prototype.setTileUrlFunction = function(tileUrlFunction) {
// FIXME cache. The tile URL function would need to be incorporated into the // FIXME cache. The tile URL function would need to be incorporated into the
// FIXME cache key somehow. // FIXME cache key somehow.
this.tileCache.clear(); this.tileCache.clear();
this.tileCacheForProjection = {};
this.tileUrlFunction = tileUrlFunction; this.tileUrlFunction = tileUrlFunction;
this.changed(); this.changed();
}; };