Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

I just wanted to post some updates to the previous answer in light of changes to the QGIS Python API since version 2.0.

As before, you would get a list of selected features with iface.activeLayer().selectedFeatures(), however in QGIS versions >= 2.0 you now use the function QgsFeature.attributes() instead of QgsFeature.attributeMap(). The two functions are not exactly the same: attributes() now returns a list, whereas the old attributeMap() returned a dictionary. Take a moment to browse the API documentation for QgsFeature.attributes(), QgsAttributes, etc to understand how attributes are stored/accessed.

If you're interested, you can read about some of the rationale behind the API changes on this mailing list thread: [Qgis-developer] new vector api select features. Briefly (in the words of QGIS developer Martin Dobias):

Access to attributes: there is no f.attributeMap() anymore, because attributes are now stored in a vector (Python: list) instead of a map (Python: dict). QgsFeature class emulates python container object 4 so you can access attributes as if QgsFeature instance was a list or dictionary, with keys being either field indices or field names:

f[0] ... first attribute

f["type"] ... attribute named "type"

It is still possible to get all attributes: f.attributes() returns a list of values.

So as an example of specifically what you asked how to do (get data for a single feature you've selected), suppose you had a layer of containing city features that each have a list of attributes, including a county attribute. To get the county value for a specific city you've selected (say Helena, AR), do the following in the Python console:

city = iface.activeLayer().selectedFeatures()[0]
# OUTPUTS: u'Phillips'

If you want the values of all of the fields in the city's attribute table, then you would just use:

cityData = city.attributes()
share|improve this answer

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.