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?
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]
Very nice use case for yet another handy QGIS feature: Layer actions!
Here the steps to configure your layer action:
Actions
.Fill the dialog inputs as indicated in the following picture.
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%]'')
Accept the dialogs, go to Actions drop-down menu and choose Select same value
.
DEMO: Since images (or, better, animations) explain it better, this is an example with my data:
Correct answer by Germán Carrillo on October 17, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP