[Qgis-developer] Is it possible to re-use print composer code to build a web service ?

Martin Dobias wonder.sk at gmail.com
Mon Dec 20 15:56:29 EST 2010


On Mon, Dec 20, 2010 at 4:54 PM, Alessandro Pasotti <apasotti at gmail.com> wrote:
>>
>> Looking at the code, I see that you create an instance of
>> QgsMapRenderer, but do not initialize. You should call its
>> setLayerSet() and setExtent() methods so that it knows what to render.
>
> Yes you're right, I did too much cut-n-paste, just added:
> lst = [ vlayer.getLayerID() ]
> mapRenderer.setLayerSet(lst)
> rect = core.QgsRectangle(mapRenderer.fullExtent())
> rect.scale(0.8)
> mapRenderer.setExtent(rect)
>
> and now the image shows the map correctly.... but it still segfaults.

Hmm, weird. When I add "del c" at the end of the script, it works
fine. Another solution you might try is to do this:

----------
def init():
  # init QApplication and QgsApplication
  return app

def do_stuff():
  # do rendering/printing here

app = init()
do_stuff()
----------

Like this, the composer related stuff will be initialized and
finalized properly within do_stuff() method and then the application
instance will be destroyed. Just remember that it's necessary to keep
the reference to application instance - if it gets out of scope it
will be destroyed and any rendering will end up with a complete
failure.


> Dou you think is it also possible (always from a standalone script) to load
> a map composer template and a qgis project from their XML files?

>From a brief look into the code (src/app/composer/qgscomposer.cpp -
QgsComposer::readXML) the loading code is bound to GUI, so it's not
possible to load them using a single api call. But if you look into
the code, you'll see that it basically just creates the composer items
and sets their properties using readXML method of each item. It should
be possible to mimic this with a dozen (or two) of lines in python.

Btw. I have updated the composer section in the cookbook - including a
sample code for pdf export. The changes will be visible tomorrow. In
the meanwhile you can use the plain source:
http://svn.osgeo.org/qgis/docs/trunk/english_us/developer_cookbook/source/composer.rst

Regards
Martin


More information about the Qgis-developer mailing list