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

@@ -3633,6 +3633,133 @@ olx.layer.VectorOptions.prototype.updateWhileInteracting;
olx.layer.VectorOptions.prototype.visible;
/**
* @typedef {{map: (ol.Map|undefined),
* minResolution: (number|undefined),
* maxResolution: (number|undefined),
* opacity: (number|undefined),
* renderBuffer: (number|undefined),
* renderOrder: (function(ol.Feature, ol.Feature):number|null|undefined),
* source: (ol.source.VectorTile|undefined),
* style: (ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction|undefined),
* updateWhileAnimating: (boolean|undefined),
* updateWhileInteracting: (boolean|undefined),
* visible: (boolean|undefined)}}
* @api
*/
olx.layer.VectorTileOptions;
/**
* The buffer around the viewport extent used by the renderer when getting
* features from the vector source for the rendering or hit-detection.
* Recommended value: the size of the largest symbol, line width or label.
* Default is 100 pixels.
* @type {number|undefined}
* @api
*/
olx.layer.VectorTileOptions.prototype.renderBuffer;
/**
* Render order. Function to be used when sorting features before rendering. By
* default features are drawn in the order that they are created. Use `null` to
* avoid the sort, but get an undefined draw order.
* @type {function(ol.Feature, ol.Feature):number|null|undefined}
* @api
*/
olx.layer.VectorTileOptions.prototype.renderOrder;
/**
* Sets the layer as overlay on a map. The map will not manage this layer in its
* layers collection, and the layer will be rendered on top. This is useful for
* temporary layers. The standard way to add a layer to a map and have it
* managed by the map is to use {@link ol.Map#addLayer}.
* @type {ol.Map|undefined}
* @api
*/
olx.layer.VectorTileOptions.prototype.map;
/**
* The bounding extent for layer rendering. The layer will not be rendered
* outside of this extent.
* @type {ol.Extent|undefined}
* @api
*/
olx.layer.VectorTileOptions.prototype.extent;
/**
* The minimum resolution (inclusive) at which this layer will be visible.
* @type {number|undefined}
* @api
*/
olx.layer.VectorTileOptions.prototype.minResolution;
/**
* The maximum resolution (exclusive) below which this layer will be visible.
* @type {number|undefined}
* @api
*/
olx.layer.VectorTileOptions.prototype.maxResolution;
/**
* Opacity. 0-1. Default is `1`.
* @type {number|undefined}
* @api
*/
olx.layer.VectorTileOptions.prototype.opacity;
/**
* Source.
* @type {ol.source.VectorTile}
* @api
*/
olx.layer.VectorTileOptions.prototype.source;
/**
* Layer style. See {@link ol.style} for default style which will be used if
* this is not defined.
* @type {ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction|undefined}
* @api
*/
olx.layer.VectorTileOptions.prototype.style;
/**
* When set to `true`, feature batches will be recreated during animations.
* This means that no vectors will be shown clipped, but the setting will have a
* performance impact for large amounts of vector data. When set to `false`,
* batches will be recreated when no animation is active. Default is `false`.
* @type {boolean|undefined}
* @api
*/
olx.layer.VectorTileOptions.prototype.updateWhileAnimating;
/**
* When set to `true`, feature batches will be recreated during interactions.
* See also `updateWhileAnimating`. Default is `false`.
* @type {boolean|undefined}
* @api
*/
olx.layer.VectorTileOptions.prototype.updateWhileInteracting;
/**
* Visibility. Default is `true` (visible).
* @type {boolean|undefined}
* @api stable
*/
olx.layer.VectorTileOptions.prototype.visible;
/**
* Namespace.
* @type {Object}
@@ -3955,6 +4082,142 @@ olx.source.TileImageOptions.prototype.tileUrlFunction;
olx.source.TileImageOptions.prototype.wrapX;
/**
* @typedef {{attributions: (Array.<ol.Attribution>|undefined),
* logo: (string|olx.LogoOptions|undefined),
* opaque: (boolean|undefined),
* projection: ol.proj.ProjectionLike,
* state: (ol.source.State|string|undefined),
* format: (ol.format.Feature|undefined),
* rightHandedPolygons: (boolean|undefined),
* tileClass: (function(new: ol.VectorTile, ol.TileCoord,
* ol.TileState, string, ol.format.Feature,
* ol.TileLoadFunctionType)|undefined),
* tileGrid: (ol.tilegrid.TileGrid|undefined),
* tileLoadFunction: (ol.TileLoadFunctionType|undefined),
* tilePixelRatio: (number|undefined),
* tileUrlFunction: (ol.TileUrlFunctionType|undefined),
* wrapX: (boolean|undefined)}}
* @api
*/
olx.source.VectorTileOptions;
/**
/**
* Attributions.
* @type {Array.<ol.Attribution>|undefined}
* @api
*/
olx.source.VectorTileOptions.prototype.attributions;
/**
* Logo.
* @type {string|olx.LogoOptions|undefined}
* @api
*/
olx.source.VectorTileOptions.prototype.logo;
/**
* Whether the layer is opaque.
* @type {boolean|undefined}
* @api
*/
olx.source.VectorTileOptions.prototype.opaque;
/**
* Projection.
* @type {ol.proj.ProjectionLike}
* @api
*/
olx.source.VectorTileOptions.prototype.projection;
/**
* Assume that all polygons provided by this source follow the right-hand rule
* (counter-clockwise for exterior and clockwise for interior rings). If `true`,
* renderers will skip the check for the ring orientation.
* @type {boolean|undefined}
* @api
*/
olx.source.VectorTileOptions.prototype.rightHandedPolygons;
/**
* Source state.
* @type {ol.source.State|string|undefined}
* @api
*/
olx.source.VectorTileOptions.prototype.state;
/**
* Feature format for tiles. Used and required by the default
* `tileLoadFunction`.
* @type {ol.format.Feature|undefined}
*/
olx.source.VectorTileOptions.prototype.format;
/**
* Class used to instantiate image tiles. Default is {@link ol.VectorTile}.
* @type {function(new: ol.VectorTile, ol.TileCoord,
* ol.TileState, string, ol.format.Feature,
* ol.TileLoadFunctionType)|undefined}
* @api
*/
olx.source.VectorTileOptions.prototype.tileClass;
/**
* Tile grid.
* @type {ol.tilegrid.TileGrid|undefined}
* @api
*/
olx.source.VectorTileOptions.prototype.tileGrid;
/**
* Optional function to load a tile given a URL.
* @type {ol.TileLoadFunctionType|undefined}
* @api
*/
olx.source.VectorTileOptions.prototype.tileLoadFunction;
/**
* The pixel ratio used by the tile service. For example, if the tile
* service advertizes 256px by 256px tiles but actually sends 512px
* by 512px tiles (for retina/hidpi devices) then `tilePixelRatio`
* should be set to `2`. Default is `1`.
* @type {number|undefined}
* @api
*/
olx.source.VectorTileOptions.prototype.tilePixelRatio;
/**
* Optional function to get tile URL given a tile coordinate and the projection.
* @type {ol.TileUrlFunctionType|undefined}
* @api
*/
olx.source.VectorTileOptions.prototype.tileUrlFunction;
/**
* Whether to wrap the world horizontally. The default, `undefined`, is to
* request out-of-bounds tiles from the server. When set to `false`, only one
* world will be rendered. When set to `true`, tiles will be requested for one
* world only, but they will be wrapped horizontally to render multiple worlds.
* @type {boolean|undefined}
* @api
*/
olx.source.VectorTileOptions.prototype.wrapX;
/**
* @typedef {{attributions: (Array.<ol.Attribution>|undefined),
* format: (ol.format.Feature|undefined),

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