QGIS Raster – How to Create a QGIS Action Which Loads a Raster

pythonqgis

I am following an article here:Actions HowTo , specifically the piece about an action to load a raster:

Python type
    Add Raster Layer
            qgis.utils.iface.addRasterLayer('PATH_FILE','NAME_FILE_IN_QGIS') 

Can anyone advise what i need to do to open a raster based on a value in the vector table (essentially I have a grid with a field containing a field of Grid_Ref for all items where a raster image exists).

I have been fiddling with the example above and got as far as an Action of Type Python, Named OpenRaster, with the following action:

qgis.utils.iface.addRasterLayer('E:\Plot Sheet Devt\1974-1984\1250k\' + "Grid_Ref" + '.TIF',"Grid_Ref") 

The Python error that I am getting on running this is

File "", line 1
qgis.utils.iface.addRasterLayer('E:\Plot Sheet Devt\1974-1984\1250k\' + "Grid_Ref" + '.TIF',"Grid_Ref")
^

SyntaxError: EOL while scanning string literal

How should I go about forming the open 'PATH_FILE' and 'NAME_FILE_IN_QGIS' so that it picks the information from the Grid_Ref field in the underlying vector data?

As always, thanks for your time

Mark

Best Answer

I have managed to resolve this by following this article and adapting it to fit my requirements, and have ended up setting the command as:

qgis.utils.iface.addRasterLayer('E:\\Plot Sheet Devt\\1974-1984\\1250k\\[% Grid_Ref %].TIF', '[% Grid_Ref %]')

therefore I am using the format

[% attributename %]

to collect the value of an attribute where the action is used.

Also I have corrected my non-escaped backslashes from original post.

Related Question