Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
QGIS Python Programming Cookbook

You're reading from   QGIS Python Programming Cookbook Over 140 recipes to help you turn QGIS from a desktop GIS tool into a powerful automated geospatial framework

Arrow left icon
Product type Paperback
Published in Mar 2015
Publisher
ISBN-13 9781783984985
Length 340 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Joel Lawhead Joel Lawhead
Author Profile Icon Joel Lawhead
Joel Lawhead
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Automating QGIS FREE CHAPTER 2. Querying Vector Data 3. Editing Vector Data 4. Using Raster Data 5. Creating Dynamic Maps 6. Composing Static Maps 7. Interacting with the User 8. QGIS Workflows 9. Other Tips and Tricks Index

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 http://qgis.org/api/2.6/modules.html.

Notice the version number, 2.2, in the URL. You can change this version number to the version of QGIS you are using in order to find the appropriate documentation.

The PyQGIS API documentation is not updated frequently because it is nearly identical to the structure of the C++ API. However, the QGIS project on github.com maintains a list of all the PyQGIS classes for the latest version. The PyQGIS 2.6 API is located at https://github.com/qgis/QGIS/blob/master/python/qsci_apis/Python-2.6.api.

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 in Python.

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 QgsLabel are located in:

  1. Go to the QGIS API page at http://qgis.org/api/2.6/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 QgsLabel.
  5. Click on the QgsLabel 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/Python-2.6.api.
  7. Scroll down the alphabetical class listing until you see qgis.core.QgsLabel.LabelField.

How it works…

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

  • Core
  • GUI
  • Analysis
  • Map composer
  • Network analysis

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.

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 looking in the wrong direction because of the similar keywords in different modules.

You have been reading a chapter from
QGIS Python Programming Cookbook
Published in: Mar 2015
Publisher:
ISBN-13: 9781783984985
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image