Add an optional feature filter to getClosestFeatureToCoordinate
This commit is contained in:
@@ -612,10 +612,13 @@ ol.source.Vector.prototype.getFeaturesInExtent = function(extent) {
|
|||||||
* This method is not available when the source is configured with
|
* This method is not available when the source is configured with
|
||||||
* `useSpatialIndex` set to `false`.
|
* `useSpatialIndex` set to `false`.
|
||||||
* @param {ol.Coordinate} coordinate Coordinate.
|
* @param {ol.Coordinate} coordinate Coordinate.
|
||||||
|
* @param {function(ol.Feature):boolean=} opt_filter Feature filter function.
|
||||||
|
* The filter function will receive one argument, the {@link ol.Feature feature}
|
||||||
|
* and it should return a boolean value. By default, no filtering is made.
|
||||||
* @return {ol.Feature} Closest feature.
|
* @return {ol.Feature} Closest feature.
|
||||||
* @api stable
|
* @api stable
|
||||||
*/
|
*/
|
||||||
ol.source.Vector.prototype.getClosestFeatureToCoordinate = function(coordinate) {
|
ol.source.Vector.prototype.getClosestFeatureToCoordinate = function(coordinate, opt_filter) {
|
||||||
// Find the closest feature using branch and bound. We start searching an
|
// Find the closest feature using branch and bound. We start searching an
|
||||||
// infinite extent, and find the distance from the first feature found. This
|
// infinite extent, and find the distance from the first feature found. This
|
||||||
// becomes the closest feature. We then compute a smaller extent which any
|
// becomes the closest feature. We then compute a smaller extent which any
|
||||||
@@ -632,11 +635,13 @@ ol.source.Vector.prototype.getClosestFeatureToCoordinate = function(coordinate)
|
|||||||
goog.asserts.assert(this.featuresRtree_,
|
goog.asserts.assert(this.featuresRtree_,
|
||||||
'getClosestFeatureToCoordinate does not work with useSpatialIndex set ' +
|
'getClosestFeatureToCoordinate does not work with useSpatialIndex set ' +
|
||||||
'to false');
|
'to false');
|
||||||
|
var filter = opt_filter ? opt_filter : ol.functions.TRUE;
|
||||||
this.featuresRtree_.forEachInExtent(extent,
|
this.featuresRtree_.forEachInExtent(extent,
|
||||||
/**
|
/**
|
||||||
* @param {ol.Feature} feature Feature.
|
* @param {ol.Feature} feature Feature.
|
||||||
*/
|
*/
|
||||||
function(feature) {
|
function(feature) {
|
||||||
|
if (filter(feature)) {
|
||||||
var geometry = feature.getGeometry();
|
var geometry = feature.getGeometry();
|
||||||
goog.asserts.assert(geometry,
|
goog.asserts.assert(geometry,
|
||||||
'feature geometry is defined and not null');
|
'feature geometry is defined and not null');
|
||||||
@@ -655,6 +660,7 @@ ol.source.Vector.prototype.getClosestFeatureToCoordinate = function(coordinate)
|
|||||||
extent[2] = x + minDistance;
|
extent[2] = x + minDistance;
|
||||||
extent[3] = y + minDistance;
|
extent[3] = y + minDistance;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return closestFeature;
|
return closestFeature;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -77,6 +77,37 @@ describe('ol.source.Vector', function() {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('when populated with 3 features', function() {
|
||||||
|
|
||||||
|
var features = [];
|
||||||
|
var vectorSource;
|
||||||
|
beforeEach(function() {
|
||||||
|
features.push(new ol.Feature(new ol.geom.LineString([[0, 0], [10, 10]])));
|
||||||
|
features.push(new ol.Feature(new ol.geom.Point([0, 10])));
|
||||||
|
features.push(new ol.Feature(new ol.geom.Point([10, 5])));
|
||||||
|
vectorSource = new ol.source.Vector({
|
||||||
|
features: features
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#getClosestFeatureToCoordinate', function() {
|
||||||
|
|
||||||
|
it('returns the expected feature', function() {
|
||||||
|
var feature = vectorSource.getClosestFeatureToCoordinate([1, 9]);
|
||||||
|
expect(feature).to.be(features[1]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns the expected feature when a filter is used', function() {
|
||||||
|
var feature = vectorSource.getClosestFeatureToCoordinate([1, 9], function(feature) {
|
||||||
|
return feature.getGeometry().getType() == 'LineString';
|
||||||
|
});
|
||||||
|
expect(feature).to.be(features[0]);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
describe('when populated with 10 random points and a null', function() {
|
describe('when populated with 10 random points and a null', function() {
|
||||||
|
|
||||||
var features;
|
var features;
|
||||||
@@ -558,5 +589,6 @@ goog.require('ol.events');
|
|||||||
goog.require('ol.Collection');
|
goog.require('ol.Collection');
|
||||||
goog.require('ol.Feature');
|
goog.require('ol.Feature');
|
||||||
goog.require('ol.geom.Point');
|
goog.require('ol.geom.Point');
|
||||||
|
goog.require('ol.geom.LineString');
|
||||||
goog.require('ol.proj');
|
goog.require('ol.proj');
|
||||||
goog.require('ol.source.Vector');
|
goog.require('ol.source.Vector');
|
||||||
|
|||||||
Reference in New Issue
Block a user