Trim lines in QGIS on each polygon border

Let's assume we want to divide a line by the border of multiple polygons in QGIS. For example, let's suppose that we have drawn the following line:

Line to be trimmed by frontier

Vector > Geoprocessing Tools > Clip

Recursively trim lines

Notice how we need to click on the arrow to make the process recursive. The algorithm will iterate through each polygon and clip the line accordingly.

As we are working with 311 polygons, the result will be a clipped line for each polygon. That means we'll obtain 311 different lines:

Trimmed lines

As we want only one layer with the lines clipped at the frontier, we can:

Vector > Data Management Tools > Merge Vector Layers

Select the multiple layers (311 in this example) and run the algorithm.

Merge vector layers

We'll get a unique layer that is trimmed at the frontier of each polygon.

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.