From 8a7ae264e11df57236fc4923fea5b008a7e9b4b0 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Fri, 20 Dec 2013 14:42:50 +0100 Subject: [PATCH] Handle feature's geometries changing to and from null in ol.source.Vector --- src/ol/source/vectorsource.js | 17 ++++++++- test/spec/ol/source/vectorsource.test.js | 48 ++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/ol/source/vectorsource.js b/src/ol/source/vectorsource.js index d4c2eb5d82..818868ae6b 100644 --- a/src/ol/source/vectorsource.js +++ b/src/ol/source/vectorsource.js @@ -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(); }; diff --git a/test/spec/ol/source/vectorsource.test.js b/test/spec/ol/source/vectorsource.test.js index 975a793af6..81611b0a4c 100644 --- a/test/spec/ol/source/vectorsource.test.js +++ b/test/spec/ol/source/vectorsource.test.js @@ -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]); + }); + + }); + });