1. An Introduction to Python
  2. An Introduction to PyQGIS
  3. Using the QGIS Python console
  4. Custom Python Expression Functions
  5. Exploring the PyQGIS API
  6. Plugins
  7. Reference Materials

Filtering Vector Features

# Just the selected features
layer = iface.activeLayer()
for feature in layer.selectedFeatures():
    print(feature['name'])

# Filter by current extent of the map
extent = iface.mapCanvas().extent()
request = QgsFeatureRequest()
request.setFilterRect(extent)
layer = iface.activeLayer()
for feature in layer.getFeatures(request):
    print(feature['name'])

# Filter by fixed extent
extent = QgsRectangle(-2.15, 52.20, 1.20, 52.60)
request = QgsFeatureRequest()
request.setFilterRect(extent)
layer = iface.activeLayer()
for feature in layer.getFeatures(request):
    print(feature['name'])

# Filter by attribute
exp = QgsExpression('name ILIKE \'%London%\'')
request = QgsFeatureRequest(exp)
layer = iface.activeLayer()
for feature in layer.getFeatures(request):
    print(feature['name'])

# If you need to use a ' in your query double it

exp = QgsExpression("\"name\" like '%Int''l'")
request = QgsFeatureRequest(exp)
layer = iface.activeLayer()
for feature in layer.getFeatures(request):
    print(feature['name'])