TransWikia.com

QGIS: Comparing attribute values from different layers with Expression string builder

Geographic Information Systems Asked by Pedda on February 11, 2021

I want to compare attribute values from different attributes from multiple layers in the Expression string builder in QGIS (3.16.1) in of these layers, but I don’t know how to do that.
I’ve tried out aggregate to get values from other layers, but I can only count, summarize etc. values. But I want to compare the values.
For example there is the attribute "OBJID"(from the first layer). And I want to say OBJID (from the first layer) = OBJID (from another layer). So that all objects in the first layer are displayed where this equation is true.

2 Answers

There are at least two possibilities. Instead of using expressions, creating a virtual layer is probably the easier way:

Solution 1: Creating a virtual layer

What you can do is create a virtual layer and add this query:

SELECT Lay1.OBJID_1 as o1, Lay2.OBJID_2 as o2 
FROM Layer_1 as lay1, Layer_2 as lay2
WHERE o1=o2

Solution 2: Using QGIS expressions

If you want to use QGIS epressions, you can use the expression get_feature, where you set the (other) layer as 1st argument (here: 'Layer_2'), the fieldname you are interested in as 2nd argument (here: 'OBJID_2') and the value (here = "OBJID_1") as third argument. Be carful with single ' ' and dobble " " quatation marks. Now you have the feature from Layer_2 in the field OBJID_2 that has a value that equals OBJID_1.

For visualisation purpose, this expression here creates a line for points on Layer_1 to those points on Layer_2 that have the same value for OBJID_2 as the OBJID_1 on layer 1:

make_line (
   ( $geometry ) ,
   geometry ( 
      get_feature ( 
         'Layer_2', 
         'OBJID_2', 
         "OBJID_1" )
   )
)

See:

enter image description here

If you are in the expression editor somewhere on layer_1 and want to get an array of all features of layer_2, use the following expression: array_foreach(array,expression). This iterates for every element of the input-array (1st argument) over an expression (2nd argument) and outputs the collected results of the iterated expression.

We create an input-array containing the numbers from 1 to the number of the last feature with the expression generate_series(start,stop) where start is 1 and stop is the number of features the layer contains. We can get this number with the expression to_int ( layer_property ('Layer_2', 'feature_count') ) - we have to convert it to integer to_int()

So now we are ready to start going through each feature of layer_2. We get the features with the expression get_feature_by_id ( layer , feature_id ) and we can now set as feature_id the integers from the array created before: we reference it by @element. All together, it looks like:

array_foreach (
   generate_series ( 
      1,
      to_int ( layer_property (
         'Layer_2', 'feature_count')
      )
   ),
   ( get_feature_by_id ( 
      'Layer_2', 
      @element)
   )
)

Answered by Babel on February 11, 2021

I would use an attribute join (Processing > Vector general > Join attributes by field value). Using your example, the tool's input layer = Layer_1, and Table field = Layer_1's OBJID. Likewise, input layer 2 = Layer_2, and Table field 2 = Layer_2's OBJID.

Following the join, open the output layer's attribute table and click the Select features using an expression button (yellow box with a superimposed sigma). In the left-hand panel, insert the expression "OBJID" = "OBJID_2" (include the double-quotes). Voila! the selected records represent the cases where the expression is true.

Answered by Stu Smith on February 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP