Saturday, 6 July 2019

attribute table - Selecting new records using values inside previous selected rows in QGIS?


I am trying to create a selection. First, I select records which have Null value in "Status" column ("Status" is NULL"). Then it starts to be a bit tricky. I want to select all the records which have the same number as selected "Status" records. Is it possible to do it in QGIS?


First, I select null values using "Status" is NULL expression.


First step


Then I need to add something to take ID values and use it to create a new selection.


The final selection should look like that:


Second step


I am using QGIS 2.16.



Answer



You may run this code from the Python Console (it works on the active layer):



layer = iface.activeLayer()
query_1 = '"Status" is NULL'
selection = layer.getFeatures(QgsFeatureRequest().setFilterExpression(query_1))
selected_ids = [k["ID"] for k in selection]

ids = []
for feat in layer.getFeatures():
if feat["ID"] in selected_ids:
ids.append(feat.id())
layer.setSelectedFeatures([l for l in ids])


You will obtain the desired output:


enter image description here


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...