TransWikia.com

Extracting features with equal field value using graphical modeler of QGIS

Geographic Information Systems Asked on October 17, 2020

Is it possible to automate the following procedure with graphical modeler in qgis 3.4 or is it easier to perform such action with a python script using PyQGIS?

  1. Identify a feature (a line) in a vector layer using the mouse pointer, features have an attribute named RLID
  2. Then let the model select all features with the same attribute value in the vector layer

Right now this is done manually, a very time-consuming process. The manual process starts by identifying a feature and copying the RLID attribute value. Then the features with the same RLID value are selected using “Select features by value”.

I am completely new to qgis but know how to program with python.

Speed is important since we have 200k features to process. I tried a little python script (with a selected feature) for extracting features with the same attribute value and it took 30s, which is very long time compared with F3 – Select features by value.

layer = iface.activeLayer();
features = layer.getFeatures()
selected = layer.getSelectedFeatures()
for f in selected:
    rlid = f['RLID']
    print(rlid)

fs = [f.id() for f in features if f['RLID'] == rlid]
print(fs)

Found by older posts that a quicker way to obtain the features fs is:

expr = QgsExpression( ""RLID"='"+rlid+"'" )
it = layer.getFeatures( QgsFeatureRequest( expr ) )
fs = [i.id() for i in it]

One Answer

Very nice use case for yet another handy QGIS feature: Layer actions!

Here the steps to configure your layer action:

  1. Open your layer Properties dialog.
  2. Click on menu Actions.
  3. Click on the green cross to add an action. enter image description here
  4. Fill the dialog inputs as indicated in the following picture.

    enter image description here

    You can Copy & Paste this couple of lines of code into the 'Action Text' box:

    layer = QgsProject.instance().mapLayer('[% @layer_id %]')
    layer.selectByExpression('"RLID"='[%RLID%]'')
    
  5. Accept the dialogs, go to Actions drop-down menu and choose Select same value. enter image description here

  6. Now click on the map on one of your layer lines and... that's it!

DEMO: Since images (or, better, animations) explain it better, this is an example with my data:

enter image description here

Correct answer by Germán Carrillo on October 17, 2020

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