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: