The following Python code is very helpful to reverse the direction of lines in QGIS:
layer = qgis.utils.iface.mapCanvas().currentLayer()
for feature in layer.selectedFeatures():
geom = feature.geometry()
nodes = geom.asPolyline()
nodes.reverse()
newgeom = QgsGeometry.fromPolyline(nodes)
layer.changeGeometry(feature.id(),newgeom)
(see https://gis.stackexchange.com/a/9285/77061)
Unfortunately, the code doesn't work for polygons. Replacing asPolyline()
and fromPolyline(nodes)
with asPolygon()
and fromPolygon(nodes)
hasn't helped. Is there a Python code that works for polygons?
if layer.geometryType() == 2:
# ?
Edit:
For PostGIS and most likely some other data providers it's possible to digitize polygons clockwise and counter-clockwise. Creating a marker line symbology (e.g. triangle) for such layer may result in inward and outward rotated markers. So I want to reverse the direction of the anti-clockwise digitized polygons through the Python console.