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.

enter image description here

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.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)


the "length44" field is the calculated length from the code and the l field is the calculated length using Geometry field calculation in QGIS.

  • 2
    Please Edit The question to specify what the coordinate reference of the layer is, what the "not correct" value is, and what you think the value should be. Including the actual geometry in WKT would be helpful.
    – Vince
    Jan 30 at 15:56


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.