Validating user input in PyQt4 using QValidator

This post demonstrates how to use the QValidator class to validate text input in PyQt4. Text validation can help the user to understand what information is required when entering data into a form. For example, preventing a user from entering letters into a text field that expects a number makes it clearer what is needed. In addition to this the colour of the text field can be changed to provide the user with a visual feedback, e.
Read more

Using Cartopy with Rasterio

This post demonstrates the basics of creating maps in Python using Cartopy with raster data read using Rasterio. Cartopy makes it easy to draw maps in matplotlib figures, using the Proj.4 library to handle any wacky projection you can throw at it, and the shapely Python module to work with vector data. Rasterio is a Python module for reading and writing raster data, built on the GDAL library. Cartopy is often used with the basic image reading function imread() from matplotlib, which can read a selection of common image formats with support from the Python Image Library (PIL), or the Iris module for formats commonly used in meteorology and climatology.
Read more

Using Rtree spatial indexing with OGR

Spatial indexing can significantly reduce the time required to perform geoprocessing operations involving intersection. This includes intersections, unions, dissolves, and nearest neighbour analysis. Spatial indexing speeds up queries by reducing the number of features that need to be evaluated with computationally expensive geometic calculations. It does this by performing a simplified query using the bounding boxes (a.k.a., envelopes) of the features only. A bounding box can always be described by four parameters – the x,y coordinates of the lower left and upper right corners – and as such these first pass queries can be done very efficiently using regular database indexing methods.
Read more

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.
Read more

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.
Read more

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.
Read more

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.
Read more