I want to split a QgsPolyline
geometry into two separate lines at a given QgsPoint
(by projection onto the line), adding the point to the line as end point of the incoming line and start point of the outgoing one.
I got the following code that works for 2D geometries (creating a QgsPointXY
point and a QgsPolylineXY
poly from the input QgsFeature
):
def breaklineatpoint(pointfeat, polyfeat): point = pointfeat.geometry().asPoint() poly = polyfeat.geometry().asPolyline() geo = QgsGeometry().fromPolylineXY(poly) (sqdist, projpoint, aftervertex, leftOf) = geo.closestSegmentWithContext(point) newline1 = poly[:aftervertex] # from start to break point newline1.append(projpoint) # append point newline2 = poly[aftervertex:] # from break point to end newline2.insert(0, projpoint) # insert at position 0 return newline1, newline2
However, it drops the feature's Z and M values. How do I translate this function so that it can handle 3D geometries (QgsPolyline
instead of QgsPolylineXY
and QgsPoint
instead of QgsPointXY
)?
I already fail creating a QgsPolyline
from a QgsGeometry
. Using a QgsLineString
by QgsGeometry().constGet()
is no alternative as I then can't apply the Python list cutting and slicing functions.