1

I've been trying to save some changes to the a dataset using PyQGIS but I can't see any changes even after closing/reloading the layer attribute table.

I have tried a few different options but none of them work.

import re
layer = iface.activeLayer()
selection = layer.getFeatures() #all features
#selection = layer.selectedFeatures() #only selected features
layer_provider=layer.dataProvider()
layer.startEditing()
for f in selection:
   text=f['RoadMntnc'] #get the unique road id to process.
   print (text)
   stext=re.split('_+', text) #split the road id into it's components.
   print(stext)
   rtype=stext[-1:] # the type of road
   newtext=' '.join(stext[:-1]) # the road name without the road type
   print (newtext)
   attr_value={2:newtext} # the column to replace and the value to use
   #layer_provider.changeAttributeValues({1:attr_value}) #attempts to write the value
   #layer.changeAttributeValue(f.id(), RoadName, 30)
   layer.changeAttributeValue(f.id(), 2, newtext)
layer.commitChanges()
layer.updateFields()

Dataset looks like enter image description here

I want to eventually autofill the RoadName, RoadType and other fields from just the unique RoadMntnc name so that there's less work/less data entry issues when adding roads to the dataset.

3
  • You defined rtype but never actually use it. Were you supposed to use it in the following line newtext=' '.join(rtype[:-1])? Perhaps you could edit your question to include a description of what your code is supposed to do?
    – Joseph
    Commented Aug 8, 2019 at 15:31
  • Yes, it for use to fill in other columns but I wanted to make sure the code for one column works first. The issue is that any of the 3+ formats I have tried for layer.changeAttributeValue don't actually update the table. I have edited the question with more details.
    – GeorgeC
    Commented Aug 8, 2019 at 22:37
  • Figured it out...I had to get the fieldIndex from the name of the column so the following works --- dpr = layer.dataProvider() fieldName='RoadName' fieldIndex = layer.fields().indexFromName(fieldName) dpr.changeAttributeValues({f.id(): {fieldIndex: ntext}})
    – GeorgeC
    Commented Aug 8, 2019 at 23:09

1 Answer 1

1

Here's what I ended up using

import re

def Update_Field(fieldName, ntext): # Function to simply updating fields
    print (ntext)
    fieldIndex = layer.fields().indexFromName(fieldName)
    dpr.changeAttributeValues({f.id(): {fieldIndex: ntext}})
    layer.commitChanges()
RoadType_dict = {'Rd':'Road', 'Avenue': 'Ave'}
layer = iface.activeLayer()
selection = layer.getFeatures() #all features
#selection = layer.selectedFeatures() #only selected features
dpr = layer.dataProvider()

for f in selection:
    print (f.id())
    text=f['RoadMntnc']
    print (text)
    stext=re.split('_+', text)
#Update RoadName
    ntext=' '.join(stext[:-1])
    fieldName='RoadName'
    Update_Field(fieldName,ntext)
#Update Road Type Abbreviation
    ntext=stext[-1:][0]
    fieldName='Type_Abb'
    Update_Field(fieldName,ntext)
#Update Road Type
    ntext=RoadType_dict.get(stext[-1:][0])
    fieldName='Type'
    Update_Field(fieldName,ntext)

layer.commitChanges()
layer.updateFields()

Your Answer

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

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