2

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

2

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

layer.dataProvider().select(layer.dataProvider().attributeIndexes(), QgsRectangle(), False)
  • 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

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.