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:
1

2 Answers 2

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?

2
  • Thank you Richard, I'll check this approach! Although at first sight I don't see the "sort fields" option.
    – Albert
    Mar 11, 2016 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 Mar 11, 2016 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
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.