9

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

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

14

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?

  • weird, the QgsFeature objects that results from the selected_features has no geometry method... – user32882 Sep 15 '18 at 7:52
8

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]
city["county"]
# 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()
1

I'd like to add something useful to the answer provided by J. Taylor and point that current accepted answer from 2012 is not correct anymore:

QgsFeature object supports useful __geo_interface__ attribute, which returns a dictionary from a given feature. The dictionary is actually in a GeoJSON standard, so you get all attribute-value pairs and geometry included. The feature exists for some time now, first described by Sean Gillies sometime in 2012, with an example usage here. I couldn't find any information from which version of QGIS is it available from and I haven't tried QGIS 3 yet, but was surely available from at least QGIS 2.14.

TL;DR:

layer = qgis.utils.iface.activeLayer()
selected_features = layer.selectedFeatures()
for i in selected_features:
    attrs = i.__geo_interface__
    print attrs # GeoJSON attributes of a feature

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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