Tip: Count Number of Vertices in a Layer using QGIS

This tutorial has moved. Please see Counting Number of Vertices in a Layer for an updated version of this tutorial.

QGIS doesn’t have a built-in function to calculate number of vertices for each feature in a layer. You can use Vector → Geometry Tools → Extract Nodes to break the geometry into individual vertices and get a points layer. But there is an easier and better way to do this using python scripting in QGIS. You do not need to know any python for doing this. The below example will help you count the total number vertices in a polygon layer as well as add an attribute named ‘Vertices’ in your attribute table.


Open QGIS. Load the polygon layer you want to count vertices. Start the Python Console by going to Plugins → Python Console.



Once in the Python Console. Copy and Paste the following code and press ‘Enter’ twice. Make sure the layer is selected before pasting the code.

layer = qgis.utils.iface.activeLayer()
provider = layer.dataProvider()
provider.select()
feat = QgsFeature()
vertex_count = 0
while provider.nextFeature(feat):
 geom = feat.geometry()
 if geom.type() == QGis.Polygon:
   layer_vertices = 0
   if geom.isMultipart():
     polygons = geom.asMultiPolygon()
   else:
     polygons = [ geom.asPolygon() ]
   for polygon in polygons:
     for ring in polygon:
       layer_vertices += len(ring)
   print "Feature %d: Vertices %d" % ( feat.id(), layer_vertices)
   vertex_count += layer_vertices
   print "Total vertices: %d" % (vertex_count)



You will see lot of statements printed in the console with the certex count for each layer. You will also see the cumulative ‘Total Vertices’. The last statement for ‘Total Vertices’ is the total number of vertices in the layer.



This is useful, but a more useful approach is to add this information directly to the attribute table of the layer, so you can use it in your analysis. This code is a little more complex and cannot be pasted in the console due to indentations. You can download vertex.py from this location and save it on your system. After that, just run it using the command execfile(“Path to your file”). I saved my file in D:/Data/scripts/vertex.py, so I would run a command like execfile(“D:/Data/scripts/vertex.py”)



If the command was successful, you will see an additional column in your Attribute Table named ‘Vertices’.



The script doesn’t do much error checking or validation. Let me know if you run into problems. The code in scripy.py is as below. You can modify the code in the script to suit your needs, save it in a file and run it to perform the action.

from PyQt4.QtCore import *
layer = qgis.utils.iface.activeLayer()
layer.startEditing()
layer.dataProvider().addAttributes( [ QgsField("Vertices", QVariant.Int) ] )
provider = layer.dataProvider()
vertices_field_index = provider.fieldNameIndex("Vertices")
provider.select()
feat = QgsFeature()
vertex_count = 0
attribute_dict = {}
while provider.nextFeature(feat):
 geom = feat.geometry()
 if geom.type() == QGis.Polygon:
   layer_vertices = 0
   if geom.isMultipart():
     polygons = geom.asMultiPolygon()
   else:
     polygons = [ geom.asPolygon() ]
   for polygon in polygons:
     for ring in polygon:
       layer_vertices += len(ring)
   print "Feature %d: Vertices %d" % ( feat.id(), layer_vertices)
   vertex_count += layer_vertices
   attribute_dict[feat.id()] = { vertices_field_index: QVariant(layer_vertices) }
print "Total vertices: %d" % (vertex_count)
layer.dataProvider().changeAttributeValues(attribute_dict)
layer.commitChanges()