Counting number of vertices of object on vector layer PyQGIS
Indentation.
The first part of your code is correct, but the rest can be greatly simplified if you just want the number of vertices:
layer = qgis.utils.iface.activeLayer()
feat = layer.getFeatures()
for feature in feat:
if feature.geometry().isMultipart(): # new part for multipolylines
vertices = feature.geometry().asMultiPolyline()
print [len(v) for v in vertices]
else:
vertices = feature.geometry().asPolyline()
n = len(vertices)
print n
If you also want the coordinates of the vertices, then you can write (polylines only):
layer = qgis.utils.iface.activeLayer()
feat = layer.getFeatures()
for feature in feat:
vertices = feature.geometry().asPolyline()
points = []
for v in vertices:
points.append(v)
In QGIS 2.14, a new function to count vertices is available in the field calculator
: