I have a vector layer (grid). Currently I select some tiles with the select tool and paste some Python code to get information about these features.
layer = qgis.utils.iface.activeLayer()
selected_features = layer.selectedFeatures()
for i in selected_features:
...
Now, I want to get the feature values from the tiles which I can see on the map canvas, without selecting them.
There were two possibilities.
- Selecting only the features which are fully visible ("contained" in the map canvas)
- Selecting all features which are also partly visible ("intersecting" the map canvas)
Is there a way to do this with PyQGIS?
Best Answer
Yes, it is possible by using a QgsFeatureRequest with 'setFilterRect' method. The argument of this method is a QgsRectangle object obtained for visible part of Map Canvas with its 'extent' method (QgsMapCanvas class). I tried out my approach with this code:
and world_borders shapefile that has 3784 features. After running the code with the next image situation:
I got the display of only 77 visible features attributes (printed result at the Python Console of GIS).