snorfalorpagus dot net

a blog about programming and gis

Find ArcMap Document Version With Python

This post shows how to find the version number of an ArcGIS map document (.mxd) programmatically using Python. It seems that arcpy does not provide a method to do this itself.

ArcGIS map documents are actually Microsoft OLE2 files (also called Structured Storage, Compound File Binary Format or Compound Document File Format). There is a Python module called oletools which can read this format, which is available on PyPI.

Masking Rasterio Layers With Vector Features

This post shows how to extract data from a raster only where it intersects a vector feature using Rasterio. Fiona is used to read the vector data, and Shapely is used only as a convenience to calculate the feature’s bounding box.

GDAL provides a function to “burn” vector shapes into rasters (i.e., to rasterize the geometries). This functionality is accessed from Rasterio using the rasterize function. This tool can be used to create a raster mask for another raster layer.

Embedding a Leaflet Map in a Qt Application

Embedding a map in a Qt application using Leaflet is surprisingly easy. The QtWebKit.QWebView object can be used to create a browser window within a Qt dialog. Within this browser window you can create an interactive map using Leaflet just as you would for any other browser. Communication between Qt and the Leaflet map is also possible, allowing you to control the map from the main application, or extract information from Leaflet.

This post demonstrates a simple application written in Python using PyQt4, which loads a large map in the main dialog and reports the coordinates (latitude and longitude) of the centre of the map in a QLabel.

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.