<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<META NAME="Generator" CONTENT="MS Exchange Server version 14.02.5004.000">
<TITLE>Python script to iterate through attributes and then refresh the canvas - doesn't show any map....</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/rtf format -->
<P DIR=LTR><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">Hi,</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"></SPAN></P>
<P DIR=LTR><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">Am</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">completely</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">stuck with a</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">QGIS</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">project in Python and tried everything but nothing seems to work.</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">The python script does work</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">as such</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">BUT the canvas never shows the map and therefor the exported maps are empty….</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"> </SPAN></P>
<P DIR=LTR><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"></SPAN></P>
<P DIR=LTR><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">What I’m trying to achieve:</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"></SPAN><SPAN LANG="en-us"></SPAN><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Symbol">·<FONT FACE="Courier New"> </FONT></FONT></SPAN><SPAN LANG="en-us"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">Create a Python script that iterates through a series of</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">ca. 2500</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">polygons</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">based on attribute value</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">, zoom to layer extend</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri"> and save the canvas for each attribute as an image file.</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"></SPAN><SPAN LANG="en-us"></SPAN><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Symbol">·<FONT FACE="Courier New"> </FONT></FONT></SPAN><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">I then use the image files in other</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">documents</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"></SPAN></P>
<P DIR=LTR><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"></SPAN></P>
<P DIR=LTR><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">The issue is that while manually I get the canvas to reload and display the map, this doesn’t happen when running the python script. Have tried couple of ways: </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"></SPAN><SPAN LANG="en-us"></SPAN><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">1. </FONT></SPAN><SPAN LANG="en-us"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">Don’t load the project each time and just change the filter</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"></SPAN><SPAN LANG="en-us"></SPAN><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">2. </FONT></SPAN><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">Add sleep timers to give the canvas time to refresh etc</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">.</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"></SPAN><SPAN LANG="en-us"></SPAN><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">3. </FONT></SPAN><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">Tried different QGIS versions (2.2, 2.4 & 2.6 on Win 64x)</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"></SPAN></P>
<P DIR=LTR><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">But all to no avail….</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">Hope somebody</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">’s got a great idea how to solve it</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">.</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"></SPAN></P>
<P DIR=LTR><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"></SPAN></P>
<P DIR=LTR><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">Regards,</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">Frank</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"></SPAN></P>
<P DIR=LTR><SPAN LANG="nl"><I></I></SPAN><SPAN LANG="nl"><I></I></SPAN><I><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">Python code:</FONT></SPAN></I></P>
<P DIR=LTR><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"></SPAN></P>
<P DIR=LTR><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri"># File location & specifications </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">PROJECTPATH = 'C:/Users/Frank/Documents</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri"></FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">/Targetting/Kaarten/'</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">PROJECTFILE = 'C:/Users/Frank/Documents</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri"></FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">/Targetting/TargettingkaartV26qgs.qgs'</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">EXTSNAPSHOTS = '.jpg'</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">EXTSNAPSHOTS2 = '.png'</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">SUFFIX = '65Plus_v1'</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">last_id = 1</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"><FONT COLOR="#1F497D" FACE="Calibri">while last_id < 5:</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri"># Clear map canvas</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">QgsMapLayerRegistry.instance().removeAllMapLayers()</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">qgis.utils.iface.mapCanvas().refresh()</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> </SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri"># Open QGIS project</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">QgsProject.instance().setFileName(PROJECTFILE)</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">QgsProject.instance().read()</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">qgis.utils.iface.mapCanvas().refresh()</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">time.sleep(30) </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">canvas = qgis.utils.iface.mapCanvas()</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">acl = canvas.layer(0)</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">qgis.utils.iface.mapCanvas().layer(0).selectAll()</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">selection = 'RijNo = ' + str(last_id) </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> </SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">acl.setSubsetString( selection )</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">acl.triggerRepaint</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">canvas.layer(1).triggerRepaint </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">time.sleep(5) </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">qgis.utils.iface.setActiveLayer(acl)</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri"># Zoom in and out to refresh potential issues with OpenLayer</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">canvas.zoomByFactor(3)</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">canvas.zoomByFactor(0.5)</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">qgis.utils.iface.zoomToActiveLayer()</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">qgis.utils.iface.mapCanvas().refresh()</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> </SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">time.sleep(10)</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri"># Save project</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">p=QgsProject.instance()</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">p.setFileName(PROJECTFILE)</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">p.write()</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri"># Save content of map canvas as image</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> </SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"> <FONT COLOR="#1F497D" FACE="Calibri">p = str(last_id) + 'kaart_' </FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="nl"> </SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="en-us"> <FONT COLOR="#1F497D" FACE="Calibri">qgis.utils.iface.mapCanvas().saveAsImage(PROJECTPATH+ p + SUFFIX + EXTSNAPSHOTS)</FONT></SPAN></P>
<P DIR=LTR><SPAN LANG="en-us"> </SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"> <FONT COLOR="#1F497D" FACE="Calibri">last_id = last_id +1</FONT></SPAN><SPAN LANG="nl"></SPAN><SPAN LANG="nl"></SPAN></P>
</BODY>
</HTML>