How do I fill the attribute with "SID_2"
of the captured image where the ID of "SID_1"
is NULL
?
I'm using QGIS 3.22
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.
Sign up to join this communityHow do I fill the attribute with "SID_2"
of the captured image where the ID of "SID_1"
is NULL
?
I'm using QGIS 3.22
If you want to update SID_1
with the value of SID_2
if SID_1
is NULL
Update existing field
in the Field Calculator
IF( "SID_1" IS NULL, "SID_2", "SID_1" )
This will run an update on all rows in that table, albeit with the desired result. For small'ish tables this should be good enough - on very large layers you may want to pre-select features having a NULL
value in SID_1
.
You can use a short PyQGIS script. First create your new field e.g. "ID_2"
and fill it with the values from "ID_1"
(the expression to do that is simply "ID_1"
).
Next open the Python Console (Ctrl+Alt+P), click the notepad icon to open an editor, copy and paste in the code below.
lyr = iface.activeLayer()
fld_index = lyr.fields().lookupField('ID_2')
atts_map = {}
id = 1
for feat in lyr.getFeatures():
atts_map[feat.id()] = {fld_index: lyr.getFeature(id)['ID_2']}
if feat['ID_2'] == NULL:
atts_map[feat.id()] = {fld_index: atts_map[id-1][fld_index]}
id+=1
lyr.dataProvider().changeAttributeValues(atts_map)
Note that I have copied the field names from your other question:
How can we fill a property in QGIS 3.22?
So if your field names are not "ID_1"
and "ID_2"
, you should adjust accordingly.
Finally, make sure your layer is selected as the active layer and click the green Run button to run the script.
See images below for example results on a test layer.
Script pasted into editor:
Attribute table before:
Attribute table after:
"SID_1" = "SID_2"
?