I am looking for a method in PyQGIS (QGIS3.x) of 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 - How can I switch line direction in QGIS? - for LineString/Polyline. - which only works for QGIS2 - and modified it slightly for use in QGIS3. However, I can't work out how to adapt the approach for MultiLineStrings.
layer = qgis.utils.iface.mapCanvas().currentLayer()
fid = 1 # feature id
feature = layer.getFeature(1)
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() 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 QGSMultiLineCurve to the original feature.
I'm not really interested in UI-based approaches, as the features to be reversed are determined programatically.
Any approaches are welcome - if there's a better general approach than above, I'm happy to hear.