I want to export each feature by zooming to certain scale (eg 1:25000 where feature will be inside map canvas) and save into PNG format. I know function


but how to iterate each feature with fix scale value.


Use the following script. Specify folder

canvas = iface.mapCanvas()
layer = iface.activeLayer()
ids = layer.allFeatureIds()

scale = 25000
delay = 250 # 250 milisecond
folder = "/path/to/folder/"

def prepare():
    if ids:
        fid = ids.pop()
        canvas.zoomToFeatureIds(layer, [fid])
        QTimer.singleShot(delay, lambda: save_as_image(fid))

def save_as_image(fid):
    file_name = folder + str(fid) + ".png"    
    QTimer.singleShot(delay, prepare)

| improve this answer | |
  • perfect code. thanks @kadir – Devenepali Oct 8 at 13:46
  • this code works perfectly inside python console of qgis. While i try to implement inside plugin the save_as_image function is not being called from lambda:save_as_image function.Can you please suggest – Devenepali Oct 22 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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