1 of 19

QGIS Actions

Ujaval Gandhi

ujaval@spatialthoughts.com

PyQGIS Masterclass

2 of 19

What are QGIS Actions?

  • A built-in feature of QGIS Desktop.
  • An action is something that happens when you click on a feature.
  • Allows you to run a command or Python code when you click on a feature

3 of 19

When should you use an action?

Use actions to add custom functionality where you need to do something in response to a user action on a layer.

4 of 19

Actions are ‘baby plugins’!

Many QGIS customizations can be an Action instead of a plugin.

Actions provide you with an built-in functionality to

  • Add a button in the toolbar/attribute table/attribute form.
  • Execute any Python code or Processing Algorithm when the user picks an action and clicks on a feature.
  • Distribute your customizations with a QGIS Project.

5 of 19

What can you do with Actions?

6 of 19

Extract a Feature from a Layer

7 of 19

Select Features

8 of 19

Update Field Values

9 of 19

Load and Remove Raster Layers

10 of 19

Select Features in a Buffer Zone

11 of 19

Reverse Line Direction

12 of 19

Run Processing Algorithms

13 of 19

View Panorama from Mapillary

14 of 19

Action Basics

  • Actions are defined for a layer.
    • Action button is enabled if there are 1 or more actions defined for the selected layer
  • Actions are saved along with a QGIS Project
    • Easy sharing of additional functionality

15 of 19

Action Scopes

An action scope defines where the action is shown.

Depending on the scope, the action code has access to additional variables.

16 of 19

Action Scopes

  • Canvas
    • Shown in the ‘Action’ button in the Toolbar
    • Access to @layer and all attributes of the clicked feature
    • Access to @click_x and @click_y in map coordinates.
  • Feature
    • Shown in attribute table and feature form.
    • Access to @layer and all attributes of the clicked feature
  • Field
    • Shown in right-click menus of attribute table and feature form
    • Access to @field_index, @field_name and @field_value
  • Layer
    • Show in attribute table and work on the layer or selection
    • Access to @layer
  • Form
    • Shown in feature form designed using the drag-and-drop mode
    • Access to form variable

17 of 19

Fields and Variables

  • [%name%] : Will replace this with the value of the name field.
  • [%@layer%]: Will be replaced with the value of the @layer variable.
  • [%$id%]: Will be replaced with the feature id.

18 of 19

Let’s build some actions

19 of 19

Resources

  • QGIS Actions Course