Allow VectorSource to load and parse data
This adds url, data and parser options to the source, and makes EPSG:4326 the default projection. It also adds a prepareFeatures method, which is used to load/parse data once the target projection is known.
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
goog.provide('ol.source.Vector');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('goog.net.XhrIo');
|
||||
goog.require('ol.proj');
|
||||
goog.require('ol.source.Source');
|
||||
|
||||
|
||||
@@ -7,9 +10,88 @@ goog.require('ol.source.Source');
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.source.Source}
|
||||
* @param {ol.source.SourceOptions} options Source options.
|
||||
* @param {ol.source.VectorOptions} options Vector source options.
|
||||
*/
|
||||
ol.source.Vector = function(options) {
|
||||
goog.base(this, options);
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Object|string}
|
||||
*/
|
||||
this.data_ = goog.isDef(options.data) ? options.data : null;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.source.VectorLoadState}
|
||||
*/
|
||||
this.loadState_ = ol.source.VectorLoadState.IDLE;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.parser.Parser}
|
||||
*/
|
||||
this.parser_ = goog.isDef(options.parser) ? options.parser : null;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {string|undefined}
|
||||
*/
|
||||
this.url_ = options.url;
|
||||
|
||||
goog.base(this, {
|
||||
attributions: options.attributions,
|
||||
extent: options.extent,
|
||||
logo: options.logo,
|
||||
projection: goog.isDef(options.projection) ?
|
||||
options.projection : ol.proj.get('EPSG:4326')
|
||||
});
|
||||
};
|
||||
goog.inherits(ol.source.Vector, ol.source.Source);
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.layer.Layer} layer Layer that parses the data.
|
||||
* @param {ol.Extent} extent Extent that needs to be fetched.
|
||||
* @param {ol.Projection} projection Projection of the view.
|
||||
* @param {Function=} opt_callback Callback which is called when features are
|
||||
* parsed after loading.
|
||||
* @return {ol.source.VectorLoadState} The current load state.
|
||||
*/
|
||||
ol.source.Vector.prototype.prepareFeatures = function(layer, extent, projection,
|
||||
opt_callback) {
|
||||
// TODO: Implement strategies. BBOX aware strategies will need the extent.
|
||||
if (goog.isDef(this.url_) &&
|
||||
this.loadState_ == ol.source.VectorLoadState.IDLE) {
|
||||
this.loadState_ = ol.source.VectorLoadState.LOADING;
|
||||
goog.net.XhrIo.send(this.url_, goog.bind(function(event) {
|
||||
var xhr = event.target;
|
||||
if (xhr.isSuccess()) {
|
||||
// TODO: Get source projection from data if supported by parser.
|
||||
layer.parseFeatures(xhr.getResponseText(), this.parser_, projection);
|
||||
this.loadState_ = ol.source.VectorLoadState.LOADED;
|
||||
if (goog.isDef(opt_callback)) {
|
||||
opt_callback();
|
||||
}
|
||||
} else {
|
||||
// TODO: Error handling.
|
||||
this.loadState_ = ol.source.VectorLoadState.ERROR;
|
||||
}
|
||||
}, this));
|
||||
} else if (!goog.isNull(this.data_)) {
|
||||
layer.parseFeatures(this.data_, this.parser_, projection);
|
||||
this.data_ = null;
|
||||
this.loadState_ = ol.source.VectorLoadState.LOADED;
|
||||
}
|
||||
return this.loadState_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @enum {number}
|
||||
*/
|
||||
ol.source.VectorLoadState = {
|
||||
IDLE: 0,
|
||||
LOADING: 1,
|
||||
LOADED: 2,
|
||||
ERROR: 3
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user