Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle wrapped worlds in Box Selection example #14608

Merged
merged 2 commits into from Mar 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 52 additions & 33 deletions examples/box-selection.js
Expand Up @@ -5,6 +5,7 @@ import VectorSource from '../src/ol/source/Vector.js';
import View from '../src/ol/View.js';
import {DragBox, Select} from '../src/ol/interaction.js';
import {Fill, Stroke, Style} from '../src/ol/style.js';
import {getWidth} from '../src/ol/extent.js';
import {platformModifierKeyOnly} from '../src/ol/events/condition.js';

const vectorSource = new VectorSource({
Expand Down Expand Up @@ -68,39 +69,57 @@ const dragBox = new DragBox({
map.addInteraction(dragBox);

dragBox.on('boxend', function () {
const extent = dragBox.getGeometry().getExtent();
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

// if the view is not obliquely rotated the box geometry and
// its extent are equalivalent so intersecting features can
// be added directly to the collection
const rotation = map.getView().getRotation();
const oblique = rotation % (Math.PI / 2) !== 0;

// when the view is obliquely rotated the box extent will
// exceed its geometry so both the box and the candidate
// feature geometries are rotated around a common anchor
// to confirm that, with the box geometry aligned with its
// extent, the geometries intersect
if (oblique) {
const anchor = [0, 0];
const geometry = dragBox.getGeometry().clone();
geometry.rotate(-rotation, anchor);
const extent = geometry.getExtent();
boxFeatures.forEach(function (feature) {
const geometry = feature.getGeometry().clone();
const boxExtent = dragBox.getGeometry().getExtent();

// if the extent crosses the antimeridian process each world separately
const worldExtent = map.getView().getProjection().getExtent();
const worldWidth = getWidth(worldExtent);
const startWorld = Math.floor((boxExtent[0] - worldExtent[0]) / worldWidth);
const endWorld = Math.floor((boxExtent[2] - worldExtent[0]) / worldWidth);

for (let world = startWorld; world <= endWorld; ++world) {
const left = Math.max(boxExtent[0] - world * worldWidth, worldExtent[0]);
const right = Math.min(boxExtent[2] - world * worldWidth, worldExtent[2]);
const extent = [left, boxExtent[1], right, boxExtent[3]];

const boxFeatures = vectorSource
.getFeaturesInExtent(extent)
.filter(
(feature) =>
!selectedFeatures.getArray().includes(feature) &&
feature.getGeometry().intersectsExtent(extent)
);

// features that intersect the box geometry are added to the
// collection of selected features

// if the view is not obliquely rotated the box geometry and
// its extent are equalivalent so intersecting features can
// be added directly to the collection
const rotation = map.getView().getRotation();
const oblique = rotation % (Math.PI / 2) !== 0;

// when the view is obliquely rotated the box extent will
// exceed its geometry so both the box and the candidate
// feature geometries are rotated around a common anchor
// to confirm that, with the box geometry aligned with its
// extent, the geometries intersect
if (oblique) {
const anchor = [0, 0];
const geometry = dragBox.getGeometry().clone();
geometry.translate(-world * worldWidth, 0);
geometry.rotate(-rotation, anchor);
if (geometry.intersectsExtent(extent)) {
selectedFeatures.push(feature);
}
});
} else {
selectedFeatures.extend(boxFeatures);
const extent = geometry.getExtent();
boxFeatures.forEach(function (feature) {
const geometry = feature.getGeometry().clone();
geometry.rotate(-rotation, anchor);
if (geometry.intersectsExtent(extent)) {
selectedFeatures.push(feature);
}
});
} else {
selectedFeatures.extend(boxFeatures);
}
}
});

Expand All @@ -112,7 +131,7 @@ dragBox.on('boxstart', function () {
const infoBox = document.getElementById('info');

selectedFeatures.on(['add', 'remove'], function () {
const names = selectedFeatures.getArray().map(function (feature) {
const names = selectedFeatures.getArray().map((feature) => {
return feature.get('ECO_NAME');
});
if (names.length > 0) {
Expand Down