In this post, I describe how we can use built-in QGIS processing tools to create a workflow to split polygons into equal parts. Using a clever algorithm and Feature Iterator tool in the Processing Framework, we can easily split all features in a given polygon layer into equal parts.
The algorithm for splitting any polygon shape into equal parts is described in this post PostGIS Polygon Splitting by Paul Ramsey. We will see how this can be implemented in QGIS.
Get the Dataset
Want to follow along? Download the
Splitting a Single Polygon
Let’s say we want to split a single polygon into approximately 4 parts of equal area. Here we have the state of California in the US.
1. Create Random Points within the Polygon
Use the Random Points Inside Polygons tool from the Processing Toolbox to create random points. The more points the better, but for a polygon like this, we can use 1000 points.
2. Create Clusters
If you want to split the polygons into N parts, create N clusters. Here we want to create 4 equal parts, so we use the K-Means clustering tool from the Processing Toolbox to create 4 clusters.
3. Group Cluster Points
Now, use the Aggregate tool to group points from the same cluster and create multipoint geometries from all points belonging to the same cluster.
4. Calculate Cluster Centroids
We can use the Centroids algorithm to calculate the centroid of each multipoint feature.
5. Create Thiessen Polygons
We have centroids for each of the parts. Using the Thiessen Polygons (SAGA) algorithm, we can now create polygons that will divide the region so that each point within the region is assigned to the closest centroid. This will closely match the clusters we computed. The Frame Size parameter controls the buffer region. We can use the size of the bounding box of the polygon to determine the ideal buffer size. It should be at least half of the largest edge of the bounding box.
framesize = max(maxy - miny , maxx - minx )/2
We can also use the QGIS’s native Voronoi Polygons algorithm here, but if all the centroids are in a single line, it can fail to create thiessen polygons that cover the original geometry – even with a large buffer specified.
6. Clip Thiessen Polygons to Original Polygon Boundary
Using the Intersection tool, we can now overlay the thiessen polygons on the original polygon layer to split it into 4 equal parts.
The result is what we want. The original polygon split into 4 equal parts.
Splitting Multiple Polygons
What if you want to split multiple polygons into equal parts? We can build a model to build a workflow of all the steps in the previous section and run it on each polygon. Below is the screenshot of a model built using the QGIS Processing Modeler. You can download and install this model from the Spatial Thoughts QGIS Resource Sharing repository.
Once installed, you can launch it from the Processing Toolbox. We will run it on a polygon layer containing of all states of the US.
When running the model, we want to split each polygon. Select the
tl_2019_us_state layer as the Polygons layer and make sure to click the Iterate Feature button. Once configured, click Run.
Once the processing finishes, you will end up with 1 layer per state. Each layer contains the split polygons for that state.
The final step is to merge all these layers into a single layer containing all split polygons. We can use the Merge vector layers algorithm to accomplish this.
The result is the original US States layer split with 4 equal parts of each state.
There are a few existing QGIS plugins and methods that can also help with the polygon division task:
- Cadastral Divisions Plugin: If you are looking to split land parcels, this plugin can interactively help you find the correct divisions.
- Polygon Divider Plugin: If you are looking to divide a large polygon into many small regularly sized polygons, this is the plugin for you.
- Split Polygins using PyQGIS: A blog post by Luísa Lucchese showing how to use PyQGIS to split many polygons using a technique similar to this post.
Found this post useful? Do let me know in the comments!
If you want to learn and master techniques for automating your GIS workflows with QGIS and earn official QGIS.org certification, check out my course Advanced QGIS.