I have a polygon layer ('anlage') and a line layer ('Linie'). I intersected these two shapefiles and created a new shapefile and a new field to calculate the length of intersected lines. But the calculated length is not correct. The coordinate reference of the layers is EPSG:25832 UTM 32N.
How can I calculate the correct length?
Here is my code:
# Input paths for the two shapefiles
anlage = "C:/shapefile/2/anlage.shp"
natur = "C:/shapefile/2/Linie.shp"
# Output path for the intersection shapefile
intersection = "C:/shapefile/2/intersection999.shp"
# Run the intersection algorithm
processing.run("qgis:intersection", {'INPUT':natur,'OVERLAY':anlage,'OUTPUT':intersection})
# Load the intersection shapefile
intersection_layer = iface.addVectorLayer(intersection, "intersection999", "ogr")
intersection_layer.startEditing()
intersection_layer.addAttribute(QgsField("length44", QVariant.Double))
for feature in intersection_layer.getFeatures():
geometry = feature.geometry()
length = geometry.length()
# Add the calculated area to the attribute table
feature.setAttribute("length44", length)
intersection_layer.updateFeature(feature)
intersection_layer.commitChanges()
the "length44"
field is the calculated length from the code and the l field is the calculated length using Geometry field calculation in QGIS.