Handle features with null geometries in ol.source.Vector

This commit is contained in:
Tom Payne
2013-12-15 23:10:38 +01:00
parent 975e0c0576
commit 4510510477
+29 -5
View File
@@ -6,10 +6,12 @@ goog.provide('ol.source.Vector');
goog.provide('ol.source.VectorEvent'); goog.provide('ol.source.VectorEvent');
goog.provide('ol.source.VectorEventType'); goog.provide('ol.source.VectorEventType');
goog.require('goog.array');
goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('goog.events'); goog.require('goog.events');
goog.require('goog.events.Event'); goog.require('goog.events.Event');
goog.require('goog.events.EventType'); goog.require('goog.events.EventType');
goog.require('goog.object');
goog.require('ol.source.Source'); goog.require('ol.source.Source');
goog.require('ol.structs.RBush'); goog.require('ol.structs.RBush');
@@ -48,6 +50,12 @@ ol.source.Vector = function(opt_options) {
*/ */
this.rBush_ = new ol.structs.RBush(); this.rBush_ = new ol.structs.RBush();
/**
* @private
* @type {Object.<string, ol.Feature>}
*/
this.nullGeometryFeatures_ = {};
/** /**
* @private * @private
* @type {Object.<string, goog.events.Key>} * @type {Object.<string, goog.events.Key>}
@@ -74,8 +82,13 @@ ol.source.Vector.prototype.addFeature = function(feature) {
goog.asserts.assert(!(featureKey in this.featureChangeKeys_)); goog.asserts.assert(!(featureKey in this.featureChangeKeys_));
this.featureChangeKeys_[featureKey] = goog.events.listen(feature, this.featureChangeKeys_[featureKey] = goog.events.listen(feature,
goog.events.EventType.CHANGE, this.handleFeatureChange_, false, this); goog.events.EventType.CHANGE, this.handleFeatureChange_, false, this);
var extent = feature.getGeometry().getExtent(); var geometry = feature.getGeometry();
this.rBush_.insert(extent, feature); if (goog.isNull(geometry)) {
this.nullGeometryFeatures_[goog.getUid(feature).toString()] = feature;
} else {
var extent = geometry.getExtent();
this.rBush_.insert(extent, feature);
}
this.dispatchEvent( this.dispatchEvent(
new ol.source.VectorEvent(ol.source.VectorEventType.ADDFEATURE, feature)); new ol.source.VectorEvent(ol.source.VectorEventType.ADDFEATURE, feature));
this.dispatchChangeEvent(); this.dispatchChangeEvent();
@@ -140,7 +153,12 @@ ol.source.Vector.prototype.forEachFeatureInExtent =
* @return {Array.<ol.Feature>} Features. * @return {Array.<ol.Feature>} Features.
*/ */
ol.source.Vector.prototype.getAllFeatures = function() { ol.source.Vector.prototype.getAllFeatures = function() {
return this.rBush_.getAll(); var features = this.rBush_.getAll();
if (!goog.object.isEmpty(this.nullGeometryFeatures_)) {
goog.array.extend(
features, goog.object.getValues(this.nullGeometryFeatures_));
}
return features;
}; };
@@ -235,7 +253,8 @@ ol.source.Vector.prototype.handleFeatureChange_ = function(event) {
* @return {boolean} Is empty. * @return {boolean} Is empty.
*/ */
ol.source.Vector.prototype.isEmpty = function() { ol.source.Vector.prototype.isEmpty = function() {
return this.rBush_.isEmpty(); return this.rBush_.isEmpty() &&
goog.object.isEmpty(this.nullGeometryFeatures_);
}; };
@@ -243,7 +262,12 @@ ol.source.Vector.prototype.isEmpty = function() {
* @param {ol.Feature} feature Feature. * @param {ol.Feature} feature Feature.
*/ */
ol.source.Vector.prototype.removeFeature = function(feature) { ol.source.Vector.prototype.removeFeature = function(feature) {
this.rBush_.remove(feature); var featureKey = goog.getUid(feature).toString();
if (featureKey in this.nullGeometryFeatures_) {
delete this.nullGeometryFeatures_[featureKey];
} else {
this.rBush_.remove(feature);
}
this.removeFeatureInternal_(feature); this.removeFeatureInternal_(feature);
this.dispatchChangeEvent(); this.dispatchChangeEvent();
}; };