Draw Holes in OpenLayers Polygon

Draw Holes in Polygon in OpenLayers. Image Credit: Author

To get full source code for this tutorial, click here.

To check the live demo for this tutorial, click here.

Split a Polygon with holes by Line in OpenLayers

A Polygon with holes have one or more interior boundaries(holes). In order to draw holes in polygon, you have to understand Polygon geometry and it’s representation in OpenLayers. The geometry is stored as array of coordinates. In case of holes in Polygon, they are also represented as coordinates, but OpenLayers has to interpret them differently.

In the below image, you can see the polygon with holes and their virtual representation as array of coordinates. In below image, the polygon have four holes. So, below polygon will be represented by five arrays of coordinates. The first array of coordinates will be our main polygon and rest four arrays will be holes for that polygon. This is the representation of polygon and holes in OpenLayers. These holes are also known as interior boundaries or linear ring.

Image Credit: Author

In order to write holes in a polygon, we will draw a regular polygon on our main polygon and then we will force OpenLayers to interpret them as holes. In Openlayers Polygon Geometry object, we have one method known as appendLinearRing. This method will add holes to polygon geometry. See line number 58 in code snippet below. We will add coordinates of hole to our polygon geometry using this method. Once the coordinates of hole are appended to geometry using this method, our main polygon will be reflected with holes. See line number 58 in code snippet below.

Draw Holes in OpenLayers Polygon

I hope this tutorial will create a good foundation for you and will help you to implement advance tools in OpenLayers

If you want tutorials on another GIS topic or you have another queries, please send an email at contact@spatial-dev.guru

To get full source code for this tutorial, click here.

To check the live demo for this tutorial, click here.

1 thought on “Draw Holes in OpenLayers Polygon”

  1. Pingback: Split a Polygon with holes by Line in OpenLayers - Spatial Dev Guru

Leave a ReplyCancel reply

Discover more from Spatial Dev Guru

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from Spatial Dev Guru

Subscribe now to keep reading and get access to the full archive.

Continue reading