forked from openlayers/openlayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbox-selection.js
98 lines (83 loc) · 2.84 KB
/
box-selection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import Map from '../src/ol/Map.js';
import View from '../src/ol/View.js';
import {platformModifierKeyOnly} from '../src/ol/events/condition.js';
import GeoJSON from '../src/ol/format/GeoJSON.js';
import {DragBox, Select} from '../src/ol/interaction.js';
import {Tile as TileLayer, Vector as VectorLayer} from '../src/ol/layer.js';
import {OSM, Vector as VectorSource} from '../src/ol/source.js';
const vectorSource = new VectorSource({
url: 'data/geojson/countries.geojson',
format: new GeoJSON()
});
const map = new Map({
layers: [
new TileLayer({
source: new OSM()
}),
new VectorLayer({
source: vectorSource
})
],
target: 'map',
view: new View({
center: [0, 0],
zoom: 2,
constrainRotation: 16
})
});
// a normal select interaction to handle click
const select = new Select();
map.addInteraction(select);
const selectedFeatures = select.getFeatures();
// a DragBox interaction used to select features by drawing boxes
const dragBox = new DragBox({
condition: platformModifierKeyOnly
});
map.addInteraction(dragBox);
dragBox.on('boxend', function() {
// 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;
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
// 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();
candidateFeatures.forEach(function(feature) {
const geometry = feature.getGeometry().clone();
geometry.rotate(-rotation, anchor);
if (geometry.intersectsExtent(extent)) {
selectedFeatures.push(feature);
}
});
}
});
// clear selection when drawing a new box and when clicking on the map
dragBox.on('boxstart', function() {
selectedFeatures.clear();
});
const infoBox = document.getElementById('info');
selectedFeatures.on(['add', 'remove'], function() {
const names = selectedFeatures.getArray().map(function(feature) {
return feature.get('name');
});
if (names.length > 0) {
infoBox.innerHTML = names.join(', ');
} else {
infoBox.innerHTML = 'No countries selected';
}
});