I am looking for a PyQGIS 3 method for reversing the direction of a MultiLineString
feature. If there is more than one part, the order of the parts should be reversed.
I have found a similar question -
Switching line direction in QGIS - for LineString/Polyline
- which only works for QGIS 2 - and modified it slightly for use in QGIS 3.
However, I can't work out how to adapt the approach for MultiLineStrings
.
from qgis.utils import iface
from qgis.core import QgsGeometry
layer = qgis.utils.iface.mapCanvas().currentLayer()
feature = layer.getFeature(1) # feature id
geom = feature.geometry()
nodes = geom.asPolyline()
nodes.reverse()
newgeom = QgsGeometry.fromPolylineXY(nodes)
layer.startEditing()
layer.changeGeometry(feature.id(), newgeom)
layer.commitChanges()
In theory, the above approach could be used with
newgeom = QgsGeometry.fromMultiPolylineXY(<QgsMultiPolylineXY>)
but I can't see how to generate the necessary QgsMultiPolylineXY
object.
I have also noted the reversed()
function on the QGSMultiLineCurve
class, but I couldn't attach the generated GSMultiLineCurve
to the original feature.
I'm not interested in UI-based approaches, as the features to be reversed are determined programmatically.