QGIS Tutorial: How to Create an Action

The previous tutorial discussed about how to perform spatial query in QGIS 3. In this tutorial, I will discuss about Actions in QGIS. What is Action? Action is a command or task to be executed based on a click trigger. For example if a user click a feature then an image will be opened. Here the click is the trigger and opening an image is the action.

We can define some types of actions in QGIS like opening an application or file in an operating system (Windows, Mac or UNIX), open a web page,  or create a specific action with a python script. In this tutorial I will cover how to create an open action and a python action. The action example is to open an URL or web link of traffic camera in Vancouver city. For that, we will use traffic camera location data that can be downloaded from Vancouver City Data Catalogue. For this tutorial I'm using QGIS 3.0.

Opening an URL Action

Let's start with creating a simple action to view a camera traffic when a camera point is clicked on QGIS map canvas. The action can be done with the following steps:

1. Add a basemap. For this case I'm using Open Street Map.
2. Add traffic camera data into QGIS map canvas. The camera location over the basemap will look like as in figure 1.

QGIS Action Vancouver City Traffic Camera Location
Figure 1. Vancouver City Traffic Camera Location
3. Open camera location layer properties and choose Actions icon. In this step, by clicking Create default actions, some actions will be listed in the Action List. To create a new one click the green + button as in figure 2.

Add a new action in QGIS
Figure 2. Add a new action
4. An Add action window will appear as in figure 3. In the Type option select Open. Give a description and a short name, for example I gave the name open traffic camera. In the Action Scope check Canvas and Feature Scope, cause we want the action will be executed for the feature in QGIS map canvas.
5. Next in the Action text, type [%URL%]. URL is a column name in the attribute table which contains a HTML link to each  traffic camera. You can see how the attribute looks like in figure 4.

Edit an action in QGIS
Figure 3. Add/Edit an action
Traffic camera attribute table QGIS Action
Figure 4. Traffic camera attribute table
6. Last step after clicking OK button both in the Add new action window and layer properties, let's run the action by selecting the action from Action Icons in the toolbar as in figure 5.
Select an Action QGIS
Figure 5. Select an Action
The mouse pointer will change into cross hair. Click a camera point. Soon the URL of the camera will be opened if the default web browser as in figure 6.
Traffic camera view in web browser
Figure 6. Traffic camera view in web browser

Python URL Action

In this part we will make an action with Python script for the same example. The action will open a QT web browser instead of the default web browser. Most steps are similar like above, except:
1. Select Python in the type option.
2. Give a new name for example: open traffic camera python.
3. In the Action text, type the listing code below. Figure 7 shows the new action with python.
from PyQt5 import QtCore
from PyQt5.QtCore import *
from PyQt5.QtWebKit import *
from PyQt5.QtWebKitWidgets import *
web = QWebView()

QGIS Action with Python
Figure 7. QGIS Action with Python
When the action is executed by clicking a traffic camera point. A QT web browser will be opened as in figure 8.
Traffic camera view in QT web browser
Figure 8. Traffic camera view in QT web browser
Congratulation! Now you can observe the traffic condition around Vancouver City.
That's all the tutorial how to create an Action in QGIS. I think this is a great tool, cause gives more functionality to our map application. If you want to explore more, visit QGIS actions documentation.         

Related Posts

Disqus Comments