I have setup a custom processing/python script to determine the shortest path on a network based on the code for Dijkstra's algorithm here,


Similar to the Road Graph plugin, I want to obtain the distance of the path as well. I've setup my code as below but I keep getting errors for the convertMeasurement function.

QgsDistanceArea.convertMeasurement(float, QGis.UnitType, QGis.UnitType, bool) 
-> (float, QGis.UnitType): not enough arguments See log for more details

Not sure what I'm doing wrong. I've been researching and googling this problem but can't seem to find a solution. I've included some commented out lines of code as well to show what other options I've tried.

distance_measure = QgsDistanceArea()
dist_unit = QGis.UnitType()
dist_unit_deg = QGis.UnitType(2)
print dist_unit
print dist_unit_deg
#print type(dist_unit)
#QgsDistanceArea.convertLengthMeasurement(distance_measure, dist_unit)

dist_val = distance_measure.measureLength(rb.asGeometry())
print dist_val
QgsDistanceArea.convertMeasurement(distance_measure, dist_unit_deg, dist_unit, False)
print distance_measure.lengthUnits()
#print QgsDistanceArea.formatDistance(dist_val,QGis.Meters,5)
  • 2
    QgsDistanceArea.convertMeasurement isn't a static method. Try distance_measure.convertMeasurement(...)
    – ndawson
    Commented Oct 6, 2016 at 4:08
  • 1
    The procedure for measuring length is not so complicated. Please, see my answer.
    – xunilk
    Commented Oct 6, 2016 at 10:17

1 Answer 1


By the way, the ellipsoid of 'GDA94' SRS is 'GRS80' so, you can use next code to calculate distances for one line type geometry.

layer = iface.activeLayer()

feat = layer.getFeatures().next()
geom = feat.geometry()

distance_measure = QgsDistanceArea()



print distance_measure.measureLength(geom)

To test my code, I used line shapefile (EPSG:4283 = GDA94) of next image:

enter image description here

After running the code, the length of this line was printed at the Python Console of QGIS: 1087287.01705 m (1087.28701705 km). When 'Measure Line Tool' is used, it is also visualized (see next image) that results are comparable. This code works.

enter image description here

  • Thanks @xunilk!! Worked! I'm new to GIS so getting my head around working with CRSs etc. Is there a good reference you would know especially for programmers? Do the unit measurements change according to the CRS set?
    – yohfdo
    Commented Oct 6, 2016 at 20:57
  • You're welcome. You can use: docs.qgis.org/2.2/pdf/en/…. In relation to last question, with QgsDistanceArea, you set the ellipsoid of CRS; where this should be in degrees. If you work with a projection in meters (or feet) you can use QgsGeometry class instead of QgsDistanceArea.
    – xunilk
    Commented Oct 7, 2016 at 8:03

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.