10

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]

1 Answer 1

18

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%]\'')
  1. Accept the dialogs, go to Actions drop-down menu and choose Select same value. enter image description here
  2. 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

1
  • 1
    very nice explanation!
    – Alexander
    Nov 4, 2019 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.