Book Image

QGIS Python Programming Cookbook, Second Edition - Second Edition

By : Joel Lawhead
Book Image

QGIS Python Programming Cookbook, Second Edition - Second Edition

By: Joel Lawhead

Overview of this book

QGIS is a desktop geographic information system that facilitates data viewing, editing, and analysis. Paired with the most efficient scripting language—Python, we can write effective scripts that extend the core functionality of QGIS. Based on version QGIS 2.18, this book will teach you how to write Python code that works with spatial data to automate geoprocessing tasks in QGIS. It will cover topics such as querying and editing vector data and using raster data. You will also learn to create, edit, and optimize a vector layer for faster queries, reproject a vector layer, reduce the number of vertices in a vector layer without losing critical data, and convert a raster to a vector. Following this, you will work through recipes that will help you compose static maps, create heavily customized maps, and add specialized labels and annotations. As well as this, we’ll also share a few tips and tricks based on different aspects of QGIS.
Table of Contents (16 chapters)
QGIS Python Programming Cookbook - Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Navigating the PyQGIS API


The QGIS Python API, also known as PyQGIS, allows you to control virtually every aspect of QGIS. The ability to find the PyQGIS object you need in order to access a particular feature of QGIS is critical to automation.

Getting ready

The PyQGIS API is based on the QGIS C++ API. The C++ API is kept up to date online and is well documented.

Note

The QGIS API's web page is located at https://qgis.org/api/modules.html.

The PyQGIS API documentation is not updated frequently because it is nearly identical to the structure of the C++ API. A tool named SWIG, which creates wrappers for multiple languages for C++ libraries, is used to create the PyQGIS API, making the documentation for the C++ API highly applicable for argument types and order. However, the QGIS project on https://github.com/ maintains a list of all the PyQGIS classes for the latest version. The PyQGIS APIs for different versions are located at https://github.com/qgis/QGIS/blob/master/python/qsci_apis/.

You can locate the documented class in the main C++ API and read about it. Then, look up the corresponding Python module and class using the PyQGIS API listing. In most cases, the C++ API name for a class is identical to that in Python. You should note that the PyQGIS versions are tracked separately from the QGIS version and have different version numbers.

In this recipe, we'll locate the PyQGIS class that controls labels in QGIS.

How to do it...

We will perform the following steps to see in which PyQGIS module the QGIS Label object and Qgs Label are located:

  1. Go to the QGIS API page at http://qgis.org/api/index.html.

  2. Click on the Modules tab.

  3. Click on the link QGIS core library.

  4. Scroll down the list of modules in alphabetical order until you see QgsPoint.

  5. Click on the QgsPoint link to access the label object documentation.

  6. Now go to the PyQGIS API listing at https://github.com/qgis/QGIS/blob/master/python/qsci_apis/PyQGIS-2.2.api.

  7. Scroll down the alphabetical class listing until you see qgis.core.QgsPoint?1().

How it works...

The QGIS API is divided into five distinct categories, as follows:

  • Core

  • GUI

  • Network analysis

  • Server

  • Plugins

Most of the time, it's easy to find the class that targets the functionality you need, with most of QGIS being contained in the catch-all core module. The more you use the API, the quicker you'll be able to locate the objects you need for your scripts. You'll also notice that the API file contains the methods for each class with the type of parameters the class accepts.

There's more...

If you're having trouble locating a class containing the keyword you need, you can use the search engine on the QGIS API website.

Tip

Beware, however, that the results returned by this search engine may contain items you don't need and can even send you in the wrong direction because of the use of similar keywords in different modules.