I am looking for an easy way or function to split 2 previously selected line features (generate vertex) at intersecting points (or touching points). See the following picture:

enter image description here

I am developing a function/plugin where the user is supposed to manually select 2 line features which intersect and wants them to be splitted at intersection (touching) point.

So something like this:

layer = iface.activeLayer()
feats =  layer.selectedFeatures()

geom1 = feats[0].geometry()
geom2 = feats[1].geometry()

# I need a function like this

The line features/layer are loaded from a PostGIS database, so it is important to edit the existing layer and not create a new layer or shapefile. It would be nice if there is a pure pyqgis solution which I overlooked to solve this problem.

I found the intersects() and touches() functions of the QgsGeometry class, but they dont return the intersection points. There has to be a native pyqgis function to retrieve the intersection point or to split lines at a given point, right?

I know there is the split lines with lines tool, but I only want to split manually selected features/lines and not all.

I am aware that there are several similar questions asked already, but none of them seem to solve my particular issue.

For example:

1 Answer 1


Have you tried the Line Intersections tool in QGIS?

I've used this tool to create intersections for my road centerline layer, as well as to create intersection points between railroads and road centerlines. The documentation kinda sucks, and I ran the tool manually, but it created points at the intersection of two line segments crossing, not where they met end to end.

processing.runalg('qgis:lineintersections', input_a, input_b, field_a, field_b, output)

Once you have the intersection point, you could then use the point to split the line feature.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.