0

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')
    });
    openlayerMap.addInteraction(draw);

    draw.on('drawstart',
            function(evt) {
        // set sketch
    }, this);

    draw.on('drawend',
            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);
8
  • seems like working fine codepen.io/anon/pen/JrvWbW
    – Chase Choi
    Commented Oct 10, 2017 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? Commented Oct 11, 2017 at 16:32
  • or could you make a fiddle to show the wrong behavior?
    – Chase Choi
    Commented Oct 12, 2017 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. Commented Oct 12, 2017 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
    Commented Oct 12, 2017 at 6:54

1 Answer 1

1

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 and acknowledge you have read our privacy policy.

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