I have two lines that overlap, so I clip the overlap, and I should get its length in kilometers.
The clip part is ok but I can't find any resources on how to get the length. It can be printed in the console or add a field in the clipped shapefile that contains the km length. I used Calculating line lengths using PyQGIS as a guide but it didn't work.
import processing
from PyQt5.QtCore import QVariant
overlay_layer = QgsProject.instance().mapLayersByName("route2")[0]
bottom_layer = QgsProject.instance().mapLayersByName("route1")[0]
overlap = r'C:\Users\imper\Documents\python\calculate_overlap_lines\overlap.shp'
# clip overlap - works
processing.run("native:clip", {'INPUT': bottom_layer, \
'OVERLAY': overlay_layer, \
'OUTPUT': overlap})
iface.addVectorLayer(overlap, '', 'ogr')
# get length - doesn't work
overlap_lyr = QgsProject.instance().mapLayersByName("overlap")[0]
for overlap_lyr in QgsMapLayerRegistry.instance().mapLayers().values():
features = overlap_lyr.getFeatures()
for f in features:
geom = f.geometry()
leng = geom.length()
res = overlap_lyr.dataProvider().addAttributes([QgsField("Length", QVariant.Int)])
overlap_lyr.updateFields()
fieldIndex = overlap_lyr.dataProvider().fieldNameIndex("Length")
attrFeatMap = {}
attrMap = { fieldIndex : leng }
for feature in overlap_lyr.getFeatures():
attrFeatMap[ feature.id() ] = attrMap
overlap_lyr.dataProvider().changeAttributeValues(attrFeatMap)
I'm getting this error:
raise QgsProcessingException(msg) _core.QgsProcessingException: There were errors executing the algorithm.
for loop
does? I'm not yet comfortable with PyQGIS so I just copied it from the GIS SE post I linked and replaced the variables.