11

Could anybody provide an example how to get the attributes of selected features?

I tried the following code in the Python Console : but I'm stuck at the point where I'd like to get the attributes:

from qgis.utils import iface
canvas = iface.mapCanvas()
cLayer = canvas.currentLayer()
selectList = []
if cLayer:
 count = cLayer.selectedFeatureCount()
 print (count)
 selectedList = layer.selectedFeaturesIds()
 for f in selectedList:
 # This is where I'm stuck
 # As I don't know how to get the Attributes of the features
Taras
35.7k5 gold badges77 silver badges151 bronze badges
asked Aug 20, 2012 at 13:56
1

3 Answers 3

18

This will work:

layer = iface.activeLayer()
features = layer.selectedFeatures()
for f in features:
 print f.attributeMap()

In PyQGIS 3:

layer = iface.activeLayer()
features = layer.selectedFeatures()
for f in features:
 # refer to all attributes
 print (f.attributes()) # results in [3, 'Group 1', 4.6]
for f in features:
 # refer to a specific values of a field index
 print(f.attribute(1)) # results in Group 1
Taras
35.7k5 gold badges77 silver badges151 bronze badges
answered Aug 20, 2012 at 14:07
6

For QGIS 3.10

Not sure if this post was asking for it in a list...

Print field of selected features

layer = iface.activeLayer()
for feature in layer.selectedFeatures():
 print(feature['field name'])

Create list of selected features

layer = iface.activeLayer()
list = []
for feature in layer.selectedFeatures():
 list.append(feature['field'])
print(list)
Taras
35.7k5 gold badges77 silver badges151 bronze badges
answered Mar 2, 2021 at 15:54
2

There are also other approaches available:

Approach 1 : QgsAggregateCalculator with ArrayAggregate

Available aggregates to calculate.

Not all aggregates are available for all field types.

This approach is available since QGIS 2.16.

layer = iface.activeLayer()
# creates a memory layer of original layer exclusively with all selected features in it
temp_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))
# specify your target field
result = temp_layer.aggregate(QgsAggregateCalculator.ArrayAggregate, "your_field")[0]
# prints an output list
print(result)

Approach 2 : QgsVectorLayerUtils

Contains utility methods for working with QgsVectorLayers.

This approach is available since QGIS 3.

layer = iface.activeLayer()
 # creates a memory layer of original layer exclusively with all selected features in it
temp_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))
# specify your target field
result = QgsVectorLayerUtils.getValues(temp_layer, "your_field")[0]
# print an output list
print(result)

References:

answered Jan 17, 2022 at 10:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.