Measuring distance along a line
In this recipe, we'll measure the distance along a line with multiple vertices.
Getting ready
For this recipe, we'll use a line shapefile with two features. You can download the shapefile as a .zip
file from https://github.com/GeospatialPython/Learn/raw/master/paths.zip.
Unzip the shapefile into a directory named qgis_data/shapes
within your root or home directory.
How to do it...
The steps for this recipe are fairly straightforward. We'll extract the geometry from the first line feature and pass it to the measurement object, as shown here:
First, we must load the QGIS constants library:
from qgis.core import QGis
Load the line layer:
lyr = QgsVectorLayer("/qgis_data/shapes/paths.shp", "Route", "ogr")
Grab the features:
fts = lyr.getFeatures()
Get the first feature:
route = fts.next()
Create the measurement object instance:
d = QgsDistanceArea()
Then, we must configure the
QgsDistanceArea
object to...