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.
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:
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') director = QgsLineVectorLayerDirector(layer,-1,'','','',3) director.addProperter(QgsDistanceArcProperter()) builder = QgsGraphBuilder(layer.crs()) additional_points = [QgsPoint(3.63715,3.60401),QgsPoint(3.86250,1.58906),QgsPoint(0.42913...