Skip to content

Commit d904da4

Browse files
author
Hugo Mercier
committedOct 5, 2012
Update of QgsAtlasComposition SIP (with Python unit tests as well)
1 parent e4371d8 commit d904da4

File tree

6 files changed

+133
-125
lines changed

6 files changed

+133
-125
lines changed
 
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/** \ingroup MapComposer
2+
* Class used to render an Atlas, iterating over geometry features.
3+
* prepareForFeature() modifies the atlas map's extent to zoom on the given feature.
4+
* This class is used for printing, exporting to PDF and images.
5+
* */
6+
class QgsAtlasComposition : public QObject
7+
{
8+
%TypeHeaderCode
9+
#include <qgsatlascomposition.h>
10+
%End
11+
12+
public:
13+
QgsAtlasComposition( QgsComposition* composition );
14+
~QgsAtlasComposition();
15+
16+
/** Is the atlas generation enabled ? */
17+
bool enabled() const;
18+
void setEnabled( bool e );
19+
20+
QgsComposerMap* composerMap() const;
21+
void setComposerMap( QgsComposerMap* map );
22+
23+
bool hideCoverage() const;
24+
void setHideCoverage( bool hide );
25+
26+
bool fixedScale() const;
27+
void setFixedScale( bool fixed );
28+
29+
float margin() const;
30+
void setMargin( float margin );
31+
32+
QString filenamePattern() const;
33+
void setFilenamePattern( const QString& pattern );
34+
35+
QgsVectorLayer* coverageLayer() const;
36+
void setCoverageLayer( QgsVectorLayer* lmap );
37+
38+
bool singleFile() const;
39+
void setSingleFile( bool single );
40+
41+
/** Begins the rendering. */
42+
void beginRender();
43+
/** Ends the rendering. Restores original extent */
44+
void endRender();
45+
46+
/** Returns the number of features in the coverage layer */
47+
size_t numFeatures() const;
48+
49+
/** Prepare the atlas map for the given feature. Sets the extent and context variables */
50+
void prepareForFeature( size_t i );
51+
52+
/** Returns the current filename. Must be called after prepareForFeature( i ) */
53+
const QString& currentFilename() const;
54+
55+
void writeXML( QDomElement& elem, QDomDocument& doc ) const;
56+
void readXML( const QDomElement& elem, const QDomDocument& doc );
57+
58+
QgsComposition* composition();
59+
60+
signals:
61+
/** emitted when one of the parameters changes */
62+
void parameterChanged();
63+
};

‎python/core/composer/qgscomposermap.sip

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -285,29 +285,9 @@ class QgsComposerMap : QgsComposerItem
285285
Usually, this function is called before adding the composer map to the composition*/
286286
void assignFreeId();
287287

288-
bool atlasHideCoverage() const;
289-
void setAtlasHideCoverage( bool hide );
290-
291-
bool atlasFixedScale() const;
292-
void setAtlasFixedScale( bool fixed );
293-
294-
float atlasMargin() const;
295-
void setAtlasMargin( float margin );
296-
297-
QString atlasFilenamePattern() const;
298-
void setAtlasFilenamePattern( const QString& pattern );
299-
300-
QgsVectorLayer* atlasCoverageLayer() const;
301-
void setAtlasCoverageLayer( QgsVectorLayer* lmap );
302-
303-
bool atlasSingleFile() const;
304-
void setAtlasSingleFile( bool single );
305-
306288
signals:
307289
void extentChanged();
308290

309-
void atlasCoverageLayerChanged( QgsVectorLayer* );
310-
311291
public slots:
312292

313293
/**Called if map canvas has changed*/

‎python/core/composer/qgscomposition.sip

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,3 @@
1-
/** \ingroup MapComposer
2-
* Class used to render an Atlas, iterating over geometry features.
3-
* prepareForFeature() modifies the atlas map's extent to zoom on the given feature.
4-
* This class is used for printing, exporting to PDF and images.
5-
* */
6-
class QgsAtlasRendering
7-
{
8-
public:
9-
QgsAtlasRendering( QgsComposition* composition );
10-
~QgsAtlasRendering();
11-
12-
/** Begins the rendering. Sets an optional output filename pattern */
13-
void begin( const QString& filenamePattern = "" );
14-
/** Ends the rendering. Restores original extent*/
15-
void end();
16-
17-
/** Returns the number of features in the coverage layer */
18-
size_t numFeatures() const;
19-
20-
/** Prepare the atlas map for the given feature. Sets the extent and context variables */
21-
void prepareForFeature( size_t i );
22-
23-
/** Returns the current filename. Must be called after prepareForFeature( i ) */
24-
const QString& currentFilename() const;
25-
};
26-
271
/** \ingroup MapComposer
282
* Graphics scene for map printing. The class manages the paper item which always
293
* is the item in the back (z-value 0). It maintains the z-Values of the items and stores
@@ -145,9 +119,6 @@ class QgsComposition : QGraphicsScene
145119
/**Returns pointer to map renderer of qgis map canvas*/
146120
QgsMapRenderer* mapRenderer();
147121

