2

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,

http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/network_analysis.html#id4

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()
distance_measure.setSourceCrs(crs)
distance_measure.setEllipsoidalMode(True)
#distance_measure.setEllipsoid('GDA94')
dist_unit = QGis.UnitType()
dist_unit_deg = QGis.UnitType(2)
#dist_unit
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)
  • 1
    QgsDistanceArea.convertMeasurement isn't a static method. Try distance_measure.convertMeasurement(...) – ndawson Oct 6 '16 at 4:08
  • The procedure for measuring length is not so complicated. Please, see my answer. – xunilk Oct 6 '16 at 10:17
2

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

distance_measure.setEllipsoid('GRS80')

distance_measure.setEllipsoidalMode(True)

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 Oct 6 '16 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 Oct 7 '16 at 8:03

Your Answer

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

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