Take the 2-minute tour ×
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 want to extract all attributes from a non geomety layer in QGIS with Python, but there is only the function

uniqueValues(fieldIndex)

More specific: I have a postgresql table which is loaded into QGIS with "Add PostGIS layers". Then I need all attributes from two columns in a dictionary (key: column1, value: column2). Here is my code so far:

dictAttr = {}
feature = QgsFeature()
layer.dataProvider().select(layer.dataProvider().attributeIndexes())
while layer.dataProvider().nextFeature(feature):
  attributes = feature.attributeMap()
  dictAttr[attributes[FIELD_INDEX1].toString()] = attributes[FIELD_INDEX2].toString()
return dictAttr

Problem: layer is not a geometry table, so it has no features and the while loop is never entered. And I can't use uniqueValues() because I need every attribute. Do you know any way to solve this? This can't be so hard...

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

Call select passing False as third argument (i.e. fetchGeometry):

layer.dataProvider().select(layer.dataProvider().attributeIndexes(), QgsRectangle(), False)
share|improve this answer
    
so simple. I think I was confused with the spatial filter (empty QgsRectangle()) and didn't noticed the fetchGeometry attribute... –  baal Dec 16 '12 at 16:25
add comment

Your Answer

 
discard

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.