Vector Source / index all features by uid
Previously features were indexed by uid only when they did not have a defined id. A new method was added: `getFeatureByUid`. This is not part of the public API. This will facilitate the lookup of features for hit detection.
This commit is contained in:
@@ -10,6 +10,7 @@ import {bbox as bboxStrategy} from '../../../../src/ol/loadingstrategy.js';
|
||||
import {get as getProjection, transformExtent, fromLonLat} from '../../../../src/ol/proj.js';
|
||||
import VectorSource from '../../../../src/ol/source/Vector.js';
|
||||
import GeoJSON from '../../../../src/ol/format/GeoJSON.js';
|
||||
import {getUid} from '../../../../src/ol/util.js';
|
||||
|
||||
|
||||
describe('ol.source.Vector', function() {
|
||||
@@ -519,6 +520,59 @@ describe('ol.source.Vector', function() {
|
||||
|
||||
});
|
||||
|
||||
describe('#getFeatureByUid()', function() {
|
||||
let source;
|
||||
beforeEach(function() {
|
||||
source = new VectorSource();
|
||||
});
|
||||
|
||||
it('returns a feature with an id', function() {
|
||||
const feature = new Feature();
|
||||
feature.setId('abcd');
|
||||
source.addFeature(feature);
|
||||
expect(source.getFeatureByUid(getUid(feature))).to.be(feature);
|
||||
});
|
||||
|
||||
it('returns a feature without id', function() {
|
||||
const feature = new Feature();
|
||||
source.addFeature(feature);
|
||||
expect(source.getFeatureByUid(getUid(feature))).to.be(feature);
|
||||
});
|
||||
|
||||
it('returns null when no feature is found', function() {
|
||||
const feature = new Feature();
|
||||
feature.setId('abcd');
|
||||
source.addFeature(feature);
|
||||
const wrongId = 'abcd';
|
||||
expect(source.getFeatureByUid(wrongId)).to.be(null);
|
||||
});
|
||||
|
||||
it('returns null after removing feature', function() {
|
||||
const feature = new Feature();
|
||||
feature.setId('abcd');
|
||||
source.addFeature(feature);
|
||||
const uid = getUid(feature);
|
||||
expect(source.getFeatureByUid(uid)).to.be(feature);
|
||||
source.removeFeature(feature);
|
||||
expect(source.getFeatureByUid(uid)).to.be(null);
|
||||
});
|
||||
|
||||
it('returns null after clear', function() {
|
||||
const feature = new Feature();
|
||||
feature.setId('abcd');
|
||||
source.addFeature(feature);
|
||||
const uid = getUid(feature);
|
||||
expect(source.getFeatureByUid(uid)).to.be(feature);
|
||||
source.clear();
|
||||
expect(source.getFeatureByUid(uid)).to.be(null);
|
||||
});
|
||||
|
||||
it('returns null when no features are present', function() {
|
||||
expect(source.getFeatureByUid('abcd')).to.be(null);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#loadFeatures', function() {
|
||||
|
||||
describe('with the "bbox" strategy', function() {
|
||||
|
||||
Reference in New Issue
Block a user