Make addFeature(s) ignore features with same id
This commit is contained in:
@@ -170,6 +170,11 @@ ol.source.Vector.prototype.addFeature = function(feature) {
|
|||||||
*/
|
*/
|
||||||
ol.source.Vector.prototype.addFeatureInternal = function(feature) {
|
ol.source.Vector.prototype.addFeatureInternal = function(feature) {
|
||||||
var featureKey = goog.getUid(feature).toString();
|
var featureKey = goog.getUid(feature).toString();
|
||||||
|
|
||||||
|
if (!this.addToIndex_(featureKey, feature)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this.setupChangeEvents_(featureKey, feature);
|
this.setupChangeEvents_(featureKey, feature);
|
||||||
|
|
||||||
var geometry = feature.getGeometry();
|
var geometry = feature.getGeometry();
|
||||||
@@ -180,7 +185,6 @@ ol.source.Vector.prototype.addFeatureInternal = function(feature) {
|
|||||||
this.nullGeometryFeatures_[featureKey] = feature;
|
this.nullGeometryFeatures_[featureKey] = feature;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.addToIndex_(featureKey, feature);
|
|
||||||
this.dispatchEvent(
|
this.dispatchEvent(
|
||||||
new ol.source.VectorEvent(ol.source.VectorEventType.ADDFEATURE, feature));
|
new ol.source.VectorEvent(ol.source.VectorEventType.ADDFEATURE, feature));
|
||||||
};
|
};
|
||||||
@@ -208,17 +212,25 @@ ol.source.Vector.prototype.setupChangeEvents_ = function(featureKey, feature) {
|
|||||||
/**
|
/**
|
||||||
* @param {string} featureKey
|
* @param {string} featureKey
|
||||||
* @param {ol.Feature} feature
|
* @param {ol.Feature} feature
|
||||||
|
* @return {boolean} `true` if the feature is "valid", in the sense that it is
|
||||||
|
* also a candidate for insertion into the Rtree, otherwise `false`.
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
ol.source.Vector.prototype.addToIndex_ = function(featureKey, feature) {
|
ol.source.Vector.prototype.addToIndex_ = function(featureKey, feature) {
|
||||||
|
var valid = true;
|
||||||
var id = feature.getId();
|
var id = feature.getId();
|
||||||
if (goog.isDef(id)) {
|
if (goog.isDef(id)) {
|
||||||
this.idIndex_[id.toString()] = feature;
|
if (!(id.toString() in this.idIndex_)) {
|
||||||
|
this.idIndex_[id.toString()] = feature;
|
||||||
|
} else {
|
||||||
|
valid = false;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
goog.asserts.assert(!(featureKey in this.undefIdIndex_),
|
goog.asserts.assert(!(featureKey in this.undefIdIndex_),
|
||||||
'Feature already added to the source');
|
'Feature already added to the source');
|
||||||
this.undefIdIndex_[featureKey] = feature;
|
this.undefIdIndex_[featureKey] = feature;
|
||||||
}
|
}
|
||||||
|
return valid;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -240,30 +252,38 @@ ol.source.Vector.prototype.addFeatures = function(features) {
|
|||||||
*/
|
*/
|
||||||
ol.source.Vector.prototype.addFeaturesInternal = function(features) {
|
ol.source.Vector.prototype.addFeaturesInternal = function(features) {
|
||||||
var featureKey, i, length, feature;
|
var featureKey, i, length, feature;
|
||||||
|
|
||||||
var extents = [];
|
var extents = [];
|
||||||
var validFeatures = [];
|
var newFeatures = [];
|
||||||
|
var geometryFeatures = [];
|
||||||
|
|
||||||
for (i = 0, length = features.length; i < length; i++) {
|
for (i = 0, length = features.length; i < length; i++) {
|
||||||
feature = features[i];
|
feature = features[i];
|
||||||
featureKey = goog.getUid(feature).toString();
|
featureKey = goog.getUid(feature).toString();
|
||||||
|
if (this.addToIndex_(featureKey, feature)) {
|
||||||
|
newFeatures.push(feature);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0, length = newFeatures.length; i < length; i++) {
|
||||||
|
feature = newFeatures[i];
|
||||||
|
featureKey = goog.getUid(feature).toString();
|
||||||
this.setupChangeEvents_(featureKey, feature);
|
this.setupChangeEvents_(featureKey, feature);
|
||||||
|
|
||||||
var geometry = feature.getGeometry();
|
var geometry = feature.getGeometry();
|
||||||
if (goog.isDefAndNotNull(geometry)) {
|
if (goog.isDefAndNotNull(geometry)) {
|
||||||
var extent = geometry.getExtent();
|
var extent = geometry.getExtent();
|
||||||
extents.push(extent);
|
extents.push(extent);
|
||||||
validFeatures.push(feature);
|
geometryFeatures.push(feature);
|
||||||
} else {
|
} else {
|
||||||
this.nullGeometryFeatures_[featureKey] = feature;
|
this.nullGeometryFeatures_[featureKey] = feature;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.rBush_.load(extents, validFeatures);
|
this.rBush_.load(extents, geometryFeatures);
|
||||||
|
|
||||||
for (i = 0, length = features.length; i < length; i++) {
|
for (i = 0, length = newFeatures.length; i < length; i++) {
|
||||||
feature = features[i];
|
|
||||||
featureKey = goog.getUid(feature).toString();
|
|
||||||
this.addToIndex_(featureKey, feature);
|
|
||||||
this.dispatchEvent(new ol.source.VectorEvent(
|
this.dispatchEvent(new ol.source.VectorEvent(
|
||||||
ol.source.VectorEventType.ADDFEATURE, feature));
|
ol.source.VectorEventType.ADDFEATURE, newFeatures[i]));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -371,14 +371,15 @@ describe('ol.source.Vector', function() {
|
|||||||
source = new ol.source.Vector();
|
source = new ol.source.Vector();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('allows adding feature with the same id', function() {
|
it('ignores features with the same id', function() {
|
||||||
var feature = new ol.Feature();
|
var feature = new ol.Feature();
|
||||||
feature.setId('foo');
|
feature.setId('foo');
|
||||||
source.addFeature(feature);
|
source.addFeature(feature);
|
||||||
var dupe = new ol.Feature();
|
var dupe = new ol.Feature();
|
||||||
dupe.setId('foo');
|
dupe.setId('foo');
|
||||||
source.addFeature(dupe);
|
source.addFeature(dupe);
|
||||||
expect(source.getFeatureById('foo')).to.be(dupe);
|
expect(source.getFeatures()).to.have.length(1);
|
||||||
|
expect(source.getFeatureById('foo')).to.be(feature);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('allows changing feature and set the same id', function() {
|
it('allows changing feature and set the same id', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user