Im reading some attributes from shapefile features in a loop. According to this:


I should be able to reference my attributes (and getting native python datatypes) like this:

for feature in myLayer.getFeatures():
   geom = feature.geometry().asPoint()

   #features have attribute called 'name'
   print feature['name']
   print feature.name

The first print statement simply prints a generic QVariant object address, second one fails saying there is no method named name on the QgsFeature object. Essentially it behaves like the old API, whilst im using 2.0.1.


  • 1
    The dot notation of accessing attributes was added (by me) and then removed (by me) before the release. Because QgsFeature contains other methods using the dot notation for attribute lookup made it possible to mask built in methods. If you had a field called id and then did feature.id you could mask the id function. It made the API inconstant so I removed it. – Nathan W Feb 2 '14 at 22:08
  • I will update my blog post. – Nathan W Feb 2 '14 at 22:09

To know the available functions, in the Python console type:

# and

and one answer is (always in the Python console, slightly different with Processing or for a plugin):

fields = mylayer.pendingFields()
field_names = [field.name() for field in fields]
for feature in mylayer.getFeatures():
      geom= feature.geometry()
      point = geom.asPoint()
      print point
      attributes = features.attributes()
      print attributes
      dip_dir = feature['dip_dir']
      print dip_dir
      # with a dictionary
      atr = dict(zip(field_names, feature.attributes()))
      print atr
      print atr['dip_direct']

Result with one of my shapefiles

 print field_names
 [u'dip_direct', u'dip', u'type']
 # some results of the for loop
 # geom.asPoint()
 # attributes of the feature
 [180, 26, u'N']
 # dip_dir value
 # attributes as dictionary
 {u'type': u'N', u'dip': 26, u'dip_direct': 180}
 # one particular value (dip_dir)
 [335, 50, u'N']
 {u'type': u'N', u'dip': 50, u'dip_direct': 335}

and also:

for ...
    print geom.exportToGeoJSON()
{ "type": "Point", "coordinates": [198235.93072444110293873, 89025.821821038480266] }
{ "type": "Point", "coordinates": [199847.19120459226542152, 89197.0735811617487343] }


 # WKT
 for ...
     print geom.exportToWkt()
 POINT(198235.93072444110293873 89025.821821038480266)
 POINT(199847.19120459226542152 89197.0735811617487343)#or

And a true GeoJSON with attributes ('properties'):

 for elem in layer.getFeatures():
     geom= elem.geometry()
     atr = dict(zip(field_names, elem.attributes()))
     print dict(geometry=geom.exportToGeoJSON(),properties=atr)

  {'geometry': u'{ "type": "Point", "coordinates": [198235.93072444110293873, 89025.821821038480266] }', 'properties': {u'type': u'N', u'dip': 26, u'dip_direct': 180}}
  {'geometry': u'{ "type": "Point", "coordinates": [199847.19120459226542152, 89197.0735811617487343] }', 'properties': {u'type': u'N', u'dip': 50, u'dip_direct': 335}}
| improve this answer | |
  • QgsFeature has a _geo_interface_ property that exports GEOJSON with properties. – Nathan W Feb 2 '14 at 22:06
  • This will be usefull. But thanks for pointing out Nathan, that you removed it so i won't hit my head against the wall for too long. – U2ros Feb 3 '14 at 10:22

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.