253 lines
5.6 KiB
JavaScript
253 lines
5.6 KiB
JavaScript
/**
|
|
* @module ol/VectorTile
|
|
*/
|
|
import {getUid, inherits} from './index.js';
|
|
import _ol_Tile_ from './Tile.js';
|
|
import _ol_TileState_ from './TileState.js';
|
|
|
|
/**
|
|
* @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.
|
|
* @param {olx.TileOptions=} opt_options Tile options.
|
|
*/
|
|
var _ol_VectorTile_ = function(tileCoord, state, src, format, tileLoadFunction, opt_options) {
|
|
|
|
_ol_Tile_.call(this, tileCoord, state, opt_options);
|
|
|
|
/**
|
|
* @type {number}
|
|
*/
|
|
this.consumers = 0;
|
|
|
|
/**
|
|
* @private
|
|
* @type {ol.Extent}
|
|
*/
|
|
this.extent_ = null;
|
|
|
|
/**
|
|
* @private
|
|
* @type {ol.format.Feature}
|
|
*/
|
|
this.format_ = format;
|
|
|
|
/**
|
|
* @private
|
|
* @type {Array.<ol.Feature>}
|
|
*/
|
|
this.features_ = null;
|
|
|
|
/**
|
|
* @private
|
|
* @type {ol.FeatureLoader}
|
|
*/
|
|
this.loader_;
|
|
|
|
/**
|
|
* Data projection
|
|
* @private
|
|
* @type {ol.proj.Projection}
|
|
*/
|
|
this.projection_;
|
|
|
|
/**
|
|
* @private
|
|
* @type {Object.<string, ol.render.ReplayGroup>}
|
|
*/
|
|
this.replayGroups_ = {};
|
|
|
|
/**
|
|
* @private
|
|
* @type {ol.TileLoadFunctionType}
|
|
*/
|
|
this.tileLoadFunction_ = tileLoadFunction;
|
|
|
|
/**
|
|
* @private
|
|
* @type {string}
|
|
*/
|
|
this.url_ = src;
|
|
|
|
};
|
|
|
|
inherits(_ol_VectorTile_, _ol_Tile_);
|
|
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
_ol_VectorTile_.prototype.disposeInternal = function() {
|
|
this.features_ = null;
|
|
this.replayGroups_ = {};
|
|
this.state = _ol_TileState_.ABORT;
|
|
this.changed();
|
|
_ol_Tile_.prototype.disposeInternal.call(this);
|
|
};
|
|
|
|
|
|
/**
|
|
* Gets the extent of the vector tile.
|
|
* @return {ol.Extent} The extent.
|
|
* @api
|
|
*/
|
|
_ol_VectorTile_.prototype.getExtent = function() {
|
|
return this.extent_ || _ol_VectorTile_.DEFAULT_EXTENT;
|
|
};
|
|
|
|
|
|
/**
|
|
* 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_;
|
|
};
|
|
|
|
|
|
/**
|
|
* Get the features for this tile. Geometries will be in the projection returned
|
|
* by {@link ol.VectorTile#getProjection}.
|
|
* @return {Array.<ol.Feature|ol.render.Feature>} Features.
|
|
* @api
|
|
*/
|
|
_ol_VectorTile_.prototype.getFeatures = function() {
|
|
return this.features_;
|
|
};
|
|
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
_ol_VectorTile_.prototype.getKey = function() {
|
|
return this.url_;
|
|
};
|
|
|
|
|
|
/**
|
|
* Get the feature projection of features returned by
|
|
* {@link ol.VectorTile#getFeatures}.
|
|
* @return {ol.proj.Projection} Feature projection.
|
|
* @api
|
|
*/
|
|
_ol_VectorTile_.prototype.getProjection = function() {
|
|
return this.projection_;
|
|
};
|
|
|
|
|
|
/**
|
|
* @param {ol.layer.Layer} layer Layer.
|
|
* @param {string} key Key.
|
|
* @return {ol.render.ReplayGroup} Replay group.
|
|
*/
|
|
_ol_VectorTile_.prototype.getReplayGroup = function(layer, key) {
|
|
return this.replayGroups_[getUid(layer) + ',' + key];
|
|
};
|
|
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
_ol_VectorTile_.prototype.load = function() {
|
|
if (this.state == _ol_TileState_.IDLE) {
|
|
this.setState(_ol_TileState_.LOADING);
|
|
this.tileLoadFunction_(this, this.url_);
|
|
this.loader_(null, NaN, null);
|
|
}
|
|
};
|
|
|
|
|
|
/**
|
|
* Handler for successful tile load.
|
|
* @param {Array.<ol.Feature>} features The loaded features.
|
|
* @param {ol.proj.Projection} dataProjection Data projection.
|
|
* @param {ol.Extent} extent Extent.
|
|
*/
|
|
_ol_VectorTile_.prototype.onLoad = function(features, dataProjection, extent) {
|
|
this.setProjection(dataProjection);
|
|
this.setFeatures(features);
|
|
this.setExtent(extent);
|
|
};
|
|
|
|
|
|
/**
|
|
* Handler for tile load errors.
|
|
*/
|
|
_ol_VectorTile_.prototype.onError = function() {
|
|
this.setState(_ol_TileState_.ERROR);
|
|
};
|
|
|
|
|
|
/**
|
|
* Function for use in an {@link ol.source.VectorTile}'s `tileLoadFunction`.
|
|
* Sets the extent of the vector tile. This is only required for tiles in
|
|
* projections with `tile-pixels` as units. The extent should be set to
|
|
* `[0, 0, tilePixelSize, tilePixelSize]`, where `tilePixelSize` is calculated
|
|
* by multiplying the tile size with the tile pixel ratio. For sources using
|
|
* {@link ol.format.MVT} as feature format, the
|
|
* {@link ol.format.MVT#getLastExtent} method will return the correct extent.
|
|
* The default is `[0, 0, 4096, 4096]`.
|
|
* @param {ol.Extent} extent The extent.
|
|
* @api
|
|
*/
|
|
_ol_VectorTile_.prototype.setExtent = function(extent) {
|
|
this.extent_ = extent;
|
|
};
|
|
|
|
|
|
/**
|
|
* Function for use in an {@link ol.source.VectorTile}'s `tileLoadFunction`.
|
|
* Sets the features for the tile.
|
|
* @param {Array.<ol.Feature>} features Features.
|
|
* @api
|
|
*/
|
|
_ol_VectorTile_.prototype.setFeatures = function(features) {
|
|
this.features_ = features;
|
|
this.setState(_ol_TileState_.LOADED);
|
|
};
|
|
|
|
|
|
/**
|
|
* Function for use in an {@link ol.source.VectorTile}'s `tileLoadFunction`.
|
|
* Sets the projection of the features that were added with
|
|
* {@link ol.VectorTile#setFeatures}.
|
|
* @param {ol.proj.Projection} projection Feature projection.
|
|
* @api
|
|
*/
|
|
_ol_VectorTile_.prototype.setProjection = function(projection) {
|
|
this.projection_ = projection;
|
|
};
|
|
|
|
|
|
/**
|
|
* @param {ol.layer.Layer} layer Layer.
|
|
* @param {string} key Key.
|
|
* @param {ol.render.ReplayGroup} replayGroup Replay group.
|
|
*/
|
|
_ol_VectorTile_.prototype.setReplayGroup = function(layer, key, replayGroup) {
|
|
this.replayGroups_[getUid(layer) + ',' + key] = replayGroup;
|
|
};
|
|
|
|
|
|
/**
|
|
* Set the feature loader for reading this tile's features.
|
|
* @param {ol.FeatureLoader} loader Feature loader.
|
|
* @api
|
|
*/
|
|
_ol_VectorTile_.prototype.setLoader = function(loader) {
|
|
this.loader_ = loader;
|
|
};
|
|
|
|
|
|
/**
|
|
* @const
|
|
* @type {ol.Extent}
|
|
*/
|
|
_ol_VectorTile_.DEFAULT_EXTENT = [0, 0, 4096, 4096];
|
|
export default _ol_VectorTile_;
|