Add VectorTile layer, source and tile

This commit is contained in:
Andreas Hocevar
2015-10-13 14:29:14 +02:00
parent fa88b578a4
commit 7d3fc3ccc7
6 changed files with 677 additions and 8 deletions

View File

@@ -46,9 +46,9 @@ ol.FeatureUrlFunction;
/**
* @param {string|ol.FeatureUrlFunction} url Feature URL service.
* @param {ol.format.Feature} format Feature format.
* @param {function(this:ol.source.Vector, Array.<ol.Feature>)} success
* Function called with the loaded features. Called with the vector
* source as `this`.
* @param {function(this:ol.source.Vector, Array.<ol.Feature>, ol.proj.Projection)|function(this:ol.source.Vector, Array.<ol.Feature>)} success
* Function called with the loaded features and optionally with the data
* projection. Called with the vector source as `this`.
* @return {ol.FeatureLoader} The feature loader.
*/
ol.featureloader.loadFeaturesXhr = function(url, format, success) {
@@ -57,7 +57,7 @@ ol.featureloader.loadFeaturesXhr = function(url, format, success) {
* @param {ol.Extent} extent Extent.
* @param {number} resolution Resolution.
* @param {ol.proj.Projection} projection Projection.
* @this {ol.source.Vector}
* @this {ol.source.Vector|ol.VectorTile}
*/
function(extent, resolution, projection) {
var xhrIo = new goog.net.XhrIo();
@@ -98,7 +98,11 @@ ol.featureloader.loadFeaturesXhr = function(url, format, success) {
if (source) {
var features = format.readFeatures(source,
{featureProjection: projection});
success.call(this, features);
if (success.length == 2) {
success.call(this, features, format.readProjection(source));
} else {
success.call(this, features);
}
} else {
goog.asserts.fail('undefined or null source');
}
@@ -117,6 +121,29 @@ ol.featureloader.loadFeaturesXhr = function(url, format, success) {
};
/**
* Create an XHR feature loader for a `url` and `format`. The feature loader
* loads features (with XHR), parses the features, and adds them to the
* vector tile.
* @param {string|ol.FeatureUrlFunction} url Feature URL service.
* @param {ol.format.Feature} format Feature format.
* @return {ol.FeatureLoader} The feature loader.
* @api
*/
ol.featureloader.tile = function(url, format) {
return ol.featureloader.loadFeaturesXhr(url, format,
/**
* @param {Array.<ol.Feature>} features The loaded features.
* @param {ol.proj.Projection} projection Data projection.
* @this {ol.VectorTile}
*/
function(features, projection) {
this.setProjection(projection);
this.setFeatures(features);
});
};
/**
* Create an XHR feature loader for a `url` and `format`. The feature loader
* loads features (with XHR), parses the features, and adds them to the

View File

@@ -0,0 +1,99 @@
goog.provide('ol.layer.VectorTile');
goog.require('goog.object');
goog.require('ol.layer.Vector');
/**
* @enum {string}
*/
ol.layer.VectorTileProperty = {
PRELOAD: 'preload',
USE_INTERIM_TILES_ON_ERROR: 'useInterimTilesOnError'
};
/**
* @classdesc
* Vector tile data that is rendered client-side.
* Note that any property set in the options is set as a {@link ol.Object}
* property on the layer object; for example, setting `title: 'My Title'` in the
* options means that `title` is observable, and has get/set accessors.
*
* @constructor
* @extends {ol.layer.Vector}
* @param {olx.layer.VectorTileOptions=} opt_options Options.
* @api
*/
ol.layer.VectorTile = function(opt_options) {
var options = goog.isDef(opt_options) ? opt_options : {};
var baseOptions = goog.object.clone(options);
delete baseOptions.preload;
delete baseOptions.useInterimTilesOnError;
goog.base(this, /** @type {olx.layer.VectorOptions} */ (baseOptions));
this.setPreload(goog.isDef(options.preload) ? options.preload : 0);
this.setUseInterimTilesOnError(goog.isDef(options.useInterimTilesOnError) ?
options.useInterimTilesOnError : true);
};
goog.inherits(ol.layer.VectorTile, ol.layer.Vector);
/**
* Return the level as number to which we will preload tiles up to.
* @return {number} The level to preload tiles up to.
* @observable
* @api
*/
ol.layer.VectorTile.prototype.getPreload = function() {
return /** @type {number} */ (this.get(ol.layer.VectorTileProperty.PRELOAD));
};
/**
* Return the associated {@link ol.source.VectorTile source} of the layer.
* @function
* @return {ol.source.VectorTile} Source.
* @api
*/
ol.layer.VectorTile.prototype.getSource;
/**
* Whether we use interim tiles on error.
* @return {boolean} Use interim tiles on error.
* @observable
* @api
*/
ol.layer.VectorTile.prototype.getUseInterimTilesOnError = function() {
return /** @type {boolean} */ (
this.get(ol.layer.VectorTileProperty.USE_INTERIM_TILES_ON_ERROR));
};
/**
* Set the level as number to which we will preload tiles up to.
* @param {number} preload The level to preload tiles up to.
* @observable
* @api
*/
ol.layer.VectorTile.prototype.setPreload = function(preload) {
this.set(ol.layer.TileProperty.PRELOAD, preload);
};
/**
* Set whether we use interim tiles on error.
* @param {boolean} useInterimTilesOnError Use interim tiles on error.
* @observable
* @api
*/
ol.layer.VectorTile.prototype.setUseInterimTilesOnError =
function(useInterimTilesOnError) {
this.set(
ol.layer.TileProperty.USE_INTERIM_TILES_ON_ERROR, useInterimTilesOnError);
};

View File

@@ -0,0 +1,107 @@
goog.provide('ol.source.VectorTile');
goog.require('goog.asserts');
goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('ol.TileState');
goog.require('ol.VectorTile');
goog.require('ol.featureloader');
goog.require('ol.source.UrlTile');
/**
* @classdesc
* Base class for sources providing images divided into a tile grid.
*
* @constructor
* @fires ol.source.TileEvent
* @extends {ol.source.UrlTile}
* @param {olx.source.VectorTileOptions} options Vector tile options.
* @api
*/
ol.source.VectorTile = function(options) {
goog.base(this, {
attributions: options.attributions,
extent: options.extent,
logo: options.logo,
opaque: options.opaque,
projection: options.projection,
state: goog.isDef(options.state) ?
/** @type {ol.source.State} */ (options.state) : undefined,
tileGrid: options.tileGrid,
tileLoadFunction: goog.isDef(options.tileLoadFunction) ?
options.tileLoadFunction : ol.source.VectorTile.defaultTileLoadFunction,
tileUrlFunction: options.tileUrlFunction,
tilePixelRatio: options.tilePixelRatio,
wrapX: options.wrapX
});
this.assumeRightHandedPolygons_ =
goog.isDef(options.assumeRightHandedPolygons) ?
options.assumeRightHandedPolygons : false;
/**
* @private
* @type {ol.format.Feature}
*/
this.format_ = goog.isDef(options.format) ? options.format : null;
/**
* @protected
* @type {function(new: ol.VectorTile, ol.TileCoord, ol.TileState, string,
* ol.format.Feature, ol.TileLoadFunctionType)}
*/
this.tileClass = goog.isDef(options.tileClass) ?
options.tileClass : ol.VectorTile;
};
goog.inherits(ol.source.VectorTile, ol.source.UrlTile);
/**
* @return {boolean} Assume right handed polygons.
*/
ol.source.VectorTile.prototype.getRightHandedPolygons = function() {
return this.assumeRightHandedPolygons_;
};
/**
* @inheritDoc
*/
ol.source.VectorTile.prototype.getTile =
function(z, x, y, pixelRatio, projection) {
var tileCoordKey = this.getKeyZXY(z, x, y);
if (this.tileCache.containsKey(tileCoordKey)) {
return /** @type {!ol.Tile} */ (this.tileCache.get(tileCoordKey));
} else {
goog.asserts.assert(projection, 'argument projection is truthy');
var tileCoord = [z, x, y];
var urlTileCoord = this.getTileCoordForTileUrlFunction(
tileCoord, projection);
var tileUrl = goog.isNull(urlTileCoord) ? undefined :
this.tileUrlFunction(urlTileCoord, pixelRatio, projection);
var tile = new this.tileClass(
tileCoord,
goog.isDef(tileUrl) ? ol.TileState.IDLE : ol.TileState.EMPTY,
goog.isDef(tileUrl) ? tileUrl : '',
this.format_,
this.tileLoadFunction);
goog.events.listen(tile, goog.events.EventType.CHANGE,
this.handleTileChange, false, this);
this.tileCache.set(tileCoordKey, tile);
return tile;
}
};
/**
* @param {ol.VectorTile} vectorTile Vector tile.
* @param {string} url URL.
*/
ol.source.VectorTile.defaultTileLoadFunction = function(vectorTile, url) {
vectorTile.setLoader(ol.featureloader.tile(url, vectorTile.getFormat()));
};

View File

@@ -3,10 +3,10 @@ goog.provide('ol.TileVectorLoadFunctionType');
/**
* A function that takes an {@link ol.ImageTile} for the image tile and a
* `{string}` for the src as arguments.
* A function that takes an {@link ol.Tile} for the tile and a
* `{string}` for the url as arguments.
*
* @typedef {function(ol.ImageTile, string)}
* @typedef {function(ol.Tile, string)}
* @api
*/
ol.TileLoadFunctionType;

173
src/ol/vectortile.js Normal file
View File

@@ -0,0 +1,173 @@
goog.provide('ol.VectorTile');
goog.require('ol.Tile');
goog.require('ol.TileCoord');
goog.require('ol.TileLoadFunctionType');
goog.require('ol.TileState');
/**
* @typedef {{dirty: boolean,
* renderedRenderOrder: (null|function(ol.Feature, ol.Feature):number),
* renderedRevision: number,
* renderedResolution: number,
* replayGroup: ol.render.IReplayGroup}}
*/
ol.TileReplayState;
/**
* @constructor
* @extends {ol.Tile}
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @param {ol.TileState} state State.
* @param {string} src Data source url.
* @param {ol.format.Feature} format Feature format.
* @param {ol.TileLoadFunctionType} tileLoadFunction Tile load function.
*/
ol.VectorTile = function(tileCoord, state, src, format, tileLoadFunction) {
goog.base(this, tileCoord, state);
/**
* @private
* @type {ol.format.Feature}
*/
this.format_ = format;
/**
* @private
* @type {Array.<ol.Feature>}
*/
this.features_ = null;
/**
* @private
* @type {ol.FeatureLoader}
*/
this.loader_;
/**
* @private
* @type {ol.proj.Projection}
*/
this.projection_ = null;
/**
* @private
* @type {ol.TileReplayState}
*/
this.replayState_ = {
dirty: true,
renderedRenderOrder: null,
renderedRevision: -1,
renderedResolution: NaN,
replayGroup: null
};
/**
* @private
* @type {ol.TileLoadFunctionType}
*/
this.tileLoadFunction_ = tileLoadFunction;
/**
* @private
* @type {string}
*/
this.url_ = src;
};
goog.inherits(ol.VectorTile, ol.Tile);
/**
* @inheritDoc
*/
ol.VectorTile.prototype.disposeInternal = function() {
goog.base(this, 'disposeInternal');
};
/**
* Get the feature format assigned for reading this tile's features.
* @return {ol.format.Feature} Feature format.
* @api
*/
ol.VectorTile.prototype.getFormat = function() {
return this.format_;
};
/**
* @return {Array.<ol.Feature>} Features.
*/
ol.VectorTile.prototype.getFeatures = function() {
return this.features_;
};
/**
* @return {ol.TileReplayState}
*/
ol.VectorTile.prototype.getReplayState = function() {
return this.replayState_;
};
/**
* @inheritDoc
*/
ol.VectorTile.prototype.getKey = function() {
return this.url_;
};
/**
* @return {ol.proj.Projection} Projection.
*/
ol.VectorTile.prototype.getProjection = function() {
return this.projection_;
};
/**
* Load the tile.
*/
ol.VectorTile.prototype.load = function() {
if (this.state == ol.TileState.IDLE) {
this.state = ol.TileState.LOADING;
this.changed();
this.tileLoadFunction_(this, this.url_);
this.loader_(null, NaN, null);
}
};
/**
* @param {Array.<ol.Feature>} features Features.
*/
ol.VectorTile.prototype.setFeatures = function(features) {
this.features_ = features;
this.state = ol.TileState.LOADED;
this.changed();
};
/**
* @param {ol.proj.Projection} projection Projection.
*/
ol.VectorTile.prototype.setProjection = function(projection) {
this.projection_ = projection;
};
/**
* Set the feeature loader for reading this tile's features.
* @param {ol.FeatureLoader} loader Feature loader.
* @api
*/
ol.VectorTile.prototype.setLoader = function(loader) {
this.loader_ = loader;
};