Matching points to the nearest line

In this recipe, we will use the QGIS network analysis library from Python console to match points to the nearest line. This is the simplest form of what is also known as map matching.

Getting ready

To follow this recipe, load network_pgr.shp from the sample data.

How to do it…

The following script will match three points, QgsPoint(3.63715,3.60401), QgsPoint(3.86250,1.58906), and QgsPoint(0.42913,2.26512), to the network:

  1. Open Python console and its editor and then load or paste the following network_analysis_match_points.py script:
    import processing from processing.tools.vector import VectorWriter from PyQt4.QtCore import * from qgis.core import * from qgis.networkanalysis import * layer = processing.getObject('network_pgr') ...

Get QGIS: Becoming a GIS Power User 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.