I know basic python, but am struggling to learn how to use it with QGIS. I have a polygon shapefile that maps the presence/absence of various species. Each species has a column in the attribute table a- if the species is present in the polygon then the species name is in the cell, otherwise "null". I have a dictionary of fullname:abbreviation which I use to create a string for each row of abbreviations so that all the data for each row is represented in a single field.
The script seems to work, but when I tried to use the shapefile in ArcGIS the new column was empty. I saved the edits afterward. Can anyone tell me what I'm missing?
from PyQt4.QtCore import QVariant
subs = {'Argent & Sable':'AS',
'Barbastelle':'Bbl',
'Barred Tooth-striped':'BTs',
}
#etc etc
layer = iface.activeLayer()
layer.startEditing()
iter = layer.getFeatures()
for feature in iter:
newStr = ''
for field in feature.attributes():
if field == 'NULL':
pass
else:
if field in subs.keys():
newStr += subs[field] + ', '
feature['Species'] = newStr
layer.updateFeature(feature)