From a2388756f26f9482936ccb6a8f7c58cd7a37ed06 Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Thu, 6 Jan 2022 20:07:34 +0100 Subject: [PATCH 1/2] Use getFeaturesInExtent --- examples/box-selection.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/box-selection.js b/examples/box-selection.js index 61962b310d..08466edae6 100644 --- a/examples/box-selection.js +++ b/examples/box-selection.js @@ -42,6 +42,9 @@ const dragBox = new DragBox({ map.addInteraction(dragBox); dragBox.on('boxend', function () { + const extent = dragBox.getGeometry().getExtent(); + const boxFeatures = vectorSource.getFeaturesInExtent(extent); + // features that intersect the box geometry are added to the // collection of selected features @@ -50,11 +53,6 @@ dragBox.on('boxend', function () { // be added directly to the collection const rotation = map.getView().getRotation(); const oblique = rotation % (Math.PI / 2) !== 0; - const candidateFeatures = oblique ? [] : selectedFeatures; - const extent = dragBox.getGeometry().getExtent(); - vectorSource.forEachFeatureIntersectingExtent(extent, function (feature) { - candidateFeatures.push(feature); - }); // when the view is obliquely rotated the box extent will // exceed its geometry so both the box and the candidate @@ -66,13 +64,15 @@ dragBox.on('boxend', function () { const geometry = dragBox.getGeometry().clone(); geometry.rotate(-rotation, anchor); const extent = geometry.getExtent(); - candidateFeatures.forEach(function (feature) { + boxFeatures.forEach(function (feature) { const geometry = feature.getGeometry().clone(); geometry.rotate(-rotation, anchor); if (geometry.intersectsExtent(extent)) { selectedFeatures.push(feature); } }); + } else { + selectedFeatures.extend(boxFeatures); } }); From c785c2813b20c1823be4819f48535bc820453fe1 Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Thu, 6 Jan 2022 21:10:26 +0100 Subject: [PATCH 2/2] Filter for actual geometry intersection --- examples/box-selection.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/box-selection.js b/examples/box-selection.js index 08466edae6..7b9af7ad13 100644 --- a/examples/box-selection.js +++ b/examples/box-selection.js @@ -43,7 +43,9 @@ map.addInteraction(dragBox); dragBox.on('boxend', function () { const extent = dragBox.getGeometry().getExtent(); - const boxFeatures = vectorSource.getFeaturesInExtent(extent); + const boxFeatures = vectorSource + .getFeaturesInExtent(extent) + .filter((feature) => feature.getGeometry().intersectsExtent(extent)); // features that intersect the box geometry are added to the // collection of selected features