TransWikia.com

Saving each feature in PNG format along with map canvas in certain scale using PyQGIS

Geographic Information Systems Asked on November 29, 2020

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.

One Answer

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()

Correct answer by Kadir Şahbaz on November 29, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP