QGIS field calculator backslash gotcha

Quantum GIS has a powerful field calculator, similar to that in ArcGIS, and described here. I was using it to update a tile index shapefile for use with GeoServer and a folder full of georeferenced TIFF images. The shapefile comprises polygons each representing the extent of one of the tiled images, having a [location] attribute containing the path to the image.

After moving things around, the path was no longer correct, so I needed to update (for example) “'D:\MapData\OrdnanceSurvey\streetview\HP40NE.TIF” to “N:\OrdnanceSurvey\streetview\HP40NE.TIF”, with a similar replacement occurring across a few thousand rows.

So in the QGIS field calculator, I tried to set the field to this expression:

replace("location" , 'D:\MapData\' , 'N:\')

QGIS field calculator grumbling about syntax

But at the bottom of the screen we can see that QGIS is not happy with this expression, and clicking “more info” tells us

Parser Error:
syntax error, unexpected COLUMN_REF, expecting COMMA or ')'

The solution was to escape the backslash characters like this:

replace("location" , 'D:\\MapData\\' , 'N:\\')

I shall internalize my feelings about whether or not this should have been necessary, or the helpfulness of the error message and the documentation about this feature – hey QGIS is free!

Comments

Find out more