Trying to extract the drawn features on openlayers. I have written the below code and it is working fine when we draw polygon on four objects. But, when we try to draw polygon on three objects it is returning four features instead of three, not sure why the function(evt.feature.getGeometry().getExtent()) is returning four features when polygon drawn on three objects on a map.

draw = new ol.interaction.Draw({
        source: sourceMeasure,
        type: /** @type {ol.geom.GeometryType} */ ('Polygon')

            function(evt) {
        // set sketch
    }, this);

            function(evt) {

        var extent = evt.feature.getGeometry().getExtent();

        vectorSource.forEachFeatureIntersectingExtent(extent, function(feature) {

            var layer = feature.get('LAYER');
            /* here my logic is written and it working fine with four coordinates */


    }, this);
  • seems like working fine codepen.io/anon/pen/JrvWbW – Chase Choi Oct 10 '17 at 5:10
  • No. It's not working. It is working only with a square box selection but for polygon it's not working. Is there any workaround? – user3474541 Oct 11 '17 at 16:32
  • or could you make a fiddle to show the wrong behavior? – Chase Choi Oct 12 '17 at 0:28
  • Chase Choi, I used the same fiddle the one you provided. I have drawn four small squares on the map in two columns, and drawn the polygon intersecting only 3 squares. But it is returning four features instead of three. – user3474541 Oct 12 '17 at 6:29
  • the coordinate of extent is always rectangle that's why you get wrong number of features. though you draw polygon, its extent is the maximum boundary rectangle. as you see openlayers.org/en/latest/apidoc/ol.html#.Extent – Chase Choi Oct 12 '17 at 6:54

You can use a third party library like JSTS to solve your issue.

You can find out how to select exactly by polygon instead of extend by looking at this sample

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.