QGIS Testing PyQGISDeveloperCookbook en
QGIS Testing PyQGISDeveloperCookbook en
QGIS Testing PyQGISDeveloperCookbook en
QGIS Project
1 Introduction 3
1.1 Scripting in the Python Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Python Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.1 Processing Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Running Python code when QGIS starts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3.1 The startup.py file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3.2 The PYQGIS_STARTUP environment variable . . . . . . . . . . . . . . . . . . . . . . 5
1.3.3 The --code parameter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3.4 Additional arguments for Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4 Python Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4.1 Using PyQGIS in standalone scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4.2 Using PyQGIS in custom applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.4.3 Running Custom Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.5 Technical notes on PyQt and SIP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2 Loading Projects 9
2.1 Resolving bad paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.2 Using flags to speed up things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3 Loading Layers 13
3.1 Vector Layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2 Raster Layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.3 QgsProject instance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
i
6.4.2 Delete Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
6.4.3 Modify Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
6.4.4 Modifying Vector Layers with an Editing Buffer . . . . . . . . . . . . . . . . . . . . . . 33
6.4.5 Adding and Removing Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
6.5 Using Spatial Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.6 The QgsVectorLayerUtils class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
6.7 Creating Vector Layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
6.7.1 From an instance of QgsVectorFileWriter . . . . . . . . . . . . . . . . . . . . . 36
6.7.2 Directly from features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
6.7.3 From an instance of QgsVectorLayer . . . . . . . . . . . . . . . . . . . . . . . . . 39
6.8 Appearance (Symbology) of Vector Layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
6.8.1 Single Symbol Renderer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
6.8.2 Categorized Symbol Renderer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
6.8.3 Graduated Symbol Renderer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
6.8.4 Working with Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
6.8.5 Creating Custom Renderers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
6.9 Further Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
7 Geometry Handling 49
7.1 Geometry Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
7.2 Access to Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
7.3 Geometry Predicates and Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
8 Projections Support 55
8.1 Coordinate reference systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
8.2 CRS Transformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
ii
13.3.2 The python built in logging module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
14 Authentication infrastructure 87
14.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
14.2 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
14.3 QgsAuthManager the entry point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
14.3.1 Init the manager and set the master password . . . . . . . . . . . . . . . . . . . . . . . . 88
14.3.2 Populate authdb with a new Authentication Configuration entry . . . . . . . . . . . . . . 89
14.3.3 Remove an entry from authdb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
14.3.4 Leave authcfg expansion to QgsAuthManager . . . . . . . . . . . . . . . . . . . . . . . . 90
14.4 Adapt plugins to use Authentication infrastructure . . . . . . . . . . . . . . . . . . . . . . . . . . 91
14.5 Authentication GUIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
14.5.1 GUI to select credentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
14.5.2 Authentication Editor GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
14.5.3 Authorities Editor GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
iii
19.3.1 Finding shortest paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
19.3.2 Areas of availability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
iv
PyQGIS testing developer cookbook
CONTENTS 1
PyQGIS testing developer cookbook
2 CONTENTS
CHAPTER
ONE
INTRODUCTION
This document is intended to be both a tutorial and a reference guide. While it does not list all possible use cases, it
should give a good overview of the principal functionality.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documenta-
tion License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections,
no Front-Cover Texts, and no Back-Cover Texts.
A copy of the license is included in the section gnu_fdl.
This license also applies to all code snippets in this document.
Python support was first introduced in QGIS 0.9. There are several ways to use Python in QGIS Desktop (covered
in the following sections):
• Issue commands in the Python console within QGIS
• Create and use plugins
• Automatically run Python code when QGIS starts
• Create processing algorithms
• Create functions for expressions in QGIS
• Create custom applications based on the QGIS API
Python bindings are also available for QGIS Server, including Python plugins (see QGIS Server and Python) and
Python bindings that can be used to embed QGIS Server into a Python application.
There is a complete QGIS C++ API reference that documents the classes from the QGIS libraries. The Pythonic
QGIS API (pyqgis) is nearly identical to the C++ API.
Another good resource for learning how to perform common tasks is to download existing plugins from the plugin
repository and examine their code.
QGIS provides an integrated Python console for scripting. It can be opened from the Plugins ► Python Console menu:
3
PyQGIS testing developer cookbook
The screenshot above illustrates how to get the layer currently selected in the layer list, show its ID and optionally, if
it is a vector layer, show the feature count. For interaction with the QGIS environment, there is an iface variable,
which is an instance of QgisInterface. This interface allows access to the map canvas, menus, toolbars and
other parts of the QGIS application.
For user convenience, the following statements are executed when the console is started (in the future it will be
possible to set further initial commands)
For those which use the console often, it may be useful to set a shortcut for triggering the console (within Settings ►
Keyboard shortcuts…)
The functionality of QGIS can be extended using plugins. Plugins can be written in Python. The main advantage
over C++ plugins is simplicity of distribution (no compiling for each platform) and easier development.
Many plugins covering various functionality have been written since the introduction of Python support. The plugin
installer allows users to easily fetch, upgrade and remove Python plugins. See the Python Plugins page for more
information about plugins and plugin development.
Creating plugins in Python is simple, see Developing Python Plugins for detailed instructions.
Note: Python plugins are also available for QGIS server. See QGIS Server and Python for further details.
4 Chapter 1. Introduction
PyQGIS testing developer cookbook
Processing Plugins can be used to process data. They are easier to develop, more specific and more lightweight than
Python Plugins. Writing a Processing plugin explains when the use of Processing algorithms is appropiate and how to
develop them.
There are different methods to run Python code every time QGIS starts.
1. Creating a startup.py script
2. Setting the PYQGIS_STARTUP environment variable to an existing Python file
3. Specifying a startup script using the --code init_qgis.py parameter.
Every time QGIS starts, the user’s Python home directory and a list of system paths are searched for a file named
startup.py. If that file exists, it is executed by the embedded Python interpreter.
The path in the user’s home directory usually is found under:
• Linux: .local/share/QGIS/QGIS3
• Windows: AppData\Roaming\QGIS\QGIS3
• macOS: Library/Application Support/QGIS/QGIS3
The default system paths depend on the operating system. To find the paths that work for you, open the Python
Console and run QStandardPaths.standardLocations(QStandardPaths.AppDataLocation)
to see the list of default directories.
The startup.py script is executed immediately upon initializing python in QGIS, early on in the start of the
application.
You can run Python code just before QGIS initialization completes by setting the PYQGIS_STARTUP environment
variable to the path of an existing Python file.
This code will run before QGIS initialization is complete. This method is very useful for cleaning sys.path, which
may have undesireable paths, or for isolating/loading the initial environment without requiring a virtual environment,
e.g. homebrew or MacPorts installs on Mac.
You can provide custom code to execute as startup parameter to QGIS. To do so, create a python file, for example
qgis_init.py, to execute and start QGIS from the command line using qgis --code qgis_init.py.
Code provided via --code is executed late in the QGIS initialization phase, after the application components have
been loaded.
To provide additional arguments for your --code script or for other python code that is executed, you can use the
--py-args argument. Any argument coming after --py-args and before a -- arg (if present) will be passed
to Python but ignored by the QGIS application itself.
In the following example, myfile.tif will be available via sys.argv in Python but will not be loaded by QGIS.
Whereas otherfile.tif will be loaded by QGIS but is not present in sys.argv.
If you want access to every command line parameter from within Python, you can use QCoreApplication.
arguments()
QgsApplication.instance().arguments()
It is often handy to create scripts for automating processes. With PyQGIS, this is perfectly possible — import the
qgis.core module, initialize it and you are ready for the processing.
Or you may want to create an interactive application that uses GIS functionality — perform measurements, export a
map as PDF, … The qgis.gui module provides various GUI components, most notably the map canvas widget that
can be incorporated into the application with support for zooming, panning and/or any further custom map tools.
PyQGIS custom applications or standalone scripts must be configured to locate the QGIS resources, such as projection
information and providers for reading vector and raster layers. QGIS Resources are initialized by adding a few lines
to the beginning of your application or script. The code to initialize QGIS for custom applications and standalone
scripts is similar. Examples of each are provided below.
Note: Do not use qgis.py as a name for your script. Python will not be able to import the bindings as the script’s
name will shadow them.
To start a standalone script, initialize the QGIS resources at the beginning of the script:
10 # Load providers
11 qgs.initQgis()
12
6 Chapter 1. Introduction
PyQGIS testing developer cookbook
First we import the qgis.core module and configure the prefix path. The prefix path is the location where QGIS is
installed on your system. It is configured in the script by calling the setPrefixPath() method. The second
argument of setPrefixPath() is set to True, specifying that default paths are to be used.
The QGIS install path varies by platform; the easiest way to find it for your system is to use the Scripting in the Python
Console from within QGIS and look at the output from running:
QgsApplication.prefixPath()
After the prefix path is configured, we save a reference to QgsApplication in the variable qgs. The second
argument is set to False, specifying that we do not plan to use the GUI since we are writing a standalone script. With
QgsApplication configured, we load the QGIS data providers and layer registry by calling the initQgis()
method.
qgs.initQgis()
With QGIS initialized, we are ready to write the rest of the script. Finally, we wrap up by calling exitQgis() to
remove the data providers and layer registry from memory.
qgs.exitQgis()
The only difference between Using PyQGIS in standalone scripts and a custom PyQGIS application is the second
argument when instantiating the QgsApplication. Pass True instead of False to indicate that we plan to use
a GUI.
12 # load providers
13 qgs.initQgis()
14
Now you can work with the QGIS API - load layers and do some processing or fire up a GUI with a map canvas. The
possibilities are endless :-)
You need to tell your system where to search for QGIS libraries and appropriate Python modules if they are not in a
well-known location - otherwise Python will complain:
This can be fixed by setting the PYTHONPATH environment variable. In the following commands, <qgispath>
should be replaced with your actual QGIS installation path:
• on Linux: export PYTHONPATH=/<qgispath>/share/qgis/python
• on Windows: set PYTHONPATH=c:\<qgispath>\python
• on macOS: export PYTHONPATH=/<qgispath>/Contents/Resources/python
Now, the path to the PyQGIS modules is known, but they depend on the qgis_core and qgis_gui libraries (the
Python modules serve only as wrappers). The path to these libraries may be unknown to the operating system, and
then you will get an import error again (the message might vary depending on the system):
Fix this by adding the directories where the QGIS libraries reside to the search path of the dynamic linker:
• on Linux: export LD_LIBRARY_PATH=/<qgispath>/lib
• on Windows: set PATH=C:\<qgispath>\bin;C:\<qgispath>\apps\<qgisrelease>\
bin;%PATH% where <qgisrelease> should be replaced with the type of release you are targeting (eg,
qgis-ltr, qgis, qgis-dev)
These commands can be put into a bootstrap script that will take care of the startup. When deploying custom appli-
cations using PyQGIS, there are usually two possibilities:
• require the user to install QGIS prior to installing your application. The application installer should look for
default locations of QGIS libraries and allow the user to set the path if not found. This approach has the
advantage of being simpler, however it requires the user to do more steps.
• package QGIS together with your application. Releasing the application may be more challenging and the
package will be larger, but the user will be saved from the burden of downloading and installing additional
pieces of software.
The two deployment models can be mixed. You can provide a standalone applications on Windows and macOS, but
for Linux leave the installation of GIS up to the user and his package manager.
We’ve decided for Python as it’s one of the most favoured languages for scripting. PyQGIS bindings in QGIS 3
depend on SIP and PyQt5. The reason for using SIP instead of the more widely used SWIG is that the QGIS code
depends on Qt libraries. Python bindings for Qt (PyQt) are done using SIP and this allows seamless integration of
PyQGIS with PyQt.
8 Chapter 1. Introduction
CHAPTER
TWO
LOADING PROJECTS
Hint: The code snippets on this page need the following imports if you’re outside the pyqgis console:
Sometimes you need to load an existing project from a plugin or (more often) when developing a standalone QGIS
Python application (see: Python Applications).
To load a project into the current QGIS application you need to create an instance of the QgsProject class. This
is a singleton class, so you must use its instance() method to do it. You can call its read() method, passing
the path of the project to be loaded:
1 # If you are not inside a QGIS console you first need to import
2 # qgis and PyQt classes you will use in this script as shown below:
3 from qgis.core import QgsProject
4 # Get the project instance
5 project = QgsProject.instance()
6 # Print the current project file name (might be empty in case no projects have␣
,→been loaded)
7 # print(project.fileName())
8
testdata/01_project.qgs
If you need to make modifications to the project (for example to add or remove some layers) and save your changes,
call the write() method of your project instance. The write() method also accepts an optional path for saving
the project to a new location:
Both read() and write() functions return a boolean value that you can use to check if the operation was suc-
cessful.
9
PyQGIS testing developer cookbook
Note: If you are writing a QGIS standalone application, in order to synchronise the loaded project with the canvas
you need to instantiate a QgsLayerTreeMapCanvasBridge as in the example below:
bridge = QgsLayerTreeMapCanvasBridge( \
QgsProject.instance().layerTreeRoot(), canvas)
# Now you can safely load your project and see it in the canvas
project.read('testdata/my_new_qgis_project.qgs')
It can happen that layers loaded in the project are moved to another location. When the project is loaded again all the
layer paths are broken. The QgsPathResolver class helps you rewrite layers path within the project.
Its setPathPreprocessor() method allows setting a custom path pre-processor function to manipulate paths
and data sources prior to resolving them to file references or layer sources.
The processor function must accept a single string argument (representing the original file path or data source) and
return a processed version of this path. The path pre-processor function is called before any bad layer handler. If
multiple preprocessors are set, they will be called in sequence based on the order in which they were originally set.
Some use cases:
1. replace an outdated path:
def my_processor(path):
return path.replace('c:/Users/ClintBarton/Documents/Projects', 'x:/
,→Projects/')
QgsPathResolver.setPathPreprocessor(my_processor)
def my_processor(path):
return path.replace('host=10.1.1.115', 'host=10.1.1.116')
QgsPathResolver.setPathPreprocessor(my_processor)
1 def my_processor(path):
2 path= path.replace("user='gis_team'", "user='team_awesome'")
3 path = path.replace("password='cats'", "password='g7as!m*'")
4 return path
5
6 QgsPathResolver.setPathPreprocessor(my_processor)
def my_processor(path):
return path.replace('c:/Users/ClintBarton/Documents/Projects', '$projectdir$')
QgsPathResolver.setPathWriter(my_processor)
Both methods return an id that can be used to remove the pre-processor or writer they added. See removePath-
Preprocessor() and removePathWriter().
In some instances where you may not need to use a fully functional project, but only want to access it for a specific
reason, flags may be helpful. A full list of flags is available under ProjectReadFlag. Multiple flags can be added
together.
As an example, if we do not care about actual layers and data and simply want to access a project (e.g. for layout or
3D view settings), we can use DontResolveLayers flag to bypass the data validation step and prevent the bad
layer dialog from appearing. The following can be done:
readflags = Qgis.ProjectReadFlags()
readflags |= Qgis.ProjectReadFlag.DontResolveLayers
project = QgsProject()
project.read('C:/Users/ClintBarton/Documents/Projects/mysweetproject.qgs',␣
,→readflags)
To add more flags the python Bitwise OR operator (|) must be used.
THREE
LOADING LAYERS
Hint: The code snippets on this page need the following imports:
Let’s open some layers with data. QGIS recognizes vector and raster layers. Additionally, custom layer types are
available, but we are not going to discuss them here.
To create and add a vector layer instance to the project, specify the layer’s data source identifier, name for the layer
and provider’s name:
The data source identifier is a string and it is specific to each vector data provider. Layer’s name is used in the layer
list widget. It is important to check whether the layer has been loaded successfully. If it was not, an invalid layer
instance is returned.
For a geopackage vector layer:
13
PyQGIS testing developer cookbook
The quickest way to open and display a vector layer in QGIS is the addVectorLayer() method of the Qgis-
Interface:
vlayer = iface.addVectorLayer(path_to_airports_layer, "Airports layer", "ogr")
if not vlayer:
print("Layer failed to load!")
This creates a new layer and adds it to the current QGIS project (making it appear in the layer list) in one step. The
function returns the layer instance or None if the layer couldn’t be loaded.
The following list shows how to access various data sources using vector data providers:
• GDAL library (Shapefile and many other file formats) — data source is the path to the file:
– for Shapefile:
vlayer = QgsVectorLayer("testdata/airports.shp", "layer_name_you_like",
,→"ogr")
QgsProject.instance().addMapLayer(vlayer)
• PostGIS database - data source is a string with all information needed to create a connection to PostgreSQL
database.
QgsDataSourceUri class can generate this string for you. Note that QGIS has to be compiled with Postgres
support, otherwise this provider isn’t available:
1 uri = QgsDataSourceUri()
2 # set host name, port, database name, username and password
3 uri.setConnection("localhost", "5432", "dbname", "johny", "xxx")
4 # set database schema, table name, geometry column and optionally
5 # subset (WHERE clause)
6 uri.setDataSource("public", "roads", "the_geom", "cityid = 2643", "primary_key_
,→field")
Note: The False argument passed to uri.uri(False) prevents the expansion of the authentication
configuration parameters, if you are not using any authentication configuration this argument does not make
any difference.
• CSV or other delimited text files — to open a file with a semicolon as a delimiter, with field “x” for X coordinate
and field “y” for Y coordinate you would use something like this:
uri = "file://{}/testdata/delimited_xy.csv?delimiter={}&xField={}&yField={}".
,→format(os.getcwd(), ";", "x", "y")
Note: The provider string is structured as a URL, so the path must be prefixed with file://. Also it allows
WKT (well known text) formatted geometries as an alternative to x and y fields, and allows the coordinate
reference system to be specified. For example:
uri = "file:///some/path/file.csv?delimiter={}&crs=epsg:4723&wktField={}".
,→format(";", "shape")
• GPX files — the “gpx” data provider reads tracks, routes and waypoints from gpx files. To open a file, the type
(track/route/waypoint) needs to be specified as part of the url:
uri = "testdata/layers.gpx?type=track"
vlayer = QgsVectorLayer(uri, "layer name you like", "gpx")
QgsProject.instance().addMapLayer(vlayer)
• SpatiaLite database — Similarly to PostGIS databases, QgsDataSourceUri can be used for generation of
data source identifier:
1 uri = QgsDataSourceUri()
2 uri.setDatabase('/home/martin/test-2.3.sqlite')
3 schema = ''
4 table = 'Towns'
5 geom_column = 'Geometry'
6 uri.setDataSource(schema, table, geom_column)
7
8 display_name = 'Towns'
9 vlayer = QgsVectorLayer(uri.uri(), display_name, 'spatialite')
10 QgsProject.instance().addMapLayer(vlayer)
• MySQL WKB-based geometries, through GDAL — data source is the connection string to the table:
uri = "MySQL:dbname,host=localhost,port=3306,user=root,
,→password=xxx|layername=my_table"
• WFS connection: the connection is defined with a URI and using the WFS provider:
uri = "https://demo.mapserver.org/cgi-bin/wfs?service=WFS&version=2.0.0&
,→request=GetFeature&typename=ms:cities"
1 import urllib
2
3 params = {
4 'service': 'WFS',
5 'version': '2.0.0',
6 'request': 'GetFeature',
7 'typename': 'ms:cities',
8 'srsname': "EPSG:4326"
9 }
10 uri2 = 'https://demo.mapserver.org/cgi-bin/wfs?' + urllib.parse.unquote(urllib.
,→parse.urlencode(params))
Note: You can change the data source of an existing layer by calling setDataSource() on a QgsVector-
Layer instance, as in the following example:
1 uri = "https://demo.mapserver.org/cgi-bin/wfs?service=WFS&version=2.0.0&
,→request=GetFeature&typename=ms:cities"
2 provider_options = QgsDataProvider.ProviderOptions()
3 # Use project's transform context
4 provider_options.transformContext = QgsProject.instance().transformContext()
(continues on next page)
7 del(vlayer)
For accessing raster files, GDAL library is used. It supports a wide range of file formats. In case you have troubles
with opening some files, check whether your GDAL has support for the particular format (not all formats are available
by default). To load a raster from a file, specify its filename and display name:
8 if not rlayer.isValid():
9 print("Layer failed to load!")
Similarly to vector layers, raster layers can be loaded using the addRasterLayer function of the QgisInterface
object:
This creates a new layer and adds it to the current project (making it appear in the layer list) in one step.
To load a PostGIS raster:
PostGIS rasters, similar to PostGIS vectors, can be added to a project using a URI string. It is efficient to keep a
reusable dictionary of strings for the database connection parameters. This makes it easy to edit the dictionary for the
applicable connection. The dictionary is then encoded into a URI using the ‘postgresraster’ provider metadata object.
After that the raster can be added to the project.
1 uri_config = {
2 # database parameters
3 'dbname':'gis_db', # The PostgreSQL database to connect to.
4 'host':'localhost', # The host IP address or localhost.
5 'port':'5432', # The port to connect on.
6 'sslmode':QgsDataSourceUri.SslDisable, # SslAllow, SslPrefer, SslRequire,␣
,→SslVerifyCa, SslVerifyFull
7 # user and password are not needed if stored in the authcfg or service
8 'authcfg':'QconfigId', # The QGIS athentication database ID holding␣
,→connection details.
27 }
28 # remove any NULL parameters
29 uri_config = {key:val for key, val in uri_config.items() if val is not None}
30 # get the metadata for the raster provider and configure the URI
31 md = QgsProviderRegistry.instance().providerMetadata('postgresraster')
32 uri = QgsDataSourceUri(md.encodeUri(uri_config))
33
layer_name = 'modis'
url = "https://demo.mapserver.org/cgi-bin/wcs?identifier={}".format(layer_name)
rlayer = QgsRasterLayer(uri, 'my wcs layer', 'wcs')
Here is a description of the parameters that the WCS URI can contain:
WCS URI is composed of key=value pairs separated by &. It is the same format like query string in URL, encoded
the same way. QgsDataSourceUri should be used to construct the URI to ensure that special characters are
encoded properly.
• url (required) : WCS Server URL. Do not use VERSION in URL, because each version of WCS is using
different parameter name for GetCapabilities version, see param version.
• identifier (required) : Coverage name
• time (optional) : time position or time period (beginPosition/endPosition[/timeResolution])
• format (optional) : Supported format name. Default is the first supported format with tif in name or the first
supported format.
• crs (optional) : CRS in form AUTHORITY:ID, e.g. EPSG:4326. Default is EPSG:4326 if supported or the
first supported CRS.
• username (optional) : Username for basic authentication.
• password (optional) : Password for basic authentication.
• IgnoreGetMapUrl (optional, hack) : If specified (set to 1), ignore GetCoverage URL advertised by GetCa-
pabilities. May be necessary if a server is not configured properly.
• InvertAxisOrientation (optional, hack) : If specified (set to 1), switch axis in GetCoverage request. May be
necessary for geographic CRS if a server is using wrong axis order.
• IgnoreAxisOrientation (optional, hack) : If specified (set to 1), do not invert axis orientation according to
WCS standard for geographic CRS.
• cache (optional) : cache load control, as described in QNetworkRequest::CacheLoadControl, but request is re-
send as PreferCache if failed with AlwaysCache. Allowed values: AlwaysCache, PreferCache, PreferNetwork,
AlwaysNetwork. Default is AlwaysCache.
Alternatively you can load a raster layer from WMS server. However currently it’s not possible to access GetCapa-
bilities response from API — you have to know what layers you want:
urlWithParams = "crs=EPSG:4326&format=image/png&layers=continents&styles&
,→url=https://demo.mapserver.org/cgi-bin/wms"
If you would like to use the opened layers for rendering, do not forget to add them to the QgsProject instance.
The QgsProject instance takes ownership of layers and they can be later accessed from any part of the application
by their unique ID. When the layer is removed from the project, it gets deleted, too. Layers can be removed by the
user in the QGIS interface, or via Python using the removeMapLayer() method.
Adding a layer to the current project is done using the addMapLayer() method:
QgsProject.instance().addMapLayer(rlayer)
# QgsProject.instance().removeMapLayer(layer_id)
QgsProject.instance().removeMapLayer(rlayer.id())
In the above code, the layer id is passed (you can get it calling the id() method of the layer), but you can also pass
the layer object itself.
For a list of loaded layers and layer ids, use the mapLayers() method:
QgsProject.instance().mapLayers()
FOUR
Hint: The code snippets on this page need the following imports if you’re outside the pyqgis console:
You can use different classes to access all the loaded layers in the TOC and use them to retrieve information:
• QgsProject
• QgsLayerTreeGroup
You can use QgsProject to retrieve information about the TOC and all the layers loaded.
You have to create an instance of QgsProject and use its methods to get the loaded layers.
The main method is mapLayers(). It will return a dictionary of the loaded layers:
layers = QgsProject.instance().mapLayers()
print(layers)
The dictionary keys are the unique layer ids while the values are the related objects.
It is now straightforward to obtain any other information about the layers:
8 print(layers_list)
You can also query the TOC using the name of the layer:
19
PyQGIS testing developer cookbook
country_layer = QgsProject.instance().mapLayersByName("countries")[0]
Note: A list with all the matching layers is returned, so we index with [0] to get the first layer with this name.
The layer tree is a classical tree structure built of nodes. There are currently two types of nodes: group nodes
(QgsLayerTreeGroup) and layer nodes (QgsLayerTreeLayer).
Note: for more information you can read these blog posts of Martin Dobias: Part 1 Part 2 Part 3
The project layer tree can be accessed easily with the method layerTreeRoot() of the QgsProject class:
root = QgsProject.instance().layerTreeRoot()
root.children()
A list of direct children is returned. Sub group children should be accessed from their own direct parent.
We can retrieve one of the children:
child0 = root.children()[0]
print(child0)
<QgsLayerTreeLayer: countries>
ids = root.findLayerIds()
# access the first layer of the ids list
root.findLayer(ids[0])
root.findGroup('Group Name')
QgsLayerTreeGroup has many other useful methods that can be used to obtain more information about the
TOC:
Now let’s add some layers to the project’s layer tree. There are two ways of doing that:
1. Explicit addition using the addLayer() or insertLayer() functions:
2. Implicit addition: since the project’s layer tree is connected to the layer registry it is enough to add a layer to
the map layer registry:
QgsProject.instance().addMapLayer(layer1)
Groups can be added with the addGroup() method. In the example below, the former will add a group to the end
of the TOC while for the latter you can add another group within an existing one:
node_group1 = root.addGroup('Simple Group')
# add a sub-group to Simple Group
node_subgroup1 = node_group1.addGroup("I'm a sub group")
Some other methods that can be used to modify the groups and layers:
1 node_group1 = root.findGroup("Group1")
2 # change the name of the group
3 node_group1.setName("Group X")
4 node_layer2 = root.findLayer(country_layer.id())
5 # change the name of the layer
6 node_layer2.setName("Layer X")
7 # change the visibility of a layer
8 node_group1.setItemVisibilityChecked(True)
9 node_layer2.setItemVisibilityChecked(False)
10 # expand/collapse the group view
11 node_group1.setExpanded(True)
12 node_group1.setExpanded(False)
FIVE
Hint: The code snippets on this page need the following imports if you’re outside the pyqgis console:
A raster layer consists of one or more raster bands — referred to as single band and multi band rasters. One band
represents a matrix of values. A color image (e.g. aerial photo) is a raster consisting of red, blue and green bands.
Single band rasters typically represent either continuous variables (e.g. elevation) or discrete variables (e.g. land use).
In some cases, a raster layer comes with a palette and the raster values refer to the colors stored in the palette.
The following code assumes rlayer is a QgsRasterLayer object.
rlayer = QgsProject.instance().mapLayersByName('srtm')[0]
# get the resolution of the raster in layer unit
print(rlayer.width(), rlayer.height())
919 619
23
PyQGIS testing developer cookbook
20.0685680819999988,-34.2700107699999990 : 20.8394528430000001,-33.7507750070000014
print(rlayer.rasterType())
RasterLayerType.GrayOrUndefined
Band 1: Height
5.2 Renderer
When a raster layer is loaded, it gets a default renderer based on its type. It can be altered either in the layer properties
or programmatically.
To query the current renderer:
print(rlayer.renderer())
print(rlayer.renderer().type())
singlebandgray
To set a renderer, use the setRenderer() method of QgsRasterLayer. There are a number of renderer
classes (derived from QgsRasterRenderer):
• QgsHillshadeRenderer
• QgsMultiBandColorRenderer
• QgsPalettedRasterRenderer
• QgsRasterContourRenderer
• QgsSingleBandColorDataRenderer
• QgsSingleBandGrayRenderer
• QgsSingleBandPseudoColorRenderer
Single band raster layers can be drawn either in gray colors (low values = black, high values = white) or with a
pseudocolor algorithm that assigns colors to the values. Single band rasters with a palette can also be drawn using the
palette. Multiband layers are typically drawn by mapping the bands to RGB colors. Another possibility is to use just
one band for drawing.
Let’s say we want a render single band raster layer with colors ranging from green to yellow (corresponding to pixel
values from 0 to 255). In the first stage we will prepare a QgsRasterShader object and configure its shader
function:
1 fcn = QgsColorRampShader()
2 fcn.setColorRampType(QgsColorRampShader.Interpolated)
3 lst = [ QgsColorRampShader.ColorRampItem(0, QColor(0,255,0)),
4 QgsColorRampShader.ColorRampItem(255, QColor(255,255,0)) ]
5 fcn.setColorRampItemList(lst)
6 shader = QgsRasterShader()
7 shader.setRasterShaderFunction(fcn)
The shader maps the colors as specified by its color map. The color map is provided as a list of pixel values with
associated colors. There are three modes of interpolation:
• linear (Interpolated): the color is linearly interpolated from the color map entries above and below the
pixel value
• discrete (Discrete): the color is taken from the closest color map entry with equal or higher value
• exact (Exact): the color is not interpolated, only pixels with values equal to color map entries will be drawn
In the second step we will associate this shader with the raster layer:
The number 1 in the code above is the band number (raster bands are indexed from one).
Finally we have to use the triggerRepaint() method to see the results:
rlayer.triggerRepaint()
By default, QGIS maps the first three bands to red, green and blue to create a color image (this is the MultiBand-
Color drawing style). In some cases you might want to override these setting. The following code interchanges red
band (1) and green band (2):
rlayer_multi = QgsProject.instance().mapLayersByName('multiband')[0]
rlayer_multi.renderer().setGreenBand(1)
rlayer_multi.renderer().setRedBand(2)
In case only one band is necessary for visualization of the raster, single band drawing can be chosen, either gray levels
or pseudocolor.
We have to use triggerRepaint() to update the map and see the result:
rlayer_multi.triggerRepaint()
5.2. Renderer 25
PyQGIS testing developer cookbook
Raster values can be queried using the sample() method of the QgsRasterDataProvider class. You have
to specify a QgsPointXY and the band number of the raster layer you want to query. The method returns a tuple
with the value and True or False depending on the results:
Another method to query raster values is using the identify() method that returns a QgsRasterIdenti-
fyResult object.
if ident.isValid():
print(ident.results())
{1: 323.0}
In this case, the results() method returns a dictionary, with band indices as keys, and band values as values. For
instance, something like {1: 323.0}
You can create a raster layer using the QgsRasterBlock class. For example, to create a 2x2 raster block with one
byte per pixel:
block = QgsRasterBlock(Qgis.Byte, 2, 2)
block.setData(b'\xaa\xbb\xcc\xdd')
Raster pixels can be overwritten thanks to the writeBlock() method. To overwrite existing raster data at position
0,0 by the 2x2 block:
provider = rlayer.dataProvider()
provider.setEditable(True)
provider.writeBlock(block, 1, 0, 0)
provider.setEditable(False)
SIX
Hint: The code snippets on this page need the following imports if you’re outside the pyqgis console:
This section summarizes various actions that can be done with vector layers.
Most work here is based on the methods of the QgsVectorLayer class.
27
PyQGIS testing developer cookbook
You can retrieve information about the fields associated with a vector layer by calling fields() on a QgsVec-
torLayer object:
1 ID Integer64
2 fk_region Integer64
3 ELEV Real
4 NAME String
5 USE String
The displayField() and mapTipTemplate() methods provide information on the field and template used
in the maptips tab.
When you load a vector layer, a field is always chosen by QGIS as the Display Name, while the HTML Map Tip
is empty by default. With these methods you can easily get both:
NAME
Note: If you change the Display Name from a field to an expression, you have to use displayExpression()
instead of displayField().
Iterating over the features in a vector layer is one of the most common tasks. Below is an example of the simple basic
code to perform this task and showing some information about each feature. The layer variable is assumed to have
a QgsVectorLayer object.
Feature ID: 1
Point: <QgsPointXY: POINT(7 45)>
[1, 'First feature']
In QGIS desktop, features can be selected in different ways: the user can click on a feature, draw a rectangle on
the map canvas or use an expression filter. Selected features are normally highlighted in a different color (default is
yellow) to draw user’s attention on the selection.
Sometimes it can be useful to programmatically select features or to change the default color.
To select all the features, the selectAll() method can be used:
# Assumes that the active layer is points.shp file from the QGIS test suite
# (Class (string) and Heading (number) are attributes in points.shp)
layer = iface.activeLayer()
layer.selectByExpression('"Class"=\'B52\' and "Heading" > 10 and "Heading" <70',␣
,→QgsVectorLayer.SetSelection)
To change the selection color you can use setSelectionColor() method of QgsMapCanvas as shown in
the following example:
iface.mapCanvas().setSelectionColor( QColor("red") )
To add features to the selected features list for a given layer, you can call select() passing to it the list of features
IDs:
1 selected_fid = []
2
layer.removeSelection()
print(feature['name'])
First feature
Alternatively, attributes can be referred to by index. This is a bit faster than using the name. For example, to get the
second attribute:
print(feature[1])
First feature
If you only need selected features, you can use the selectedFeatures() method from the vector layer:
selection = layer.selectedFeatures()
for feature in selection:
# do whatever you need with the feature
pass
If you want to iterate over a given subset of features in a layer, such as those within a given area, you have to add a
QgsFeatureRequest object to the getFeatures() call. Here’s an example:
3 request = QgsFeatureRequest().setFilterRect(areaOfInterest)
4
For the sake of speed, the intersection is often done only using feature’s bounding box. There is however a flag
ExactIntersect that makes sure that only intersecting features will be returned:
request = QgsFeatureRequest().setFilterRect(areaOfInterest) \
.setFlags(QgsFeatureRequest.ExactIntersect)
With setLimit() you can limit the number of requested features. Here’s an example:
request = QgsFeatureRequest()
request.setLimit(2)
for feature in layer.getFeatures(request):
print(feature)
If you need an attribute-based filter instead (or in addition) of a spatial one like shown in the examples above, you
can build a QgsExpression object and pass it to the QgsFeatureRequest constructor. Here’s an example:
# The expression will filter the features where the field "location_name"
# contains the word "Lake" (case insensitive)
exp = QgsExpression('location_name ILIKE \'%Lake%\'')
request = QgsFeatureRequest(exp)
See Expressions, Filtering and Calculating Values for the details about the syntax supported by QgsExpression.
The request can be used to define the data retrieved for each feature, so the iterator returns all features, but returns
partial data for each of them.
1 # Only return selected fields to increase the "speed" of the request
2 request.setSubsetOfAttributes([0,2])
3
Most vector data providers support editing of layer data. Sometimes they support just a subset of possible editing
actions. Use the capabilities() function to find out what set of functionality is supported.
caps = layer.dataProvider().capabilities()
# Check if a particular capability is supported:
if caps & QgsVectorDataProvider.DeleteFeatures:
print('The layer supports DeleteFeatures')
For a list of all available capabilities, please refer to the API Documentation of QgsVectorDat-
aProvider.
To print layer’s capabilities textual description in a comma separated list you can use capabilitiesString()
as in the following example:
1 caps_string = layer.dataProvider().capabilitiesString()
2 # Print:
3 # 'Add Features, Delete Features, Change Attribute Values, Add Attributes,
4 # Delete Attributes, Rename Attributes, Fast Access to Features at ID,
(continues on next page)
By using any of the following methods for vector layer editing, the changes are directly committed to the underlying
data store (a file, database etc). In case you would like to do only temporary changes, skip to the next section that
explains how to do modifications with editing buffer.
Note: If you are working inside QGIS (either from the console or from a plugin), it might be necessary to force a
redraw of the map canvas in order to see the changes you’ve done to the geometry, to the style or to the attributes:
1 # If caching is enabled, a simple canvas refresh might not be sufficient
2 # to trigger a redraw and you must clear the cached image for the layer
3 if iface.mapCanvas().isCachingEnabled():
4 layer.triggerRepaint()
5 else:
6 iface.mapCanvas().refresh()
Create some QgsFeature instances and pass a list of them to provider’s addFeatures() method. It will return
two values: result (True or False) and list of added features (their ID is set by the data store).
To set up the attributes of the feature, you can either initialize the feature passing a QgsFields object (you can
obtain that from the fields() method of the vector layer) or call initAttributes() passing the number of
fields you want to be added.
1 if caps & QgsVectorDataProvider.AddFeatures:
2 feat = QgsFeature(layer.fields())
3 feat.setAttributes([0, 'hello'])
4 # Or set a single attribute by key or by index:
5 feat.setAttribute('name', 'hello')
6 feat.setAttribute(0, 'hello')
7 feat.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(123, 456)))
8 (res, outFeats) = layer.dataProvider().addFeatures([feat])
It is possible to either change feature’s geometry or to change some attributes. The following example first changes
values of attributes with index 0 and 1, then it changes the feature’s geometry.
1 fid = 100 # ID of the feature we will modify
2
When editing vectors within QGIS application, you have to first start editing mode for a particular layer, then do some
modifications and finally commit (or rollback) the changes. All the changes you make are not written until you commit
them — they stay in layer’s in-memory editing buffer. It is possible to use this functionality also programmatically —
it is just another method for vector layer editing that complements the direct usage of data providers. Use this option
when providing some GUI tools for vector layer editing, since this will allow user to decide whether to commit/rollback
and allows the usage of undo/redo. When changes are committed, all changes from the editing buffer are saved to
data provider.
The methods are similar to the ones we have seen in the provider, but they are called on the QgsVectorLayer
object instead.
For these methods to work, the layer must be in editing mode. To start the editing mode, use the startEditing()
method. To stop editing, use the commitChanges() or rollBack() methods. The first one will commit all
your changes to the data source, while the second one will discard them and will not modify the data source at all.
To find out whether a layer is in editing mode, use the isEditable() method.
Here you have some examples that demonstrate how to use these editing methods.
In order to make undo/redo work properly, the above mentioned calls have to be wrapped into undo commands. (If
you do not care about undo/redo and want to have the changes stored immediately, then you will have easier work by
editing with data provider.)
Here is how you can use the undo functionality:
1 layer.beginEditCommand("Feature triangulation")
2
5 if problem_occurred:
6 layer.destroyEditCommand()
7 # ... tell the user that there was a problem
8 # and return
9
12 layer.endEditCommand()
The beginEditCommand() method will create an internal “active” command and will record subsequent changes
in vector layer. With the call to endEditCommand() the command is pushed onto the undo stack and the user
will be able to undo/redo it from GUI. In case something went wrong while doing the changes, the destroyEd-
itCommand() method will remove the command and rollback all changes done while this command was active.
You can also use the with edit(layer)-statement to wrap commit and rollback into a more semantic code
block as shown in the example below:
with edit(layer):
feat = next(layer.getFeatures())
feat[0] = 5
layer.updateFeature(feat)
This will automatically call commitChanges() in the end. If any exception occurs, it will rollBack() all
the changes. In case a problem is encountered within commitChanges() (when the method returns False) a
QgsEditError exception will be raised.
To add fields (attributes), you need to specify a list of field definitions. For deletion of fields just provide a list of field
indexes.
1 from qgis.PyQt.QtCore import QVariant
2
4 layer.updateFields()
5 count=layer.fields().count() # count of layer fields
6 ind_list=list((count-3, count-2)) # create list
7
After adding or removing fields in the data provider the layer’s fields need to be updated because the changes are not
automatically propagated.
layer.updateFields()
Spatial indexes can dramatically improve the performance of your code if you need to do frequent queries to a vector
layer. Imagine, for instance, that you are writing an interpolation algorithm, and that for a given location you need
to know the 10 closest points from a points layer, in order to use those point for calculating the interpolated value.
Without a spatial index, the only way for QGIS to find those 10 points is to compute the distance from each and every
point to the specified location and then compare those distances. This can be a very time consuming task, especially
if it needs to be repeated for several locations. If a spatial index exists for the layer, the operation is much more
effective.
Think of a layer without a spatial index as a telephone book in which telephone numbers are not ordered or indexed.
The only way to find the telephone number of a given person is to read from the beginning until you find it.
Spatial indexes are not created by default for a QGIS vector layer, but you can create them easily. This is what you
have to do:
• create spatial index using the QgsSpatialIndex class:
index = QgsSpatialIndex()
• add features to index — index takes QgsFeature object and adds it to the internal data structure. You can
create the object manually or use one from a previous call to the provider’s getFeatures() method.
index.addFeature(feat)
• alternatively, you can load all features of a layer at once using bulk loading
index = QgsSpatialIndex(layer.getFeatures())
• once spatial index is filled with some values, you can do some queries
You can also use the QgsSpatialIndexKDBush spatial index. This index is similar to the standard QgsSpa-
tialIndex but:
• supports only single point features
• is static (no additional features can be added to the index after the construction)
• is much faster!
• allows direct retrieval of the original feature’s points, without requiring additional feature requests
• supports true distance based searches, i.e. return all points within a radius from a search point
The QgsVectorLayerUtils class contains some very useful methods that you can use with vector layers.
For example the createFeature() method prepares a QgsFeature to be added to a vector layer keeping all
the eventual constraints and default values of each field:
vlayer = QgsVectorLayer("testdata/airports.shp", "airports", "ogr")
feat = QgsVectorLayerUtils.createFeature(vlayer)
The getValues() method allows you to quickly get the values of a field or expression:
1 vlayer = QgsVectorLayer("testdata/airports.shp", "airports", "ogr")
2 # select only the first feature to make the output shorter
3 vlayer.selectByIds([1])
4 val = QgsVectorLayerUtils.getValues(vlayer, "NAME", selectedOnly=True)
5 print(val)
(['AMBLER'], True)
You can also convert fields to make them compatible with different formats by using the FieldValueConverter.
For example, to convert array variable types (e.g. in Postgres) to a text type, you can do the following:
1 LIST_FIELD_NAME = 'xxxx'
2
3 class ESRIValueConverter(QgsVectorFileWriter.FieldValueConverter):
4
Optionally you can set whether to export only selected features, pass further driver-specific options for creation or
tell the writer not to create attributes… There are a number of other (optional) parameters; see the QgsVector-
FileWriter documentation for details.
8 """ create an instance of vector file writer, which will create the vector file.
9 Arguments:
10 1. path to new file (will fail if exists already)
11 2. field map
12 3. geometry type - from WKBTYPE enum
13 4. layer's spatial reference (instance of
14 QgsCoordinateReferenceSystem)
15 5. coordinate transform context
16 6. save options (driver name for the output file, encoding etc.)
17 """
18
19 crs = QgsProject.instance().crs()
20 transform_context = QgsProject.instance().transformContext()
21 save_options = QgsVectorFileWriter.SaveVectorOptions()
22 save_options.driverName = "ESRI Shapefile"
23 save_options.fileEncoding = "UTF-8"
24
25 writer = QgsVectorFileWriter.create(
26 "testdata/my_new_shapefile.shp",
27 fields,
28 QgsWkbTypes.Point,
29 crs,
30 transform_context,
31 save_options
32 )
33
34 if writer.hasError() != QgsVectorFileWriter.NoError:
35 print("Error when creating shapefile: ", writer.errorMessage())
36
37 # add a feature
38 fet = QgsFeature()
39
40 fet.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(10,10)))
41 fet.setAttributes([1, "text"])
42 writer.addFeature(fet)
43
Among all the data providers supported by the QgsVectorLayer class, let’s focus on the memory-based layers.
Memory provider is intended to be used mainly by plugin or 3rd party app developers. It does not store data on disk,
allowing developers to use it as a fast backend for some temporary layers.
The provider supports string, int and double fields.
The memory provider also supports spatial indexing, which is enabled by calling the provider’s createSpa-
tialIndex() function. Once the spatial index is created you will be able to iterate over features within smaller
regions faster (since it’s not necessary to traverse all the features, only those in specified rectangle).
A memory provider is created by passing "memory" as the provider string to the QgsVectorLayer constructor.
The constructor also takes a URI defining the geometry type of the layer, one of: "Point", "LineString",
"Polygon", "MultiPoint", "MultiLineString", "MultiPolygon" or "None".
The URI can also specify the coordinate reference system, fields, and indexing of the memory provider in the URI.
The syntax is:
crs=definition
Specifies the coordinate reference system, where definition may be any of the forms accepted by
QgsCoordinateReferenceSystem.createFromString()
index=yes
Specifies that the provider will use a spatial index
field=name:type(length,precision)
Specifies an attribute of the layer. The attribute has a name, and optionally a type (integer, double, or string),
length, and precision. There may be multiple field definitions.
The following example of a URI incorporates all these options
"Point?crs=epsg:4326&field=id:integer&field=name:string(20)&index=yes"
The following example code illustrates creating and populating a memory provider
3 # create layer
4 vl = QgsVectorLayer("Point", "temporary_points", "memory")
5 pr = vl.dataProvider()
6
7 # add fields
8 pr.addAttributes([QgsField("name", QVariant.String),
9 QgsField("age", QVariant.Int),
10 QgsField("size", QVariant.Double)])
11 vl.updateFields() # tell the vector layer to fetch changes from the provider
12
13 # add a feature
14 fet = QgsFeature()
15 fet.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(10,10)))
16 fet.setAttributes(["Johny", 2, 0.3])
17 pr.addFeatures([fet])
18
fields: 3
features: 1
extent: 10.0 10.0 10.0 10.0
F: 1 ['Johny', 2, 0.3] <QgsPointXY: POINT(10 10)>
When a vector layer is being rendered, the appearance of the data is given by renderer and symbols associated
with the layer. Symbols are classes which take care of drawing of visual representation of features, while renderers
determine what symbol will be used for a particular feature.
The renderer for a given layer can be obtained as shown below:
renderer = layer.renderer()
print("Type:", renderer.type())
Type: singleSymbol
There are several known renderer types available in the QGIS core library:
There might be also some custom renderer types, so never make an assumption there are just these types. You can
query the application’s QgsRendererRegistry to find out currently available renderers:
print(QgsApplication.rendererRegistry().renderersList())
It is possible to obtain a dump of a renderer contents in text form — can be useful for debugging
renderer.dump()
You can get the symbol used for rendering by calling symbol() method and change it with setSymbol() method
(note for C++ devs: the renderer takes ownership of the symbol.)
You can change the symbol used by a particular vector layer by calling setSymbol() passing an instance of the
appropriate symbol instance. Symbols for point, line and polygon layers can be created by calling the createSim-
ple() function of the corresponding classes QgsMarkerSymbol, QgsLineSymbol and QgsFillSymbol.
The dictionary passed to createSimple() sets the style properties of the symbol.
For example you can replace the symbol used by a particular point layer by calling setSymbol() passing an
instance of a QgsMarkerSymbol, as in the following code example:
symbol = QgsMarkerSymbol.createSimple({'name': 'square', 'color': 'red'})
layer.renderer().setSymbol(symbol)
# show the change
layer.triggerRepaint()
name indicates the shape of the marker, and can be any of the following:
• circle
• square
• cross
• rectangle
• diamond
• pentagon
• triangle
• equilateral_triangle
• star
• regular_star
• arrow
• filled_arrowhead
• x
To get the full list of properties for the first symbol layer of a symbol instance you can follow the example code:
print(layer.renderer().symbol().symbolLayers()[0].properties())
,→'outline_color': '35,35,35,255,rgb:0.13725490196078433,0.13725490196078433,0.
When using a categorized renderer, you can query and set the attribute that is used for classification: use the clas-
sAttribute() and setClassAttribute() methods.
To get a list of categories
1 categorized_renderer = QgsCategorizedSymbolRenderer()
2 # Add a few categories
3 cat1 = QgsRendererCategory('1', QgsMarkerSymbol(), 'category 1')
4 cat2 = QgsRendererCategory('2', QgsMarkerSymbol(), 'category 2')
5 categorized_renderer.addCategory(cat1)
6 categorized_renderer.addCategory(cat2)
7
Where value() is the value used for discrimination between categories, label() is a text used for category
description and symbol() method returns the assigned symbol.
The renderer usually stores also original symbol and color ramp which were used for the classification: source-
ColorRamp() and sourceSymbol() methods.
This renderer is very similar to the categorized symbol renderer described above, but instead of one attribute value
per class it works with ranges of values and thus can be used only with numerical attributes.
To find out more about ranges used in the renderer
1 graduated_renderer = QgsGraduatedSymbolRenderer()
2 # Add a few categories
3 graduated_renderer.addClassRange(QgsRendererRange(QgsClassificationRange('class 0-
,→100', 0, 100), QgsMarkerSymbol()))
4 graduated_renderer.addClassRange(QgsRendererRange(QgsClassificationRange('class␣
,→101-200', 101, 200), QgsMarkerSymbol()))
you can again use the classAttribute() (to find the classification attribute name), sourceSymbol() and
sourceColorRamp() methods. Additionally there is the mode() method which determines how the ranges
were created: using equal intervals, quantiles or some other method.
If you wish to create your own graduated symbol renderer you can do so as illustrated in the example snippet below
(which creates a simple two class arrangement)
30 myRenderer.setClassificationMethod(myClassificationMethod)
31 myRenderer.setClassAttribute(myTargetField)
32
33 myVectorLayer.setRenderer(myRenderer)
For representation of symbols, there is QgsSymbol base class with three derived classes:
• QgsMarkerSymbol — for point features
• QgsLineSymbol — for line features
• QgsFillSymbol — for polygon features
Every symbol consists of one or more symbol layers (classes derived from QgsSymbolLayer). The symbol
layers do the actual rendering, the symbol class itself serves only as a container for the symbol layers.
Having an instance of a symbol (e.g. from a renderer), it is possible to explore it: the type() method says whether
it is a marker, line or fill symbol. There is a dump() method which returns a brief description of the symbol. To get
a list of symbol layers:
marker_symbol = QgsMarkerSymbol()
for i in range(marker_symbol.symbolLayerCount()):
lyr = marker_symbol.symbolLayer(i)
print("{}: {}".format(i, lyr.layerType()))
0: SimpleMarker
To find out symbol’s color use color() method and setColor() to change its color. With marker symbols
additionally you can query for the symbol size and rotation with the size() and angle() methods. For line
symbols the width() method returns the line width.
Size and width are in millimeters by default, angles are in degrees.
As said before, symbol layers (subclasses of QgsSymbolLayer) determine the appearance of the features. There
are several basic symbol layer classes for general use. It is possible to implement new symbol layer types and thus
arbitrarily customize how features will be rendered. The layerType() method uniquely identifies the symbol
layer class — the basic and default ones are SimpleMarker, SimpleLine and SimpleFill symbol layers
types.
You can get a complete list of the types of symbol layers you can create for a given symbol layer class with the
following code:
1 from qgis.core import QgsSymbolLayerRegistry
2 myRegistry = QgsApplication.symbolLayerRegistry()
3 myMetadata = myRegistry.symbolLayerMetadata("SimpleFill")
4 for item in myRegistry.symbolLayersForType(QgsSymbol.Marker):
5 print(item)
1 AnimatedMarker
2 EllipseMarker
3 FilledMarker
4 FontMarker
5 GeometryGenerator
6 MaskMarker
7 RasterMarker
8 SimpleMarker
9 SvgMarker
10 VectorField
The QgsSymbolLayerRegistry class manages a database of all available symbol layer types.
To access symbol layer data, use its properties() method that returns a key-value dictionary of properties which
determine the appearance. Each symbol layer type has a specific set of properties that it uses. Additionally, there are
the generic methods color(), size(), angle() and width(), with their setter counterparts. Of course size
and angle are available only for marker symbol layers and width for line symbol layers.
Imagine you would like to customize the way how the data gets rendered. You can create your own symbol layer class
that will draw the features exactly as you wish. Here is an example of a marker that draws red circles with specified
radius
1 from qgis.core import QgsMarkerSymbolLayer
2 from qgis.PyQt.QtGui import QColor
3
4 class FooSymbolLayer(QgsMarkerSymbolLayer):
5
11 def layerType(self):
12 return "FooMarker"
13
30 def clone(self):
31 return FooSymbolLayer(self.radius)
The layerType() method determines the name of the symbol layer; it has to be unique among all symbol layers.
The properties() method is used for persistence of attributes. The clone() method must return a copy of
the symbol layer with all attributes being exactly the same. Finally there are rendering methods: startRender()
is called before rendering the first feature, stopRender() when the rendering is done, and renderPoint() is
called to do the rendering. The coordinates of the point(s) are already transformed to the output coordinates.
For polylines and polygons the only difference would be in the rendering method: you would use renderPoly-
line() which receives a list of lines, while renderPolygon() receives a list of points on the outer ring as the
first parameter and a list of inner rings (or None) as a second parameter.
Usually it is convenient to add a GUI for setting attributes of the symbol layer type to allow users to customize the
appearance: in case of our example above we can let user set circle radius. The following code implements such
widget
3 class FooSymbolLayerWidget(QgsSymbolLayerWidget):
4 def __init__(self, parent=None):
5 QgsSymbolLayerWidget.__init__(self, parent)
6
7 self.layer = None
8
9 # setup a simple UI
10 self.label = QLabel("Radius:")
11 self.spinRadius = QDoubleSpinBox()
12 self.hbox = QHBoxLayout()
13 self.hbox.addWidget(self.label)
14 self.hbox.addWidget(self.spinRadius)
15 self.setLayout(self.hbox)
16 self.connect(self.spinRadius, SIGNAL("valueChanged(double)"), \
17 self.radiusChanged)
18
25 def symbolLayer(self):
26 return self.layer
27
This widget can be embedded into the symbol properties dialog. When the symbol layer type is selected in symbol
properties dialog, it creates an instance of the symbol layer and an instance of the symbol layer widget. Then it calls
the setSymbolLayer() method to assign the symbol layer to the widget. In that method the widget should update
the UI to reflect the attributes of the symbol layer. The symbolLayer() method is used to retrieve the symbol
layer again by the properties dialog to use it for the symbol.
On every change of attributes, the widget should emit the changed() signal to let the properties dialog update the
symbol preview.
Now we are missing only the final glue: to make QGIS aware of these new classes. This is done by adding the symbol
layer to registry. It is possible to use the symbol layer also without adding it to the registry, but some functionality
will not work: e.g. loading of project files with the custom symbol layers or inability to edit the layer’s attributes in
GUI.
We will have to create metadata for the symbol layer
1 from qgis.core import QgsSymbol, QgsSymbolLayerAbstractMetadata,␣
,→QgsSymbolLayerRegistry
3 class FooSymbolLayerMetadata(QgsSymbolLayerAbstractMetadata):
4
5 def __init__(self):
6 super().__init__("FooMarker", "My new Foo marker", QgsSymbol.Marker)
7
12 fslmetadata = FooSymbolLayerMetadata()
QgsApplication.symbolLayerRegistry().addSymbolLayerType(fslmetadata)
You should pass layer type (the same as returned by the layer) and symbol type (marker/line/fill) to the constructor
of the parent class. The createSymbolLayer() method takes care of creating an instance of symbol layer with
attributes specified in the props dictionary. And there is the createSymbolLayerWidget() method which
returns the settings widget for this symbol layer type.
The last step is to add this symbol layer to the registry — and we are done.
It might be useful to create a new renderer implementation if you would like to customize the rules how to select
symbols for rendering of features. Some use cases where you would want to do it: symbol is determined from a
combination of fields, size of symbols changes depending on current scale etc.
The following code shows a simple custom renderer that creates two marker symbols and chooses randomly one of
them for every feature
1 import random
2 from qgis.core import QgsWkbTypes, QgsSymbol, QgsFeatureRenderer
3
5 class RandomRenderer(QgsFeatureRenderer):
6 def __init__(self, syms=None):
7 super().__init__("RandomRenderer")
8 self.syms = syms if syms else [
9 QgsSymbol.defaultSymbol(QgsWkbTypes.geometryType(QgsWkbTypes.Point)),
(continues on next page)
29 def clone(self):
30 return RandomRenderer(self.syms)
The constructor of the parent QgsFeatureRenderer class needs a renderer name (which has to be unique among
renderers). The symbolForFeature() method is the one that decides what symbol will be used for a particular
feature. startRender() and stopRender() take care of initialization/finalization of symbol rendering. The
usedAttributes() method can return a list of field names that the renderer expects to be present. Finally, the
clone() method should return a copy of the renderer.
Like with symbol layers, it is possible to attach a GUI for configuration of the renderer. It has to be derived from
QgsRendererWidget. The following sample code creates a button that allows the user to set the first symbol
4 class RandomRendererWidget(QgsRendererWidget):
5 def __init__(self, layer, style, renderer):
6 super().__init__(layer, style)
7 if renderer is None or renderer.type() != "RandomRenderer":
8 self.r = RandomRenderer()
9 else:
10 self.r = renderer
11 # setup UI
12 self.btn1 = QgsColorButton()
13 self.btn1.setColor(self.r.syms[0].color())
14 self.vbox = QVBoxLayout()
15 self.vbox.addWidget(self.btn1)
16 self.setLayout(self.vbox)
17 self.btn1.colorChanged.connect(self.setColor1)
18
19 def setColor1(self):
20 color = self.btn1.color()
21 if not color.isValid(): return
22 self.r.syms[0].setColor(color)
23
24 def renderer(self):
25 return self.r
The constructor receives instances of the active layer (QgsVectorLayer), the global style (QgsStyle) and the
current renderer. If there is no renderer or the renderer has different type, it will be replaced with our new renderer,
otherwise we will use the current renderer (which has already the type we need). The widget contents should be
updated to show current state of the renderer. When the renderer dialog is accepted, the widget’s renderer()
method is called to get the current renderer — it will be assigned to the layer.
The last missing bit is the renderer metadata and registration in registry, otherwise loading of layers with the renderer
will not work and user will not be able to select it from the list of renderers. Let us finish our RandomRenderer
example
7 class RandomRendererMetadata(QgsRendererAbstractMetadata):
8
9 def __init__(self):
10 super().__init__("RandomRenderer", "Random renderer")
11
18 rrmetadata = RandomRendererMetadata()
QgsApplication.rendererRegistry().addRenderer(rrmetadata)
Similarly as with symbol layers, abstract metadata constructor awaits renderer name, name visible for users and
optionally name of renderer’s icon. The createRenderer() method passes a QDomElement instance that can
be used to restore the renderer’s state from the DOM tree. The createRendererWidget() method creates the
configuration widget. It does not have to be present or can return None if the renderer does not come with GUI.
To associate an icon with the renderer you can assign it in the QgsRendererAbstractMetadata constructor as
a third (optional) argument — the base class constructor in the RandomRendererMetadata __init__() function
becomes
QgsRendererAbstractMetadata.__init__(self,
"RandomRenderer",
"Random renderer",
QIcon(QPixmap("RandomRendererIcon.png", "png")))
The icon can also be associated at any later time using the setIcon() method of the metadata class. The icon
can be loaded from a file (as shown above) or can be loaded from a Qt resource (PyQt5 includes .qrc compiler for
Python).
TODO:
• creating/modifying symbols
• working with style (QgsStyle)
• working with color ramps (QgsColorRamp)
• exploring symbol layer and renderer registries
SEVEN
GEOMETRY HANDLING
Hint: The code snippets on this page need the following imports if you’re outside the pyqgis console:
1 from qgis.core import (
2 QgsGeometry,
3 QgsGeometryCollection,
4 QgsPoint,
5 QgsPointXY,
6 QgsWkbTypes,
7 QgsProject,
8 QgsFeatureRequest,
9 QgsVectorLayer,
10 QgsDistanceArea,
11 QgsUnitTypes,
12 QgsCoordinateTransform,
13 QgsCoordinateReferenceSystem
14 )
Points, linestrings and polygons that represent a spatial feature are commonly referred to as geometries. In QGIS they
are represented with the QgsGeometry class.
Sometimes one geometry is actually a collection of simple (single-part) geometries. Such a geometry is called a
multi-part geometry. If it contains just one type of simple geometry, we call it multi-point, multi-linestring or multi-
polygon. For example, a country consisting of multiple islands can be represented as a multi-polygon.
The coordinates of geometries can be in any coordinate reference system (CRS). When fetching features from a layer,
associated geometries will have coordinates in CRS of the layer.
Description and specifications of all possible geometries construction and relationships are available in the OGC
Simple Feature Access Standards for advanced details.
Coordinates are given using QgsPoint class or QgsPointXY class. The difference between these classes
is that QgsPoint supports M and Z dimensions.
49
PyQGIS testing developer cookbook
1 g = QgsGeometry()
2 wkb = bytes.fromhex("010100000000000000000045400000000000001440")
3 g.fromWkb(wkb)
4
First, you should find out the geometry type. The wkbType() method is the one to use. It returns a value from the
QgsWkbTypes.Type enumeration.
1 print(gPnt.wkbType())
2 # output: 'WkbType.Point'
3 print(gLine.wkbType())
4 # output: 'WkbType.LineString'
5 print(gPolygon.wkbType())
6 # output: 'WkbType.Polygon'
As an alternative, one can use the type() method which returns a value from the QgsWkbTypes.
GeometryType enumeration.
print(gLine.type())
# output: 'GeometryType.Line'
You can use the displayString() function to get a human readable geometry type.
1 print(QgsWkbTypes.displayString(gPnt.wkbType()))
2 # output: 'Point'
3 print(QgsWkbTypes.displayString(gLine.wkbType()))
4 # output: 'LineString'
5 print(QgsWkbTypes.displayString(gPolygon.wkbType()))
6 # output: 'Polygon'
There is also a helper function isMultipart() to find out whether a geometry is multipart or not.
To extract information from a geometry there are accessor functions for every vector type. Here’s an example on how
to use these accessors:
1 print(gPnt.asPoint())
2 # output: <QgsPointXY: POINT(1 1)>
3 print(gLine.asPolyline())
4 # output: [<QgsPointXY: POINT(1 1)>, <QgsPointXY: POINT(2 2)>]
5 print(gPolygon.asPolygon())
(continues on next page)
Note: The tuples (x,y) are not real tuples, they are QgsPoint objects, the values are accessible with x() and y()
methods.
For multipart geometries there are similar accessor functions: asMultiPoint(), asMultiPolyline() and
asMultiPolygon().
It is possible to iterate over all the parts of a geometry, regardless of the geometry’s type. E.g.
Point (0 0)
Point (1 1)
Point (2 2)
LineString (0 0, 10 10)
gc = QgsGeometryCollection()
gc.fromWkt('GeometryCollection( Point(1 2), Point(11 12), LineString(33 34, 44 45))
,→')
print(gc[1].asWkt())
It’s also possible to modify each part of the geometry using QgsGeometry.parts() method.
9 print(geom.asWkt())
,→5072021.45942386891692877))
QGIS uses GEOS library for advanced geometry operations such as geometry predicates (contains(), inter-
sects(), …) and set operations (combine(), difference(), …). It can also compute geometric properties
of geometries, such as area (in the case of polygons) or lengths (for polygons and lines).
Let’s see an example that combines iterating over the features in a given layer and performing some geometric com-
putations based on their geometries. The below code will compute and print the area and perimeter of each country
in the countries layer within our tutorial QGIS project.
The following code assumes layer is a QgsVectorLayer object that has Polygon feature type.
4 # let's filter for countries that begin with Z, then get their features
5 query = '"name" LIKE \'Z%\''
6 features = layer.getFeatures(QgsFeatureRequest().setFilterExpression(query))
7
8 # now loop through the features, perform geometry computation and print the results
9 for f in features:
10 geom = f.geometry()
11 name = f.attribute('NAME')
12 print(name)
13 print('Area: ', geom.area())
14 print('Perimeter: ', geom.length())
1 Zambia
2 Area: 62.82279065343119
3 Perimeter: 50.65232014052552
4 Zimbabwe
5 Area: 33.41113559136517
6 Perimeter: 26.608288555013935
Now you have calculated and printed the areas and perimeters of the geometries. You may however quickly notice
that the values are strange. That is because areas and perimeters don’t take CRS into account when computed using
the area() and length() methods from the QgsGeometry class. For a more powerful area and distance
calculation, the QgsDistanceArea class can be used, which can perform ellipsoid based calculations:
The following code assumes layer is a QgsVectorLayer object that has Polygon feature type.
1 d = QgsDistanceArea()
2 d.setEllipsoid('WGS84')
3
4 layer = QgsProject.instance().mapLayersByName('countries')[0]
5
6 # let's filter for countries that begin with Z, then get their features
7 query = '"name" LIKE \'Z%\''
8 features = layer.getFeatures(QgsFeatureRequest().setFilterExpression(query))
9
10 for f in features:
11 geom = f.geometry()
12 name = f.attribute('NAME')
13 print(name)
14 print("Perimeter (m):", d.measurePerimeter(geom))
15 print("Area (m2):", d.measureArea(geom))
16
17 # let's calculate and print the area again, but this time in square kilometers
18 print("Area (km2):", d.convertAreaMeasurement(d.measureArea(geom), QgsUnitTypes.
,→AreaSquareKilometers))
1 Zambia
2 Perimeter (m): 5539361.250294601
3 Area (m2): 751989035032.9031
4 Area (km2): 751989.0350329031
5 Zimbabwe
6 Perimeter (m): 2865021.3325076113
7 Area (m2): 389267821381.6008
8 Area (km2): 389267.8213816008
Alternatively, you may want to know the distance between two points.
1 d = QgsDistanceArea()
2 d.setEllipsoid('WGS84')
3
You can find many example of algorithms that are included in QGIS and use these methods to analyze and transform
vector data. Here are some links to the code of a few of them.
• Distance and area using the QgsDistanceArea class: Distance matrix algorithm
• Lines to polygons algorithm
EIGHT
PROJECTIONS SUPPORT
Hint: The code snippets on this page need the following imports if you’re outside the pyqgis console:
Coordinate reference systems (CRS) are encapsulated by the QgsCoordinateReferenceSystem class. In-
stances of this class can be created in several different ways:
• specify CRS by its ID
True
55
PyQGIS testing developer cookbook
True
• create an invalid CRS and then use one of the create* functions to initialize it. In the following example we
use a Proj string to initialize the projection.
crs = QgsCoordinateReferenceSystem()
crs.createFromProj("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
print(crs.isValid())
True
It’s wise to check whether creation (i.e. lookup in the database) of the CRS has been successful: isValid() must
return True.
Note that for initialization of spatial reference systems QGIS needs to look up appropriate values in its inter-
nal database srs.db. Thus in case you create an independent application you need to set paths correctly with
QgsApplication.setPrefixPath(), otherwise it will fail to find the database. If you are running the com-
mands from the QGIS Python console or developing a plugin you do not care: everything is already set up for you.
Accessing spatial reference system information:
1 crs = QgsCoordinateReferenceSystem("EPSG:4326")
2
Output:
1 QGIS CRS ID: 3452
2 PostGIS SRID: 4326
3 Description: WGS 84
4 Projection Acronym: longlat
5 Ellipsoid Acronym: EPSG:7030
6 Proj String: +proj=longlat +datum=WGS84 +no_defs
7 Is geographic: True
8 Map units: DistanceUnit.Degrees
You can do transformation between different spatial reference systems by using the QgsCoordinateTrans-
form class. The easiest way to use it is to create a source and destination CRS and construct a QgsCoordinate-
Transform instance with them and the current project. Then just repeatedly call transform() function to do
the transformation. By default it does forward transformation, but it is capable to do also inverse transformation.
1 crsSrc = QgsCoordinateReferenceSystem("EPSG:4326") # WGS 84
2 crsDest = QgsCoordinateReferenceSystem("EPSG:32633") # WGS 84 / UTM zone 33N
3 transformContext = QgsProject.instance().transformContext()
4 xform = QgsCoordinateTransform(crsSrc, crsDest, transformContext)
5
Output:
NINE
Hint: The code snippets on this page need the following imports if you’re outside the pyqgis console:
The Map canvas widget is probably the most important widget within QGIS because it shows the map composed
from overlaid map layers and allows interaction with the map and layers. The canvas always shows a part of the
map defined by the current canvas extent. The interaction is done through the use of map tools: there are tools for
panning, zooming, identifying layers, measuring, vector editing and others. Similar to other graphics programs, there
is always one tool active and the user can switch between the available tools.
The map canvas is implemented with the QgsMapCanvas class in the qgis.gui module. The implementation is
based on the Qt Graphics View framework. This framework generally provides a surface and a view where custom
graphics items are placed and user can interact with them. We will assume that you are familiar enough with Qt to
understand the concepts of the graphics scene, view and items. If not, please read the overview of the framework.
Whenever the map has been panned, zoomed in/out (or some other action that triggers a refresh), the map is rendered
again within the current extent. The layers are rendered to an image (using the QgsMapRendererJob class) and
that image is displayed on the canvas. The QgsMapCanvas class also controls refreshing of the rendered map.
Besides this item which acts as a background, there may be more map canvas items.
Typical map canvas items are rubber bands (used for measuring, vector editing etc.) or vertex markers. The canvas
items are usually used to give visual feedback for map tools, for example, when creating a new polygon, the map tool
59
PyQGIS testing developer cookbook
creates a rubber band canvas item that shows the current shape of the polygon. All map canvas items are subclasses
of QgsMapCanvasItem which adds some more functionality to the basic QGraphicsItem objects.
To summarize, the map canvas architecture consists of three concepts:
• map canvas — for viewing of the map
• map canvas items — additional items that can be displayed on the map canvas
• map tools — for interaction with the map canvas
Map canvas is a widget like any other Qt widget, so using it is as simple as creating and showing it.
canvas = QgsMapCanvas()
canvas.show()
This produces a standalone window with map canvas. It can be also embedded into an existing widget or window.
When using .ui files and Qt Designer, place a QWidget on the form and promote it to a new class: set QgsMap-
Canvas as class name and set qgis.gui as header file. The pyuic5 utility will take care of it. This is a very
convenient way of embedding the canvas. The other possibility is to manually write the code to construct map canvas
and other widgets (as children of a main window or dialog) and create a layout.
By default, map canvas has black background and does not use anti-aliasing. To set white background and enable
anti-aliasing for smooth rendering
canvas.setCanvasColor(Qt.white)
canvas.enableAntiAliasing(True)
(In case you are wondering, Qt comes from PyQt.QtCore module and Qt.white is one of the predefined
QColor instances.)
Now it is time to add some map layers. We will first open a layer and add it to the current project. Then we will set
the canvas extent and set the list of layers for the canvas.
After executing these commands, the canvas should show the layer you have loaded.
To show some additional data on top of the map in canvas, use map canvas items. It is possible to create custom
canvas item classes (covered below), however there are two useful canvas item classes for convenience: QgsRub-
berBand for drawing polylines or polygons, and QgsVertexMarker for drawing points. They both work with
map coordinates, so the shape is moved/scaled automatically when the canvas is being panned or zoomed.
To show a polyline:
To show a polygon
Note that points for polygon is not a plain list: in fact, it is a list of rings containing linear rings of the polygon: first
ring is the outer border, further (optional) rings correspond to holes in the polygon.
Rubber bands allow some customization, namely to change their color and line width
r.setColor(QColor(0, 0, 255))
r.setWidth(3)
The canvas items are bound to the canvas scene. To temporarily hide them (and show them again), use the hide()
and show() combo. To completely remove the item, you have to remove it from the scene of the canvas
canvas.scene().removeItem(r)
(in C++ it’s possible to just delete the item, however in Python del r would just delete the reference and the object
will still exist as it is owned by the canvas)
Rubber band can be also used for drawing points, but the QgsVertexMarker class is better suited for this
(QgsRubberBand would only draw a rectangle around the desired point).
You can use the vertex marker like this:
m = QgsVertexMarker(canvas)
m.setCenter(QgsPointXY(10,40))
This will draw a red cross on position [10,45]. It is possible to customize the icon type, size, color and pen width
For temporary hiding of vertex markers and removing them from canvas, use the same methods as for rubber bands.
The following example constructs a window that contains a map canvas and basic map tools for map panning and
zooming. Actions are created for activation of each tool: panning is done with QgsMapToolPan, zooming in/out
with a pair of QgsMapToolZoom instances. The actions are set as checkable and later assigned to the tools to allow
automatic handling of checked/unchecked state of the actions – when a map tool gets activated, its action is marked
as selected and the action of the previous map tool is deselected. The map tools are activated using setMapTool()
method.
5 class MyWnd(QMainWindow):
6 def __init__(self, layer):
7 QMainWindow.__init__(self)
8
9 self.canvas = QgsMapCanvas()
10 self.canvas.setCanvasColor(Qt.white)
11
12 self.canvas.setExtent(layer.extent())
13 self.canvas.setLayers([layer])
14
15 self.setCentralWidget(self.canvas)
16
21 self.actionZoomIn.setCheckable(True)
22 self.actionZoomOut.setCheckable(True)
23 self.actionPan.setCheckable(True)
24
25 self.actionZoomIn.triggered.connect(self.zoomIn)
26 self.actionZoomOut.triggered.connect(self.zoomOut)
27 self.actionPan.triggered.connect(self.pan)
28
42 self.pan()
43
44 def zoomIn(self):
45 self.canvas.setMapTool(self.toolZoomIn)
46
47 def zoomOut(self):
48 self.canvas.setMapTool(self.toolZoomOut)
49
50 def pan(self):
51 self.canvas.setMapTool(self.toolPan)
You can try the above code in the Python console editor. To invoke the canvas window, add the following lines to
instantiate the MyWnd class. They will render the currently selected layer on the newly created canvas
w = MyWnd(iface.activeLayer())
w.show()
You can use the map tool QgsMapToolIdentifyFeature for asking to the user to select a feature that will be
sent to a callback function.
1 def callback(feature):
2 """Code called when the feature is selected by the user"""
3 print("You clicked on feature {}".format(feature.id()))
4
5 canvas = iface.mapCanvas()
6 feature_identifier = QgsMapToolIdentifyFeature(canvas)
7
11 # use the callback as a slot triggered when the user identifies a feature
12 feature_identifier.featureIdentified.connect(callback)
13
Interaction with map canvas can also be done through entries you may add to its contextual menu using the con-
textMenuAboutToShow signal.
The following code adds My menu ► My Action action next to default entries when you right-click over the map
canvas.
8 canvas.contextMenuAboutToShow.connect(populateContextMenu)
9 canvas.show()
You can write your custom tools, to implement a custom behavior to actions performed by users on the canvas.
Map tools should inherit from the QgsMapTool, class or any derived class, and selected as active tools in the canvas
using the setMapTool() method as we have already seen.
Here is an example of a map tool that allows to define a rectangular extent by clicking and dragging on the canvas.
When the rectangle is defined, it prints its boundary coordinates in the console. It uses the rubber band elements
described before to show the selected rectangle as it is being defined.
1 class RectangleMapTool(QgsMapToolEmitPoint):
2 def __init__(self, canvas):
3 self.canvas = canvas
4 QgsMapToolEmitPoint.__init__(self, self.canvas)
5 self.rubberBand = QgsRubberBand(self.canvas, QgsWkbTypes.PolygonGeometry)
6 self.rubberBand.setColor(Qt.red)
7 self.rubberBand.setWidth(1)
8 self.reset()
9
10 def reset(self):
11 self.startPoint = self.endPoint = None
12 self.isEmittingPoint = False
13 self.rubberBand.reset(QgsWkbTypes.PolygonGeometry)
14
33 self.endPoint = self.toMapCoordinates(e.pos())
34 self.showRect(self.startPoint, self.endPoint)
35
46 self.rubberBand.addPoint(point1, False)
47 self.rubberBand.addPoint(point2, False)
48 self.rubberBand.addPoint(point3, False)
49 self.rubberBand.addPoint(point4, True) # true to update canvas
50 self.rubberBand.show()
51
52 def rectangle(self):
53 if self.startPoint is None or self.endPoint is None:
54 return None
55 elif (self.startPoint.x() == self.endPoint.x() or \
56 self.startPoint.y() == self.endPoint.y()):
57 return None
58
61 def deactivate(self):
62 QgsMapTool.deactivate(self)
(continues on next page)
1 class CircleCanvasItem(QgsMapCanvasItem):
2 def __init__(self, canvas):
3 super().__init__(canvas)
4 self.center = QgsPoint(0, 0)
5 self.size = 100
6
10 def center(self):
11 return self.center
12
16 def size(self):
17 return self.size
18
19 def boundingRect(self):
20 return QRectF(self.center.x() - self.size/2,
21 self.center.y() - self.size/2,
22 self.center.x() + self.size/2,
23 self.center.y() + self.size/2)
24
31
TEN
Hint: The code snippets on this page need the following imports:
1 import os
2
There are generally two approaches when input data should be rendered as a map: either do it quick way using
QgsMapRendererJob or produce more fine-tuned output by composing the map with the QgsLayout class.
67
PyQGIS testing developer cookbook
The rendering is done creating a QgsMapSettings object to define the rendering settings, and then constructing
a QgsMapRendererJob with those settings. The latter is then used to create the resulting image.
Here’s an example:
1 image_location = os.path.join(QgsProject.instance().homePath(), "render.png")
2
3 vlayer = iface.activeLayer()
4 settings = QgsMapSettings()
5 settings.setLayers([vlayer])
6 settings.setBackgroundColor(QColor(255, 255, 255))
7 settings.setOutputSize(QSize(800, 600))
8 settings.setExtent(vlayer.extent())
9
10 render = QgsMapRendererParallelJob(settings)
11
12 def finished():
13 img = render.renderedImage()
14 # save the image; e.g. img.save("/Users/myuser/render.png","png")
15 img.save(image_location, "png")
16
17 render.finished.connect(finished)
18
If you have more than one layer and they have a different CRS, the simple example above will probably not work: to
get the right values from the extent calculations you have to explicitly set the destination CRS
layers = [iface.activeLayer()]
settings = QgsMapSettings()
settings.setLayers(layers)
settings.setDestinationCrs(layers[0].crs())
Print layout is a very handy tool if you would like to do a more sophisticated output than the simple rendering shown
above. It is possible to create complex map layouts consisting of map views, labels, legend, tables and other elements
that are usually present on paper maps. The layouts can be then exported to PDF, SVG, raster images or directly
printed on a printer.
The layout consists of a bunch of classes. They all belong to the core library. QGIS application has a convenient GUI
for placement of the elements, though it is not available in the GUI library. If you are not familiar with Qt Graphics
View framework, then you are encouraged to check the documentation now, because the layout is based on it.
The central class of the layout is the QgsLayout class, which is derived from the Qt QGraphicsScene class. Let us
create an instance of it:
project = QgsProject.instance()
layout = QgsPrintLayout(project)
layout.initializeDefaults()
This initializes the layout with some default settings, specifically by adding an empty A4 page to the layout. You can
create layouts without calling the initializeDefaults() method, but you’ll need to take care of adding pages
to the layout yourself.
The previous code creates a “temporary” layout that is not visible in the GUI. It can be handy to e.g. quickly add
some items and export without modifying the project itself nor expose these changes to the user. If you want the
layout to be saved/restored along with the project and available in the layout manager, then add:
layout.setName("MyLayout")
project.layoutManager().addLayout(layout)
Now we can add various elements (map, label, …) to the layout. All these objects are represented by classes that
inherit from the base QgsLayoutItem class.
Here’s a description of some of the main layout items that can be added to a layout.
• map — Here we create a map of a custom size and render the current map canvas
1 map = QgsLayoutItemMap(layout)
2 # Set map item position and size (by default, it is a 0 width/0 height item␣
,→placed at 0,0)
3 map.attemptMove(QgsLayoutPoint(5,5, QgsUnitTypes.LayoutMillimeters))
4 map.attemptResize(QgsLayoutSize(200,200, QgsUnitTypes.LayoutMillimeters))
5 # Provide an extent to render
6 map.zoomToExtent(iface.mapCanvas().extent())
7 layout.addLayoutItem(map)
• label — allows displaying labels. It is possible to modify its font, color, alignment and margin
label = QgsLayoutItemLabel(layout)
label.setText("Hello world")
label.adjustSizeToText()
layout.addLayoutItem(label)
• legend
legend = QgsLayoutItemLegend(layout)
legend.setLinkedMap(map) # map is an instance of QgsLayoutItemMap
layout.addLayoutItem(legend)
• scale bar
1 item = QgsLayoutItemScaleBar(layout)
2 item.setStyle('Numeric') # optionally modify the style
3 item.setLinkedMap(map) # map is an instance of QgsLayoutItemMap
4 item.applyDefaultSize()
5 layout.addLayoutItem(item)
1 polygon = QPolygonF()
2 polygon.append(QPointF(0.0, 0.0))
3 polygon.append(QPointF(100.0, 0.0))
4 polygon.append(QPointF(200.0, 100.0))
5 polygon.append(QPointF(100.0, 200.0))
6
10 props = {}
11 props["color"] = "green"
12 props["style"] = "solid"
13 props["style_border"] = "solid"
14 props["color_border"] = "black"
15 props["width_border"] = "10.0"
16 props["joinstyle"] = "miter"
17
18 symbol = QgsFillSymbol.createSimple(props)
19 polygonItem.setSymbol(symbol)
A frame is drawn around each item by default. You can remove it as follows:
# for a composer label
label.setFrameEnabled(False)
Besides creating the layout items by hand, QGIS has support for layout templates which are essentially compositions
with all their items saved to a .qpt file (with XML syntax).
Once the composition is ready (the layout items have been created and added to the composition), we can proceed to
produce a raster and/or vector output.
A layout is a made of a set of interconnected items and it can happen that these connections are broken during
modifications (a legend connected to a removed map, an image item with missing source file,…) or you may want to
apply custom constraints to the layout items. The QgsAbstractValidityCheck helps you achieve this.
A basic check looks like:
@check.register(type=QgsAbstractValidityCheck.TypeLayoutCheck)
def my_layout_check(context, feedback):
results = ...
return results
Here’s a check which throws a warning whenever a layout map item is set to the web mercator projection:
1 @check.register(type=QgsAbstractValidityCheck.TypeLayoutCheck)
2 def layout_map_crs_choice_check(context, feedback):
3 layout = context.layout
4 results = []
5 for i in layout.items():
6 if isinstance(i, QgsLayoutItemMap) and i.crs().authid() == 'EPSG:3857':
7 res = QgsValidityCheckResult()
8 res.type = QgsValidityCheckResult.Warning
9 res.title = 'Map projection is misleading'
10 res.detailedDescription = 'The projection for the map item {} is set to <i>
,→Web Mercator (EPSG:3857)</i> which misrepresents areas and shapes. Consider␣
11 results.append(res)
12
13 return results
And here’s a more complex example, which throws a warning if any layout map items are set to a CRS which is only
valid outside of the extent shown in that map item:
1 @check.register(type=QgsAbstractValidityCheck.TypeLayoutCheck)
2 def layout_map_crs_area_check(context, feedback):
3 layout = context.layout
4 results = []
5 for i in layout.items():
6 if isinstance(i, QgsLayoutItemMap):
7 bounds = i.crs().bounds()
8 ct = QgsCoordinateTransform(QgsCoordinateReferenceSystem('EPSG:4326'),␣
,→i.crs(), QgsProject.instance())
9 bounds_crs = ct.transformBoundingBox(bounds)
10
11 if not bounds_crs.contains(i.extent()):
12 res = QgsValidityCheckResult()
13 res.type = QgsValidityCheckResult.Warning
14 res.title = 'Map projection is incorrect'
15 res.detailedDescription = 'The projection for the map item {} is␣
,→set to \'{}\', which is not valid for the area displayed within the map.'.
,→format(i.displayName(), i.crs().authid())
16 results.append(res)
17
18 return results
1 base_path = os.path.join(QgsProject.instance().homePath())
2 pdf_path = os.path.join(base_path, "output.pdf")
3
4 exporter = QgsLayoutExporter(layout)
5 exporter.exportToPdf(pdf_path, QgsLayoutExporter.PdfExportSettings())
Use exportToSvg() or exportToImage() in case you want to export to respectively an SVG or image file
instead of a PDF file.
If you want to export all pages from a layout that has the atlas option configured and enabled, you need to use the
atlas() method in the exporter (QgsLayoutExporter) with small adjustments. In the following example, the
pages are exported to PNG images:
Notice that the outputs will be saved in the base path folder, using the output filename expression configured on atlas.
ELEVEN
Hint: The code snippets on this page need the following imports if you’re outside the pyqgis console:
1 from qgis.core import (
2 edit,
3 QgsExpression,
4 QgsExpressionContext,
5 QgsFeature,
6 QgsFeatureRequest,
7 QgsField,
8 QgsFields,
9 QgsVectorLayer,
10 QgsPointXY,
11 QgsGeometry,
12 QgsProject,
13 QgsExpressionContextUtils
14 )
QGIS has some support for parsing of SQL-like expressions. Only a small subset of SQL syntax is supported. The
expressions can be evaluated either as boolean predicates (returning True or False) or as functions (returning a
scalar value). See vector_expressions in the User Manual for a complete list of available functions.
Three basic types are supported:
• number — both whole numbers and decimal numbers, e.g. 123, 3.14
• string — they have to be enclosed in single quotes: 'hello world'
• column reference — when evaluating, the reference is substituted with the actual value of the field. The names
are not escaped.
The following operations are available:
• arithmetic operators: +, -, *, /, ^
• parentheses: for enforcing the operator precedence: (1 + 1) * 3
• unary plus and minus: -12, +5
• mathematical functions: sqrt, sin, cos, tan, asin, acos, atan
• conversion functions: to_int, to_real, to_string, to_date
• geometry functions: $area, $length
• geometry handling functions: $x, $y, $geometry, num_geometries, centroid
And the following predicates are supported:
• comparison: =, !=, >, >=, <, <=
• pattern matching: LIKE (using % and _), ~ (regular expressions)
• logical predicates: AND, OR, NOT
73
PyQGIS testing developer cookbook
The following example shows how to check if a given expression can be parsed correctly:
Expressions can be used in different contexts, for example to filter features or to compute new field values. In any case,
the expression has to be evaluated. That means that its value is computed by performing the specified computational
steps, which can range from simple arithmetic to aggregate expressions.
To evaluate an expression against a feature, a QgsExpressionContext object has to be created and passed to
the evaluate function in order to allow the expression to access the feature’s field values.
The following example shows how to create a feature with a field called “Column” and how to add this feature to the
expression context.
1 fields = QgsFields()
2 field = QgsField('Column')
3 fields.append(field)
4 feature = QgsFeature()
5 feature.setFields(fields)
6 feature.setAttribute(0, 99)
7
8 exp = QgsExpression('"Column"')
9 context = QgsExpressionContext()
10 context.setFeature(feature)
11 exp.evaluate(context)
12 # 99
The following is a more complete example of how to use expressions in the context of a vector layer, in order to
compute new field values:
27 vl.updateExtents()
28 QgsProject.instance().addMapLayer(vl)
29
47 with edit(vl):
48 for f in vl.getFeatures():
49 context.setFeature(f)
50 f['Rev. per employee'] = expression1.evaluate(context)
51 f['Sum'] = expression2.evaluate(context)
52 f['Fun'] = expression3.evaluate(context)
53 vl.updateFeature(f)
54
55 print(f['Sum'])
876.5
The following example can be used to filter a layer and return any feature that matches a predicate.
1 layer = QgsVectorLayer("Point?field=Test:integer",
2 "addfeat", "memory")
3
4 layer.startEditing()
5
6 for i in range(10):
7 feature = QgsFeature()
8 feature.setAttributes([i])
9 assert(layer.addFeature(feature))
10 layer.commitChanges()
11
15 matches = 0
16 for f in layer.getFeatures(request):
17 matches += 1
18
19 print(matches)
5 value = exp.evaluate()
(continues on next page)
TWELVE
Hint: The code snippets on this page need the following imports if you’re outside the pyqgis console:
Many times it is useful for a plugin to save some variables so that the user does not have to enter or select them again
next time the plugin is run.
These variables can be saved and retrieved with help of Qt and QGIS API. For each variable, you should pick a key
that will be used to access the variable — for user’s favourite color you could use key “favourite_color” or any other
meaningful string. It is recommended to give some structure to naming of keys.
We can differentiate between several types of settings:
• global settings — they are bound to the user at a particular machine. QGIS itself stores a lot of global settings,
for example, main window size or default snapping tolerance. Settings are handled using the QgsSettings
class, through for example the setValue() and value() methods.
Here you can see an example of how these methods are used.
1 def store():
2 s = QgsSettings()
3 s.setValue("myplugin/mytext", "hello world")
4 s.setValue("myplugin/myint", 10)
5 s.setValue("myplugin/myreal", 3.14)
6
7 def read():
8 s = QgsSettings()
9 mytext = s.value("myplugin/mytext", "default text")
10 myint = s.value("myplugin/myint", 123)
11 myreal = s.value("myplugin/myreal", 2.71)
12 nonexistent = s.value("myplugin/nonexistent", None)
13 print(mytext)
14 print(myint)
15 print(myreal)
16 print(nonexistent)
The second parameter of the value() method is optional and specifies the default value that is returned if
there is no previous value set for the passed setting name.
For a method to pre-configure the default values of the global settings through the
qgis_global_settings.ini file, see deploying_organization for further details.
• project settings — vary between different projects and therefore they are connected with a project file. Map
canvas background color or destination coordinate reference system (CRS) are examples — white background
79
PyQGIS testing developer cookbook
and WGS84 might be suitable for one project, while yellow background and UTM projection are better for
another one.
An example of usage follows.
1 proj = QgsProject.instance()
2
3 # store values
4 proj.writeEntry("myplugin", "mytext", "hello world")
5 proj.writeEntry("myplugin", "myint", 10)
6 proj.writeEntryDouble("myplugin", "mydouble", 0.01)
7 proj.writeEntryBool("myplugin", "mybool", True)
8
9 # read values (returns a tuple with the value, and a status boolean
10 # which communicates whether the value retrieved could be converted to
11 # its type, in these cases a string, an integer, a double and a boolean
12 # respectively)
13
As you can see, the writeEntry() method is used for many data types (integer, string, list), but several
methods exist for reading the setting value back, and the corresponding one has to be selected for each data
type.
• map layer settings — these settings are related to a particular instance of a map layer with a project. They
are not connected with underlying data source of a layer, so if you create two map layer instances of one
shapefile, they will not share the settings. The settings are stored inside the project file, so if the user opens the
project again, the layer-related settings will be there again. The value for a given setting is retrieved using the
customProperty() method, and can be set using the setCustomProperty() one.
1 vlayer = QgsVectorLayer()
2 # save a value
3 vlayer.setCustomProperty("mytext", "hello world")
4
THIRTEEN
Hint: The code snippets on this page need the following imports if you’re outside the pyqgis console:
1 from qgis.core import (
2 QgsMessageLog,
3 QgsGeometry,
4 )
5
This section shows some methods and elements that should be used to communicate with the user, in order to keep
consistency in the User Interface.
Using message boxes can be a bad idea from a user experience point of view. For showing a small info line or a
warning/error messages, the QGIS message bar is usually a better option.
Using the reference to the QGIS interface object, you can show a message in the message bar with the following code
from qgis.core import Qgis
iface.messageBar().pushMessage("Error", "I'm sorry Dave, I'm afraid I can't do that
,→", level=Qgis.Critical)
81
PyQGIS testing developer cookbook
The examples above show an error bar, but the level parameter can be used to creating warning messages or info
messages, using the Qgis.MessageLevel enumeration. You can use up to 4 different levels:
0. Info
1. Warning
2. Critical
3. Success
Widgets can be added to the message bar, like for instance a button to show more info
1 def showError():
2 pass
3
You can even use a message bar in your own dialog so you don’t have to show a message box, or if it doesn’t make
sense to show it in the main QGIS window
1 class MyDialog(QDialog):
2 def __init__(self):
3 QDialog.__init__(self)
4 self.bar = QgsMessageBar()
5 self.bar.setSizePolicy( QSizePolicy.Minimum, QSizePolicy.Fixed )
6 self.setLayout(QGridLayout())
7 self.layout().setContentsMargins(0, 0, 0, 0)
8 self.buttonbox = QDialogButtonBox(QDialogButtonBox.Ok)
9 self.buttonbox.accepted.connect(self.run)
10 self.layout().addWidget(self.buttonbox, 0, 0, 2, 1)
11 self.layout().addWidget(self.bar, 0, 0, 1, 1)
12 def run(self):
13 self.bar.pushMessage("Hello", "World", level=Qgis.Info)
14
15 myDlg = MyDialog()
16 myDlg.show()
Progress bars can also be put in the QGIS message bar, since, as we have seen, it accepts widgets. Here is an example
that you can try in the console.
1 import time
2 from qgis.PyQt.QtWidgets import QProgressBar
3 from qgis.PyQt.QtCore import *
4 progressMessageBar = iface.messageBar().createMessage("Doing something boring...")
5 progress = QProgressBar()
6 progress.setMaximum(10)
7 progress.setAlignment(Qt.AlignLeft|Qt.AlignVCenter)
8 progressMessageBar.layout().addWidget(progress)
9 iface.messageBar().pushWidget(progressMessageBar, Qgis.Info)
10
11 for i in range(10):
12 time.sleep(1)
13 progress.setValue(i + 1)
14
15 iface.messageBar().clearWidgets()
Also, you can use the built-in status bar to report progress, as in the next example:
1 vlayer = iface.activeLayer()
2
3 count = vlayer.featureCount()
4 features = vlayer.getFeatures()
5
12 iface.statusBarIface().showMessage("Processed {} %".format(int(percent)))
13
14 iface.statusBarIface().clearMessage()
13.3 Logging
There are three different types of logging available in QGIS to log and save all the information about the execution of
your code. Each has its specific output location. Please consider to use the correct way of logging for your purpose:
• QgsMessageLog is for messages to communicate issues to the user. The output of the QgsMessageLog is
shown in the Log Messages Panel.
• The python built in logging module is for debugging on the level of the QGIS Python API (PyQGIS). It is
recommended for Python script developers that need to debug their python code, e.g. feature ids or geometries
• QgsLogger is for messages for QGIS internal debugging / developers (i.e. you suspect something is triggered
by some broken code). Messages are only visible with developer versions of QGIS.
Examples for the different logging types are shown in the following sections below.
Warning: Use of the Python print statement is unsafe to do in any code which may be multithreaded and
extremely slows down the algorithm. This includes expression functions, renderers, symbol layers and
Processing algorithms (amongst others). In these cases you should always use the python logging module or
thread safe classes (QgsLogger or QgsMessageLog) instead.
13.3.1 QgsMessageLog
Note: You can see the output of the QgsMessageLog in the log_message_panel
1 import logging
2 formatter = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
3 logfilename=r'c:\temp\example.log'
4 logging.basicConfig(filename=logfilename, level=logging.DEBUG, format=formatter)
5 logging.info("This logging info text goes into the file")
6 logging.debug("This logging debug text goes into the file as well")
The basicConfig method configures the basic setup of the logging. In the above code the filename, logging level and
the format are defined. The filename refers to where to write the logfile to, the logging level defines what levels to
output and the format defines the format in which each message is output.
2020-10-08 13:14:42,998 - root - INFO - This logging text goes into the file
2020-10-08 13:14:42,998 - root - DEBUG - This logging debug text goes into the␣
,→file as well
If you want to erase the log file every time you execute your script you can do something like:
if os.path.isfile(logfilename):
with open(logfilename, 'w') as file:
pass
Further resources on how to use the python logging facility are available at:
• https://docs.python.org/3/library/logging.html
• https://docs.python.org/3/howto/logging.html
• https://docs.python.org/3/howto/logging-cookbook.html
Warning: Please note that without logging to a file by setting a filename the logging may be multithreaded which
heavily slows down the output.
13.3. Logging 85
PyQGIS testing developer cookbook
FOURTEEN
AUTHENTICATION INFRASTRUCTURE
Hint: The code snippets on this page need the following imports if you’re outside the pyqgis console:
14.1 Introduction
User reference of the Authentication infrastructure can be read in the User Manual in the authentication_overview
paragraph.
This chapter describes the best practices to use the Authentication system from a developer perspective.
The authentication system is widely used in QGIS Desktop by data providers whenever credentials are required to
access a particular resource, for example when a layer establishes a connection to a Postgres database.
There are also a few widgets in the QGIS gui library that plugin developers can use to easily integrate the authentication
infrastructure into their code:
• QgsAuthConfigEditor
• QgsAuthConfigSelect
• QgsAuthSettingsWidget
A good code reference can be read from the authentication infrastructure tests code.
87
PyQGIS testing developer cookbook
Warning: Due to the security constraints that were taken into account during the authentication infrastructure
design, only a selected subset of the internal methods are exposed to Python.
14.2 Glossary
Here are some definition of the most common objects treated in this chapter.
Master Password
Password to allow access and decrypt credential stored in the QGIS Authentication DB
Authentication Database
A Master Password crypted sqlite db qgis-auth.db where Authentication Configuration are stored. e.g
user/password, personal certificates and keys, Certificate Authorities
Authentication DB
Authentication Database
Authentication Configuration
A set of authentication data depending on Authentication Method. e.g Basic authentication method stores the
couple of user/password.
Authentication Config
Authentication Configuration
Authentication Method
A specific method used to get authenticated. Each method has its own protocol used to gain the authenti-
cated level. Each method is implemented as shared library loaded dynamically during QGIS authentication
infrastructure init.
The QgsAuthManager singleton is the entry point to use the credentials stored in the QGIS encrypted Authenti-
cation DB, i.e. the qgis-auth.db file under the active user profile folder.
This class takes care of the user interaction: by asking to set a master password or by transparently using it to access
encrypted stored information.
The following snippet gives an example to set master password to open the access to the authentication settings. Code
comments are important to understand the snippet.
1 authMgr = QgsApplication.authManager()
2
Any stored credential is a Authentication Configuration instance of the QgsAuthMethodConfig class accessed
using a unique string like the following one:
authcfg = 'fm1s770'
that string is generated automatically when creating an entry using the QGIS API or GUI, but it might be useful to
manually set it to a known value in case the configuration must be shared (with different credentials) between multiple
users within an organization.
QgsAuthMethodConfig is the base class for any Authentication Method. Any Authentication Method sets a
configuration hash map where authentication information will be stored. Hereafter a useful snippet to store PKI-path
credentials for a hypothetical alice user:
1 authMgr = QgsApplication.authManager()
2 # set alice PKI data
3 config = QgsAuthMethodConfig()
4 config.setName("alice")
5 config.setMethod("PKI-Paths")
6 config.setUri("https://example.com")
7 config.setConfig("certpath", "path/to/alice-cert.pem" )
8 config.setConfig("keypath", "path/to/alice-key.pem" )
9 # check if method parameters are correctly set
10 assert config.isValid()
11
Authentication Method libraries are loaded dynamically during authentication manager init. Available authentication
methods are:
1. Basic User and password authentication
2. EsriToken ESRI token based authentication
3. Identity-Cert Identity certificate authentication
4. OAuth2 OAuth2 authentication
5. PKI-Paths PKI paths authentication
6. PKI-PKCS#12 PKI PKCS#12 authentication
Populate Authorities
1 authMgr = QgsApplication.authManager()
2 # add authorities
3 cacerts = QSslCertificate.fromPath( "/path/to/ca_chains.pem" )
4 assert cacerts is not None
5 # store CA
6 authMgr.storeCertAuthorities(cacerts)
7 # and rebuild CA caches
8 authMgr.rebuildCaCertsCache()
9 authMgr.rebuildTrustedCaCertsCache()
A convenience class to pack PKI bundles composed on SslCert, SslKey and CA chain is the QgsPkiBundle class.
Hereafter a snippet to get password protected:
1 # add alice cert in case of key with pwd
2 caBundlesList = [] # List of CA bundles
3 bundle = QgsPkiBundle.fromPemPaths( "/path/to/alice-cert.pem",
4 "/path/to/alice-key_w-pass.pem",
5 "unlock_pwd",
6 caBundlesList )
7 assert bundle is not None
8 # You can check bundle validity by calling:
9 # bundle.isValid()
We can remove an entry from Authentication Database using it’s authcfg identifier with the following snippet:
authMgr = QgsApplication.authManager()
authMgr.removeAuthenticationConfig( "authCfg_Id_to_remove" )
The best way to use an Authentication Config stored in the Authentication DB is referring it with the unique identifier
authcfg. Expanding, means convert it from an identifier to a complete set of credentials. The best practice to use
stored Authentication Configs, is to leave it managed automatically by the Authentication manager. The common use
of a stored configuration is to connect to an authentication enabled service like a WMS or WFS or to a DB connection.
Note: Take into account that not all QGIS data providers are integrated with the Authentication infrastructure. Each
authentication method, derived from the base class QgsAuthMethod and support a different set of Providers. For
example the certIdentity() method supports the following list of providers:
authM = QgsApplication.authManager()
print(authM.authMethod("Identity-Cert").supportedDataProviders())
Sample output:
['ows', 'wfs', 'wcs', 'wms', 'postgres']
For example, to access a WMS service using stored credentials identified with authcfg = 'fm1s770', we just
have to use the authcfg in the data source URL like in the following snippet:
1 authCfg = 'fm1s770'
2 quri = QgsDataSourceUri()
3 quri.setParam("layers", 'usa:states')
4 quri.setParam("styles", '')
5 quri.setParam("format", 'image/png')
6 quri.setParam("crs", 'EPSG:4326')
7 quri.setParam("dpiMode", '7')
8 quri.setParam("featureCount", '10')
9 quri.setParam("authcfg", authCfg) # <---- here my authCfg url parameter
10 quri.setParam("contextualWMSLegend", '0')
11 quri.setParam("url", 'https://my_auth_enabled_server_ip/wms')
12 rlayer = QgsRasterLayer(str(quri.encodedUri(), "utf-8"), 'states', 'wms')
In the upper case, the wms provider will take care to expand authcfg URI parameter with credential just before
setting the HTTP connection.
Warning: The developer would have to leave authcfg expansion to the QgsAuthManager, in this way he
will be sure that expansion is not done too early.
Usually an URI string, built using the QgsDataSourceURI class, is used to set a data source in the following way:
authCfg = 'fm1s770'
quri = QgsDataSourceUri("my WMS uri here")
quri.setParam("authcfg", authCfg)
rlayer = QgsRasterLayer( quri.uri(False), 'states', 'wms')
Note: The False parameter is important to avoid URI complete expansion of the authcfg id present in the URI.
Other example can be read directly in the QGIS tests upstream as in test_authmanager_pki_ows or
test_authmanager_pki_postgres.
Many third party plugins are using httplib2 or other Python networking libraries to manage HTTP connections instead
of integrating with QgsNetworkAccessManager and its related Authentication Infrastructure integration.
To facilitate this integration a helper Python function has been created called NetworkAccessManager. Its code
can be found here.
This helper class can be used as in the following snippet:
2 try:
3 response, content = http.request( "my_rest_url" )
4 except My_FailedRequestError, e:
5 # Handle exception
6 pass
In this paragraph are listed the available GUIs useful to integrate authentication infrastructure in custom interfaces.
If it’s necessary to select a Authentication Configuration from the set stored in the Authentication DB it is available in
the GUI class QgsAuthConfigSelect.
The above example is taken from the QGIS source code. The second parameter of the GUI constructor refers to data
provider type. The parameter is used to restrict the compatible Authentication Methods with the specified provider.
The complete GUI used to manage credentials, authorities and to access to Authentication utilities is managed by the
QgsAuthEditorWidgets class.
FIFTEEN
Hint: The code snippets on this page need the following imports if you’re outside the pyqgis console:
15.1 Introduction
Background processing using threads is a way to maintain a responsive user interface when heavy processing is going
on. Tasks can be used to achieve threading in QGIS.
A task (QgsTask) is a container for the code to be performed in the background, and the task manager
(QgsTaskManager) is used to control the running of the tasks. These classes simplify background processing in
QGIS by providing mechanisms for signaling, progress reporting and access to the status for background processes.
Tasks can be grouped using subtasks.
The global task manager (found with QgsApplication.taskManager()) is normally used. This means that
your tasks may not be the only tasks that are controlled by the task manager.
There are several ways to create a QGIS task:
• Create your own task by extending QgsTask
class SpecialisedTask(QgsTask):
pass
1 def heavyFunction():
2 # Some CPU intensive processing ...
3 pass
4
5 def workdone():
6 # ... do something useful with the results
7 pass
8
95
PyQGIS testing developer cookbook
1 params = dict()
2 context = QgsProcessingContext()
3 context.setProject(QgsProject.instance())
4 feedback = QgsProcessingFeedback()
5
6 buffer_alg = QgsApplication.instance().processingRegistry().algorithmById(
,→'native:buffer')
Warning: Any background task (regardless of how it is created) must NEVER use any QObject that lives
on the main thread, such as accessing QgsVectorLayer, QgsProject or perform any GUI based operations like
creating new widgets or interacting with existing widgets. Qt widgets must only be accessed or modified from the
main thread. Data that is used in a task must be copied before the task is started. Attempting to use them from
background threads will result in crashes.
Moreover always make sure that context and feedback live for at least as long as the tasks that use them.
QGIS will crash if, upon completion of a task, QgsTaskManager fails to access the context and feedback against
which the task was scheduled.
Note: It is a common pattern to call setProject() shortly after calling QgsProcessingContext. This
allows the task as well as its callback function to use most of the project-wide settings. This is especially valuable
when working with spatial layers in the callback function.
Dependencies between tasks can be described using the addSubTask() function of QgsTask. When a depen-
dency is stated, the task manager will automatically determine how these dependencies will be executed. Wherever
possible dependencies will be executed in parallel in order to satisfy them as quickly as possible. If a task on which an-
other task depends is canceled, the dependent task will also be canceled. Circular dependencies can make deadlocks
possible, so be careful.
If a task depends on a layer being available, this can be stated using the setDependentLayers() function of
QgsTask. If a layer on which a task depends is not available, the task will be canceled.
Once the task has been created it can be scheduled for running using the addTask() function of the task manager.
Adding a task to the manager automatically transfers ownership of that task to the manager, and the manager will
cleanup and delete tasks after they have executed. The scheduling of the tasks is influenced by the task priority, which
is set in addTask().
The status of tasks can be monitored using QgsTask and QgsTaskManager signals and functions.
15.2 Examples
In this example RandomIntegerSumTask extends QgsTask and will generate 100 random integers between 0
and 500 during a specified period of time. If the random number is 42, the task is aborted and an exception is raised.
Several instances of RandomIntegerSumTask (with subtasks) are generated and added to the task manager,
demonstrating two types of dependencies.
1 import random
2 from time import sleep
3
8 MESSAGE_CATEGORY = 'RandomIntegerSumTask'
9
10 class RandomIntegerSumTask(QgsTask):
11 """This shows how to subclass QgsTask"""
12
20 def run(self):
21 """Here you implement your heavy lifting.
22 Should periodically test for isCanceled() to gracefully
23 abort.
24 This method MUST return True or False.
25 Raising exceptions will crash QGIS, so we handle them
26 internally and raise them in self.finished
27 """
28 QgsMessageLog.logMessage('Started task "{}"'.format(
29 self.description()),
30 MESSAGE_CATEGORY, Qgis.Info)
31 wait_time = self.duration / 100
32 for i in range(100):
33 sleep(wait_time)
34 # use setProgress to report progress
35 self.setProgress(i)
36 arandominteger = random.randint(0, 500)
37 self.total += arandominteger
38 self.iterations += 1
39 # check isCanceled() to handle cancellation
40 if self.isCanceled():
41 return False
42 # simulate exceptions to show how to abort task
43 if arandominteger == 42:
44 # DO NOT raise Exception('bad value!')
45 # this would crash QGIS
46 self.exception = Exception('bad value!')
47 return False
48 return True
49
15.2. Examples 97
PyQGIS testing developer cookbook
85 def cancel(self):
86 QgsMessageLog.logMessage(
87 'RandomTask "{name}" was canceled'.format(
88 name=self.description()),
89 MESSAGE_CATEGORY, Qgis.Info)
90 super().cancel()
91
92
109 QgsApplication.taskManager().addTask(longtask)
110 QgsApplication.taskManager().addTask(shorttask)
111 QgsApplication.taskManager().addTask(minitask)
Create a task from a function (doSomething in this example). The first parameter of the function will hold the
QgsTask for the function. An important (named) parameter is on_finished, that specifies a function that will
be called when the task has completed. The doSomething function in this example has an additional named
parameter wait_time.
1 import random
2 from time import sleep
3
4 MESSAGE_CATEGORY = 'TaskFromFunction'
5
36 def stopped(task):
37 QgsMessageLog.logMessage(
38 'Task "{name}" was canceled'.format(
39 name=task.description()),
40 MESSAGE_CATEGORY, Qgis.Info)
41
15.2. Examples 99
PyQGIS testing developer cookbook
Create a task that uses the algorithm qgis:randompointsinextent to generate 50000 random points inside a specified
extent. The result is added to the project in a safe way.
7 MESSAGE_CATEGORY = 'AlgRunnerTask'
8
SIXTEEN
Before starting to write a new plugin, have a look at the Official Python plugin repository. The source code of existing
plugins can help you to learn more about programming. You may also find that a similar plugin already exists and
you may be able to extend it or at least build on it to develop your own.
To get started with a new plugin, we need to set up the necessary plugin files.
There are two plugin template resources that can help get you started:
• For educational purposes or whenever a minimalist approach is desired, the minimal plugin template provides
the basic files (skeleton) necessary to create a valid QGIS Python plugin.
• For a more fully feature plugin template, the Plugin Builder can create templates for multiple different plugin
types, including features such as localization (translation) and testing.
A typical plugin directory includes the following files:
• metadata.txt - required - Contains general info, version, name and some other metadata used by plugins
website and plugin infrastructure.
• __init__.py - required - The starting point of the plugin. It has to have the classFactory() method
and may have any other initialisation code.
103
PyQGIS testing developer cookbook
• mainPlugin.py - core code - The main working code of the plugin. Contains all the information about the
actions of the plugin and the main code.
• form.ui - for plugins with custom GUI - The GUI created by Qt Designer.
• form.py - compiled GUI - The translation of the form.ui described above to Python.
• resources.qrc - optional - An .xml document created by Qt Designer. Contains relative paths to resources
used in the GUI forms.
• resources.py - compiled resources, optional - The translation of the .qrc file described above to Python.
Warning: If you plan to upload the plugin to the Official Python plugin repository you must check that your
plugin follows some additional rules, required for plugin Validation.
The following section shows what content should be added in each of the files introduced above.
metadata.txt
First, the Plugin Manager needs to retrieve some basic information about the plugin such as its name, description etc.
This information is stored in metadata.txt.
By default, plugins are placed in the Plugins menu (we will see in the next section how to add a menu entry for your
plugin) but they can also be placed into Raster, Vector, Database, Mesh and Web menus.
A corresponding “category” metadata entry exists to specify that, so the plugin can be classified accordingly. This
metadata entry is used as tip for users and tells them where (in which menu) the plugin can be found. Allowed values
for “category” are: Vector, Raster, Database or Web. For example, if your plugin will be available from Raster menu,
add this to metadata.txt
category=Raster
Note: If qgisMaximumVersion is empty, it will be automatically set to the major version plus .99 when uploaded to
the Official Python plugin repository.
[general]
name=HelloWorld
[email protected]
author=Just Me
qgisMinimumVersion=3.0
description=This is an example plugin for greeting the world.
Multiline is allowed:
lines starting with spaces belong to the same
field, in this case to the "description" field.
(continues on next page)
; Tags are in comma separated value format, spaces are allowed within the
; tag name.
; Tags should be in English language. Please also check for existing tags and
; synonyms before creating a new one.
tags=wkt,raster,hello world
; deprecated flag (applies to the whole plugin and not only to the uploaded␣
,→version)
deprecated=False
__init__.py
This file is required by Python’s import system. Also, QGIS requires that this file contains a classFactory()
function, which is called when the plugin gets loaded into QGIS. It receives a reference to the instance of Qgis-
Interface and must return an object of your plugin’s class from the mainplugin.py — in our case it’s called
TestPlugin (see below). This is how __init__.py should look like
def classFactory(iface):
from .mainPlugin import TestPlugin
return TestPlugin(iface)
mainPlugin.py
This is where the magic happens and this is how magic looks like: (e.g. mainPlugin.py)
class TestPlugin:
def initGui(self):
# create action that will start plugin configuration
self.action = QAction(QIcon("testplug:icon.png"),
"Test plugin",
self.iface.mainWindow())
self.action.setObjectName("testAction")
self.action.setWhatsThis("Configuration for test plugin")
self.action.setStatusTip("This is status tip")
self.action.triggered.connect(self.run)
def unload(self):
# remove the plugin menu item and icon
self.iface.removePluginMenu("&Test plugins", self.action)
self.iface.removeToolBarIcon(self.action)
def run(self):
# create and show a configuration dialog or something similar
print("TestPlugin: run called!")
The only plugin functions that must exist in the main plugin source file (e.g. mainPlugin.py) are:
• __init__ which gives access to QGIS interface
• initGui() called when the plugin is loaded
• unload() called when the plugin is unloaded
In the above example, addPluginToMenu() is used. This will add the corresponding menu action to the Plugins
menu. Alternative methods exist to add the action to a different menu. Here is a list of those methods:
• addPluginToRasterMenu()
• addPluginToVectorMenu()
• addPluginToDatabaseMenu()
• addPluginToWebMenu()
All of them have the same syntax as the addPluginToMenu() method.
Adding your plugin menu to one of those predefined method is recommended to keep consistency in how plugin
entries are organized. However, you can add your custom menu group directly to the menu bar, as the next example
demonstrates:
def initGui(self):
self.menu = QMenu(self.iface.mainWindow())
self.menu.setObjectName("testMenu")
self.menu.setTitle("MyMenu")
self.action = QAction(QIcon("testplug:icon.png"),
"Test plugin",
self.iface.mainWindow())
self.action.setObjectName("testAction")
self.action.setWhatsThis("Configuration for test plugin")
self.action.setStatusTip("This is status tip")
self.action.triggered.connect(self.run)
self.menu.addAction(self.action)
menuBar = self.iface.mainWindow().menuBar()
menuBar.insertMenu(self.iface.firstRightStandardMenu().menuAction(),
self.menu)
def unload(self):
self.menu.deleteLater()
Don’t forget to set QAction and QMenu objectName to a name specific to your plugin so that it can be cus-
tomized.
While help and about actions can also be added to your custom menu, a convenient place to make them available is
in the QGIS main Help ► Plugins menu. This is done using the pluginHelpMenu() method.
def initGui(self):
self.help_action = QAction(
QIcon("testplug:icon.png"),
self.tr("Test Plugin..."),
self.iface.mainWindow()
)
# Add the action to the Help menu
self.iface.pluginHelpMenu().addAction(self.help_action)
self.help_action.triggered.connect(self.show_help)
@staticmethod
def show_help():
""" Open the online help. """
QDesktopServices.openUrl(QUrl('https://docs.qgis.org'))
def unload(self):
self.iface.pluginHelpMenu().removeAction(self.help_action)
del self.help_action
When working on a real plugin it’s wise to write the plugin in another (working) directory and create a makefile which
will generate UI + resource files and install the plugin into your QGIS installation.
The documentation for the plugin can be written as HTML help files. The qgis.utils module provides a function,
showPluginHelp() which will open the help file browser, in the same way as other QGIS help.
The showPluginHelp() function looks for help files in the same directory as the calling module. It will look for,
in turn, index-ll_cc.html, index-ll.html, index-en.html, index-en_us.html and index.
html, displaying whichever it finds first. Here ll_cc is the QGIS locale. This allows multiple translations of the
documentation to be included with the plugin.
The showPluginHelp() function can also take parameters packageName, which identifies a specific plugin for
which the help will be displayed, filename, which can replace “index” in the names of files being searched, and section,
which is the name of an html anchor tag in the document on which the browser will be positioned.
With a few steps you can set up the environment for the plugin localization so that depending on the locale settings
of your computer the plugin will be loaded in different languages.
Software requirements
The easiest way to create and manage all the translation files is to install Qt Linguist. In a Debian-based GNU/Linux
environment you can install it typing:
When you create the plugin you will find the i18n folder within the main plugin directory.
All the translation files have to be within this directory.
.pro file
First you should create a .pro file, that is a project file that can be managed by Qt Linguist.
In this .pro file you have to specify all the files and forms you want to translate. This file is used to set up the
localization files and variables. A possible project file, matching the structure of our example plugin:
FORMS = ../form.ui
SOURCES = ../your_plugin.py
TRANSLATIONS = your_plugin_it.ts
Your plugin might follow a more complex structure, and it might be distributed across several files. If this is the case,
keep in mind that pylupdate5, the program we use to read the .pro file and update the translatable string, does
not expand wild card characters, so you need to place every file explicitly in the .pro file. Your project file might
then look like something like this:
Furthermore, the your_plugin.py file is the file that calls all the menu and sub-menus of your plugin in the
QGIS toolbar and you want to translate them all.
Finally with the TRANSLATIONS variable you can specify the translation languages you want.
Warning: Be sure to name the ts file like your_plugin_ + language + .ts otherwise the language
loading will fail! Use the 2 letter shortcut for the language (it for Italian, de for German, etc…)
.ts file
Once you have created the .pro you are ready to generate the .ts file(s) for the language(s) of your plugin.
Open a terminal, go to your_plugin/i18n directory and type:
pylupdate5 your_plugin.pro
.qm file
When you finish to translate your plugin (if some strings are not completed the source language for those strings will
be used) you have to create the .qm file (the compiled .ts file that will be used by QGIS).
Just open a terminal cd in your_plugin/i18n directory and type:
lrelease your_plugin.ts
now, in the i18n directory you will see the your_plugin.qm file(s).
Alternatively you can use the makefile to extract messages from python code and Qt dialogs, if you created your
plugin with Plugin Builder. At the beginning of the Makefile there is a LOCALES variable:
LOCALES = en
Add the abbreviation of the language to this variable, for example for Hungarian language:
LOCALES = en hu
Now you can generate or update the hu.ts file (and the en.ts too) from the sources by:
make transup
After this, you have updated .ts file for all languages set in the LOCALES variable. Use Qt Linguist to translate
the program messages. Finishing the translation the .qm files can be created by the transcompile:
make transcompile
In order to see the translation of your plugin, open QGIS, change the language (Settings ► Options ► General) and
restart QGIS.
You should see your plugin in the correct language.
Warning: If you change something in your plugin (new UIs, new menu, etc..) you have to generate again the
update version of both .ts and .qm file, so run again the command of above.
QGIS is hosting hundreds of plugins in the plugin repository. Consider sharing yours! It will extend the possibilities
of QGIS and people will be able to learn from your code. All hosted plugins can be found and installed from within
QGIS with the Plugin Manager.
Information and requirements are here: plugins.qgis.org.
Plugin Reloader
During development of your plugin you will frequently need to reload it in QGIS for testing. This is very easy using
the Plugin Reloader plugin. You can find it with the Plugin Manager.
qgis-plugin-ci provides a command line interface to perform automated packaging and deployment for QGIS plug-
ins on your computer, or using continuous integration like GitHub workflows or Gitlab-CI as well as Transifex for
translation.
It allows releasing, translating, publishing or generating an XML plugin repository file via CLI or in CI actions.
Accessing Plugins
You can access all the classes of installed plugins from within QGIS using python, which can be handy for debugging
purposes.
Log Messages
Resource File
Some plugins use resource files, for example resources.qrc which define resources for the GUI, such as icons:
<RCC>
<qresource prefix="/plugins/testplug" >
<file>icon.png</file>
</qresource>
</RCC>
It is good to use a prefix that will not collide with other plugins or any parts of QGIS, otherwise you might get
resources you did not want. Now you just need to generate a Python file that will contain the resources. It’s done
with pyrcc5 command:
Note: In Windows environments, attempting to run the pyrcc5 from Command Prompt or Powershell will proba-
bly result in the error “Windows cannot access the specified device, path, or file […]”. The easiest solution is probably
to use the OSGeo4W Shell but if you are comfortable modifying the PATH environment variable or specifiying the
path to the executable explicitly you should be able to find it at <Your QGIS Install Directory>\bin\
pyrcc5.exe.
Hint: The code snippets on this page need the following imports if you’re outside the pyqgis console:
To unload() add
self.iface.unregisterMainWindowAction(self.key_action)
It is also possible to allow users to customize key shortcuts for the provided actions. This is done by adding:
1 # in the initGui() function
2 QgsGui.shortcutsManager().registerAction(self.key_action)
3
Because they are well-known and convey a clear message to the users, you may want sometimes to reuse QGIS icons
in your plugin instead of drawing and setting a new one. Use the getThemeIcon() method.
For example, to reuse the mActionFileOpen.svg icon available in the QGIS code repository:
1 # e.g. somewhere in the initGui
2 self.file_open_action = QAction(
3 QgsApplication.getThemeIcon("/mActionFileOpen.svg"),
4 self.tr("Select a File..."),
5 self.iface.mainWindow()
6 )
7 self.iface.addPluginToMenu("MyPlugin", self.file_open_action)
iconPath() is another method to call QGIS icons. Find examples of calls to theme icons at QGIS embedded
images - Cheatsheet.
You can add a custom plugin options tab to Settings ► Options. This is preferable over adding a specific main menu
entry for your plugin’s options, as it keeps all of the QGIS application settings and plugin settings in a single place
which is easy for users to discover and navigate.
The following snippet will just add a new blank tab for the plugin’s settings, ready for you to populate with all the
options and settings specific to your plugin. You can split the following classes into different files. In this example,
we are adding two classes into the main mainPlugin.py file.
1 class MyPluginOptionsFactory(QgsOptionsWidgetFactory):
2
3 def __init__(self):
4 super().__init__()
(continues on next page)
6 def icon(self):
7 return QIcon('icons/my_plugin_icon.svg')
8
12
13 class ConfigOptionsPage(QgsOptionsPageWidget):
14
Finally we are adding the imports and modifying the __init__ function:
5 class MyPlugin:
6 """QGIS Plugin Implementation."""
7
19
20 def initGui(self):
21 self.options_factory = MyPluginOptionsFactory()
22 self.options_factory.setTitle(self.tr('My Plugin'))
23 iface.registerOptionsWidgetFactory(self.options_factory)
24
25 def unload(self):
26 iface.unregisterOptionsWidgetFactory(self.options_factory)
Beside usual layer symbology elements displayed next or below the layer entry in the Layers panel, you can add your
own widgets, allowing for quick access to some actions that are often used with a layer (setup filtering, selection,
style, refreshing a layer with a button widget, create a layer based time slider or just show extra layer information in a
Label there, or …). These so-called Layer tree embedded widgets are made available through the layer’s properties
Legend tab for individual layers.
The following code snippet creates a drop-down in the legend which shows you the layer-styles available for the layer,
allowing to quickly switch between the different layer styles.
1 class LayerStyleComboBox(QComboBox):
2 def __init__(self, layer):
3 QComboBox.__init__(self)
4 self.layer = layer
5 for style_name in layer.styleManager().styles():
6 self.addItem(style_name)
7
8 idx = self.findText(layer.styleManager().currentStyle())
9 if idx != -1:
10 self.setCurrentIndex(idx)
11
12 self.currentIndexChanged.connect(self.on_current_changed)
13
17 class LayerStyleWidgetProvider(QgsLayerTreeEmbeddedWidgetProvider):
18 def __init__(self):
19 QgsLayerTreeEmbeddedWidgetProvider.__init__(self)
20
21 def id(self):
22 return "style"
23
24 def name(self):
25 return "Layer style chooser"
26
33 provider = LayerStyleWidgetProvider()
34 QgsGui.layerTreeEmbeddedWidgetRegistry().addProvider(provider)
Then from a given layer’s Legend properties tab, drag the Layer style chooser from the Available widgets
to Used widgets to enable the widget in the layer tree. Embedded widgets are ALWAYS displayed at the top of their
associated layer node subitems.
If you want to use the widgets from within e.g. a plugin, you can add them like this:
1 layer = iface.activeLayer()
2 counter = int(layer.customProperty("embeddedWidgets/count", 0))
3 layer.setCustomProperty("embeddedWidgets/count", counter+1)
4 layer.setCustomProperty("embeddedWidgets/{}/id".format(counter), "style")
5 view = self.iface.layerTreeView()
6 view.layerTreeModel().refreshLayerLegend(view.currentLegendNode())
7 view.currentNode().setExpanded(True)
Although each programmer has his preferred IDE/Text editor, here are some recommendations for setting up popular
IDE’s for writing and debugging QGIS Python plugins.
Some plugins are convenient when writing Python plugins. From Plugins ► Manage and Install plugins…, install:
• Plugin reloader: This will let you reload a plugin and pull new changes without restarting QGIS.
• First Aid: This will add a Python console and local debugger to inspect variables when an exception is raised
from a plugin.
Warning: Despite our constant efforts, information beyond this line may not be updated for QGIS 3.
On Linux, all that usually needs to be done is to add the QGIS library locations to the user’s PYTHONPATH environ-
ment variable. Under most distributions, this can be done by editing ~/.bashrc or ~/.bash-profile with
the following line (tested on OpenSUSE Tumbleweed):
export PYTHONPATH="$PYTHONPATH:/usr/share/qgis/python/plugins:/usr/share/qgis/
,→python"
Save the file and implement the environment settings by using the following shell command:
source ~/.bashrc
On Windows, you need to make sure that you have the same environment settings and use the same libraries and
interpreter as QGIS. The fastest way to do this is to modify the startup batch file of QGIS.
If you used the OSGeo4W Installer, you can find this under the bin folder of your OSGeo4W install. Look for
something like C:\OSGeo4W\bin\qgis-unstable.bat.
@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
call "%OSGEO4W_ROOT%"\bin\gdal16.bat
@echo off
path %PATH%;%GISBASE%\bin
Start C:\pyscripter\pyscripter.exe --python25 --pythondllpath=C:\OSGeo4W\bin
5. Now when you double click this batch file it will start Pyscripter, with the correct path.
More popular than Pyscripter, Eclipse is a common choice among developers. In the following section, we will be
explaining how to configure it for developing and testing plugins.
Installation
call "C:\OSGeo4W\bin\o4w_env.bat"
set PATH=%PATH%;C:\path\to\your\qgis_core.dll\parent\folder
start /B C:\path\to\your\eclipse.exe
Setting up Eclipse
1. In Eclipse, create a new project. You can select General Project and link your real sources later on, so it does
not really matter where you place this project.
A very interesting thing you can make use of now is the debug console. Make sure that the execution is currently
stopped at a break point, before you proceed.
1. Open the Console view (Window ► Show view). It will show the Debug Server console which is not very
interesting. But there is a button Open Console which lets you change to a more interesting PyDev Debug
Console.
2. Click the arrow next to the Open Console button and choose PyDev Console. A window opens up to ask you
which console you want to start.
3. Choose PyDev Debug Console. In case its greyed out and tells you to Start the debugger and select the valid
frame, make sure that you’ve got the remote debugger attached and are currently on a breakpoint.
You have now an interactive console which lets you test any commands from within the current context. You can
manipulate variables or make API calls or whatever you like.
Tip: A little bit annoying is, that every time you enter a command, the console switches back to the Debug Server.
To stop this behavior, you can click the Pin Console button when on the Debug Server page and it should remember
this decision at least for the current debug session.
A very handy feature is to have Eclipse actually know about the QGIS API. This enables it to check your code for
typos. But not only this, it also enables Eclipse to help you with autocompletion from the imports to API calls.
To do this, Eclipse parses the QGIS library files and gets all the information out there. The only thing you have to do
is to tell Eclipse where to find the libraries.
1. Click Window ► Preferences ► PyDev ► Interpreter ► Python.
You will see your configured python interpreter in the upper part of the window (at the moment python2.7 for
QGIS) and some tabs in the lower part. The interesting tabs for us are Libraries and Forced Builtins.
5. Next jump to the Forced Builtins tab, click on New… and enter qgis. This will make Eclipse parse the QGIS
API. You probably also want Eclipse to know about the PyQt API. Therefore also add PyQt as forced builtin.
That should probably already be present in your libraries tab.
6. Click OK and you’re done.
Note: Every time the QGIS API changes (e.g. if you’re compiling QGIS master and the SIP file changed), you
should go back to this page and simply click Apply. This will let Eclipse parse all the libraries again.
PyCharm is an IDE for Python developed by JetBrains. There is a free version called Community Edition and a paid
one called Professional. You can download PyCharm on the website: https://www.jetbrains.com/pycharm/download
We are assuming that you have compiled QGIS on Ubuntu with the given build directory ~/dev/qgis/build/
master. It’s not compulsory to have a self compiled QGIS, but only this has been tested. Paths must be adapted.
1. In PyCharm, in your Project Properties, Project Interpreter, we are going to create a Python Virtual environment
called QGIS.
2. Click the small gear and then Add.
3. Select Virtualenv environment.
4. Select a generic location for all your Python projects such as ~/dev/qgis/venv because we will use this
Python interpreter for all our plugins.
5. Choose a Python 3 base interpreter available on your system and check the next two options Inherit global
site-packages and Make available to all projects.
1. Click OK, come back on the small gear and click Show all.
2. In the new window, select your new interpreter QGIS and click the last icon in the vertical menu Show paths
for the selected interpreter.
3. Finally, add the following absolute path to the list ~/dev/qgis/build/master/output/python.
1. Restart PyCharm and you can start using this new Python virtual environment for all your plugins.
PyCharm will be aware of the QGIS API and also of the PyQt API if you use Qt provided by QGIS like from
qgis.PyQt.QtCore import QDir. The autocompletion should work and PyCharm can inspect your code.
In the professional version of PyCharm, remote debugging is working well. For the Community edition, remote
debugging is not available. You can only have access to a local debugger, meaning that the code must run inside
PyCharm (as script or unittest), not in QGIS itself. For Python code running in QGIS, you might use the First Aid
plugin mentioned above.
If you do not use an IDE such as Eclipse or PyCharm, you can debug using PDB, following these steps.
1. First add this code in the spot where you would like to debug
$ ./Qgis
On macOS do:
$ /Applications/Qgis.app/Contents/MacOS/Qgis
3. And when the application hits your breakpoint you can type in the console!
TODO:
Add testing information
Once your plugin is ready and you think the plugin could be helpful for some people, do not hesitate to upload it to
Official Python plugin repository. On that page you can also find packaging guidelines about how to prepare the plugin
to work well with the plugin installer. Or in case you would like to set up your own plugin repository, create a simple
XML file that will list the plugins and their metadata.
Please take special care to the following suggestions:
• do not include generated file (ui_*.py, resources_rc.py, generated help files…) and useless stuff (e.g. .gitignore)
in repository
• add the plugin to the appropriate menu (Vector, Raster, Web, Database)
• when appropriate (plugins performing analyses), consider adding the plugin as a subplugin of Processing frame-
work: this will allow users to run it in batch, to integrate it in more complex workflows, and will free you from
the burden of designing an interface
• include at least minimal documentation and, if useful for testing and understanding, sample data.
Permissions
Trust management
Staff members can grant trust to selected plugin creators setting plugins.can_approve permission through the front-end
application.
The plugin details view offers direct links to grant trust to the plugin creator or the plugin owners.
Validation
Plugin’s metadata are automatically imported and validated from the compressed package when the plugin is uploaded.
Here are some validation rules that you should aware of when you want to upload a plugin on the official repository:
1. the name of the main folder containing your plugin must contain only ASCII characters (A-Z and a-z), digits
and the characters underscore (_) and minus (-), also it cannot start with a digit
2. metadata.txt is required
3. all required metadata listed in metadata table must be present
4. the version metadata field must be unique
Plugin structure
Following the validation rules the compressed (.zip) package of your plugin must have a specific structure to validate
as a functional plugin. As the plugin will be unzipped inside the users plugins folder it must have it’s own directory
inside the .zip file to not interfere with other plugins. Mandatory files are: metadata.txt and __init__.py.
But it would be nice to have a README and of course an icon to represent the plugin (resources.qrc). Following
is an example of how a plugin.zip should look like.
plugin.zip
pluginfolder/
|-- i18n
| |-- translation_file_de.ts
|-- img
| |-- icon.png
| `-- iconsource.svg
|-- __init__.py
|-- Makefile
|-- metadata.txt
|-- more_code.py
|-- main_code.py
|-- README
|-- resources.qrc
|-- resources_rc.py
`-- ui_Qt_user_interface_file.ui
It is possible to create plugins in the Python programming language. In comparison with classical plugins written in
C++ these should be easier to write, understand, maintain and distribute due to the dynamic nature of the Python
language.
Python plugins are listed together with C++ plugins in QGIS plugin manager. They are searched for in ~/
(UserProfile)/python/plugins and these paths:
• UNIX/Mac: (qgis_prefix)/share/qgis/python/plugins
• Windows: (qgis_prefix)/python/plugins
For definitions of ~ and (UserProfile) see core_and_external_plugins.
Note: By setting QGIS_PLUGINPATH to an existing directory path, you can add this path to the list of paths that
are searched for plugins.
SEVENTEEN
Depending on the kind of plugin that you are going to develop, it might be a better option to add its functionality as a
Processing algorithm (or a set of them). That would provide a better integration within QGIS, additional functionality
(since it can be run in the components of Processing, such as the modeler or the batch processing interface), and a
quicker development time (since Processing will take of a large part of the work).
To distribute those algorithms, you should create a new plugin that adds them to the Processing Toolbox. The plugin
should contain an algorithm provider, which has to be registered when the plugin is instantiated.
To create a plugin from scratch which contains an algorithm provider, you can follow these steps using the Plugin
Builder:
1. Install the Plugin Builder plugin
2. Create a new plugin using the Plugin Builder. When the Plugin Builder asks you for the template to use, select
“Processing provider”.
3. The created plugin contains a provider with a single algorithm. Both the provider file and the algorithm file
are fully commented and contain information about how to modify the provider and add additional algorithms.
Refer to them for more information.
If you want to add your existing plugin to Processing, you need to add some code.
1. In your metadata.txt file, you need to add a variable:
hasProcessingProvider=yes
2. In the Python file where your plugin is setup with the initGui method, you need to adapt some lines like
this:
1 from qgis.core import QgsApplication
2 from processing_provider.provider import Provider
3
4 class YourPluginName():
5
6 def __init__(self):
7 self.provider = None
8
9 def initProcessing(self):
10 self.provider = Provider()
11 QgsApplication.processingRegistry().addProvider(self.provider)
12
127
PyQGIS testing developer cookbook
16 def unload(self):
17 QgsApplication.processingRegistry().removeProvider(self.provider)
6 class Provider(QgsProcessingProvider):
7
29 def icon(self):
30 """Should return a QIcon which is used for your provider inside
31 the Processing toolbox.
32 """
33 return QgsProcessingProvider.icon(self)
EIGHTEEN
Hint: The code snippets on this page need the following imports if you’re outside the pyqgis console:
If your plugin uses its own methods to render a map layer, writing your own layer type based on QgsPluginLayer
might be the best way to implement that.
Below is an example of a minimal QgsPluginLayer implementation. It is based on the original code of the Watermark
example plugin.
The custom renderer is the part of the implement that defines the actual drawing on the canvas.
1 class WatermarkLayerRenderer(QgsMapLayerRenderer):
2
6 def render(self):
7 image = QImage("/usr/share/icons/hicolor/128x128/apps/qgis.png")
8 painter = self.renderContext().painter()
9 painter.save()
10 painter.drawImage(10, 10, image)
11 painter.restore()
12 return True
13
14 class WatermarkPluginLayer(QgsPluginLayer):
15
16 LAYER_TYPE="watermark"
17
18 def __init__(self):
19 super().__init__(WatermarkPluginLayer.LAYER_TYPE, "Watermark plugin layer")
20 self.setValid(True)
21
129
PyQGIS testing developer cookbook
28 # Methods for reading and writing specific information to the project file can
29 # also be added:
30
The plugin layer can be added to the project and to the canvas as any other map layer:
plugin_layer = WatermarkPluginLayer()
QgsProject.instance().addMapLayer(plugin_layer)
1 class WatermarkPluginLayerType(QgsPluginLayerType):
2
3 def __init__(self):
4 super().__init__(WatermarkPluginLayer.LAYER_TYPE)
5
6 def createLayer(self):
7 return WatermarkPluginLayer()
8
9 # You can also add GUI code for displaying custom information
10 # in the layer properties
11 def showLayerProperties(self, layer):
12 pass
13
14
19 assert QgsApplication.pluginLayerRegistry().addPluginLayerType(plt)
NINETEEN
Hint: The code snippets on this page need the following imports if you’re outside the pyqgis console:
The first thing you need to do — is to prepare input data, that is to convert a vector layer into a graph. All further
actions will use this graph, not the layer.
As a source we can use any polyline vector layer. Nodes of the polylines become graph vertexes, and segments of the
polylines are graph edges. If several nodes have the same coordinates then they are the same graph vertex. So two
lines that have a common node become connected to each other.
Additionally, during graph creation it is possible to “fix” (“tie”) to the input vector layer any number of additional
points. For each additional point a match will be found — the closest graph vertex or closest graph edge. In the latter
case the edge will be split and a new vertex added.
Vector layer attributes and length of an edge can be used as the properties of an edge.
Converting from a vector layer to the graph is done using the Builder programming pattern. A graph is constructed
using a so-called Director. There is only one Director for now: QgsVectorLayerDirector. The director sets
the basic settings that will be used to construct a graph from a line vector layer, used by the builder to create the graph.
Currently, as in the case with the director, only one builder exists: QgsGraphBuilder, that creates QgsGraph
131
PyQGIS testing developer cookbook
objects. You may want to implement your own builders that will build a graph compatible with such libraries as BGL
or NetworkX.
To calculate edge properties the programming pattern strategy is used. For now only QgsNetworkDistanceS-
trategy strategy (that takes into account the length of the route) and QgsNetworkSpeedStrategy (that also
considers the speed) are availabile. You can implement your own strategy that will use all necessary parameters.
For example, RoadGraph plugin uses a strategy that computes travel time using edge length and speed value from
attributes.
It’s time to dive into the process.
First of all, to use this library we should import the analysis module
To construct a director, we should pass a vector layer that will be used as the source for the graph structure and
information about allowed movement on each road segment (one-way or bidirectional movement, direct or reverse
direction). The call looks like this
1 director = QgsVectorLayerDirector(vectorLayer,
2 directionFieldId,
3 directDirectionValue,
4 reverseDirectionValue,
5 bothDirectionValue,
6 defaultDirection)
1 # The index of the field that contains information about the edge speed
2 attributeId = 1
3 # Default speed value
4 defaultValue = 50
5 # Conversion from speed to metric units ('1' means no conversion)
6 toMetricFactor = 1
7 strategy = QgsNetworkSpeedStrategy(attributeId, defaultValue, toMetricFactor)
Now we can use the builder, which will create the graph. The QgsGraphBuilder class constructor takes several
arguments:
• crs — coordinate reference system to use. Mandatory argument.
• otfEnabled — use “on the fly” reprojection or no. By default True (use OTF).
• topologyTolerance — topological tolerance. Default value is 0.
• ellipsoidID — ellipsoid to use. By default “WGS84”.
Also we can define several points, which will be used in the analysis. For example
Now all is in place so we can build the graph and “tie” these points to it
Building the graph can take some time (which depends on the number of features in a layer and layer size). tied-
Points is a list with coordinates of “tied” points. When the build operation is finished we can get the graph and
use it for the analysis
graph = builder.graph()
With the next code we can get the vertex indexes of our points
startId = graph.findVertex(tiedPoints[0])
endId = graph.findVertex(tiedPoints[1])
Networks analysis is used to find answers to two questions: which vertexes are connected and how to find a shortest
path. To solve these problems the network analysis library provides Dijkstra’s algorithm.
Dijkstra’s algorithm finds the shortest route from one of the vertexes of the graph to all the others and the values of
the optimization parameters. The results can be represented as a shortest path tree.
The shortest path tree is a directed weighted graph (or more precisely a tree) with the following properties:
• only one vertex has no incoming edges — the root of the tree
• all other vertexes have only one incoming edge
• if vertex B is reachable from vertex A, then the path from A to B is the single available path and it is optimal
(shortest) on this graph
To get the shortest path tree use the methods shortestTree() and dijkstra() of the QgsGraphAna-
lyzer class. It is recommended to use the dijkstra() method because it works faster and uses memory more
efficiently.
The shortestTree() method is useful when you want to walk around the shortest path tree. It always creates a
new graph object (QgsGraph) and accepts three variables:
• source — input graph
• startVertexIdx — index of the point on the tree (the root of the tree)
• criterionNum — number of edge property to use (started from 0).
The dijkstra() method has the same arguments, but returns two arrays. In the first array element n contains
index of the incoming edge or -1 if there are no incoming edges. In the second array element n contains the distance
from the root of the tree to vertex n or DOUBLE_MAX if vertex n is unreachable from the root.
Here is some very simple code to display the shortest path tree using the graph created with the shortestTree()
method (select linestring layer in Layers panel and replace coordinates with your own).
Warning: Use this code only as an example, it creates a lot of QgsRubberBand objects and may be slow on
large datasets.
7 vectorLayer = QgsVectorLayer('testdata/network.gpkg|layername=network_lines',
,→'lines')
9 strategy = QgsNetworkDistanceStrategy()
10 director.addStrategy(strategy)
11 builder = QgsGraphBuilder(vectorLayer.crs())
12
13 pStart = QgsPointXY(1179661.925139,5419188.074362)
14 tiedPoint = director.makeGraph(builder, [pStart])
15 pStart = tiedPoint[0]
16
17 graph = builder.graph()
18
19 idStart = graph.findVertex(pStart)
20
23 i = 0
24 while (i < tree.edgeCount()):
25 rb = QgsRubberBand(iface.mapCanvas())
26 rb.setColor (Qt.red)
27 rb.addPoint (tree.vertex(tree.edge(i).fromVertex()).point())
28 rb.addPoint (tree.vertex(tree.edge(i).toVertex()).point())
29 i = i + 1
7 vectorLayer = QgsVectorLayer('testdata/network.gpkg|layername=network_lines',
,→'lines')
10 strategy = QgsNetworkDistanceStrategy()
11 director.addStrategy(strategy)
12 builder = QgsGraphBuilder(vectorLayer.crs())
13
14 pStart = QgsPointXY(1179661.925139,5419188.074362)
15 tiedPoint = director.makeGraph(builder, [pStart])
16 pStart = tiedPoint[0]
17
18 graph = builder.graph()
19
20 idStart = graph.findVertex(pStart)
21
To find the optimal path between two points the following approach is used. Both points (start A and end B) are “tied”
to the graph when it is built. Then using the shortestTree() or dijkstra() method we build the shortest
path tree with root in the start point A. In the same tree we also find the end point B and start to walk through the
tree from point B to point A. The whole algorithm can be written as:
1 assign T = B
2 while T != B
3 add point T to path
4 get incoming edge for point T
5 look for point TT, that is start point of this edge
6 assign T = TT
7 add point A to path
At this point we have the path, in the form of the inverted list of vertexes (vertexes are listed in reversed order from
end point to start point) that will be visited during traveling by this path.
Here is the sample code for QGIS Python Console (you may need to load and select a linestring layer in TOC and
replace coordinates in the code with yours) that uses the shortestTree() method
1 from qgis.core import *
2 from qgis.gui import *
3 from qgis.analysis import *
4
8 vectorLayer = QgsVectorLayer('testdata/network.gpkg|layername=network_lines',
,→'lines')
9 builder = QgsGraphBuilder(vectorLayer.sourceCrs())
10 director = QgsVectorLayerDirector(vectorLayer, -1, '', '', '',␣
,→QgsVectorLayerDirector.DirectionBoth)
11 strategy = QgsNetworkDistanceStrategy()
12 director.addStrategy(strategy)
13
14 startPoint = QgsPointXY(1179661.925139,5419188.074362)
15 endPoint = QgsPointXY(1180942.970617,5420040.097560)
16
20 graph = builder.graph()
21 idxStart = graph.findVertex(tStart)
22
25 idxStart = tree.findVertex(tStart)
26 idxEnd = tree.findVertex(tStop)
27
28 if idxEnd == -1:
29 raise Exception('No route!')
30
43 # Display
44 rb = QgsRubberBand(iface.mapCanvas())
45 rb.setColor(Qt.green)
46
And here is the same sample but using the dijkstra() method
8 vectorLayer = QgsVectorLayer('testdata/network.gpkg|layername=network_lines',
,→'lines')
13 builder = QgsGraphBuilder(vectorLayer.sourceCrs())
14
15 startPoint = QgsPointXY(1179661.925139,5419188.074362)
16 endPoint = QgsPointXY(1180942.970617,5420040.097560)
17
21 graph = builder.graph()
22 idxStart = graph.findVertex(tStart)
23 idxEnd = graph.findVertex(tStop)
24
27 if tree[idxEnd] == -1:
28 raise Exception('No route!')
29
30 # Total cost
31 cost = costs[idxEnd]
32
41 # Display
42 rb = QgsRubberBand(iface.mapCanvas())
43 rb.setColor(Qt.red)
44
The area of availability for vertex A is the subset of graph vertexes that are accessible from vertex A and the cost of
the paths from A to these vertexes are not greater that some value.
More clearly this can be shown with the following example: “There is a fire station. Which parts of city can a fire
truck reach in 5 minutes? 10 minutes? 15 minutes?”. Answers to these questions are fire station’s areas of availability.
To find the areas of availability we can use the dijkstra() method of the QgsGraphAnalyzer class. It is
enough to compare the elements of the cost array with a predefined value. If cost[i] is less than or equal to a predefined
value, then vertex i is inside the area of availability, otherwise it is outside.
A more difficult problem is to get the borders of the area of availability. The bottom border is the set of vertexes
that are still accessible, and the top border is the set of vertexes that are not accessible. In fact this is simple: it is the
availability border based on the edges of the shortest path tree for which the source vertex of the edge is accessible
and the target vertex of the edge is not.
Here is an example
2 strategy = QgsNetworkDistanceStrategy()
3 director.addStrategy(strategy)
4 builder = QgsGraphBuilder(vectorLayer.crs())
5
10 rb = QgsRubberBand(iface.mapCanvas())
11 rb.setColor(Qt.green)
12 rb.addPoint(QgsPointXY(pStart.x() - delta, pStart.y() - delta))
13 rb.addPoint(QgsPointXY(pStart.x() + delta, pStart.y() - delta))
14 rb.addPoint(QgsPointXY(pStart.x() + delta, pStart.y() + delta))
15 rb.addPoint(QgsPointXY(pStart.x() - delta, pStart.y() + delta))
16
21 idStart = graph.findVertex(tStart)
22
25 upperBound = []
26 r = 1500.0
27 i = 0
28 tree.reverse()
29
37 for i in upperBound:
38 centerPoint = graph.vertex(i).point()
39 rb = QgsRubberBand(iface.mapCanvas())
40 rb.setColor(Qt.red)
41 rb.addPoint(QgsPointXY(centerPoint.x() - delta, centerPoint.y() - delta))
42 rb.addPoint(QgsPointXY(centerPoint.x() + delta, centerPoint.y() - delta))
43 rb.addPoint(QgsPointXY(centerPoint.x() + delta, centerPoint.y() + delta))
44 rb.addPoint(QgsPointXY(centerPoint.x() - delta, centerPoint.y() + delta))
TWENTY
20.1 Introduction
139
PyQGIS testing developer cookbook
Inside the QgsServer.handleRequest(request, response) method the filter plugins callbacks are
called and QgsServerRequest and QgsServerResponse are made available to the plugins through the
QgsServerInterface class.
Warning: QGIS server classes are not thread safe, you should always use a multiprocessing model or containers
when building scalable applications based on QGIS Server API.
For standalone server applications or embedding, you will need to use the above mentioned server classes directly,
wrapping them up into a web server implementation that manages all the HTTP protocol interactions with the client.
A minimal example of the QGIS Server API usage (without the HTTP part) follows:
9 # Create the request by specifying the full URL and an optional body
10 # (for example for POST requests)
11 request = QgsBufferServerRequest(
12 'http://localhost:8081/?MAP=/qgis-server/projects/helloworld.qgs' +
13 '&SERVICE=WMS&REQUEST=GetCapabilities')
14
21 print(response.headers())
22 print(response.body().data().decode('utf8'))
23
24 app.exitQgis()
Here is a complete standalone application example developed for the continuous integrations testing on QGIS source
code repository, it showcases a wide set of different plugin filters and authentication schemes (not mean for production
because they were developed for testing purposes only but still interesting for learning): qgis_wrapped_server.py
Server python plugins are loaded once when the QGIS Server application starts and can be used to register filters,
services or APIs.
The structure of a server plugin is very similar to their desktop counterpart, a QgsServerInterface object
is made available to the plugins and the plugins can register one or more custom filters, services or APIs to the
corresponding registry by using one of the methods exposed by the server interface.
Filters come in three different flavors and they can be instanciated by subclassing one of the classes below and by
calling the corresponding method of QgsServerInterface:
I/O filters
I/O filters can modify the server input and output (the request and the response) of the core services (WMS, WFS
etc.) allowing to do any kind of manipulation of the services workflow. It is possible for example to restrict the access
to selected layers, to inject an XSL stylesheet to the XML response, to add a watermark to a generated WMS image
and so on.
From this point, you might find useful a quick look to the server plugins API docs.
Each filter should implement at least one of three callbacks:
• onRequestReady()
• onResponseComplete()
• onSendResponse()
All filters have access to the request/response object (QgsRequestHandler) and can manipulate all its properties
(input/output) and raise exceptions (while in a quite particular way as we’ll see below).
All these methods return a boolean value indicating if the call should be propagated to the subsequent filters. If one
of these method returns False then the chain stop, otherwise the call will propagate to the next filter.
Here is the pseudo code showing how the server handles a typical request and when the filter’s callbacks are called:
onRequestReady
This is called when the request is ready: incoming URL and data have been parsed and before entering the core
services (WMS, WFS etc.) switch, this is the point where you can manipulate the input and perform actions like:
• authentication/authorization
• redirects
• add/remove certain parameters (typenames for example)
• raise exceptions
You could even substitute a core service completely by changing SERVICE parameter and hence bypassing the core
service completely (not that this make much sense though).
onSendResponse
This is called whenever any partial output is flushed from response buffer (i.e to FCGI stdout if the fcgi server is
used) and from there, to the client. This occurs when huge content is streamed (like WFS GetFeature). In this case
onSendResponse() may be called multiple times.
Note that if the response is not streamed, then onSendResponse() will not be called at all.
In all case, the last (or unique) chunk will be sent to client after a call to onResponseComplete().
Returning False will prevent flushing of data to the client. This is desirable when a plugin wants to collect all chunks
from a response and examine or change the response in onResponseComplete().
onResponseComplete
This is called once when core services (if hit) finish their process and the request is ready to be sent to the client.
As discussed above, this method will be called before the last (or unique) chunk of data is sent to the client. For
streaming services, multiple calls to onSendResponse() might have been called.
onResponseComplete() is the ideal place to provide new services implementation (WPS or custom services)
and to perform direct manipulation of the output coming from core services (for example to add a watermark upon
a WMS image).
Note that returning False will prevent the next plugins to execute onResponseComplete() but, in any case,
prevent response to be sent to the client.
Some work has still to be done on this topic: the current implementation can distinguish between handled and un-
handled exceptions by setting a QgsRequestHandler property to an instance of QgsMapServiceException, this
way the main C++ code can catch handled python exceptions and ignore unhandled exceptions (or better: log them).
This approach basically works but it is not very “pythonic”: a better approach would be to raise exceptions from
python code and see them bubbling up into C++ loop for being handled there.
A server plugin is a standard QGIS Python plugin as described in Developing Python Plugins, that just provides
an additional (or alternative) interface: a typical QGIS desktop plugin has access to QGIS application through the
QgisInterface instance, a server plugin has only access to a QgsServerInterface when it is executed
within the QGIS Server application context.
To make QGIS Server aware that a plugin has a server interface, a special metadata entry is needed (in metadata.
txt):
server=True
Important: Only plugins that have the server=True metadata set will be loaded and executed by QGIS Server.
The qgis3-server-vagrant example plugin discussed here (with many more) is available on github, a few server plugins
are also published in the official QGIS plugins repository.
Plugin files
1 PYTHON_PLUGINS_PATH/
2 HelloServer/
3 __init__.py --> *required*
4 HelloServer.py --> *required*
5 metadata.txt --> *required*
__init__.py
This file is required by Python’s import system. Also, QGIS Server requires that this file contains a server-
ClassFactory() function, which is called when the plugin gets loaded into QGIS Server when the server starts.
It receives reference to instance of QgsServerInterface and must return instance of your plugin’s class. This
is how the example plugin __init__.py looks like:
def serverClassFactory(serverIface):
from .HelloServer import HelloServerServer
return HelloServerServer(serverIface)
HelloServer.py
This is where the magic happens and this is how magic looks like: (e.g. HelloServer.py)
A server plugin typically consists in one or more callbacks packed into instances of a QgsServerFilter.
Each QgsServerFilter implements one or more of the following callbacks:
• onRequestReady()
• onResponseComplete()
• onSendResponse()
The following example implements a minimal filter which prints HelloServer! in case the SERVICE parameter
equals to “HELLO”:
1 class HelloFilter(QgsServerFilter):
2
The filters must be registered into the serverIface as in the following example:
class HelloServerServer:
def __init__(self, serverIface):
serverIface.registerFilter(HelloFilter(serverIface), 100)
The second parameter of registerFilter() sets a priority which defines the order for the callbacks with the
same name (the lower priority is invoked first).
By using the three callbacks, plugins can manipulate the input and/or the output of the server in many different ways.
In every moment, the plugin instance has access to the QgsRequestHandler through the QgsServerInter-
face. The QgsRequestHandler class has plenty of methods that can be used to alter the input parameters
before entering the core processing of the server (by using requestReady()) or after the request has been pro-
cessed by the core services (by using sendResponse()).
The following examples cover some common use cases:
The example plugin contains a test example that changes input parameters coming from the query string, in this
example a new parameter is injected into the (already parsed) parameterMap, this parameter is then visible by
core services (WMS etc.), at the end of core services processing we check that the parameter is still there:
1 class ParamsFilter(QgsServerFilter):
2
On the highlighted line the “SUCCESS” string indicates that the plugin passed the test.
The same technique can be exploited to use a custom service instead of a core one: you could for example skip a
WFS SERVICE request or any other core request just by changing the SERVICE parameter to something different
and the core service will be skipped. Then you can inject your custom results into the output and send them to the
client (this is explained below).
Tip: If you really want to implement a custom service it is recommended to subclass QgsService and register
your service on registerFilter() by calling its registerService(service)
The watermark filter example shows how to replace the WMS output with a new image obtained by adding a water-
mark image on the top of the WMS image generated by the WMS core service:
1 from qgis.server import *
2 from qgis.PyQt.QtCore import *
3 from qgis.PyQt.QtGui import *
4
5 class WatermarkFilter(QgsServerFilter):
6
23 p = QPainter(img)
24 p.drawImage(QRect( 20, 20, 40, 40), watermark)
25 p.end()
26 ba = QByteArray()
27 buffer = QBuffer(ba)
28 buffer.open(QIODevice.WriteOnly)
29 img.save(buffer, "PNG" if "png" in request.parameter("FORMAT") else
,→"JPG")
In this example the SERVICE parameter value is checked and if the incoming request is a WMS GETMAP and
no exceptions have been set by a previously executed plugin or by the core service (WMS in this case), the WMS
generated image is retrieved from the output buffer and the watermark image is added. The final step is to clear the
output buffer and replace it with the newly generated image. Please note that in a real-world situation we should also
check for the requested image type instead of supporting PNG or JPG only.
Access control filters gives the developer a fine-grained control over which layers, features and attributes can be
accessed, the following callbacks can be implemented in an access control filter:
• layerFilterExpression(layer)
• layerFilterSubsetString(layer)
• layerPermissions(layer)
• authorizedLayerAttributes(layer, attributes)
• allowToEdit(layer, feature)
• cacheKey()
Plugin files
1 PYTHON_PLUGINS_PATH/
2 MyAccessControl/
3 __init__.py --> *required*
4 AccessControl.py --> *required*
5 metadata.txt --> *required*
__init__.py
This file is required by Python’s import system. As for all QGIS server plugins, this file contains a serverClass-
Factory() function, which is called when the plugin gets loaded into QGIS Server at startup. It receives a refer-
ence to an instance of QgsServerInterface and must return an instance of your plugin’s class. This is how the
example plugin __init__.py looks like:
def serverClassFactory(serverIface):
from MyAccessControl.AccessControl import AccessControlServer
return AccessControlServer(serverIface)
AccessControl.py
1 class AccessControlFilter(QgsAccessControlFilter):
2
26 def cacheKey(self):
27 return super().cacheKey()
28
29 class AccessControlServer:
30
layerFilterExpression
layerFilterSubsetString
Same than the previous but use the SubsetString (executed in the database)
For example, to limit to features where the attribute role is equal to user.
layerPermissions
authorizedLayerAttributes
allowToEdit
cacheKey
QGIS Server maintains a cache of the capabilities then to have a cache per role you can return the role in this method.
Or return None to completely disable the cache.
In QGIS Server, core services such as WMS, WFS and WCS are implemented as subclasses of QgsService.
To implement a new service that will be executed when the query string parameter SERVICE matches the service
name, you can implement your own QgsService and register your service on the serviceRegistry() by
calling its registerService(service).
Here is an example of a custom service named CUSTOM:
4 class CustomServiceService(QgsService):
5
6 def __init__(self):
7 QgsService.__init__(self)
8
9 def name(self):
10 return "CUSTOM"
11
12 def version(self):
13 return "1.0.0"
14
20
21 class CustomService():
22
In QGIS Server, core OGC APIs such OAPIF (aka WFS3) are implemented as collections of QgsServerOgcApi-
Handler subclasses that are registered to an instance of QgsServerOgcApi (or it’s parent class QgsServer-
Api).
To implement a new API that will be executed when the url path matches a certain URL, you can implement your
own QgsServerOgcApiHandler instances, add them to an QgsServerOgcApi and register the API on the
serviceRegistry() by calling its registerApi(api).
Here is an example of a custom API that will be executed when the URL contains /customapi:
1 import json
2 import os
3
23
24 class CustomApiHandler(QgsServerOgcApiHandler):
25
26 def __init__(self):
27 super(CustomApiHandler, self).__init__()
28 self.setContentTypes([QgsServerOgcApi.HTML, QgsServerOgcApi.JSON])
29
30 def path(self):
31 return QRegularExpression("/customapi")
32
33 def operationId(self):
34 return "CustomApiXYCircle"
35
36 def summary(self):
37 return "Creates a circle around a point"
38
39 def description(self):
40 return "Creates a circle around a point"
41
42 def linkTitle(self):
43 return "Custom Api XY Circle"
44
45 def linkType(self):
46 return QgsServerOgcApi.data
47
51 values = self.values(context)
(continues on next page)
67 QgsServerQueryStringParameter(
68 'y', True, QgsServerQueryStringParameter.Type.Double, 'Y␣
,→coordinate'),
69 QgsServerQueryStringParameter('r', True,␣
,→QgsServerQueryStringParameter.Type.Double, 'radius')]
70
71
72 class CustomApi():
73
TWENTYONE
Hint: The code snippets on this page need the following imports if you’re outside the pyqgis console:
3 app = QApplication.instance()
4 app.setStyleSheet(".QWidget {color: blue; background-color: yellow;}")
5 # You can even read the stylesheet from a file
6 with open("testdata/file.qss") as qss_file_content:
7 app.setStyleSheet(qss_file_content.read())
3 icon = QIcon("/path/to/logo/file.png")
4 iface.mainWindow().setWindowIcon(icon)
5 iface.mainWindow().setWindowTitle("My QGIS")
153
PyQGIS testing developer cookbook
21.2 Settings
3 qs = QgsSettings()
4
5 for k in sorted(qs.allKeys()):
6 print (k)
21.3 Toolbars
Remove toolbar
1 toolbar = iface.helpToolBar()
2 parent = toolbar.parentWidget()
3 parent.removeToolBar(toolbar)
4
actions = iface.attributesToolBar().actions()
iface.attributesToolBar().clear()
iface.attributesToolBar().addAction(actions[4])
iface.attributesToolBar().addAction(actions[3])
21.4 Menus
Remove menu
21.5 Canvas
Access canvas
canvas = iface.mapCanvas()
iface.mapCanvas().setCanvasColor(Qt.black)
iface.mapCanvas().refresh()
21.6 Layers
layer = iface.activeLayer()
QgsProject.instance().mapLayers().values()
5 layers_names = []
6 for layer in QgsProject.instance().mapLayers().values():
7 layers_names.append(layer.name())
8
Otherwise
Show methods
dir(layer)
3 feat = QgsFeature()
4 geom = QgsGeometry()
5 feat.setGeometry(geom)
6 feat.setFields(layer.fields())
7
3 pr = layer.dataProvider()
4 feat = QgsFeature()
5 feat.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(10,10)))
6 pr.addFeatures([feat])
Get features
for f in layer.getFeatures():
print (f)
for f in layer.selectedFeatures():
print (f)
selected_ids = layer.selectedFeatureIds()
print(selected_ids)
memory_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.
(continues on next page)
Get geometry
# Point layer
for f in layer.getFeatures():
geom = f.geometry()
print ('%f, %f' % (geom.asPoint().y(), geom.asPoint().x()))
10.000000, 10.000000
Move geometry
3 fileName = "testdata/sublayers.gpkg"
4 layer = QgsVectorLayer(fileName, "test", "ogr")
5 subLayers = layer.dataProvider().subLayers()
6
7 urlWithParams = 'https://tile.openstreetmap.org/%7Bz%7D/%7Bx%7D/%7By%7D.png&
,→zmax=19&zmin=0&crs=EPSG3857'
8 loadXYZ(urlWithParams, 'OpenStreetMap')
QgsProject.instance().removeAllMapLayers()
Remove all
QgsProject.instance().clear()
iface.mapCanvas().layers()
1 ltv = iface.layerTreeView()
2 mp = ltv.menuProvider()
3 ltv.setMenuProvider(None)
4 # Restore
5 ltv.setMenuProvider(mp)
Root node
3 root = QgsProject.instance().layerTreeRoot()
4 node_group = root.addGroup("My Group")
5
9 node_group.addLayer(layer)
10
11 print(root)
12 print(root.children())
3 child0 = root.children()[0]
4 print (child0.name())
5 print (type(child0))
6 print (isinstance(child0, QgsLayerTreeLayer))
7 print (isinstance(child0.parent(), QgsLayerTree))
My Group
<class 'qgis._core.QgsLayerTreeGroup'>
False
True
3 def get_group_layers(group):
4 print('- group: ' + group.name())
5 for child in group.children():
6 if isinstance(child, QgsLayerTreeGroup):
7 # Recursive call to get nested groups
8 get_group_layers(child)
9 else:
10 print(' - layer: ' + child.name())
11
12
13 root = QgsProject.instance().layerTreeRoot()
14 for child in root.children():
15 if isinstance(child, QgsLayerTreeGroup):
16 get_group_layers(child)
17 elif isinstance(child, QgsLayerTreeLayer):
18 print ('- layer: ' + child.name())
- group: My Group
- layer: layer name you like
<QgsLayerTreeGroup: My Group>
Find layer by id
print(root.findLayer(layer.id()))
Add layer
Add group
4 myLayer = root.findLayer(layer.id())
5 myClone = myLayer.clone()
6 parent = myLayer.parent()
7
12 parent.removeChildNode(myLayer)
1 QgsProject.instance().addMapLayer(layer, False)
2
3 root = QgsProject.instance().layerTreeRoot()
4 myGroup = root.findGroup("My Group")
5 myOriginalLayer = root.findLayer(layer.id())
6 myLayer = myOriginalLayer.clone()
7 myGroup.insertChildNode(0, myLayer)
8 parent.removeChildNode(myOriginalLayer)
root = QgsProject.instance().layerTreeRoot()
node = root.findLayer(layer.id())
new_state = Qt.Checked if node.isVisible() == Qt.Unchecked else Qt.Unchecked
node.setItemVisibilityChecked(new_state)
Is group selected
False
Expand node
print(myGroup.isExpanded())
myGroup.setExpanded(False)
3 model = iface.layerTreeView().layerTreeModel()
4 ltv = iface.layerTreeView()
5 root = QgsProject.instance().layerTreeRoot()
6
Node signals
7 root.willAddChildren.connect(onWillAddChildren)
8 root.addedChildren.connect(onAddedChildren)
Remove layer
root.removeLayer(layer)
Remove group
root.removeChildNode(node_group2)
1 root = QgsProject.instance().layerTreeRoot()
2 model = QgsLayerTreeModel(root)
3 view = QgsLayerTreeView()
4 view.setModel(model)
5 view.show()
Move node
cloned_group1 = node_group.clone()
root.insertChildNode(0, cloned_group1)
root.removeChildNode(node_group)
Rename node
cloned_group1.setName("Group X")
node_layer1.setName("Layer X")
...
len(QgsApplication.processingRegistry().algorithms())
len(QgsApplication.processingRegistry().providers())
len(QgsExpression.Functions())
21.10 Decorators
CopyRight
1 from qgis.PyQt.Qt import QTextDocument
2 from qgis.PyQt.QtGui import QFont
3
19 def _on_render_complete(p):
20 deviceHeight = p.device().height() # Get paint device height on which this␣
,→painter is currently painting
33 # RenderMillimeters
34 pixelsInchX = p.device().logicalDpiX()
35 pixelsInchY = p.device().logicalDpiY()
36 xOffset = pixelsInchX * INCHES_TO_MM * int(mMarginHorizontal)
37 yOffset = pixelsInchY * INCHES_TO_MM * int(mMarginVertical)
38
39 # Calculate positions
40 if case == 0:
41 # Top Left
42 add_copyright(p, text, xOffset, yOffset)
43
44 elif case == 1:
45 # Bottom Left
46 yOffset = deviceHeight - yOffset - size.height()
47 add_copyright(p, text, xOffset, yOffset)
48
49 elif case == 2:
50 # Top Right
51 xOffset = deviceWidth - xOffset - size.width()
52 add_copyright(p, text, xOffset, yOffset)
53
54 elif case == 3:
55 # Bottom Right
56 yOffset = deviceHeight - yOffset - size.height()
(continues on next page)
60 elif case == 4:
61 # Top Center
62 xOffset = deviceWidth / 2
63 add_copyright(p, text, xOffset, yOffset)
64
65 else:
66 # Bottom Center
67 yOffset = deviceHeight - yOffset - size.height()
68 xOffset = deviceWidth / 2
69 add_copyright(p, text, xOffset, yOffset)
70
21.11 Composer
3 project = QgsProject.instance()
4 projectLayoutManager = project.layoutManager()
5 layout = projectLayoutManager.layoutByName(composerTitle)
21.12 Sources