[GIS] Split polylines at polygon boundaries in QGIS

analysisgeoprocessingintersectionqgisspatial-join

I have a set of contour lines and a polygon set containing glaciers.
Because I want the glaciers to have blue contours, I need to split the contour lines at the glacier boundaries.

I can achieve this using the following method:

1) Intersect the contours with the glacier polygon to create the glacier contours.
2) Create a spatial difference between the glacier polygon and an extent polygon.
3) Intersect the contours with the spatial difference from (2) to create the blue conoturs.
4) Merge the two results together if I want it in a single data set.

But the following method would be more convenient, and usable for other types of analyses as well.

1) Combine a polyline dataset (or point dataset for that matter) with a polygon dataset. The lines with be split at the polygon boundaries and each output line (or point) will take the attributes from the polygon it is inside.

Such a function would make my problem so much simpler to solve, as I can do it in one single operation.

Is there a function for this in QGIS?

Best Answer

I'm not aware of a ready-made function that does this, but you can use the Processing Modeller in QGIS to build such a function yourself.

To do this, you can run the "Vector/Overlay/Intersection" and "Vectors/Overlay/Difference" tool, both using the contours layer as the "Input Layer" and the glacier layer as the "Intersect Layer". The outputs of those functions are then fed to the "Vector/General tools/Merge vector layers" function to create the combined layer:

The .model file:

NAME:Split lines at polygons
GROUP:GIS.stackexchange
PARAMETER:ParameterVector|VECTORLAYER_GLACIER|Glacier|2|False
120.0,60.0
PARAMETER:ParameterVector|VECTORLAYER_CONTOURS|Contours|1|False
340.0,60.0
ALGORITHM:qgis:intersection
115.0,221.0
None
-1|VECTORLAYER_CONTOURS
-1|VECTORLAYER_GLACIER
Intersect|178.0,338.0
ALGORITHM:qgis:difference
378.0,213.0
None
-1|VECTORLAYER_CONTOURS
-1|VECTORLAYER_GLACIER
Difference|407.0,342.0
ALGORITHM:qgis:mergevectorlayers
314.0,443.0
None
0|OUTPUT
1|OUTPUT
Result|562.0,483.0
Related Question