3

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

  • Which QGIS version? – Nathan W Aug 23 '13 at 11:08
  • @NathanW - currently looking at 1.8, but will have to replicate in 2 when I get up and running on that – Mark Aug 23 '13 at 11:16
  • Great Action. I'm using it. Is it possible to assign the Coordinate reference systems and to build pyramids? – Daniele Piccolo Jan 16 '17 at 10:36
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.