Save a Print Composer from the command line?

395 views
Skip to first unread message

Nyall Dawson

unread,
Oct 21, 2012, 10:47:46 PM10/21/12
to australian-qg...@googlegroups.com
Does anyone know of a way to save a print composer from the qgis command line? I'm trying to schedule some automated map production, and while I've read up on the snapshot command line parameter, this doesn't appear to work with composers. 

The situation is that I've got a composer containing a number of maps (predictions at various hours throughout the day), and I'd like to automate the production of this map so that at a given time of day a pdf of the composer is generated. Any ideas how I could do this in QGIS or with any other open source tools?

Nyall

Nathan Woodrow

unread,
Oct 22, 2012, 10:34:35 PM10/22/12
to australian-qg...@googlegroups.com
Hey Nyall,

Not from the command line as far as I'm aware.  You would have to do a little Python scripting using the QGIS libs.  I have some code at work that I think I got working one day.  Once I get back to work I will post it.

There is some work being done as part of some world bank funded project that is meant to be improving QGIS and the composer for doing this exact thing.  No ETA yet that I know of, but I will check.

- Nathan

Nyall Dawson

unread,
Oct 22, 2012, 11:14:50 PM10/22/12
to australian-qg...@googlegroups.com

Not from the command line as far as I'm aware.  You would have to do a little Python scripting using the QGIS libs.  I have some code at work that I think I got working one day.  Once I get back to work I will post it.

That'd be great -- at least if you've got something I can start from it will save a lot of work. I did come across an old thread from yourself on the qgis list yesterday in which you asked a nearly identical question, so I was curious to know if you ever got a working solution going...

Nyall

Nathan Woodrow

unread,
Nov 7, 2012, 2:00:00 AM11/7/12
to australian-qg...@googlegroups.com
Hey Nyall,

Managed to come up with something that works quite well.  The code and some sample data is attached in the zip file. The code can also be found at https://gist.github.com/4029400

The basic idea with this is that you load the project file creating a new vector layer instance for each one in the project. You then give these layers to a map renderer and give it to the composition object.  In the code I'm only handling creating vector layers but you just have to check what the layer type is and create a new instance of that type.  The magic is in the readXML method which takes the layers xml from the project file and reconstructs it, e.g. adds name, datasource, style, etc. This could be done better by QGIS so I'll look into cleaning that up at some stage.

We then have a QGIS composer template file (.qpt) - which you can save out using a composer window under the file menu - which we give to the loadFromTemplate function, finally calling exportAsPDF to render the composer out to a file.  You don't have to use a template file and just get the composer xml section from inside the project, but I think the template method is a little cleaner and portable.

The loadFromTemplate function is also pretty cool as you can pass in a dictionary of key: value to do runtime text replacement. 

You can just run this in a OSGeo4W shell once you have set the current variables:

Set PATH=C:\OSGeo4W\apps\qgis-dev\bin;%PATH%
set PYTHONPATH=C:\OSGeo4W\apps\qgis-dev\python
python composer.py

You will need one of the nightly builds of QGIS as I am using the new loadFromTemplate and exportAsPDF functions.  It can be done without those and run on 1.8 but just needs some extra work.

Hope this helps to get you started.

I might do a blog post on it at some stage too.

Regards,
Nathan

On Monday, 22 October 2012 12:47:47 UTC+10, Nyall Dawson wrote:
composer.zip
composer_before.pdf
composer_output.pdf

Andy Tice

unread,
Nov 7, 2012, 7:44:11 PM11/7/12
to australian-qg...@googlegroups.com
Hi,
This is a work around I've used in the past - although it generates .jpgs rather then pdf, so depending on the quality of the line work it might not be valid. But here goes.

Using this screen capture software:

Set the screen you've got the composition on to it's highest possible resolution - this helps achieve a good dpi. Specify the image capture extent (in the screen capture) and time periods.


Cheers,
Andy

--
You received this message because you are subscribed to the Google Groups "Australian QGIS User Group" group.
To post to this group, send an email to australian-qg...@googlegroups.com.
Visit this group at http://groups.google.com/group/australian-qgis-user-group?hl=en-GB.
 
 

Reply all
Reply to author
Forward
0 new messages