QgsJsonUtils
class has exportAttributes
method for getting attributes as dict
.
Solution 1:
Making a dictionary including feature id
as key, attributes map as value like:
{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 without id
s like:
[{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()]