I am working on an OpenLayers 3 digitizing tool, which works very fine at this point.

I would like to add a functionality that lets me add holes to existing polygons.

I know this is doable in OpenLayers 2, as you can see here:

enter image description here

Is there a way to achieve the same effect in OpenLayers 3?


This is currently not supported out of the box by the library. There is an issue on its GitHub project page marked with PR accepted. However, this problem can be solved without this capability and the result might be even more robust. I'm planning to make a digitizing software as well in the future, and I would mimic the QGIS approach at this point.

In QGIS, you can only draw polygons by default. However, there is a dedicated tool for drawing rings. It is fully automatic, thus if you activate that tool, you get a drawing interaction just like the one you draw your polygons with. After you finished the ring, the software decides if it can form a valid ring of an existing polygon (e.g. no intersects), and if it can, it punches the ring into the polygon. BTW, you can check if a polygon is completely inside another one by calling the intersectsCoordinate method of the outer ring with every coordinate of the hole. A little bit clumsy for my taste, but if you don't want to write or adapt an algorithm for checking intersections between polygons, it's working.

  • Pro:
    • This method is fully automatic.
  • Contra:
    • The feature order decides the chosen polygon, if there are more than one valid host for the given ring.

Because of the contra, I would alter QGIS's method, and require the user to select a polygon prior to making the "ring puncher" drawing tool active.

  • Thank you for your elaborate answer. I might have to develop my own functionality. Is there a way to erase a newly drawn geometry from another one? Maybe I could use that logic then. Btw, your book is helping me a lot :-) – BritishSteel Dec 2 '16 at 14:11

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.