[GIS] Constrained Voronoi polygons QGIS

polygonqgisvoronoi-thiessen

I have a shapefile of polygons, which are effectively borders of a geographic unit (consider as an example the 50 u.s. states, each state has within it some number of points.).

I'd like to create Voronoi polygons from the points, with the caveat that the state borders effectively constrain the Voronoi polygons created from the points within that state.

The labor-intensive way to do this would be to create 50 individual shapefiles for each state, create Voronoi polygons within each, then manually merge them back together.

Is there an easier way to do this, ideally in QGIS (am using 2.18 on Mac)?

Best Answer

It's a multi-step process, so you should create a custom model or script. In order to avoid slivers of polygons that belong to neighboring polygon points, you'll need to iterate through your states layer. Working with state polygons and a set of random points, I'll go through the process so that you can understand what's going on inside our model.

state points

Inside the Model

  1. Extract by location
    • Extract features from: points_layer
    • Where the features: intersect
    • By comparing to the features from: polygons_layer

state extracts of points

  1. Voronoi polygons
    • Input layer: 'Extracted (location)' from algorithm 'Extract by location'
    • Buffer region: 500 (It seems excessive, but we want to ensure that your Voronoi polygons completely cover the state they're in. Since we'll clip this layer in the next step, it's irrelevant how big we make this.)
    • Parent algorithms: Extract by location

voronoi by state

  1. Clip
    • Input layer: 'Voronoi polygons' from algorithm 'Voronoi polygons'
    • Overlay layer: polygons_layer
    • Clipped: output
    • Parent algorithms: Extract by location and Voronoi polygons

clipped

As you can see, we have our state-constrained Voronoi polygons in which points from neighboring states do not affect their arrangement.

Here's an image of the model:

processing model

The image doesn't convey this, but note that the polygon layer input of the model is a Vector Features object, not a Vector Layer.

Running the Model

When you go to run your model it is vital that you click on the icon to iterate through the features of your States layer.

iterate option

Once you've run the model, you can simply run Merge vector layers on the outputs to get a single-layer output, if that is important to you. Unfortunately, I'm not aware of an easy way to merge multiple model outputs within the model itself (please, someone correct me if there is something).

If you're comfortable with Python, you can easily modify the exported script of this model to handle that additional step. As you did not mention code-based solutions in your question, however, I will leave that out.

Related Question