In theQgsDistanceArea object, PyQGIS has excellent capabilities for measuring the distance. We'll use this object for several recipes, starting with measuring the distance between two points.
Iterate through the features until you get the last one:
for f in fts: last = f
Create a measurement object:
d = QgsDistanceArea()
Measure the distance:
m = d.measureLine(first.geometry().asPoint(), last.geometry().asPoint())
Convert the measurement value from decimal degrees to meters:
d.convertMeasurement(m, 2, 0, False)
Ensure that your Python console output looks similar to this tuple:
(4401.1622240174165, 0)
How it works...
The QgsDistanceArea object accepts different types of geometry as input. In this case, we use two points. The map units for this layer are in decimal degrees, which isn't meaningful for a distance measurement. So, we use the QgsDistanceArea.convertMeasurement() method to covert the output to meters. The parameters for the method are the measurement output, the input units (in decimal degrees), the output units (meters), and a boolean to denote whether this conversion is an area calculation verses a linear measurement.
The returned tuple is the measurement value and the units. The value 0 tells us that the output is in meters.