Handle feature's geometries changing to and from null in ol.source.Vector

This commit is contained in:
Tom Payne
2013-12-20 14:42:50 +01:00
parent 4b9451de81
commit 8a7ae264e1
2 changed files with 64 additions and 1 deletions

View File

@@ -279,7 +279,22 @@ ol.source.Vector.prototype.getExtent = function() {
*/
ol.source.Vector.prototype.handleFeatureChange_ = function(event) {
var feature = /** @type {ol.Feature} */ (event.target);
this.rBush_.update(feature.getGeometry().getExtent(), feature);
var featureKey = goog.getUid(feature).toString();
var geometry = feature.getGeometry();
if (goog.isNull(geometry)) {
if (!(featureKey in this.nullGeometryFeatures_)) {
this.rBush_.remove(feature);
this.nullGeometryFeatures_[featureKey] = feature;
}
} else {
var extent = geometry.getExtent();
if (featureKey in this.nullGeometryFeatures_) {
delete this.nullGeometryFeatures_[featureKey];
this.rBush_.insert(extent, feature);
} else {
this.rBush_.update(extent, feature);
}
}
this.dispatchChangeEvent();
};

View File

@@ -187,6 +187,54 @@ describe('ol.source.Vector', function() {
});
describe('tracking changes to features', function() {
var vectorSource;
beforeEach(function() {
vectorSource = new ol.source.Vector();
});
it('keeps its index up-to-date', function() {
var feature = new ol.Feature(new ol.geom.Point([1, 1]));
vectorSource.addFeature(feature);
expect(vectorSource.getAllFeaturesInExtent([0, 0, 2, 2])).
to.eql([feature]);
feature.getGeometry().setCoordinates([3, 3]);
expect(vectorSource.getAllFeaturesInExtent([0, 0, 2, 2])).
to.be.empty();
expect(vectorSource.getAllFeaturesInExtent([2, 2, 4, 4])).
to.eql([feature]);
});
it('handles features with null geometries', function() {
var feature = new ol.Feature(null);
vectorSource.addFeature(feature);
expect(vectorSource.getAllFeatures()).to.eql([feature]);
});
it('handles features with geometries changing from null', function() {
var feature = new ol.Feature(null);
vectorSource.addFeature(feature);
expect(vectorSource.getAllFeatures()).to.eql([feature]);
feature.setGeometry(new ol.geom.Point([1, 1]));
expect(vectorSource.getAllFeaturesInExtent([0, 0, 2, 2])).
to.eql([feature]);
expect(vectorSource.getAllFeatures()).to.eql([feature]);
});
it('handles features with geometries changing to null', function() {
var feature = new ol.Feature(new ol.geom.Point([1, 1]));
vectorSource.addFeature(feature);
expect(vectorSource.getAllFeatures()).to.eql([feature]);
expect(vectorSource.getAllFeaturesInExtent([0, 0, 2, 2])).
to.eql([feature]);
feature.setGeometry(null);
expect(vectorSource.getAllFeaturesInExtent([0, 0, 2, 2])).to.be.empty();
expect(vectorSource.getAllFeatures()).to.eql([feature]);
});
});
});