148-
QgsComposerMap* atlasMap();
149-
void setAtlasMap( QgsComposerMap* map );
150-
151122
QgsComposition::PlotStyle plotStyle() const;
152123
void setPlotStyle( QgsComposition::PlotStyle style );
153124

‎python/core/core.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
%Include composer/qgscomposershape.sip
9999
%Include composer/qgscomposertable.sip
100100
%Include composer/qgscomposition.sip
101+
%Include composer/qgsatlascomposition.sip
101102
%Include composer/qgsdoubleboxscalebarstyle.sip
102103
%Include composer/qgslegendmodel.sip
103104
%Include composer/qgsnumericscalebarstyle.sip

‎tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ ADD_PYTHON_TEST(PyQgsComposition test_qgscomposition.py)
1414
ADD_PYTHON_TEST(PyQgsAnalysis test_qgsanalysis.py)
1515
#ADD_PYTHON_TEST(PyQgsComposerMap test_qgscomposermap.py)
1616
ADD_PYTHON_TEST(PyQgsSymbolLayerV2 test_qgssymbollayerv2.py)
17-
ADD_PYTHON_TEST(PyQgsComposerMapAtlas test_qgscomposermapatlas.py)
17+
ADD_PYTHON_TEST(PyQgsAtlasComposition test_qgsatlascomposition.py)
1818
ADD_PYTHON_TEST(PyQgsComposerLabel test_qgscomposerlabel.py)

‎tests/src/python/test_qgscomposermapatlas.py renamed to ‎tests/src/python/test_qgsatlascomposition.py

