Summarising QGIS project layers

With sometimes dozens of layers in a QGIS project, consolidating everything into one directory can be really handy (Plugins | QConsolidate). I’m finding this really useful when sending a project file to another device (like an Android tablet running QGIS) or to a colleague.

Multiple¬†layers with varying projections is a big performance killer in QGIS. Plus, it looks like there’s a serious bug using a GPS connection with non-WGS84 projections – so, I started looking at a current project (Luangwa Valley aerial survey) and removing or reprojecting layers so that I had a single set of projections and could turn off on-the-fly projections.

Tedious. Rick-click and look at CRS for every one of 25 layers? I really need an overview of layers, data sources and CRS for every layer.

I’ve tried to use the XML package in R before without much success – this time I made sure to go through the excellent XPath tutorial on W3Schools.com and found some worked examples on stackoverflow.

The result, a function that takes a QGIS project file (an XML document) and spits out a dataframe with columns for the layer name, CRS and location of the data source.

library(XML)

QGIS.summary <- function (fn) {
# Get the QGIS project file and parse it:
file <- xmlParse(fn)

# Main doc called 'qgis' and the layers are in the 'maplayer' level;
# we need the srid and the layername xmlValues:
file.CRS <- xpathApply(file, "/qgis//maplayer/srs/spatialrefsys/description", xmlValue, "description")
file.layers <- xpathApply(file, "/qgis//maplayer/layername", xmlValue, "layername")
file.datasource <- xpathApply(file, "/qgis//maplayer/datasource", xmlValue, "datasource")

# Return a dataframe:
return(data.frame(layers = as.character(file.layers), CRS = as.character(file.CRS), sourcefiles = as.character(file.datasource)))
}