- QGIS Python Programming Cookbook
- Joel Lawhead
- 427字
- 2021-07-23 19:48:51
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:
- Go to the QGIS API page at http://qgis.org/api/2.6/index.html.
- Click on the Modules tab.
- Click on the link QGIS Core Library.
- Scroll down the list of modules in alphabetical order until you see QgsLabel.
- Click on the QgsLabel link to access the label object documentation.
- Now, go to the PyQGIS API listing at https://github.com/qgis/QGIS/blob/master/python/qsci_apis/Python-2.6.api.
- 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.
- Java 9 Concurrency Cookbook(Second Edition)
- CentOS 7 Linux Server Cookbook(Second Edition)
- C語(yǔ)言程序設(shè)計(jì)實(shí)訓(xùn)教程
- INSTANT Weka How-to
- Mastering Kali Linux for Web Penetration Testing
- 小程序,巧運(yùn)營(yíng):微信小程序運(yùn)營(yíng)招式大全
- Learning Hunk
- Android驅(qū)動(dòng)開發(fā)權(quán)威指南
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)項(xiàng)目化教程
- Xcode 6 Essentials
- 深入解析Java編譯器:源碼剖析與實(shí)例詳解
- C語(yǔ)言程序設(shè)計(jì)實(shí)踐
- Python Digital Forensics Cookbook
- Spark技術(shù)內(nèi)幕:深入解析Spark內(nèi)核架構(gòu)設(shè)計(jì)與實(shí)現(xiàn)原理
- 信息安全技術(shù)(第2版)