Book Image

QGIS Python Programming Cookbook

Book Image

QGIS Python Programming Cookbook

Overview of this book

Table of Contents (16 chapters)
QGIS Python Programming Cookbook
About the Author
About the Reviewers

Measuring the distance between two points

In the QgsDistanceArea 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.

Getting ready

If you don't already have the New York City Museums layer used in the previous recipes in this chapter, download the layer from

Unzip that file and place the shapefile's contents in a directory named nyc within your qgis_data directory, within your root or home directory.

How to do it...

In the following steps, we'll extract the first and last points in the layer's point order and measure their distance:

  1. First, import the library that contains the QGIS contents:

    from qgis.core import QGis
  2. Then, load the layer:

    lyr = QgsVectorLayer("/qgis_data/nyc/NYC_MUSEUMS_GEO.shp", "Museums", "ogr")
  3. Access the features:

    fts = lyr.getFeatures()
  4. Get the first feature:

    first =
  5. Set a placeholder...