Estimation of Length of Line Features using Python Program in QGIS

Опубликовано: 22 Июль 2026
на канале: MSR Geo Spatial Solutions
1,377
37

Hi Viewers,
From this video, you will come to know the process of estimation of length of line features by using Python program in QGIS.
Find the program code here:
Steps to follow:
01. Add Vector layer
iface.addVectorLayer("d:/length/drain.shp","drainage", "ogr")
02. Active Layer
layer = iface.activeLayer()
##03. Feature details (Fields)
features = layer.getFeatures()
for f in features:
print (f.attributes()) ## RUN Python##
##04. Add Field
from PyQt5.QtCore import QVariant
layer_provider=layer.dataProvider()
layer_provider.addAttributes([QgsField("Length",QVariant.Double)])
layer.updateFields()
print (layer.fields().names())
##05. Code to update length field
features=layer.getFeatures()
for f in features:
id=f.id()
length=f.geometry().length()
attr_value={4:length}
layer_provider.changeAttributeValues({id:attr_value})
RUN Python
layer.commitChanges()