Select by drawn polygon
by Vladimir Vershinin
HTML
<script src="https://unpkg.com/vue@latest/dist/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io/en/v5.3.0/build/ol.js"></script>
<script src="https://unpkg.com/[email protected]/turf.js"></script>
<script src="https://unpkg.com/[email protected]/lib/index.umd.js"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/lib/style.css">
<div id="app">
<div>
Current selection: {{ selectedFeatures }}
</div>
<div v-if="drawnFeatures.length === 0">
Draw polygon to select features first
</div>
<div v-else>
<button @click="selectInDrawnPolygon">Select intersected features</button>
</div>
<vl-map ref="map" @created="mapCreated">
<vl-view ref="mapView" :zoom.sync="zoom" :center.sync="center"></vl-view>
<vl-layer-tile>
<vl-source-osm />
</vl-layer-tile>
<vl-layer-vector ref="vectorLayer" id="my-features">
<vl-source-vector ref="vectorSource">
<vl-feature v-for="feature in geojsonObject.features" :key="feature.id"
:id="feature.id" :properties="feature.properties">
<vl-geom-polygon :coordinates="feature.geometry.coordinates" />
</vl-feature>
</vl-source-vector>
</vl-layer-vector>
<vl-interaction-select ref="selectInteraction" :features.sync="selectedFeatures" :filter="selectFilter">
</vl-interaction-select>
<!-- draw components -->
<vl-layer-vector id="draw-pane">
<vl-source-vector ref="drawSource" ident="draw-target" :features.sync="drawnFeatures"></vl-source-vector>
</vl-layer-vector>
<vl-interaction-draw source="draw-target" type="Polygon"></vl-interaction-draw>
<vl-interaction-modify source="draw-target"></vl-interaction-modify>
</vl-map>
</div>
CSS
html, body, #app {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
JavaScript
let geojsonObject = {
'type': 'FeatureCollection',
'crs': {
'type': 'name',
'properties': {
'name': 'EPSG:3857'
}
},
'features': [{
'id': 1,
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [[[-5e6, 6e6], [-5e6, 8e6], [-3e6, 8e6],
[-3e6, 6e6], [-5e6, 6e6]]]
}
}, {
'id': 2,
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [[[-2e6, 6e6], [-2e6, 8e6], [0, 8e6],
[0, 6e6], [-2e6, 6e6]]]
}
}, {
'id': 3,
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [[[1e6, 6e6], [1e6, 8e6], [3e6, 8e6],
[3e6, 6e6], [1e6, 6e6]]]
}
}, {
'id': 4,
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [[[-2e6, -1e6], [-1e6, 1e6],
[0, -1e6], [-2e6, -1e6]]]
}
}]
};
new Vue({
el: '#app',
data () {
return {
zoom: 5,
center: [0, 3000000],
geojsonObject: geojsonObject,
selectedFeatures: [],
drawnFeatures: [],
}
},
mounted () {
setTimeout(() => {
this.$refs.mapView.$view.fit(this.$refs.vectorSource.$source.getExtent(), {
size: this.$refs.map.$map.getSize(),
duration: 1000,
})
}, 1000)
},
methods: {
/**
* @param {Vue} map - vl-map instance
*/
mapCreated (map) {
// a DragBox interaction used to select features by drawing boxes
const dragBox = new ol.interaction.DragBox({
condition: ol.events.condition.platformModifierKeyOnly
})
map.$map.addInteraction(dragBox)
dragBox.on('boxend', () => {
// features that intersect the box are added to the collection of
// selected features
const extent = dragBox.getGeometry().getExtent()
/** @var {ol.source.Vector} source */
const source = this.$refs.vectorSource.$source
...