Book Image

QGIS 2 Cookbook

By : Mandel, Olaya Ferrero, Anita Graser
Book Image

QGIS 2 Cookbook

By: Mandel, Olaya Ferrero, Anita Graser

Overview of this book

QGIS is a user-friendly, cross-platform desktop geographic information system used to make maps and analyze spatial data. QGIS allows users to understand, question, interpret, and visualize spatial data in many ways that reveal relationships, patterns, and trends in the form of maps. This book is a collection of simple to advanced techniques that are needed in everyday geospatial work, and shows how to accomplish them with QGIS. You will begin by understanding the different types of data management techniques, as well as how data exploration works. You will then learn how to perform classic vector and raster analysis with QGIS, apart from creating time-based visualizations. Finally, you will learn how to create interactive and visually appealing maps with custom cartography. By the end of this book, you will have all the necessary knowledge to handle spatial data management, exploration, and visualization tasks in QGIS.
Table of Contents (14 chapters)
13
Index

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')
    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...