[Qgis-developer] qgsAtlasComposition

Stott James fmrojst at fylkesmannen.no
Thu Apr 11 02:20:47 PDT 2013


Thanks for the reply.

I am using QGIS master from OSGeo4W. At the moment I am just trying to get the example in the test to work. But it always falls over at beginRender(). My vector layer is a query from a PostGIS database.

Code is below (as you can see it is pretty much the same as the test code):

           query = """(SELECT * From test.tablename WHERE substring(knrgnrbnr from 1 for 4) = '""" + KommuneNr + """')"""
            
            vlayer = loadPostGISLayerQuery("localhost", "5432", "database", "user", "password",query,"geom","gid","layername")

            QgsMapLayerRegistry.instance().addMapLayer(vlayer)
            mMapRenderer = QgsMapRenderer()
            layerStringList = QStringList()
            layerStringList.append( vlayer.id() )
            mMapRenderer.setLayerSet( layerStringList )
            mMapRenderer.setProjectionsEnabled( True )
            crs = QgsCoordinateReferenceSystem()
            crs.createFromSrid( 32632 )
            mMapRenderer.setDestinationCrs( crs )

            mComposition = QgsComposition( mMapRenderer )
            mComposition.setPaperSize( 297, 210 )
            
            props = { "color": "0,127,0" }
            fillSymbol = QgsFillSymbolV2.createSimple( props )
            renderer = QgsSingleSymbolRendererV2( fillSymbol )
            vlayer.setRendererV2( renderer )

            mAtlasMap = QgsComposerMap( mComposition, 20, 20, 130, 130 )
            mAtlasMap.setFrameEnabled( True )
            mComposition.addComposerMap( mAtlasMap )
            
            mAtlas = QgsAtlasComposition( mComposition )
            mAtlas.setCoverageLayer( vlayer )
            mAtlas.setComposerMap( mAtlasMap )

            mAtlas.setFilenamePattern( "'output_' || $feature" )
            mAtlas.beginRender()
            for i in range(0, mAtlas.numFeatures()):
                mAtlas.prepareForFeature( i )
                expected = QString( "output_%1" ).arg(i+1)
                assert mAtlas.currentFilename() == expected
                
            mAtlas.endRender()

-----Opprinnelig melding-----
Fra: qgis-developer-bounces at lists.osgeo.org [mailto:qgis-developer-bounces at lists.osgeo.org] På vegne av Hugo Mercier
Sendt: 11. april 2013 10:16
Til: qgis-developer at lists.osgeo.org
Emne: Re: [Qgis-developer] qgsAtlasComposition

Hi James,

Le 11/04/2013 08:37, Stott James a écrit :
> Hi all,
> 
> I am trying to use the atlas functionality in a standalone python script. I am using the qgsAtlasComposition class. I get an error running this line:
> 
> myAtlas.beginRender()
> 
> I get an unknown exception.

Can you share the code you're using ? Or a stack trace or a line number where the error occurs ?
What version / git revision are you using ?

You have to call setCoverageLayer and setComposerMap before calling beginRender(), is it the case ?

> 
> I have also found that running myAtlas.numFeatures() always returns 0 even though I have added a layer that has data using setCoverageLayer().

This is linked to the previous issue. Features are iterated over during
beginRender()

> 
> I did take a look at this page and followed the steps in the test:
> 
> https://github.com/qgis/Quantum-GIS/blob/master/tests/src/python/test_
> qgsatlascomposition.py
> 
> but I cannot get it to work in my script. I cannot get the test to work either. It always errors on the beginRender() line.

I was about to say everything work fine here, but ... I realized there is something wrong with the last changes I did.

Should be fixed with this:
https://github.com/qgis/Quantum-GIS/pull/516

Not sure if it will fix your problem ...


--
Hugo Mercier
Oslandia
_______________________________________________
Qgis-developer mailing list
Qgis-developer at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer


More information about the Qgis-developer mailing list