Sign up ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a problem / question regarding python and QGIS. Is it, and yes how, possible to get the data of one selected feature in QGIS using python?

In my example I have a layer which shows some parcels for urban planning. If I now select one of the parcels I want to get the data for exactly this parcel. This data should be read out by an python script and then added to an automatically created PDF file. The last step is easy, but I didn´t get the data from this selected feature...

It is possible to get data from the active layer, but is it also possible to get the data from one selected feature of this layer?

Hope you can help me. Regards

share|improve this question

1 Answer 1

up vote 9 down vote accepted

Calling layer.selectedFeatures() will return a list with your selected feature(s). You can then call feature.attributeMap() on each of the selected features to get a dictionary of each feature's attributes.

layer = qgis.utils.iface.activeLayer()
selected_features = layer.selectedFeatures()
for i in selected_features:
    attrs = i.attributeMap()
    for (k,attr) in attrs.iteritems():
    print "%d: %s" % (k, attr.toString())

I recently had a similar question answered here: When iterating over a vector layer with pyqgis, how do I check whether a feature is selected?

share|improve this answer
Thank you very much! This works! – Sven Aug 6 '12 at 13:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.