2

What is the equivalent in QGIS (or other FOSS) to this:

rows = arcpy.SearchCursor(table,
                          fields="ORIG_FID; field1; field2; field3; field4",
                          sort_fields="ORIG_FID A")

for row in rows:
5

Check out the PyQGIS Cookbook's section on Using Vector Layers.

Hacking together the examples there:

# The expression will filter the features where the field "location_name" contains
# the word "Lake" (case insensitive)
exp = QgsExpression('location_name ILIKE \'%Lake%\'')
request = QgsFeatureRequest(exp)
request.setSubsetOfAttributes(['name','id'],layer.pendingFields())
for feature in layer.getFeatures(request):
    # do whatever you need with the feature

Also, see this question for some other hints: What are QGIS Arcpy Equivalent Functions for Update/Delete Row/Field?

  • Thank you Richard, I'll check this approach! Although at first sight I don't see the "sort fields" option. – Albert Mar 11 '16 at 7:15
  • 1
    @Albert As a newbie, I am also not seeing where in the Cookbook they show examples of the sort. However, I did find this answer: gis.stackexchange.com/q/138769/43 – Richard Morgan Mar 11 '16 at 13:01
2

Use .getFeatuers(), this returns all layer field values:

Query the provider for features specified in request.

for feature in table.getFeatures():
    if feature['myField'] == someVariable:
        # do something

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.