2

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

enter image description here

3
  • What have you tried? How did you achieve it in your example? How do you determine the order in your attribute table? Is there a logic which NULL is replaced with which SID?
    – Erik
    May 11 at 8:59
  • 1
    Why not updating values like "SID_1" = "SID_2" ?
    – Taras
    May 11 at 9:31
  • Currently, Excel is filling in the field. The order of the properties table is sequence fields.
    – Don
    May 11 at 23:34

2 Answers 2

6

If you want to update SID_1 with the value of SID_2 if SID_1 is NULL

  • choose Update existing field in the Field Calculator
  • execute a simple conditional:
    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.

3
  • 3
    You can also use COALESCE("SO_1", "SO_2"). May 11 at 9:10
  • 2
    True. But both should compile to the same executor, and some verbosity may help at times ,)
    – geozelot
    May 11 at 9:12
  • I think the expression of the image caused a misunderstanding I want to implement the value of sid_1 like sid_2. sid_2 is the result value.
    – Don
    May 11 at 23:38
5

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:

enter image description here

Attribute table before:

enter image description here

Attribute table after:

enter image description here

1
  • Thank you. Ben I applied it and it works well. I'd like to use a modeler to do this. Can you make it work on the modeler? The reason why I use a modeler is because I want to do several repetitive steps at once.
    – Don
    May 13 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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