How to create a distance matrix in QGIS

This tutorial shows GIS users how to easily create a distance matrix and calculate travel times between multiple origins and destinations using QGIS and the TravelTime plugin.


A distance matrix is ​​a two-dimensional array containing the distances between different locations. Creating a distance matrix can be beneficial for a variety of reasons. For example, they are used in transportation planning to calculate travel times for popular origin-to-destination routes. In retail store planning, they are used to calculate the travel times of customers from their home to current retailer locations, competitor locations, and prospect locations.

There are several tools available to create a distance matrix. For example, QGIS has a built-in distance matrix tool which can be found under Vector, then clicking on “Analysis Tools” and then on “Distance Matrix”. Additionally, you can search for plugins that calculate distances between locations by using the search term “distance” under “Plugins” and clicking “Manage and install plugins”.

Which tool you choose depends on the use case: for example, if you just want to calculate distances in a straight line, the QGIS Distance Matrix tool will do the trick, but to incorporate real routes, real travel times with different modes of transport, the TravelTime plugin would be the best fit. In this tutorial, we are going to calculate the travel times between multiple random origins and destinations using the TravelTime plugin, which can generate many origin-destination pairs very quickly.

How to use the plugin to create a travel time matrix

Before we can use the TravelTime plugin and generate a distance matrix, we need to create location data in the form of multiple origins and destinations. This is a two-step process of generating random UK postcodes and then geocoding them with the TravelTime plugin.

To generate a random UK origin and destinations, you can use this website and copy and paste the results into two different Notepad files: one called origins.csv with five addresses and the other called destinations.csv, containing 10 addresses. Save both files as CSV files with a header that names the address field, which is handy when importing them into QGIS later. In this tutorial, we’ll use the following five random origins:

For destinations we will use the following ten UK post codes:


Then open QGIS and create a new project file. Add a basemap of your choice to your canvas. Here we have used the Esri Standard basemap which is available through the Quick Map Services plug-in.

We are now going to geocode the addresses so that they can be placed on a map. For this part of the tutorial, you must have installed and activated the TravelTime plugin for QGIS with an API key. You can get the plugin and API key here.

After receiving the API key, click the “Configure TravelTime Platform Plugin” button to the right of the TravelTime menu in QGIS and copy-paste the app ID and API key to start using the plug-in:


This is the TravelTime menu button in QGIS to configure the plugin. (Photo: Eric van Rees)

To start geocoding, choose “Processing Toolbox” in QGIS, choose the TravelTime plugin, then “Geocoding” under “Utilities”. Make sure the input fields match the image below.


Your entry must match these settings in the QGIS TravelTime geocoding tool. (Photo: Eric van Rees)

Then click on “Execute” and repeat the same procedure for the destination table. The map below shows the origins in red and the destinations in blue, renamed “origins” and “destinations”:


The geocoded origins are in red and the destinations are in blue on the QGIS map. (Photo: Eric van Rees)

To check if the tool has geocoded all the addresses, open both attribute tables to see the number of rows. The origins table should list five rows and the destinations table should list 10.

With these geocoded addresses, we’re ready to start using the TravelTime plugin and create a distance matrix with the travel times for each origin-destination trip.

Generation of a distance matrix from public transport data

We will now generate two different travel matrices: one for public transport trips and one using driving times. Let’s start with the distance matrix for public transport.

Using public transport data, we can perform in-depth distance analysis and calculate travel times for many origin-destination routes very quickly. The distance matrix functionality can be accessed by choosing the TravelTime Toolkit under Processing Toolkit and clicking “Simplified”, then “Simple Time Filter”.


Select the Time Filter – Simple tool from the TravelTime plugin for QGIS. (Photo: Eric van Rees)

Next, select the origin and destination point entities as inputs for the tool and set the drive time to 240 minutes, which is the maximum time that the tool accepts as input.


Your TravelTime Time Filter – Simple Tool Settings window will look like this. (Photo: Eric van Rees)

Click “Run” and look at the map, where new data has been added. You will see a new output layer with “accessible” and “unreachable” destinations in the maximum driving time from the different origins.


This is the output layer of the Time Filter – Simple tool. (Photo: Eric van Rees)

A quick inspection of the map shows us three inaccessible destinations in the north of the country, which makes sense: they are probably difficult to reach in less than four hours using public transport from the specified places of origin. Then open the attribute table to see the distance matrix, which is the main output of the tool.

A quick inspection of the attribute table shows a total of 50 rows, which is the number of possible origin / destination combinations (5×10). The second column, named “accessible”, contains a 0 or 1 which is a Boolean value with 0 corresponding to False and 1 to True.


These are the first two rows of the distance matrix for the Time Filter – Simple tool. (Photo: Eric van Rees)

However, it is difficult to distinguish the individual origin-destination information from this attribute table. Using the Advanced Time Filter tool, we can specify the different addresses for the different input and output fields, which are kept in the output. For origins, specify the Start ID field as “address” and the journey time in seconds (four hours is the maximum amount, or 14,400 seconds). Leave the arrival fields open, specify the Destinations layer as the Locations parameter and click “Execute”.


The Time Filter – Advanced Tool settings window completed. (Photo: Eric van Rees)

Looking at the output attribute table, we see both the origin and destination address mentioned for each row, making it easier to see the different origin / destination pairs for each row compared to the Filter tool. temporal – Simple.


The output of the distance matrix for the time filter – advanced tool will look like this. (Photo: Eric van Rees)

Generate distance matrix using driving time data

Now we’ll create a distance matrix using the travel time data instead of the public transport data, which also returns the distance between origin and destination. Repeat the same steps as before when creating the distance matrix for public transport, but now use “drive” as the transport type.

Looking at the output, we see a new feature layer added to the map with accessible and non-accessible locations. The tool now returns the distance traveled (in meters) to a destination accessible by car, in the prop_distance column listed in the attribute table. (You have to scroll to the far right columns to be able to see it.)


The distance matrix using “drive” as the mode of transport returns the distances traveled. (Photo: Eric van Rees)

Using the Time Filter Advanced tool, we get more information about origin-destination pairs per row. To verify that the distances listed are correct, we see that the first entry points to a place of origin / destination from Birmingham to Liverpool, with a travel distance of around 160km.


(Photo: Eric van Rees)

We can verify this is correct using Google’s routing tool, which gives a corresponding drive distance by car.


Google’s routing tool returns a car trip distance similar to the TravelTime QGIS plug-in for the Birmingham-Liverpool route. (Photo: Eric van Rees)


The TravelTime plugin has the ability to create travel time and distance matrices and evaluate public transport data. In the examples above, we’ve shown that we can create multiple origins-destinations at once, as well as travel distances using the plugin’s Time Filter tool.

This information is very valuable for a number of real use cases. For example, the tool can be used to see how many employees of a company can reach a workplace from their home within a certain time frame. By adding a GIS layer of demographic data to travel times, it becomes possible to see how many people of a certain demographic are living within commuting distance of a healthcare facility or store. The add-in can also be used to analyze local amenities required by a new office location, such as the walking time to points of interest such as supermarkets, cafes, etc.

About Florence L. Silvia

Check Also

How to Stream Every ‘Matrix’ Movie in 2022

ex_artist/ The Wachowskis created a revolution in blockbuster action when they created a genre-blending adventure …

Leave a Reply

Your email address will not be published.