I'm using OL 6.3.1 and have a need to detect when the geometries of polygons overlap or intersect. Specifically I am trying to do something like this when a user draws a polygon:

self.drawInteraction.on('drawend', function(e){
self.newFeature = e.feature;
var drawnFeatureGeometry = self.newFeature.getGeometry();
var allFeatures = self.displaySource.getFeatures();
for(var i = 0;i < allFeatures.length; i++){
    var thisGeometry = allFeatures[i].getGeometry();
    if(drawnFeatureGeometry intersects with thisGeometry) // pseudoCode here. What can I do?
        alert('Features may not overlap. Please try again.');


I've found examples in previous versions, but those classes seem to have been deprecated.

containsExtent and containsPixel do not meet the use case.

I read the API documentation for Intersects (https://openlayers.org/en/latest/apidoc/module-ol_format_filter_Intersects-Intersects.html) but I couldn't see how it applies.

Any ideas?

Also, abortDrawing() doesn't seem to work as described in the API documentation.

  • 1
    You would need a third party utility such as turf turfjs.org/docs/#intersect You would first need to convert the features to turf features as in openlayers.org/en/latest/examples/turf.html – Mike Jun 29 at 22:16
  • You can have a look at an example of using turf.js to detect intersections here: gis.stackexchange.com/questions/344068/… – TomazicM Jun 30 at 6:31
  • Thank you. I saw a couple of examples using turf.js. I was hoping third party libraries wouldn't be necessary, but if that's the best or only way forward I'll try turf.js. Thank you again. – m.t.Dutch Jun 30 at 11:47
  • My link above to an example of using turf.js to detect intersections is mistakenly for Leaflet. Here is the one for OpenLayers: gis.stackexchange.com/questions/339622/… – TomazicM Jun 30 at 14:38
  • Thanks for the tip on turf.js. I am now detecting intersections just fine. The problem I continue to see is once the intersection is detected I can't seem to prevent the sketch from being added to the vector source. I have tried using the abortDraw() function, as well as drawInteraction.setActive('false'). In all cases the sketch is added to the vector source. – m.t.Dutch Jul 6 at 20:32

Your Answer

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

Browse other questions tagged or ask your own question.