|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +''' |
| 3 | +test_qgscomposermap.py |
| 4 | + -------------------------------------- |
| 5 | + Date : 31 Juli 2012 |
| 6 | + Copyright : (C) 2012 by Dr. Horst Düster / Dr. Marco Hugentobler |
| 7 | + email : horst.duester@sourcepole.ch |
| 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 TestQgsComposerMap(unittest.TestCase): |
| 28 | + |
| 29 | + def testCase(self): |
| 30 | + TEST_DATA_DIR = unitTestDataPath() |
| 31 | + rasterFileInfo = QFileInfo(TEST_DATA_DIR+QDir().separator().toAscii()+"landsat.tif") |
| 32 | + mRasterLayer = QgsRasterLayer(rasterFileInfo.filePath(), rasterFileInfo.completeBaseName()) |
| 33 | + rasterRenderer = QgsMultiBandColorRenderer( mRasterLayer.dataProvider(), 2, 3, 4 ) |
| 34 | + mRasterLayer.setRenderer( rasterRenderer ) |
| 35 | + QgsMapLayerRegistry.instance().addMapLayer( mRasterLayer ) |
| 36 | + |
| 37 | + # create composition with composer map |
| 38 | + mMapRenderer = QgsMapRenderer() |
| 39 | + layerStringList = QStringList() |
| 40 | + layerStringList.append( mRasterLayer.id() ) |
| 41 | + mMapRenderer.setLayerSet( layerStringList ) |
| 42 | + mMapRenderer.setProjectionsEnabled( False ) |
| 43 | + mComposition = QgsComposition( mMapRenderer ) |
| 44 | + mComposition.setPaperSize( 297, 210 ) |
| 45 | + mComposerMap = QgsComposerMap( mComposition, 20, 20, 200, 100 ) |
| 46 | + mComposition.addComposerMap( mComposerMap ) |
| 47 | + self.grid(mComposerMap, mComposition, TEST_DATA_DIR) |
| 48 | + self.overviewMap(mComposerMap, mComposition, TEST_DATA_DIR) |
| 49 | + |
| 50 | + def grid(self, mComposerMap, mComposition, TEST_DATA_DIR): |
| 51 | + mComposerMap.setNewExtent( QgsRectangle( 781662.375, 3339523.125, 793062.375, 3345223.125 ) ) |
| 52 | + mComposerMap.setGridEnabled( True ) |
| 53 | + mComposerMap.setGridIntervalX( 2000 ) |
| 54 | + mComposerMap.setGridIntervalY( 2000 ) |
| 55 | + mComposerMap.setShowGridAnnotation( True ) |
| 56 | + mComposerMap.setGridPenWidth( 0.5 ) |
| 57 | + mComposerMap.setGridAnnotationPrecision( 0 ) |
| 58 | + mComposerMap.setGridAnnotationPosition( QgsComposerMap.Disabled, QgsComposerMap.Left ) |
| 59 | + mComposerMap.setGridAnnotationPosition( QgsComposerMap.OutsideMapFrame, QgsComposerMap.Right ) |
| 60 | + mComposerMap.setGridAnnotationPosition( QgsComposerMap.Disabled, QgsComposerMap.Top ) |
| 61 | + mComposerMap.setGridAnnotationPosition( QgsComposerMap.OutsideMapFrame, QgsComposerMap.Bottom ) |
| 62 | + mComposerMap.setGridAnnotationDirection( QgsComposerMap.Horizontal, QgsComposerMap.Right ) |
| 63 | + mComposerMap.setGridAnnotationDirection( QgsComposerMap.Horizontal, QgsComposerMap.Bottom ) |
| 64 | + checker = QgsCompositionChecker() |
| 65 | + testResult = checker.testComposition( "Composer map grid", mComposition, TEST_DATA_DIR + QDir().separator().toAscii() + "control_images" + QDir().separator().toAscii() + "expected_composermap" + QDir().separator().toAscii() + "composermap_landsat_grid.png" ) |
| 66 | + mComposerMap.setGridEnabled( False ) |
| 67 | + mComposerMap.setShowGridAnnotation( False ) |
| 68 | + mTestName = "gaga" |
| 69 | +# myMessage = "<DartMeasurementFile name=\"Rendered Image " + mTestName + "\" type=\"image/png\">" + renderedFilePath + "</DartMeasurementFile> <DartMeasurementFile name=\"Expected Image " + mTestName + "\" type=\"image/png\">" + mExpectedImageFile + "</DartMeasurementFile> <DartMeasurementFile name=\"Difference Image " + mTestName + "\" type=\"image/png\">" + diffFilePath + "</DartMeasurementFile>" |
| 70 | + assert testResult == True #, myMessage |
| 71 | + |
| 72 | + def overviewMap(self, mComposerMap, mComposition, TEST_DATA_DIR): |
| 73 | + overviewMap = QgsComposerMap( mComposition, 20, 130, 70, 70 ) |
| 74 | + mComposition.addComposerMap( overviewMap ) |
| 75 | + # zoom in |
| 76 | + mComposerMap.setNewExtent( QgsRectangle( 785462.375, 3341423.125, 789262.375, 3343323.125 ) ) |
| 77 | + overviewMap.setNewExtent( QgsRectangle( 781662.375, 3339523.125, 793062.375, 3350923.125 ) ) |
| 78 | + overviewMap.setOverviewFrameMap( mComposerMap.id() ) |
| 79 | + checker = QgsCompositionChecker() |
| 80 | + testResult = checker.testComposition( "Composer map overview", mComposition, TEST_DATA_DIR + QDir().separator().toAscii() +"control_images" + QDir().separator().toAscii() + "expected_composermap" + QDir().separator().toAscii() + "composermap_landsat_overview.png" ) |
| 81 | + mComposition.removeComposerItem( overviewMap ) |
| 82 | + assert testResult == True |
| 83 | + |
| 84 | + |
| 85 | +# def uniqueId(self, mComposerMap, mComposition): |
| 86 | +# doc = QDomDocument() |
| 87 | +# documentElement = doc.createElement( "ComposerItemClipboard" ) |
| 88 | +# mComposerMap.writeXML( documentElement, doc ) |
| 89 | +# mComposition.addItemsFromXML( documentElement, doc, 0, false ) |
| 90 | +# |
| 91 | +# #test if both composer maps have different ids |
| 92 | +# newMap = QgsComposerMap() |
| 93 | +# mapList = mComposition.composerMapItems() |
| 94 | +# |
| 95 | +# for mapIt in mapList: |
| 96 | +# if mapIt != mComposerMap: |
| 97 | +# newMap = mapIt |
| 98 | +# break |
| 99 | +# |
| 100 | +# oldId = mComposerMap.id() |
| 101 | +# newId = newMap.id() |
| 102 | +# |
| 103 | +# mComposition.removeComposerItem( newMap ); |
| 104 | +# print "old: "+str(oldId) |
| 105 | +# print "new "+str(newId) |
| 106 | +# assert oldId != newId |
| 107 | + |
| 108 | +if __name__ == '__main__': |
| 109 | + unittest.main() |
| 110 | + |
0 commit comments