Lines changed: 68 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
'''
3-
test_qgscomposermapatlas.py
3+
test_qgsatlascomposition.py
44
--------------------------------------
55
Date : Oct 2012
66
Copyright : (C) 2012 by Dr. Hugo Mercier
@@ -24,11 +24,11 @@
2424

2525
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
2626

27-
class TestQgsComposerMapAtlas(unittest.TestCase):
27+
class TestQgsAtlasComposition(unittest.TestCase):
2828

2929
def testCase(self):
30-
TEST_DATA_DIR = unitTestDataPath()
31-
vectorFileInfo = QFileInfo( TEST_DATA_DIR + QDir().separator().toAscii() + "france_parts.shp")
30+
self.TEST_DATA_DIR = unitTestDataPath()
31+
vectorFileInfo = QFileInfo( self.TEST_DATA_DIR + QDir().separator().toAscii() + "france_parts.shp")
3232
mVectorLayer = QgsVectorLayer( vectorFileInfo.filePath(), vectorFileInfo.completeBaseName(), "ogr" )
3333

3434
QgsMapLayerRegistry.instance().addMapLayer( mVectorLayer )
@@ -45,8 +45,8 @@ def testCase(self):
4545
crs.createFromSrid( 2154 )
4646
mMapRenderer.setDestinationCrs( crs )
4747

48-
mComposition = QgsComposition( mMapRenderer )
49-
mComposition.setPaperSize( 297, 210 )
48+
self.mComposition = QgsComposition( mMapRenderer )
49+
self.mComposition.setPaperSize( 297, 210 )
5050

5151
# fix the renderer, fill with green
5252
props = { "color": "0,127,0" }
@@ -55,121 +55,114 @@ def testCase(self):
5555
mVectorLayer.setRendererV2( renderer )
5656

5757
# the atlas map
58-
mAtlasMap = QgsComposerMap( mComposition, 20, 20, 130, 130 )
59-
mAtlasMap.setFrameEnabled( True )
60-
mAtlasMap.setAtlasCoverageLayer( mVectorLayer )
61-
mComposition.addComposerMap( mAtlasMap )
62-
mComposition.setAtlasMap( mAtlasMap )
58+
self.mAtlasMap = QgsComposerMap( self.mComposition, 20, 20, 130, 130 )
59+
self.mAtlasMap.setFrameEnabled( True )
60+
self.mComposition.addComposerMap( self.mAtlasMap )
61+
62+
# the atlas
63+
self.mAtlas = QgsAtlasComposition( self.mComposition )
64+
self.mAtlas.setCoverageLayer( mVectorLayer )
65+
self.mAtlas.setComposerMap( self.mAtlasMap )
6366

6467
# an overview
65-
mOverview = QgsComposerMap( mComposition, 180, 20, 50, 50 )
68+
mOverview = QgsComposerMap( self.mComposition, 180, 20, 50, 50 )
6669
mOverview.setFrameEnabled( True )
67-
mOverview.setOverviewFrameMap( mAtlasMap.id() )
68-
mComposition.addComposerMap( mOverview )
70+
mOverview.setOverviewFrameMap( self.mAtlasMap.id() )
71+
self.mComposition.addComposerMap( mOverview )
6972
nextent = QgsRectangle( 49670.718, 6415139.086, 699672.519, 7065140.887 )
7073
mOverview.setNewExtent( nextent )
7174

7275
# set the fill symbol of the overview map
7376
props2 = { "color": "127,0,0,127" }
7477
fillSymbol2 = QgsFillSymbolV2.createSimple( props2 )
7578
mOverview.setOverviewFrameMapSymbol( fillSymbol2 );
76-
7779

7880
# header label
79-
mLabel1 = QgsComposerLabel( mComposition )
80-
mComposition.addComposerLabel( mLabel1 )
81-
mLabel1.setText( "[% \"NAME_1\" %] area" )
82-
mLabel1.adjustSizeToText()
83-
mLabel1.setItemPosition( 150, 5 )
81+
self.mLabel1 = QgsComposerLabel( self.mComposition )
82+
self.mComposition.addComposerLabel( self.mLabel1 )
83+
self.mLabel1.setText( "[% \"NAME_1\" %] area" )
84+
self.mLabel1.adjustSizeToText()
85+
self.mLabel1.setItemPosition( 150, 5 )
8486

8587
# feature number label
86-
mLabel2 = QgsComposerLabel( mComposition )
87-
mComposition.addComposerLabel( mLabel2 )
88-
mLabel2.setText( "# [%$feature || ' / ' || $numfeatures%]" )
89-
mLabel2.adjustSizeToText()
90-
mLabel2.setItemPosition( 150, 200 )
91-
92-
self.filename_test( mComposition )
93-
self.autoscale_render_test( mComposition, mLabel1, TEST_DATA_DIR )
94-
self.fixedscale_render_test( mComposition, mLabel1, TEST_DATA_DIR )
95-
self.hidden_render_test( mComposition, mLabel1, TEST_DATA_DIR )
88+
self.mLabel2 = QgsComposerLabel( self.mComposition )
89+
self.mComposition.addComposerLabel( self.mLabel2 )
90+
self.mLabel2.setText( "# [%$feature || ' / ' || $numfeatures%]" )
91+
self.mLabel2.adjustSizeToText()
92+
self.mLabel2.setItemPosition( 150, 200 )
93+
94+
self.filename_test()
95+
self.autoscale_render_test()
96+
self.fixedscale_render_test()
97+
self.hidden_render_test()
9698

97-
def filename_test( self, mComposition ):
98-
atlasRender = QgsAtlasRendering( mComposition )
99-
atlasRender.begin( "'output_' || $feature" )
100-
for i in range(0, atlasRender.numFeatures()):
101-
atlasRender.prepareForFeature( i )
102-
expected = QString( "output_%1" ).arg(i+1)
103-
print atlasRender.currentFilename()
104-
assert atlasRender.currentFilename() == expected
105-
atlasRender.end()
99+
def filename_test( self ):
106100

107-
def autoscale_render_test( self, mComposition, mLabel1, TEST_DATA_DIR ):
108-
atlasMap = mComposition.atlasMap()
109-
atlasMap.setAtlasFixedScale( False )
110-
atlasMap.setAtlasMargin( 0.10 )
101+
self.mAtlas.setFilenamePattern( "'output_' || $feature" )
102+
self.mAtlas.beginRender()
103+
for i in range(0, self.mAtlas.numFeatures()):
104+
self.mAtlas.prepareForFeature( i )
105+
expected = QString( "output_%1" ).arg(i+1)
106+
assert self.mAtlas.currentFilename() == expected
107+
self.mAtlas.endRender()
111108

112-
atlasRender = QgsAtlasRendering( mComposition )
109+
def autoscale_render_test( self ):
110+
self.mAtlas.setFixedScale( False )
111+
self.mAtlas.setMargin( 0.10 )
113112

114-
atlasRender.begin()
113+
self.mAtlas.beginRender()
115114

116115
for i in range(0, 2):
117-
atlasRender.prepareForFeature( i )
118-
mLabel1.adjustSizeToText()
116+
self.mAtlas.prepareForFeature( i )
117+
self.mLabel1.adjustSizeToText()
119118

120119
checker = QgsCompositionChecker()
121-
res = checker.testComposition( "Atlas autoscale test", mComposition, \
122-
QString( TEST_DATA_DIR ) + QDir.separator() + \
120+
res = checker.testComposition( "Atlas autoscale test", self.mComposition, \
121+
QString( self.TEST_DATA_DIR ) + QDir.separator() + \
123122
"control_images" + QDir.separator() + \
124123
"expected_composermapatlas" + QDir.separator() + \
125124
QString( "autoscale_%1.png" ).arg( i ) )
126125
assert res[0] == True
127-
atlasRender.end()
126+
self.mAtlas.endRender()
128127

129-
def fixedscale_render_test( self, mComposition, mLabel1, TEST_DATA_DIR ):
130-
atlasMap = mComposition.atlasMap()
131-
atlasMap.setNewExtent( QgsRectangle( 209838.166, 6528781.020, 610491.166, 6920530.620 ) );
132-
atlasMap.setAtlasFixedScale( True )
133-
134-
atlasRender = QgsAtlasRendering( mComposition )
128+
def fixedscale_render_test( self ):
129+
self.mAtlasMap.setNewExtent( QgsRectangle( 209838.166, 6528781.020, 610491.166, 6920530.620 ) );
130+
self.mAtlas.setFixedScale( True )
135131

136-
atlasRender.begin()
132+
self.mAtlas.beginRender()
137133

138134
for i in range(0, 2):
139-
atlasRender.prepareForFeature( i )
140-
mLabel1.adjustSizeToText()
135+
self.mAtlas.prepareForFeature( i )
136+
self.mLabel1.adjustSizeToText()
141137

142138
checker = QgsCompositionChecker()
143-
res = checker.testComposition( "Atlas fixed scale test", mComposition, \
144-
QString( TEST_DATA_DIR ) + QDir.separator() + \
139+
res = checker.testComposition( "Atlas fixed scale test", self.mComposition, \
140+
QString( self.TEST_DATA_DIR ) + QDir.separator() + \
145141
"control_images" + QDir.separator() + \
146142
"expected_composermapatlas" + QDir.separator() + \
147143
QString( "fixedscale_%1.png" ).arg( i ) )
148144
assert res[0] == True
149-
atlasRender.end()
150-
151-
def hidden_render_test( self, mComposition, mLabel1, TEST_DATA_DIR ):
152-
atlasMap = mComposition.atlasMap()
153-
atlasMap.setNewExtent( QgsRectangle( 209838.166, 6528781.020, 610491.166, 6920530.620 ) );
154-
atlasMap.setAtlasFixedScale( True )
155-
atlasMap.setAtlasHideCoverage( True )
145+
self.mAtlas.endRender()
156146

157-
atlasRender = QgsAtlasRendering( mComposition )
147+
def hidden_render_test( self ):
148+
self.mAtlasMap.setNewExtent( QgsRectangle( 209838.166, 6528781.020, 610491.166, 6920530.620 ) );
149+
self.mAtlas.setFixedScale( True )
150+
self.mAtlas.setHideCoverage( True )
158151

159-
atlasRender.begin()
152+
self.mAtlas.beginRender()
160153

161154
for i in range(0, 2):
162-
atlasRender.prepareForFeature( i )
163-
mLabel1.adjustSizeToText()
155+
self.mAtlas.prepareForFeature( i )
156+
self.mLabel1.adjustSizeToText()
164157

165158
checker = QgsCompositionChecker()
166-
res = checker.testComposition( "Atlas hidden test", mComposition, \
167-
QString( TEST_DATA_DIR ) + QDir.separator() + \
159+
res = checker.testComposition( "Atlas hidden test", self.mComposition, \
160+
QString( self.TEST_DATA_DIR ) + QDir.separator() + \
168161
"control_images" + QDir.separator() + \
169162
"expected_composermapatlas" + QDir.separator() + \
170163
QString( "hiding_%1.png" ).arg( i ) )
171164
assert res[0] == True
172-
atlasRender.end()
165+
self.mAtlas.endRender()
173166

174167
if __name__ == '__main__':
175168
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.