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,28 +635,31 @@ 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) {
|
||||||
var geometry = feature.getGeometry();
|
if (filter(feature)) {
|
||||||
goog.asserts.assert(geometry,
|
var geometry = feature.getGeometry();
|
||||||
'feature geometry is defined and not null');
|
goog.asserts.assert(geometry,
|
||||||
var previousMinSquaredDistance = minSquaredDistance;
|
'feature geometry is defined and not null');
|
||||||
minSquaredDistance = geometry.closestPointXY(
|
var previousMinSquaredDistance = minSquaredDistance;
|
||||||
x, y, closestPoint, minSquaredDistance);
|
minSquaredDistance = geometry.closestPointXY(
|
||||||
if (minSquaredDistance < previousMinSquaredDistance) {
|
x, y, closestPoint, minSquaredDistance);
|
||||||
closestFeature = feature;
|
if (minSquaredDistance < previousMinSquaredDistance) {
|
||||||
// This is sneaky. Reduce the extent that it is currently being
|
closestFeature = feature;
|
||||||
// searched while the R-Tree traversal using this same extent object
|
// This is sneaky. Reduce the extent that it is currently being
|
||||||
// is still in progress. This is safe because the new extent is
|
// searched while the R-Tree traversal using this same extent object
|
||||||
// strictly contained by the old extent.
|
// is still in progress. This is safe because the new extent is
|
||||||
var minDistance = Math.sqrt(minSquaredDistance);
|
// strictly contained by the old extent.
|
||||||
extent[0] = x - minDistance;
|
var minDistance = Math.sqrt(minSquaredDistance);
|
||||||
extent[1] = y - minDistance;
|
extent[0] = x - minDistance;
|
||||||
extent[2] = x + minDistance;
|
extent[1] = y - minDistance;
|
||||||
extent[3] = y + minDistance;
|
extent[2] = x + 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