Build dynamic filters for values in drill down forms in QGIS with Form (Aware) Value Relation Widget.

The Form (Aware) Value Relation Widget is an enhancement of the QgsValueRelationWidgetWrapper which is available as a core component of the QGIS Edit Widget covered in this last post on creating dropdown lists in QGIS.

This Plugin is available in the official QGIS Plugin Repository and the  source code is on Github.

How the Plugin works internally;

As a clone of the core Value Relation Widget already implemented in the core Edit Widget, Form Value Relation offers some cutting edge differences;

  • the form refreshes the related item values on every change in the form values and that makes available the form values to the
    context.
  • The expression is re-evaluated every time the form changes if the changed fields are part of the expression.
  • A custom expression function “CurrentFormValue()” provided allow to read the form values from the expression context.

This widget can be used to implement drill-down forms, where the values available in one field depend on the values of other fields. Check more on Alesandro Pasottis’s illustration.

Illustration on Usage of the Form (Aware) Value Relation.

In the last tutorial on setting up Option Lists in QGIS (you can find this here) we managed to create amazing Options for different fields that can make data entry quite a breeze.

Unfiltered Attribute Options

This is a good form, however, there is one little thing intrinsic in it that could make your data messy. Obviosuly, Nyeri Town in not a constituency in Nairobi County and therefor you wouldn’t want to see it together with others like it. A better form which we’ll be building today is one that will let you see only constituencies that are in the county selected in the fields [counties]. So that, when Nairobi is already selected as the county, you only see constituencies in Nairobi in the [consti] Options List. Awesome right!! Then let’s get down to it.

First, we have to build a structure for the option lists that will allow the form to do the filter

Two, I assume by  now you have pulled down the Form value Relation Plugin from the officially repository  and installed it (automatically or manually). If not find it here .

RELATED:  How to increase swap space and install GeoNode in Ubuntu 14.04

Create a table to store the list of constituencies with this structure

Sub-category list structure

The important thing to note here is the county_code field, this field will help us filter those constituency according to the county selected. If you have a another sub-category for example ward_list, you will have in the table the constituency_code for filtering based on selected constituency.

In QGIS, Right click on the feature layer (in my case universities feature layer). In this tutorial we are going to be creating data for universities locations and adding the name, county and constituency it’s located in.

Go to Layer Properties window and then the Field Tab. Open the Edit Widget dialog of the county field.

From this Dialog, Open the Edit Widget by Clicking on the Edit Widget in front of the county field, it’s label Text Edit by default.

 

 

That will take  you to the next window Edit Widget dialog, below

 

As opposed to using Value Relation as in previous tutorial, we will use the Form Value Relation shown at the bottom of he available Widgets. This widget is not available by default until the Plugin is installed as indicated earlier.

Click on Form Value Relation and set the category source list a shown in the dialog to the right.

Click OK and then Apply and OK.

Test the category options lists (in my case counties).

 

 

 

Return to the Layer Properties window under Field Tab and this time Open the Edit Widget for the sub-category (my case is constituencies)

Steve Ochieng
About Steve Ochieng 18 Articles
Geospatial Specialist | Avid Reader and Vivid Writer here | Geospatial Tech Advocacy and Evangelism | GeoProgrammer and Follower of @geohipster