PyQGIS getting a dictionary for each feature of a layer with their attribute values
QgsJsonUtils
class has exportAttributes
method for getting attributes as dict
.
Solution 1:
Making a dictionary including feature id
as key, attributes map as value:
{feature1.id: {attr1: value, attr2: value, ...},
feature2.id: {attr1: value, attr2: value, ...},
...}
layer = QgsProject.instance().mapLayersByName('LAYER_NAME')[0]
features_dict = {f.id(): QgsJsonUtils.exportAttributes(f) for f in layer.getFeatures()}
Solution 2:
Making a list including attributes map (dict
) of features:
[{attr1: value, attr2: value, ...}, # feature 1
{attr1: value, attr2: value, ...}, # feature 2
...]
layer = QgsProject.instance().mapLayersByName('LAYER_NAME')[0]
features_list = [QgsJsonUtils.exportAttributes(f) for f in layer.getFeatures()]
The snippet below should help you. It just prints a dictionary for each feature to the console, but you could do something else with the feature attribute dictionaries if you wanted.
layer = iface.activeLayer()
def print_atts_dict(layer, feature):
flds = [f for f in layer.fields()]
atts = {}
for f in flds:
atts[f.name()] = feature[f.name()]
print(atts)
for f in layer.getFeatures():
print_atts_dict(layer, f)
# get the fields and features:
fields = indiv.fields()
features = indiv.getFeatures()
# initialise an empty dict with each field name:
attdict = {}
for field in fields:
attdict[field.name()] = []
# for each field in each feature, append the relevant field data to the dict item:
for feature in features:
for field in fields:
v = feature[field.name()]
attdict[field.name()].append(v)
print(attdict)
That gives a dict that looks like this for some Ghana regions:
{'shapeName': ['Greater Accra', 'Central', 'Western', 'Eastern', 'Ashanti', 'Volta', 'Brong Ahafo', 'Northern', 'Upper West', 'Upper East'],
'shapeISO': ['GH-AA', 'GH-CP', 'GH-WP', 'GH-EP', 'GH-AH', 'GH-TV', 'GH-BA', 'GH-NP', 'GH-UW', 'GH-UE'],
'shapeID': ['GHA-ADM1-1590546715-B1', 'GHA-ADM1-1590546715-B2', 'GHA-ADM1-1590546715-B3', 'GHA-ADM1-1590546715-B4', 'GHA-ADM1-1590546715-B5', 'GHA-ADM1-1590546715-B6', 'GHA-ADM1-1590546715-B7', 'GHA-ADM1-1590546715-B8', 'GHA-ADM1-1590546715-B9', 'GHA-ADM1-1590546715-B10'],
'shapeGroup': ['GHA', 'GHA', 'GHA', 'GHA', 'GHA', 'GHA', 'GHA', 'GHA', 'GHA', 'GHA'], 'shapeType': ['ADM1', 'ADM1', 'ADM1', 'ADM1', 'ADM1', 'ADM1', 'ADM1', 'ADM1', 'ADM1', 'ADM1']}
Which I think is what you are after.