snorfalorpagus dot net

a blog about programming and gis

Converting British National Grid and Irish Grid References: A Practical Example

The Environment Agency recently released their database of River Habitat Surveys (RHS) as open data, available from the GeoStore under the Open Government Licence. RHS is described as the ‘standard for collecting data on the physical character and quality of river habitats across the UK’. The data comes in the from of an Open Office XML spreadsheet, and includes survey data collected in England, Wales, Scotland and Northern Ireland from 1994 - present.

The location of the survey points are given as grid references, either using the British National Grid (EPSG:27700) for sites in Great Britain, or the Irish Grid (EPSG:29903) for sites in Northern Ireland. To display the data in a GIS these grid references need to be converted into easting and northing coordinates. This post describes how this can be achieved using Python.

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.g. setting the text colour to red when an invalid input has been entered.

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. Using Rasterio enables you to read any of the long list of supported formats readable by GDAL used for geospatial data.

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.

The Rtree module provides a Python interface to the spatial indexing functions in libspatialindex. The Rtree tutorial is clear, succinct and worth reading. See also the Wikipedia page on R-trees.

This post gives examples of how Rtree can be used with both the OSGeo OGR Python module and the Fiona and Shapely modules to speed up spatial queries.

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.