By Keshav Sharma


2019-04-10 13:47:13 8 Comments

I am new to QGIS and wish to automate my current process.

In my project I have to open the attribute table of the batch_16 layer having 5 fields (date, checked_by, td_created, td_catagor and op_comment).

There are 4 additional layers also (woody, sand, builtup, baresoil)

The logic is, if I make any new features in any of these 4 layers after jumping to a grid tile of batch_16 layer as shown in the pic, then I have to assign some attributes to the selected grid tile attribute field as below:

td_catagor: what layers I have worked on after pressing "zoom to selected" to the grid tile and before moving to another selection. woody - W, baresoil - B, sand - S, builtup - BU. If there are more than one layers I have created new features then each letter should be separated by comma as depicted in the image.

How can I automate this process?

Attributes

Layers

2 comments

@spatialthoughts 2019-04-19 12:01:17

Here's a way to automate your workflow. (Tested on QGIS 3.4)

Add a last_updated field to your 4 layers and set a default field value to the expression now(). This will ensure a timestamp is captured every time you make an edit to those layers.

The td_created and td_catagor should be setup as Virtual Fields so they are auto-updated based on an expression.

As you need to query another layer from your grid layer, you need to use the aggregate function. I am giving you example expression for 2 layers, woody and sand as the expression can get very long for the purpose of this answer. You can extend it for other layers. The expression assumes you have a field called last_checked in your grid layer.

For td_created, add a new virtual field with the following expression

if(
aggregate(
 layer:= 'woody',
 aggregate:='max',
 expression:=day(last_updated - attribute(@parent, 'last_checked')),
 filter:=intersects($geometry, geometry(@parent))
 ) > 0 

 OR

 aggregate(
 layer:= 'sand',
 aggregate:='max',
 expression:=day(last_updated - attribute(@parent, 'last_checked')),
 filter:=intersects($geometry, geometry(@parent))
 ) > 0, 'Y', 'N')

Similarly, add another virtual field td_categor with the following expression

array_to_string(
array_remove_all(array(
  if(aggregate(
   layer:= 'woody',
   aggregate:='max',
   expression:=day(last_updated - attribute(@parent, 'last_checked')),
   filter:=intersects($geometry, geometry(@parent))
   ) > 0, 'woody', 'none'),
  if(aggregate(
   layer:= 'sand',
   aggregate:='max',
   expression:=day(last_updated - attribute(@parent, 'last_checked')),
   filter:=intersects($geometry, geometry(@parent))
   ) > 0, 'sand', 'none')), 'none'))

This will add a comma-separated string with layer names like woody,sand

Now every-time you open the attribute table of the grid tile, these expressions will be evaluated and updated based on the timestamps in other layers.

I posted a more detailed explanation at https://spatialthoughts.com/2019/04/19/advanced-aggregate-expressions/

@Keshav Sharma 2019-04-16 14:31:50

I used AutoFields plugin in QGIS and entered expression as in the image and unchecked the "calculate expression on existing features."

AutoFields

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Selecting features from multiple layers for export QGIS 3.6

1 Answered Questions

Use previous entry as default in attribute table

2 Answered Questions

Updating two or more fields in QGIS?

2 Answered Questions

[SOLVED] Automating selection and sliver elimination using QGIS

0 Answered Questions

Automate a process in QGIS using graphical modeler

  • 2015-12-29 23:43:47
  • DennisTG
  • 331 View
  • 1 Score
  • 0 Answer
  • Tags:   qgis qgis-modeler

2 Answered Questions

[SOLVED] How to get layer type in OpenLayers 3?

2 Answered Questions

[SOLVED] Automate attribute table changes in QGIS

1 Answered Questions

[SOLVED] Random selection within subset

1 Answered Questions

[SOLVED] Update OSM data already imported in qGIS

Sponsored Content