Monday, 29 February 2016

qgis - PyQGIS make screenshot of mapCanvas after setExtent is called


I have the following function:


def createScreenshots(layer):
counter = 0
for feature in layer.getFeatures():
counter += 1

point = feature.geometry().asPoint()
qgis.utils.iface.mapCanvas().setCenter(point)
qgis.utils.iface.mapCanvas().refreshAllLayers()
qgis.utils.iface.mapCanvas().saveAsImage("D:\\\\m\\testing\\" + str(counter) + ".png")

Screenshots are not in real coordinates, .pgw header is not in right coordinates, therefore resulting png images are shifted.


Question: How to wait for rendering of canvas after setCenter is called?



Answer



ids = None


def createScreenshot4(layer):
global ids
ids = layer.allFeatureIds()

def exportMap():
global ids
qgis.utils.iface.mapCanvas().saveAsImage( "D:\\\\ma\\bo\\{}.png".format( ids.pop() ) )
if ids:
setNextFeatureExtent()
else: # We're done

qgis.utils.iface.mapCanvas().mapCanvasRefreshed.disconnect( exportMap )

def setNextFeatureExtent():
reqq = QgsFeatureRequest()
reqq.setFilterFid(ids[-1])
for feature in layer.getFeatures(reqq):
point = feature.geometry().asPoint()
qgis.utils.iface.mapCanvas().setCenter(point)
qgis.utils.iface.mapCanvas().refreshAllLayers()



qgis.utils.iface.mapCanvas().mapCanvasRefreshed.connect( exportMap )
setNextFeatureExtent() # Let's start

Resulted code with setCenter and refreshAllLayers.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...