[Qgis-user] Python script to iterate through attributes and then refresh the canvas - doesn't show any map....

Frank van Baar frank at van-baar.com
Wed Nov 5 13:08:36 PST 2014


Hi Jorris,

 

Thanks and have found a slightly different solution - not 100% automatic but
only thing I need to do is push a button everytime the map has loaded in
full. I ran through and created 250 maps as JPG in ca. 15 minutes this way..
This might be a solution for anybody who need to:

 

.        Iterate through a number of different values for features (in this
case 'clusters' and 'number on inhabitants' and get the right focus and
styles for each layer.

.        Either toggle on and off different layers

 

For those interested - here's the python code.

 

Frank

 

 

import sys

 

sys.path.insert(0,'C:/Python27')

sys.path.insert(0,'C:/Python27/Lib/site-packages')

 

 

from qgis.core import *

from qgis.gui import *

from qgis.utils import *

from PyQt4.QtCore import *

from PyQt4.QtGui import *

 

 

import os, time

 

# File location & specifications 

 

PROJECTPATH = 'C:/Users/Frank/ Targetting/maps/'

EXTSNAPSHOTS = '.jpg'

EXTSNAPSHOTS2 = '.png'

SUFFIX = 'Sterfte_v1'

 

# Clusterlijst

 

clusters = {1: 'Apeldoorn',

                  2: 'Arnhem',

                  3: 'Breda',

                  4: 'Bussum',

                  5: 'Den Haag',

                  6: 'Deventer_Almelo',

                  7: 'Eindhoven_Helmond',

                  8: 'Emmen',

                  9: 'Gouda',

                  10: 'Groningen',

                  11: 'Kerkrade',

                  12: 'Leiden',

                  13: 'Rotterdam',

                  14: 'Tilburg',

                  15: 'Utrecht_Zeist_Woerden',

                  16: 'Venlo',

                  17: 'Wageningen',

                  18: 'Winterswijk_Doetinchem',

                  19: 'Zeeland',

                  20: 'Zutphen_Lochem_Vorden',

                  21: 'Zwolle'

}

 

# real code 

last_id = 1

 

while last_id < 22:

               

 

               # set filters

               canvas = qgis.utils.iface.mapCanvas()

               acl = canvas.layer(0)

               qgis.utils.iface.mapCanvas().layer(0).selectAll()

               selection = '"Cluster" = '  + "\'" + str(clusters[last_id])+
"\'"

               acl.setSubsetString( selection )

 

               acl = canvas.layer(1)

               qgis.utils.iface.mapCanvas().layer(1).selectAll()

               selection = '"Cluster" = '  + "\'" + str(clusters[last_id])+
"\'" + " AND " '"Inwoners" > 15000'

               acl.setSubsetString( selection )

 

               acl = canvas.layer(2)

               selection = '"Cluster" = '  + "\'" + str(clusters[last_id])+
"\'" + " AND " '"Inwoners" > 15000'

               acl.setSubsetString( selection )

               

               acl = canvas.layer(0)

               qgis.utils.iface.mapCanvas().refresh()      

               qgis.utils.iface.setActiveLayer(acl)

               

               # Zoom in and out to refresh potential issues with OpenLayer

               canvas.zoomByFactor(3)

               canvas.zoomByFactor(0.5)

               qgis.utils.iface.zoomToActiveLayer()

 

               # specific code for cluster 10 as this is too large and only
main part needs to be focussed

 

               if last_id == 10 :

                              point = QgsPoint(698254,7.01707e+06)

                              geom = QgsGeometry.fromPoint(point)


                              center = geom.asPoint()

                              rect = QgsRectangle(center, center)


                              iface.mapCanvas().setExtent(rect)


                              iface.mapCanvas().zoomScale(750000)

 

 

               acl = canvas.layer(0)

               qgis.utils.iface.mapCanvas().refresh()

 

               # Save project

 

               p=QgsProject.instance()

               p.setFileName(PROJECTFILE)

               p.write()

 

                msgBox = QMessageBox()

               msgBox.setText("Map ready?")

               msgBox.exec_()

               

               p = str(last_id) + 'mapCluster_' 

               qgis.utils.iface.mapCanvas().saveAsImage(PROJECTPATH+ p +
SUFFIX + EXTSNAPSHOTS)

 

              last_id = last_id +1

 

 

Van: Joris Hintjens [mailto:jorishin at gmail.com] 
Verzonden: 04 November 2014 20:43
Aan: Frank van Baar
CC: Qgis-user at lists.osgeo.org
Onderwerp: Re: [Qgis-user] Python script to iterate through attributes and
then refresh the canvas - doesn't show any map....

 

hello frank, for the OSM problem, see thread: 

 "Printing with OSM layer"

form earlier today and the answers of Joost and Paulo to that. 

 

 

 

 

Op 4-nov.-2014, om 13:59 heeft Frank van Baar <frank at van-baar.com> het
volgende geschreven:





Hi Joris,

 

Thanks for your suggestion - didn't realize atlas worked this way. It
however does not completely do what I want to do - the layer which it
changes the filter for also has specific formatting to highlight the region
etc. AND the OSM layer doesn't always display right.

 

Any other suggestions anyone?


Frank

 

Van: Joris Hintjens [mailto:jorishin at gmail.com] 
Verzonden: 04 November 2014 11:27
Aan: Frank van Baar
CC: Qgis-user at lists.osgeo.org
Onderwerp: Re: [Qgis-user] Python script to iterate through attributes and
then refresh the canvas - doesn't show any map....

 

Why not use atlas generation in the print composer?

 Seems to me this corresponds with your "what i'm trying to achieve"

grts

Joris

 

 

Op 4-nov.-2014, om 09:14 heeft Frank van Baar < <mailto:frank at van-baar.com>
frank at van-baar.com> het volgende geschreven:






Hi,

 

Am completely stuck with a QGIS project in Python and tried everything but
nothing seems to work. The python script does work as such BUT the canvas
never shows the map and therefor the exported maps are empty..

 

What I'm trying to achieve:

.       Create a Python script that iterates through a series of ca. 2500
polygons based on attribute value, zoom to layer extend and save the canvas
for each attribute as an image file.

.       I then use the image files in other documents

 

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:

1.      Don't load the project each time and just change the filter

2.      Add sleep timers to give the canvas time to refresh etc.

3.      Tried different QGIS versions (2.2, 2.4 & 2.6 on Win 64x)

 

But all to no avail.. Hope somebody's got a great idea how to solve it.

 

Regards,

Frank

 

Python code:

 

# File location & specifications

PROJECTPATH = 'C:/Users/Frank/Documents /Targetting/Kaarten/'

PROJECTFILE = 'C:/Users/Frank/Documents
/Targetting/TargettingkaartV26qgs.qgs'

EXTSNAPSHOTS = '.jpg'

EXTSNAPSHOTS2 = '.png'

SUFFIX = '65Plus_v1'

last_id = 1

while last_id < 5:

        # Clear map canvas

        QgsMapLayerRegistry.instance().removeAllMapLayers()

        qgis.utils.iface.mapCanvas().refresh()

       

        # Open QGIS project

        QgsProject.instance().setFileName(PROJECTFILE)

        QgsProject.instance().read()

        qgis.utils.iface.mapCanvas().refresh()

        time.sleep(30) 

        canvas = qgis.utils.iface.mapCanvas()

        acl = canvas.layer(0)

        qgis.utils.iface.mapCanvas().layer(0).selectAll()

        selection = 'RijNo = ' + str(last_id)

       

        acl.setSubsetString( selection )

        acl.triggerRepaint

        canvas.layer(1).triggerRepaint 

        time.sleep(5)  

        qgis.utils.iface.setActiveLayer(acl)

        # Zoom in and out to refresh potential issues with OpenLayer

        canvas.zoomByFactor(3)

        canvas.zoomByFactor(0.5)

        qgis.utils.iface.zoomToActiveLayer()

        qgis.utils.iface.mapCanvas().refresh()

       

        time.sleep(10)

        # Save project

        p=QgsProject.instance()

        p.setFileName(PROJECTFILE)

        p.write()

        # Save content of map canvas as image

        p = str(last_id) + 'kaart_'

        qgis.utils.iface.mapCanvas().saveAsImage(PROJECTPATH+ p + SUFFIX +
EXTSNAPSHOTS)

        last_id = last_id +1

_______________________________________________
Qgis-user mailing list
 <mailto:Qgis-user at lists.osgeo.org> Qgis-user at lists.osgeo.org
 <http://lists.osgeo.org/mailman/listinfo/qgis-user>
http://lists.osgeo.org/mailman/listinfo/qgis-user

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20141105/4d7d9c00/attachment.html>


More information about the Qgis-user mailing list