[Qgis-user] Actions HowTo

Alister Hood Alister.Hood at synergine.com
Tue Mar 6 18:05:02 PST 2012


Hi Paulo,

> Hi all.
> We are writing a short howto for the new actions. We would like to add
> some more
> complex example that the usual "open the browser with this argument",
> possibly using
> coordinates and functions. Ideas and suggestions welcome.

This one is not particularly complex, but I think it would be good to include in standard tutorials or the user manual, as a lot of people would find it useful:
With the new tools to run an action directly when clicking on a feature, you can reimplement the functionality of the showhideimg plugin (and the old "raster catalog" plugin?).

1) When you have a lot of raster layers spread over a wide area, build an index polygon layer with a polygon each raster extent, and the raster filename / path in an attribute field.   You can do this using gdaltindex (in QGIS "Raster>Miscellaneous>Tile index").

2) Set up an action something like this (I'm using the syntax for QGIS master):
qgis.utils.iface.addRasterLayer('[% "location" %]',os.path.basename('[% "location" %]'))
Now when you use the action tool it loads the raster image  for the location that you click.

There are two issues though:

(a) If there are overlapping features, the built-in "Run feature action" tool in QGIS master only runs the action for the first feature found (#5126).  Similarly, the "hotlink" plugin only runs one action on one feature each time - but at least it lists all the possible actions, so you know that there are overlapping features.

(b) Unfortunately, if "Settings>Options>General>Add new layers to selected or current group" is not set, or the tile index is not in a group, then QGIS always selects a new layer when it is added.  So unless you set that option and put the tile index in a group, if you then want to load raster images for another location (perhaps an adjacent tile) you need to click on the tile index layer before using the action tool again.
I'm filed http://hub.qgis.org/issues/5125 about this inconsistency, but I'm not sure which behaviour is intended.  If QGIS *is* intended to automatically select new layers when they are added, then I guess it wouldn't be too hard to write an action that would load the raster layer and then select the index layer again.

The other feature of the showhideimg plugin is that if you click in the same area again it turns the raster layer off.  I guess this could also be implemented in the layer action with a bit of work...


Regards,
Alister



More information about the Qgis-user mailing list