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?

5
  • This probably helps you: stackoverflow.com/questions/7255463/…
    – Martin
    Commented Feb 5, 2015 at 8:04
  • Do you want it to abort iteration or continue to the next feature? If you want it to abort, it will only zoom to the first feature.
    – gsherman
    Commented Feb 5, 2015 at 16:02
  • I would like to continue to the next feature, until the user presses esc/spacebar...
    – Pugazh
    Commented Feb 9, 2015 at 8:04
  • I have not tried your code gcarrillo, will let you know soon...
    – Pugazh
    Commented Feb 20, 2015 at 14:00
  • Ok, let me know if you face any problem. Commented Mar 3, 2015 at 3:34

1 Answer 1

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.

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.