4

I want to get the length in meters of features with line-type geometry independent of the CRS of the layer and project.

Is there any way to do this?

I used the code below but I'm not sure of the unit of measurement.

p = QgsProject.instance()
layer = p.mapLayersByName('rede')[0]

d = QgsDistanceArea()
d.setEllipsoid('WGS84')

for feat in layer.getFeatures():
    print(d.convertAreaMeasurement(d.measureLength(feat.geometry()), 1)*1000)

1 Answer 1

3

You can run:

print(d.lengthUnits())

after you have defined your ellipsoid to see what the units of measurement for length are. Using your example code above, it results in DistanceUnit.Meters.

Ref: https://qgis.org/pyqgis/master/core/QgsDistanceArea.html

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.