Date Sun 25 March 2012

One of the really cool features in QGIS that doesn't get much press is the ability to run actions based on a feature selection. Under 1.7.4 this works by using the identify tool and then choosing an action from the action list in the identified feature(s) attributes. A new improvement in QGIS master (and should be in the upcoming 1.8) added by Giuseppe Sucameli adds an action map tool which allows you to click on any feature and run a related action. Actions can include tokens referencing feature attributes so that you can, for example, display a picture related to a feature using an external program, passing the program a file name taken from an attribute of the selected feature. Most people immediately grasp that you can just add the name of an external app e.g. firefox and pass attributes as parameters. There are some shortcomings with this approach in that it adds an unknowable dependency - will the end-user have the software required to execute the action on their system? Its easy to forget that QGIS sits on top of the incredibly powerful and versatile Qt4 library, and all that goodness is just a heartbeat away with a python line or two. Also, in QGIS actions can be arbitrary, tokenised (i.e. using variable information based on the contents of a field attribute) python commands! In this short article I will demonstrate how to use an a python action to show a web page. The url will be generated based on an attribute value and best of all, it requires no browser on the user's system since it uses the Qt4 QWebView class (which is a webkit based html widget) to display the content in a pop up window.

Our webkit popup window after clicking on South Africa with the action map tool.

For this article I used the 'gadm' global borders dataset which I described a little in a previous article. My intention is to use the 'name_0' attribute to construct a wikipedia url like this:

Note: that this is a completely contrived example, I haven't checked to see if it will work in all possible values obtained from the gadm name_0 field.

Ok so to create our layer action, open the gadm layer properties dialog and head over to the Actions tab.

Now set up a new action using the following properties for the action:

Type: Python

Name: Wikipedia

Action (as listed below but all on one single line):

from PyQt4.QtCore import QUrl; from PyQt4.QtWebKit import QWebView;  myWV = QWebView(None);
myWV.load(QUrl('[% "name_0" %]'));

Ok so there are a couple of things going on here:

  • All the python code is in a single line with semi-colons separating commands instead of newlines
  • [% "name_0" %] will be replaced by the actual attribute value when the action is invoked
  • The code simply creates a new QWebView instance, sets its url and then calls show to make it visible as a window on the user's desktop

As I mentioned, this is a contrived example - in the real world more likely you might import your logic from a python module and then call a function with a field attribute as parameter. You could equally use the approach to display an image without requiring that the user has a particular image viewer on their system.


comments powered by Disqus