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:
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Albert
  • 2,607
  • 12
  • 31

2 Answers2

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?

Richard Morgan
  • 2,819
  • 1
  • 21
  • 28
  • 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 07: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: http://gis.stackexchange.com/q/138769/43 – Richard Morgan Mar 11 '16 at 13:01
2

Use .getFeatures(), 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
Aaron
  • 51,658
  • 28
  • 154
  • 317
artwork21
  • 35,114
  • 8
  • 66
  • 134