The preemptive loading of UTFGrid tiles is now optional

This commit is contained in:
Petr Sloup
2014-09-26 17:16:07 +02:00
parent 99eab24b20
commit ef816d30ed
2 changed files with 87 additions and 28 deletions
+2 -1
View File
@@ -3542,7 +3542,8 @@ olx.source.GPXOptions.prototype.urls;
/** /**
* @typedef {{url: string}} * @typedef {{url: string,
* preemptive: (boolean|undefined)}}
* @api * @api
*/ */
olx.source.TileUTFGridOptions; olx.source.TileUTFGridOptions;
+85 -27
View File
@@ -1,15 +1,17 @@
goog.provide('ol.source.TileUTFGrid'); goog.provide('ol.source.TileUTFGrid');
goog.require('goog.asserts') goog.require('goog.asserts');
goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('goog.net.Jsonp'); goog.require('goog.net.Jsonp');
goog.require('ol.Attribution'); goog.require('ol.Attribution');
goog.require('ol.Tile'); goog.require('ol.Tile');
goog.require('ol.TileCache') goog.require('ol.TileCache');
goog.require('ol.TileState'); goog.require('ol.TileState');
goog.require('ol.TileUrlFunction'); goog.require('ol.TileUrlFunction');
goog.require('ol.extent'); goog.require('ol.extent');
goog.require('ol.proj'); goog.require('ol.proj');
goog.require('ol.source.State') goog.require('ol.source.State');
goog.require('ol.source.Tile'); goog.require('ol.source.Tile');
goog.require('ol.tilegrid.XYZ'); goog.require('ol.tilegrid.XYZ');
@@ -17,9 +19,7 @@ goog.require('ol.tilegrid.XYZ');
/** /**
* @classdesc * @classdesc
* TODO: desc * Layer source for UTFGrid interaction data loaded from TileJSON format.
* TODO: getTilePixelSize ?
* TODO: lazy loading
* *
* @constructor * @constructor
* @extends {ol.source.Tile} * @extends {ol.source.Tile}
@@ -32,6 +32,19 @@ ol.source.TileUTFGrid = function(options) {
state: ol.source.State.LOADING state: ol.source.State.LOADING
}); });
/**
* @private
* @type {boolean}
*/
this.preemptive_ = goog.isDef(options.preemptive) ?
options.preemptive : false;
/**
* @protected
* @type {ol.TileUrlFunctionType}
*/
this.tileUrlFunction = ol.TileUrlFunction.nullTileUrlFunction;
/** /**
* @protected * @protected
* @type {ol.TileCache} * @type {ol.TileCache}
@@ -63,22 +76,20 @@ ol.source.TileUTFGrid.prototype.expireCache = function(usedTiles) {
/** /**
* @param {ol.Coordinate} coordinate Coordinate. * @param {ol.Coordinate} coordinate Coordinate.
* @param {number} resolution Resolution. * @param {number} resolution Resolution.
* @param {function(Object)} callback Info callback. * @param {function(this: T, (Object|undefined))} f Callback.
* @param {T=} opt_this The object to use as `this` in `f`.
* @param {boolean=} opt_noRequest Only process already loaded data.
* @template T
* @api * @api
*/ */
ol.source.TileUTFGrid.prototype.getFeatureInfo = function( ol.source.TileUTFGrid.prototype.forDataAtCoordinateAndResolution = function(
coordinate, resolution, callback) { coordinate, resolution, f, opt_this, opt_noRequest) {
if (!goog.isNull(this.tileGrid)) { if (!goog.isNull(this.tileGrid)) {
var tileCoord = this.tileGrid.getTileCoordForCoordAndResolution( var tileCoord = this.tileGrid.getTileCoordForCoordAndResolution(
coordinate, resolution); coordinate, resolution);
var tileCoordKey = this.getKeyZXY.apply(this, tileCoord); var tile = /** @type {!ol.source.TileUTFGridTile_} */(this.getTile(
if (this.tileCache.containsKey(tileCoordKey)) { tileCoord[0], tileCoord[1], tileCoord[2], 1, this.getProjection()));
var tile = /** @type {!ol.source.TileUTFGridTile_} */ tile.forDataAtCoordinate(coordinate, f, opt_this, opt_noRequest);
(this.tileCache.get(tileCoordKey));
callback(tile.getData(coordinate));
} else {
//TODO: async?
}
} }
}; };
@@ -112,11 +123,17 @@ ol.source.TileUTFGrid.prototype.handleTileJSONResponse = function(tileJSON) {
}); });
this.tileGrid = tileGrid; this.tileGrid = tileGrid;
var grids = tileJSON.grids;
if (!goog.isDefAndNotNull(grids)) {
this.setState(ol.source.State.ERROR);
return;
}
this.tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform( this.tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform(
tileGrid.createTileCoordTransform({ tileGrid.createTileCoordTransform({
extent: extent extent: extent
}), }),
ol.TileUrlFunction.createFromTemplates(tileJSON.grids)); ol.TileUrlFunction.createFromTemplates(grids));
if (goog.isDef(tileJSON.attribution)) { if (goog.isDef(tileJSON.attribution)) {
var attributionExtent = goog.isDef(extent) ? var attributionExtent = goog.isDef(extent) ?
@@ -151,13 +168,15 @@ ol.source.TileUTFGrid.prototype.getTile =
if (this.tileCache.containsKey(tileCoordKey)) { if (this.tileCache.containsKey(tileCoordKey)) {
return /** @type {!ol.Tile} */ (this.tileCache.get(tileCoordKey)); return /** @type {!ol.Tile} */ (this.tileCache.get(tileCoordKey));
} else { } else {
goog.asserts.assert(projection);
var tileCoord = [z, x, y]; var tileCoord = [z, x, y];
var tileUrl = this.tileUrlFunction(tileCoord, pixelRatio, projection); var tileUrl = this.tileUrlFunction(tileCoord, pixelRatio, projection);
var tile = new ol.source.TileUTFGridTile_( var tile = new ol.source.TileUTFGridTile_(
tileCoord, tileCoord,
goog.isDef(tileUrl) ? ol.TileState.IDLE : ol.TileState.EMPTY, goog.isDef(tileUrl) ? ol.TileState.IDLE : ol.TileState.EMPTY,
goog.isDef(tileUrl) ? tileUrl : '', goog.isDef(tileUrl) ? tileUrl : '',
this.tileGrid.getTileCoordExtent(tileCoord)); this.tileGrid.getTileCoordExtent(tileCoord),
this.preemptive_);
this.tileCache.set(tileCoordKey, tile); this.tileCache.set(tileCoordKey, tile);
return tile; return tile;
} }
@@ -183,9 +202,11 @@ ol.source.TileUTFGrid.prototype.useTile = function(z, x, y) {
* @param {ol.TileState} state State. * @param {ol.TileState} state State.
* @param {string} src Image source URI. * @param {string} src Image source URI.
* @param {ol.Extent} extent Extent of the tile. * @param {ol.Extent} extent Extent of the tile.
* @param {boolean} preemptive Load the tile when visible (before it's needed).
* @private * @private
*/ */
ol.source.TileUTFGridTile_ = function(tileCoord, state, src, extent) { ol.source.TileUTFGridTile_ =
function(tileCoord, state, src, extent, preemptive) {
goog.base(this, tileCoord, state); goog.base(this, tileCoord, state);
@@ -201,6 +222,12 @@ ol.source.TileUTFGridTile_ = function(tileCoord, state, src, extent) {
*/ */
this.extent_ = extent; this.extent_ = extent;
/**
* @private
* @type {boolean}
*/
this.preemptive_ = preemptive;
/** /**
* @private * @private
* @type {?Array.<string>} * @type {?Array.<string>}
@@ -225,10 +252,13 @@ goog.inherits(ol.source.TileUTFGridTile_, ol.Tile);
/** /**
* @inheritDoc * @inheritDoc
*/ */
ol.source.TileUTFGridTile_.prototype.getImage = goog.nullFunction; ol.source.TileUTFGridTile_.prototype.getImage = function(opt_context) {
return null;
};
/** /**
* Synchronously returns data at given coordinate (if available).
* @param {ol.Coordinate} coordinate Coordinate. * @param {ol.Coordinate} coordinate Coordinate.
* @return {Object|undefined} * @return {Object|undefined}
*/ */
@@ -255,11 +285,29 @@ ol.source.TileUTFGridTile_.prototype.getData = function(coordinate) {
var key = this.keys_[code]; var key = this.keys_[code];
if (!goog.isDefAndNotNull(key)) { return goog.isDefAndNotNull(key) ? this.data_[key] : undefined;
return undefined; };
}
return this.data_[key];
/**
* Calls `f` when the data for given coordinate is available.
* @param {ol.Coordinate} coordinate Coordinate.
* @param {function(this: T, (Object|undefined))} f Callback.
* @param {T=} opt_this The object to use as `this` in `f`.
* @param {boolean=} opt_noRequest If not loaded, callback with `undefined`
* without loading the tile.
* @template T
*/
ol.source.TileUTFGridTile_.prototype.forDataAtCoordinate =
function(coordinate, f, opt_this, opt_noRequest) {
if (this.state == ol.TileState.IDLE && opt_noRequest !== true) {
this.listenOnce(goog.events.EventType.CHANGE, function(e) {
f.call(opt_this, this.getData(coordinate));
}, false, this);
this.loadInternal_();
} else {
f.call(opt_this, this.getData(coordinate));
}
}; };
@@ -295,9 +343,9 @@ ol.source.TileUTFGridTile_.prototype.handleLoad_ = function(json) {
/** /**
* Load not yet loaded URI. * @private
*/ */
ol.source.TileUTFGridTile_.prototype.load = function() { ol.source.TileUTFGridTile_.prototype.loadInternal_ = function() {
if (this.state == ol.TileState.IDLE) { if (this.state == ol.TileState.IDLE) {
this.state = ol.TileState.LOADING; this.state = ol.TileState.LOADING;
var request = new goog.net.Jsonp(this.src_); var request = new goog.net.Jsonp(this.src_);
@@ -305,3 +353,13 @@ ol.source.TileUTFGridTile_.prototype.load = function() {
goog.bind(this.handleError_, this)); goog.bind(this.handleError_, this));
} }
}; };
/**
* Load not yet loaded URI.
*/
ol.source.TileUTFGridTile_.prototype.load = function() {
if (this.preemptive_) {
this.loadInternal_();
}
};