|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +''' |
| 3 | +test_qgscomposermapatlas.py |
| 4 | + -------------------------------------- |
| 5 | + Date : Oct 2012 |
| 6 | + Copyright : (C) 2012 by Dr. Hugo Mercier |
| 7 | + email : hugo dot mercier at oslandia dot com |
| 8 | + *************************************************************************** |
| 9 | + * * |
| 10 | + * This program is free software; you can redistribute it and/or modify * |
| 11 | + * it under the terms of the GNU General Public License as published by * |
| 12 | + * the Free Software Foundation; either version 2 of the License, or * |
| 13 | + * (at your option) any later version. * |
| 14 | + * * |
| 15 | + ***************************************************************************/ |
| 16 | +''' |
| 17 | +import unittest |
| 18 | +from utilities import * |
| 19 | +from PyQt4.QtCore import * |
| 20 | +from PyQt4.QtGui import * |
| 21 | +from PyQt4.QtXml import * |
| 22 | +from qgis.core import * |
| 23 | +from qgscompositionchecker import QgsCompositionChecker |
| 24 | + |
| 25 | +QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() |
| 26 | + |
| 27 | +class TestQgsComposerMapAtlas(unittest.TestCase): |
| 28 | + |
| 29 | + def testCase(self): |
| 30 | + TEST_DATA_DIR = unitTestDataPath() |
| 31 | + vectorFileInfo = QFileInfo( TEST_DATA_DIR + QDir().separator().toAscii() + "france_parts.shp") |
| 32 | + mVectorLayer = QgsVectorLayer( vectorFileInfo.filePath(), vectorFileInfo.completeBaseName(), "ogr" ) |
| 33 | + |
| 34 | + QgsMapLayerRegistry.instance().addMapLayer( mVectorLayer ) |
| 35 | + |
| 36 | + # create composition with composer map |
| 37 | + mMapRenderer = QgsMapRenderer() |
| 38 | + layerStringList = QStringList() |
| 39 | + layerStringList.append( mVectorLayer.id() ) |
| 40 | + mMapRenderer.setLayerSet( layerStringList ) |
| 41 | + mMapRenderer.setProjectionsEnabled( True ) |
| 42 | + |
| 43 | + # select epsg:2154 |
| 44 | + crs = QgsCoordinateReferenceSystem() |
| 45 | + crs.createFromSrid( 2154 ) |
| 46 | + mMapRenderer.setDestinationCrs( crs ) |
| 47 | + |
| 48 | + mComposition = QgsComposition( mMapRenderer ) |
| 49 | + mComposition.setPaperSize( 297, 210 ) |
| 50 | + |
| 51 | + # fix the renderer, fill with green |
| 52 | + props = { "color": "0,127,0" } |
| 53 | + fillSymbol = QgsFillSymbolV2.createSimple( props ) |
| 54 | + renderer = QgsSingleSymbolRendererV2( fillSymbol ) |
| 55 | + mVectorLayer.setRendererV2( renderer ) |
| 56 | + |
| 57 | + # 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 ) |
| 63 | + |
| 64 | + # an overview |
| 65 | + mOverview = QgsComposerMap( mComposition, 180, 20, 50, 50 ) |
| 66 | + mOverview.setFrameEnabled( True ) |
| 67 | + mOverview.setOverviewFrameMap( mAtlasMap.id() ) |
| 68 | + mComposition.addComposerMap( mOverview ) |
| 69 | + nextent = QgsRectangle( 49670.718, 6415139.086, 699672.519, 7065140.887 ) |
| 70 | + mOverview.setNewExtent( nextent ) |
| 71 | + |
| 72 | + # header label |
| 73 | + mLabel1 = QgsComposerLabel( mComposition ) |
| 74 | + mComposition.addComposerLabel( mLabel1 ) |
| 75 | + mLabel1.setText( "[% \"NAME_1\" %] area" ) |
| 76 | + mLabel1.adjustSizeToText() |
| 77 | + mLabel1.setItemPosition( 150, 5 ) |
| 78 | + |
| 79 | + # feature number label |
| 80 | + mLabel2 = QgsComposerLabel( mComposition ) |
| 81 | + mComposition.addComposerLabel( mLabel2 ) |
| 82 | + mLabel2.setText( "# [%$feature || ' / ' || $numfeatures%]" ) |
| 83 | + mLabel2.adjustSizeToText() |
| 84 | + mLabel2.setItemPosition( 150, 200 ) |
| 85 | + |
| 86 | + self.filename_test( mComposition ) |
| 87 | + self.autoscale_render_test( mComposition, mLabel1, TEST_DATA_DIR ) |
| 88 | + self.fixedscale_render_test( mComposition, mLabel1, TEST_DATA_DIR ) |
| 89 | + self.hidden_render_test( mComposition, mLabel1, TEST_DATA_DIR ) |
| 90 | + |
| 91 | + def filename_test( self, mComposition ): |
| 92 | + atlasRender = QgsAtlasRendering( mComposition ) |
| 93 | + atlasRender.begin( "'output_' || $feature" ) |
| 94 | + for i in range(0, atlasRender.numFeatures()): |
| 95 | + atlasRender.prepareForFeature( i ) |
| 96 | + expected = QString( "output_%1" ).arg(i+1) |
| 97 | + print atlasRender.currentFilename() |
| 98 | + assert atlasRender.currentFilename() == expected |
| 99 | + atlasRender.end() |
| 100 | + |
| 101 | + def autoscale_render_test( self, mComposition, mLabel1, TEST_DATA_DIR ): |
| 102 | + atlasMap = mComposition.atlasMap() |
| 103 | + atlasMap.setAtlasFixedScale( False ) |
| 104 | + atlasMap.setAtlasMargin( 0.10 ) |
| 105 | + |
| 106 | + atlasRender = QgsAtlasRendering( mComposition ) |
| 107 | + |
| 108 | + atlasRender.begin() |
| 109 | + |
| 110 | + for i in range(0, 2): |
| 111 | + atlasRender.prepareForFeature( i ) |
| 112 | + mLabel1.adjustSizeToText() |
| 113 | + |
| 114 | + checker = QgsCompositionChecker() |
| 115 | + res = checker.testComposition( "Atlas autoscale test", mComposition, \ |
| 116 | + QString( TEST_DATA_DIR ) + QDir.separator() + \ |
| 117 | + "control_images" + QDir.separator() + \ |
| 118 | + "expected_composermapatlas" + QDir.separator() + \ |
| 119 | + QString( "autoscale_%1.png" ).arg( i ) ) |
| 120 | + assert res[0] == True |
| 121 | + atlasRender.end() |
| 122 | + |
| 123 | + def fixedscale_render_test( self, mComposition, mLabel1, TEST_DATA_DIR ): |
| 124 | + atlasMap = mComposition.atlasMap() |
| 125 | + atlasMap.setNewExtent( QgsRectangle( 209838.166, 6528781.020, 610491.166, 6920530.620 ) ); |
| 126 | + atlasMap.setAtlasFixedScale( True ) |
| 127 | + |
| 128 | + atlasRender = QgsAtlasRendering( mComposition ) |
| 129 | + |
| 130 | + atlasRender.begin() |
| 131 | + |
| 132 | + for i in range(0, 2): |
| 133 | + atlasRender.prepareForFeature( i ) |
| 134 | + mLabel1.adjustSizeToText() |
| 135 | + |
| 136 | + checker = QgsCompositionChecker() |
| 137 | + res = checker.testComposition( "Atlas fixed scale test", mComposition, \ |
| 138 | + QString( TEST_DATA_DIR ) + QDir.separator() + \ |
| 139 | + "control_images" + QDir.separator() + \ |
| 140 | + "expected_composermapatlas" + QDir.separator() + \ |
| 141 | + QString( "fixedscale_%1.png" ).arg( i ) ) |
| 142 | + assert res[0] == True |
| 143 | + atlasRender.end() |
| 144 | + |
| 145 | + def hidden_render_test( self, mComposition, mLabel1, TEST_DATA_DIR ): |
| 146 | + atlasMap = mComposition.atlasMap() |
| 147 | + atlasMap.setNewExtent( QgsRectangle( 209838.166, 6528781.020, 610491.166, 6920530.620 ) ); |
| 148 | + atlasMap.setAtlasFixedScale( True ) |
| 149 | + atlasMap.setAtlasHideCoverage( True ) |
| 150 | + |
| 151 | + atlasRender = QgsAtlasRendering( mComposition ) |
| 152 | + |
| 153 | + atlasRender.begin() |
| 154 | + |
| 155 | + for i in range(0, 2): |
| 156 | + atlasRender.prepareForFeature( i ) |
| 157 | + mLabel1.adjustSizeToText() |
| 158 | + |
| 159 | + checker = QgsCompositionChecker() |
| 160 | + res = checker.testComposition( "Atlas hidden test", mComposition, \ |
| 161 | + QString( TEST_DATA_DIR ) + QDir.separator() + \ |
| 162 | + "control_images" + QDir.separator() + \ |
| 163 | + "expected_composermapatlas" + QDir.separator() + \ |
| 164 | + QString( "hiding_%1.png" ).arg( i ) ) |
| 165 | + assert res[0] == True |
| 166 | + atlasRender.end() |
| 167 | + |
| 168 | +if __name__ == '__main__': |
| 169 | + unittest.main() |
| 170 | + |
0 commit comments