Calculating the length of a geometry line object using the Field Calculator gives you 2 options:

  1. $length where the length calculated by this function respects both the current project's ellipsoid setting and distance unit settings
  2. length that performs planimetric calculations in the Spatial Reference System (SRS) of this geometry, and the units of the returned length will match the units for the SRS.

Both these functions result different numbers.

That I am trying to do is to make a plugin using python that performs these 2 different functions.

With the following code I can access to geometry length of my selected features and thus the $length in my case:

layer = qgis.utils.iface.activeLayer()
features = layer.selectedFeatures()
for f in features:
    geom = f.geometry()
    print ("Length:", geom.length())

How I can access to length as well?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.