I would like to make a selection with DragBox interaction and get the geometry type to Multipolygon.
Right now I'm getting a Polygon type back :
var source = new ol.source.Vector({wrapX: false});
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
new ol.layer.Vector({
source: source
})
],
view: new ol.View({
center: ol.proj.fromLonLat([-3, 48]),
zoom: 7
}),
});
var select = new ol.interaction.Select();
var dragBox = new ol.interaction.DragBox({
condition: ol.events.condition.platformModifierKeyOnly });
dragBox.on('boxend', function() {
var geom = dragBox.getGeometry();
var format = new ol.format.WKT();
var wktRepresentation = format.writeGeometry(geom);
console.log("wktRepresentation: " + wktRepresentation);
});
Can someone help me?
Best Answer
The reason for that is because
dragBox.getGeometry()
returns bbox geometry. You should get geometry from features that intersect with bbox. Lets suppose that geojson file that is loaded have both polygon and multipolygon geometry type. The function in this case for 'bboxend
' event is as follow.Here is the fiddle: https://jsfiddle.net/v3yqf78L/