7

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

3 Answers 3

10

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

For more information, please review QgsDistanceArea documentation.

8

This is my answer:

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

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
        )
    )
5
  • 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))
    – vitorcsm
    Commented 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
    – cabesuon
    Commented 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))``
    – vitorcsm
    Commented 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))
    – vitorcsm
    Commented Jan 21, 2020 at 12:23
  • @vitorcsm Please feel free to add your comment as the answer of your question. Commented Jan 21, 2020 at 13:20

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.