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 https://github.com/GeospatialPython/Learn/raw/master/NYC_MUSEUMS_GEO.zip.

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 the distance between them:

  1. First, import the ...

Get QGIS Python Programming Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.