# Calculating elipsoidal length of line in PyQGIS

I am using PyQGIS with QGIS 3.

This code below is what I developed already, what I am doing wrong?

``````layer = iface.activeLayer()
feature = layer.getFeatures()

for feat in feature:
feature.setSourceCrs(##What goes here?)
print (QgsDistanceArea.measureLength(feat))
``````

If you use just `QgsDistanceArea.measureLength(feat)`, you get an error: `first argument of unbound method must have type 'QgsDistanceArea'`. That means you need to create an object of `QgsDistanceArea`

``````d = QgsDistanceArea()
``````

Then you need to set ellipsoid. Use `QgsEllipsoidUtils::acronyms()` to get a list of all known ellipsoid acronyms. If you don't set ellipsoid, calculations will be performed in plane geometry.

``````d.setEllipsoid('WGS84')
``````

Then you can use `measureLength` to get the length of the line on ellipsoid. Normally the measurement is in meters.

``````measure = d.measureLength(feat.geometry())
``````

Or you can use `measureLine` method instead of `measureLength`.

``````measure = d.measureLine(feat.geometry().asPolyline())

# If geometry type is multipolyline
measure = d.measureLine(feat.geometry().asMultiPolyline()[0])
``````

``````layer = iface.activeLayer()
features = layer.getFeatures()

d = QgsDistanceArea()
d.setSourceCrs(layer.crs(), QgsProject.instance().transformContext())
d.setEllipsoid(QgsProject.instance().ellipsoid())

for feat in features:
print((d.convertLengthMeasurement(d.measureLength(feat.geometry()), 1))*1000)
``````

One way could be:

``````d = QgsDistanceArea()
d.setEllipsoid('WGS84')
layer = iface.activeLayer()
features = layer.getFeatures()

for feat in features:
print(
d.convertAreaMeasurement(
d.measureLength(feat.geometry()),
1
)
)
``````
• It worked, but in the case i want use the projected coordinate system, what should I do? `layer = iface.activeLayer() feature = layer.getFeatures() lyr_crs = layer.crs() trans_context = QgsCoordinateTransformContext() trans_context.calculateDatumTransforms(lyr_crs, elps_crs) d = QgsDistanceArea() d.setEllipsoid('WGS84') d.setSourceCrs(lyr_crs, trans_context) for feat in feature: print(d.convertAreaMeasurement(d.measureLength(feat.geometry()),1))` Jan 17, 2020 at 22:15
• You are correct, you have to use a transformation context, check this post it will help you Calculate ellipsoidal area for a projected layer in PyQGIS 3 Jan 19, 2020 at 17:34
• `` layer = iface.activeLayer() feature = layer.getFeatures() d = QgsDistanceArea() d.setSourceCrs(layer.crs(), QgsProject.instance().transformContext()) d.setEllipsoid(QgsProject.instance().ellipsoid()) for feat in feature: print(d.convertLengthMeasurement(d.measureLength(feat.geometry()),1))`` Jan 21, 2020 at 12:20
• Thanks, the answer is below: `layer = iface.activeLayer() feature = layer.getFeatures() d = QgsDistanceArea() d.setSourceCrs(layer.crs(), QgsProject.instance().transformContext()) d.setEllipsoid(QgsProject.instance().ellipsoid()) for feat in feature: print(d.convertLengthMeasurement(d.measureLength(feat.geometry()),1))` Jan 21, 2020 at 12:23