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()
   newgeom = QgsGeometry.fromPolyline(nodes)

(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:
    # ?


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.

enter image description here

  • 1
    There is no such thing as reversed polygon. Outer ring vertices go clockwise.
    – FelixIP
    Commented Mar 19, 2017 at 9:00
  • 1
    As far as I know, this depends on the data provider. In PostGIS it's possible to create polygon vertices clockwise and anti-clockwise. Commented Mar 19, 2017 at 9:05
  • Internal handling of polygons in QGIS is uniform as far as I know. Drivers turn direction when data are saved if needed.
    – user30184
    Commented Mar 19, 2017 at 9:27
  • 2
    Is it please possible to tell us why you want it?
    – Miro
    Commented Mar 19, 2017 at 11:10
  • Shapefile outer rings are clockwise; other data formats use left hand rule. If you succeed in flipping polygons to the incorrect orientation, you will likely cause random geometry manipulation failures. This goal is self-defeating.
    – Vince
    Commented Mar 19, 2017 at 11:17

1 Answer 1


This code works for polygons but, 'changeGeometry' method has no effect even in edit mode. It's necessary to create a new geometry as memory layer.

layer = iface.mapCanvas().currentLayer()

new_geom = []

for feature in layer.getFeatures():
    geom = feature.geometry()
    nodes = geom.asPolygon()
    print nodes
    print nodes
    print layer.changeGeometry(feature.id(), newgeom)

epsg = layer.crs().postgisSrid()

uri = "Polygon?crs=epsg:" + str(epsg) + "&field=id:integer""&index=yes"

mem_layer = QgsVectorLayer(uri,

prov = mem_layer.dataProvider()

feats = [ QgsFeature() for i in range(len(new_geom)) ]

for i, feat in enumerate(feats):


polygon_memlayer = [ feat.geometry().asPolygon() 
                     for feat in mem_layer.getFeatures() ]

print polygon_memlayer

After running the code at the Python Console of QGIS with this shapefile:

enter image description here

it could be observed, at Python Console, that the node order was effectively reversed.

enter image description here

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