Skip to main content
Commonmark migration
Source Link

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][1]][1]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. [1]: https://i.sstatic.net/risqW.png

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][1]][1]

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. [1]: https://i.sstatic.net/risqW.png

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.

added explanations
Source Link
GeorgeC
  • 8.3k
  • 7
  • 56
  • 143

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 (ntextnewtext)
   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)
   print (f.id())
   layer.changeAttributeValue(f.id(), 2, newtext)
layer.commitChanges()
layer.updateFields()

Dataset looks like [![enter image description here][1]][1]

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. [1]: enter image description herehttps://i.sstatic.net/risqW.png

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']
   print (text)
   stext=re.split('_+', text)
   print(stext)
   rtype=stext[-1:]
   newtext=' '.join(stext[:-1])
   print (ntext)
   attr_value={2:newtext}
   #layer_provider.changeAttributeValues({1:attr_value})
   #layer.changeAttributeValue(f.id(), RoadName, 30)
   print (f.id())
   layer.changeAttributeValue(f.id(), 2, newtext)
layer.commitChanges()
layer.updateFields()

Dataset looks like enter image description here

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][1]][1]

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. [1]: https://i.sstatic.net/risqW.png

Source Link
GeorgeC
  • 8.3k
  • 7
  • 56
  • 143

PyQGIS changeAttributeValue not saving modified records

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']
   print (text)
   stext=re.split('_+', text)
   print(stext)
   rtype=stext[-1:]
   newtext=' '.join(stext[:-1])
   print (ntext)
   attr_value={2:newtext}
   #layer_provider.changeAttributeValues({1:attr_value})
   #layer.changeAttributeValue(f.id(), RoadName, 30)
   print (f.id())
   layer.changeAttributeValue(f.id(), 2, newtext)
layer.commitChanges()
layer.updateFields()

Dataset looks like enter image description here