1

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

iface.mapCanvas().saveAsImage("image_path")

but how to iterate each feature with fix scale value.

3

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])
        canvas.zoomScale(scale)
        canvas.refresh()
        QTimer.singleShot(delay, lambda: save_as_image(fid))

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

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.