snorfalorpagus dot net

a blog about programming and gis

Symbology of Vector Layers in QGIS Python Plugins

The symbology of a layer describes how the layer’s data should be displayed on the map canvas. A layers symbology, or style, is composed of two things: a symbol (or multiple symbols) and a renderer.

The layer’s renderer decides which features should be drawn using which symbols. The renderer can be a simple thing, giving all features the same symbol, or something more complex, giving each feature a different symbol depending on its attributes. The renderer also assigns labels to symbols for use in the legend.

A symbol describes how an individual feature is drawn. In QGIS, a symbol is a container for symbol layers. Each symbol layer contributes to the overall appearance of a symbol. For example, a polygon with a grey fill and red hatched lines is created using two layers: a grey fill layer and a red line pattern layer.

This post describes how to work with layer symbologies in QGIS using Python.

Embedding PNGs in SVG Markers in QGIS

QGIS has support for using Scaleable Vector Graphics (SVGs) as marker icons. This gives you a lot of control over how markers appear on your maps. But what if the marker you want to use doesn’t have a vector version available? Fear not! Other image formats, including raster formats such as PNG and JPG, can be embedded in an SVG.

To do this is actually quite simple using the Inkscape vector graphics editor. Inkscape can be downloaded for free from the Inkscape website or via your operating systems package repository.

Writing Unit Tests for QGIS Python Plugins

Testing your code is a good idea. Testing can help you find bugs early and spot regressions where you didn’t expect them. The tests themselves can form a kind of “living” documentation, and can even drive the design of your project. A unit test is a test written for a single module of code; it might check to see that a drop down menu has been populated correctly, or that a layer was added to the map canvas once an algorithm has finished processing. By testing small parts of code in isolation you can quickly identify which components in a much larger system are failing when things start to go wrong. The Python Guide provides some useful guidance for writing unit tests.

This post describes some of the specifics you need to know for writing unit tests for QGIS Python plugins. It assumes an existing understanding of writing plugins for QGIS using Python.

Multithreading in QGIS Python Plugins

The Python bindings in QGIS allow plugins to be written in Python. Writing a plugin for QGIS involves two things: writing the geoprocessing algorithm itself, and writing a graphical user interface (GUI) that the user can interact with without writing any code.

Using a GIS often requires patience; geoprocessing tasks can take a long time to complete when working with large datasets. If you run a time consuming task and the UI in the same thread, the UI will become unresponsive; it will appear to the user as if the application has crashed, even if it is still working on the task. The solution is to do all the heavy lifting of the algorithm in another thread. The UI is responsible for passing the input layers and any parameters to the ‘worker’ thread, and handling the output when it’s ready. In the time between these events it can continue doing what it does (updating progress bars, allowing the user to press buttons, etc.).

This post assumes some existing experience with the QGIS Python bindings, but no experience with threading in Python. If you’ve not heard of threads before the Wikipedia page on threads isn’t a bad place to start.