3

I would like to run a loop with PyQGIS, it should wait for the user to press a key either SpaceBar or Escape to continue.

canvas = qgis.utils.iface.mapCanvas()
aLayer = canvas.currentLayer()

feat = QgsFeature()
iter = aLayer.getFeatures()

while iter.nextFeature(feat):
    aLayer.removeSelection()
    aLayer.select(feat.id())
    qgis.utils.iface.actionZoomToSelected().trigger()

    <wait for user input>

How can I do that?

Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
Pugazh
  • 607
  • 6
  • 17

1 Answers1

7

Note: This answer was posted for QGIS 2. Here you can find a QGIS 3 version.


If I got you right, you can follow this workflow to iterate through features by pressing the space bar:

  1. Open QGIS, load your layer(s), and save your project.

  2. Download the script iterate_features.py and save it in the same folder as your QGIS project.

  3. Open the QGIS Python console (Plugins->Python console), type the following line, and then press Enter:

     import iterate_features
    
  4. Now click anywhere on your map or ToC.

You should now be able to select and zoom in to the next feature by pressing the space bar or to finish the iteration by pressing Escape.

It should work on the active layer in the ToC, each time you change the active layer, the iterator is restarted.

Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178