def testInteriorTicks(self): layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) map.setFrameEnabled(True) map.setBackgroundColor(QColor(150, 100, 100)) layout.addLayoutItem(map) map.grid().setFrameStyle(QgsLayoutItemMapGrid.Zebra) myRectangle = QgsRectangle(781662.375, 3339523.125, 793062.375, 3345223.125) map.setExtent(myRectangle) map.grid().setIntervalX(2000) map.grid().setIntervalY(2000) map.grid().setAnnotationFontColor(QColor(0, 0, 0)) map.grid().setBlendMode(QPainter.CompositionMode_SourceOver) map.grid().setFrameStyle(QgsLayoutItemMapGrid.InteriorTicks) map.grid().setFrameWidth(10) map.grid().setFramePenSize(1) map.grid().setFramePenColor(QColor(0, 0, 0)) map.grid().setEnabled(True) map.grid().setStyle(QgsLayoutItemMapGrid.FrameAnnotationsOnly) map.updateBoundingRect() checker = QgsLayoutChecker('composermap_interiorticks', layout) checker.setControlPathPrefix("composer_mapgrid") myTestResult, myMessage = checker.testLayout(0, 100) assert myTestResult, myMessage
def testZebraStyle(self): layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) map.setFrameEnabled(True) map.setBackgroundColor(QColor(150, 100, 100)) layout.addLayoutItem(map) map.grid().setFrameStyle(QgsLayoutItemMapGrid.Zebra) myRectangle = QgsRectangle(785462.375, 3341423.125, 789262.375, 3343323.125) map.setExtent(myRectangle) map.grid().setIntervalX(2000) map.grid().setIntervalY(2000) map.grid().setGridLineColor(QColor(0, 0, 0)) map.grid().setAnnotationFontColor(QColor(0, 0, 0)) map.grid().setBlendMode(QPainter.CompositionMode_SourceOver) map.grid().setFrameStyle(QgsLayoutItemMapGrid.Zebra) map.grid().setFrameWidth(10) map.grid().setFramePenSize(1) map.grid().setGridLineWidth(0.5) map.grid().setFramePenColor(QColor(255, 100, 0, 200)) map.grid().setFrameFillColor1(QColor(50, 90, 50, 100)) map.grid().setFrameFillColor2(QColor(200, 220, 100, 60)) map.grid().setEnabled(True) map.updateBoundingRect() checker = QgsLayoutChecker('composermap_zebrastyle', layout) checker.setControlPathPrefix("composer_mapgrid") myTestResult, myMessage = checker.testLayout(0, 100) assert myTestResult, myMessage
def testCrossGrid(self): layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) map.setFrameEnabled(True) map.setBackgroundColor(QColor(150, 100, 100)) layout.addLayoutItem(map) myRectangle = QgsRectangle(781662.375, 3339523.125, 793062.375, 3345223.125) map.setExtent(myRectangle) map.grid().setEnabled(True) map.grid().setStyle(QgsLayoutItemMapGrid.Cross) map.grid().setCrossLength(2.0) map.grid().setIntervalX(2000) map.grid().setIntervalY(2000) map.grid().setAnnotationEnabled(False) map.grid().setGridLineColor(QColor(0, 255, 0)) map.grid().setGridLineWidth(0.5) map.grid().setBlendMode(QPainter.CompositionMode_SourceOver) map.updateBoundingRect() checker = QgsLayoutChecker('composermap_crossgrid', layout) checker.setControlPathPrefix("composer_mapgrid") myTestResult, myMessage = checker.testLayout() map.grid().setStyle(QgsLayoutItemMapGrid.Solid) map.grid().setEnabled(False) map.grid().setAnnotationEnabled(False) assert myTestResult, myMessage
def testLegendScopeVariables(self): layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() legend = QgsLayoutItemLegend(layout) legend.setTitle("Legend") layout.addLayoutItem(legend) legend.setColumnCount(2) legend.setWrapString('d') legend.setLegendFilterOutAtlas(True) expc = legend.createExpressionContext() exp1 = QgsExpression("@legend_title") self.assertEqual(exp1.evaluate(expc), "Legend") exp2 = QgsExpression("@legend_column_count") self.assertEqual(exp2.evaluate(expc), 2) exp3 = QgsExpression("@legend_wrap_string") self.assertEqual(exp3.evaluate(expc), 'd') exp4 = QgsExpression("@legend_split_layers") self.assertEqual(exp4.evaluate(expc), False) exp5 = QgsExpression("@legend_filter_out_atlas") self.assertEqual(exp5.evaluate(expc), True) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(20, 20, 80, 80)) map.setFrameEnabled(True) map.setExtent(QgsRectangle(781662.375, 3339523.125, 793062.375, 3345223.125)) layout.addLayoutItem(map) map.setScale(15000) legend.setLinkedMap(map) expc2 = legend.createExpressionContext() exp6 = QgsExpression("@map_scale") self.assertAlmostEqual(exp6.evaluate(expc2), 15000, 2)
def testTrueNorth(self): """Test syncing picture to true north""" layout = QgsLayout(QgsProject.instance()) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(0, 0, 10, 10)) map.setCrs(QgsCoordinateReferenceSystem.fromEpsgId(3575)) map.setExtent(QgsRectangle(-2126029.962, -2200807.749, -119078.102, -757031.156)) layout.addLayoutItem(map) picture = QgsLayoutItemPicture(layout) layout.addLayoutItem(picture) picture.setLinkedMap(map) self.assertEqual(picture.linkedMap(), map) picture.setNorthMode(QgsLayoutItemPicture.TrueNorth) self.assertAlmostEqual(picture.pictureRotation(), 37.20, 1) # shift map map.setExtent(QgsRectangle(2120672.293, -3056394.691, 2481640.226, -2796718.780)) self.assertAlmostEqual(picture.pictureRotation(), -38.18, 1) # rotate map map.setMapRotation(45) self.assertAlmostEqual(picture.pictureRotation(), -38.18 + 45, 1) # add an offset picture.setNorthOffset(-10) self.assertAlmostEqual(picture.pictureRotation(), -38.18 + 35, 1)
def testInitialSizeSymbolMapUnits(self): """Test initial size of legend with a symbol size in map units""" point_path = os.path.join(TEST_DATA_DIR, 'points.shp') point_layer = QgsVectorLayer(point_path, 'points', 'ogr') QgsProject.instance().addMapLayers([point_layer]) marker_symbol = QgsMarkerSymbol.createSimple({'color': '#ff0000', 'outline_style': 'no', 'size': '5', 'size_unit': 'MapUnit'}) point_layer.setRenderer(QgsSingleSymbolRenderer(marker_symbol)) s = QgsMapSettings() s.setLayers([point_layer]) layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(20, 20, 80, 80)) map.setFrameEnabled(True) map.setLayers([point_layer]) layout.addLayoutItem(map) map.setExtent(point_layer.extent()) legend = QgsLayoutItemLegend(layout) legend.setTitle("Legend") legend.attemptSetSceneRect(QRectF(120, 20, 80, 80)) legend.setFrameEnabled(True) legend.setFrameStrokeWidth(QgsLayoutMeasurement(2)) legend.setBackgroundColor(QColor(200, 200, 200)) legend.setTitle('') layout.addLayoutItem(legend) legend.setLinkedMap(map) checker = QgsLayoutChecker( 'composer_legend_mapunits', layout) checker.setControlPathPrefix("composer_legend") result, message = checker.testLayout() self.assertTrue(result, message) # resize with non-top-left reference point legend.setResizeToContents(False) legend.setReferencePoint(QgsLayoutItem.LowerRight) legend.attemptMove(QgsLayoutPoint(120, 90)) legend.attemptResize(QgsLayoutSize(50, 60)) self.assertEqual(legend.positionWithUnits().x(), 120.0) self.assertEqual(legend.positionWithUnits().y(), 90.0) self.assertAlmostEqual(legend.pos().x(), 70, -1) self.assertAlmostEqual(legend.pos().y(), 30, -1) legend.setResizeToContents(True) legend.updateLegend() self.assertEqual(legend.positionWithUnits().x(), 120.0) self.assertEqual(legend.positionWithUnits().y(), 90.0) self.assertAlmostEqual(legend.pos().x(), 91, -1) self.assertAlmostEqual(legend.pos().y(), 71, -1) QgsProject.instance().removeMapLayers([point_layer.id()])
def testAsMapLayer(self): l = QgsLayout(QgsProject.instance()) l.initializeDefaults() map = QgsLayoutItemMap(l) map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) l.addLayoutItem(map) overviewMap = QgsLayoutItemMap(l) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) l.addLayoutItem(overviewMap) # zoom in myRectangle = QgsRectangle(96, -152, 160, -120) map.setExtent(myRectangle) myRectangle2 = QgsRectangle(0, -256, 256, 0) overviewMap.setExtent(myRectangle2) overviewMap.overview().setLinkedMap(map) layer = overviewMap.overview().asMapLayer() self.assertIsNotNone(layer) self.assertTrue(layer.isValid()) self.assertEqual([f.geometry().asWkt() for f in layer.getFeatures()], ['Polygon ((96 -120, 160 -120, 160 -152, 96 -152, 96 -120))']) # check that layer has correct renderer fill_symbol = QgsFillSymbol.createSimple({'color': '#00ff00', 'outline_color': '#ff0000', 'outline_width': '10'}) overviewMap.overview().setFrameSymbol(fill_symbol) layer = overviewMap.overview().asMapLayer() self.assertIsInstance(layer.renderer(), QgsSingleSymbolRenderer) self.assertEqual(layer.renderer().symbol().symbolLayer(0).properties()['color'], '0,255,0,255') self.assertEqual(layer.renderer().symbol().symbolLayer(0).properties()['outline_color'], '255,0,0,255') # test layer blend mode self.assertEqual(layer.blendMode(), QPainter.CompositionMode_SourceOver) overviewMap.overview().setBlendMode(QPainter.CompositionMode_Clear) layer = overviewMap.overview().asMapLayer() self.assertEqual(layer.blendMode(), QPainter.CompositionMode_Clear) # should have no effect overviewMap.setMapRotation(45) layer = overviewMap.overview().asMapLayer() self.assertEqual([f.geometry().asWkt() for f in layer.getFeatures()], ['Polygon ((96 -120, 160 -120, 160 -152, 96 -152, 96 -120))']) map.setMapRotation(15) layer = overviewMap.overview().asMapLayer() self.assertEqual([f.geometry().asWkt(0) for f in layer.getFeatures()], ['Polygon ((93 -129, 155 -112, 163 -143, 101 -160, 93 -129))']) # with reprojection map.setCrs(QgsCoordinateReferenceSystem('EPSG:3875')) layer = overviewMap.overview().asMapLayer() self.assertEqual([f.geometry().asWkt(0) for f in layer.getFeatures()], ['Polygon ((93 -129, 96 -128, 99 -127, 102 -126, 105 -126, 108 -125, 111 -124, 114 -123, 116 -123, 119 -122, 122 -121, 125 -120, 128 -119, 131 -119, 134 -118, 137 -117, 140 -116, 143 -115, 146 -115, 149 -114, 152 -113, 155 -112, 155 -114, 156 -115, 156 -117, 156 -118, 157 -120, 157 -121, 158 -123, 158 -124, 158 -126, 159 -127, 159 -128, 160 -130, 160 -131, 160 -133, 161 -134, 161 -136, 161 -137, 162 -139, 162 -140, 163 -142, 163 -143, 160 -144, 157 -145, 154 -146, 151 -146, 148 -147, 145 -148, 142 -149, 140 -149, 137 -150, 134 -151, 131 -152, 128 -153, 125 -153, 122 -154, 119 -155, 116 -156, 113 -157, 110 -157, 107 -158, 104 -159, 101 -160, 101 -158, 100 -157, 100 -155, 100 -154, 99 -152, 99 -151, 98 -149, 98 -148, 98 -146, 97 -145, 97 -144, 96 -142, 96 -141, 96 -139, 95 -138, 95 -136, 95 -135, 94 -133, 94 -132, 93 -130, 93 -129))']) map.setCrs(overviewMap.crs()) # with invert overviewMap.overview().setInverted(True) layer = overviewMap.overview().asMapLayer() self.assertEqual([f.geometry().asWkt(0) for f in layer.getFeatures()], ['Polygon ((-53 -128, 128 53, 309 -128, 128 -309, -53 -128),(93 -129, 101 -160, 163 -143, 155 -112, 93 -129))'])
def testZebraStyleSides(self): layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) map.setFrameEnabled(True) map.setBackgroundColor(QColor(150, 100, 100)) layout.addLayoutItem(map) map.grid().setFrameStyle(QgsLayoutItemMapGrid.Zebra) myRectangle = QgsRectangle(781662.375, 3339523.125, 793062.375, 3345223.125) map.setExtent(myRectangle) map.grid().setIntervalX(2000) map.grid().setIntervalY(2000) map.grid().setGridLineColor(QColor(0, 0, 0)) map.grid().setAnnotationFontColor(QColor(0, 0, 0)) map.grid().setBlendMode(QPainter.CompositionMode_SourceOver) map.grid().setFrameStyle(QgsLayoutItemMapGrid.Zebra) map.grid().setFrameWidth(10) map.grid().setFramePenSize(1) map.grid().setGridLineWidth(0.5) map.grid().setFramePenColor(QColor(0, 0, 0)) map.grid().setFrameFillColor1(QColor(0, 0, 0)) map.grid().setFrameFillColor2(QColor(255, 255, 255)) map.grid().setEnabled(True) map.grid().setFrameSideFlag(QgsLayoutItemMapGrid.FrameLeft, True) map.grid().setFrameSideFlag(QgsLayoutItemMapGrid.FrameRight, False) map.grid().setFrameSideFlag(QgsLayoutItemMapGrid.FrameTop, False) map.grid().setFrameSideFlag(QgsLayoutItemMapGrid.FrameBottom, False) map.updateBoundingRect() checker = QgsLayoutChecker('composermap_zebrastyle_left', layout) checker.setControlPathPrefix("composer_mapgrid") myTestResult, myMessage = checker.testLayout(0, 100) assert myTestResult, myMessage map.grid().setFrameSideFlag(QgsLayoutItemMapGrid.FrameTop, True) map.updateBoundingRect() checker = QgsLayoutChecker('composermap_zebrastyle_lefttop', layout) checker.setControlPathPrefix("composer_mapgrid") myTestResult, myMessage = checker.testLayout(0, 100) assert myTestResult, myMessage map.grid().setFrameSideFlag(QgsLayoutItemMapGrid.FrameRight, True) map.updateBoundingRect() checker = QgsLayoutChecker('composermap_zebrastyle_lefttopright', layout) checker.setControlPathPrefix("composer_mapgrid") myTestResult, myMessage = checker.testLayout(0, 100) assert myTestResult, myMessage map.grid().setFrameSideFlag(QgsLayoutItemMapGrid.FrameBottom, True) map.grid().setFrameStyle(QgsLayoutItemMapGrid.NoFrame)
def test_ModifyMapLayerList(self): l = QgsLayout(QgsProject.instance()) l.initializeDefaults() overviewMap = QgsLayoutItemMap(l) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) l.addLayoutItem(overviewMap) map = QgsLayoutItemMap(l) map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) l.addLayoutItem(map) self.assertFalse(overviewMap.overviews().modifyMapLayerList([])) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [self.raster_layer, self.vector_layer]) overviewMap.overview().setLinkedMap(map) overviewMap.overview().setStackingPosition(QgsLayoutItemMapItem.StackBelowMap) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [self.raster_layer, self.vector_layer, overviewMap.overview().asMapLayer()]) overviewMap.overview().setStackingPosition(QgsLayoutItemMapItem.StackBelowMapLayer) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [self.raster_layer, self.vector_layer]) overviewMap.overview().setStackingLayer(self.raster_layer) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [self.raster_layer, overviewMap.overview().asMapLayer(), self.vector_layer]) overviewMap.overview().setStackingLayer(self.vector_layer) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [self.raster_layer, self.vector_layer, overviewMap.overview().asMapLayer()]) overviewMap.overview().setStackingPosition(QgsLayoutItemMapItem.StackAboveMapLayer) overviewMap.overview().setStackingLayer(None) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [self.raster_layer, self.vector_layer]) overviewMap.overview().setStackingLayer(self.raster_layer) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [overviewMap.overview().asMapLayer(), self.raster_layer, self.vector_layer]) overviewMap.overview().setStackingLayer(self.vector_layer) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [self.raster_layer, overviewMap.overview().asMapLayer(), self.vector_layer]) overviewMap.overview().setStackingPosition(QgsLayoutItemMapItem.StackBelowMapLabels) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [overviewMap.overview().asMapLayer(), self.raster_layer, self.vector_layer]) overviewMap.overview().setStackingPosition(QgsLayoutItemMapItem.StackAboveMapLabels) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [self.raster_layer, self.vector_layer]) # two overviews overviewMap.overview().setStackingPosition(QgsLayoutItemMapItem.StackBelowMap) overviewMap.overviews().addOverview(QgsLayoutItemMapOverview('x', overviewMap)) overviewMap.overviews().overview(1).setLinkedMap(map) overviewMap.overviews().overview(1).setStackingPosition(QgsLayoutItemMapItem.StackBelowMapLabels) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [overviewMap.overviews().overview(1).asMapLayer(), self.raster_layer, self.vector_layer, overviewMap.overview().asMapLayer()])
def testDataDefinedColumnCount(self): layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() legend = QgsLayoutItemLegend(layout) layout.addLayoutItem(legend) legend.setColumnCount(2) self.assertEqual(legend.columnCount(), 2) self.assertEqual(legend.legendSettings().columnCount(), 2) legend.dataDefinedProperties().setProperty(QgsLayoutObject.LegendColumnCount, QgsProperty.fromExpression("5")) legend.refreshDataDefinedProperty() self.assertEqual(legend.columnCount(), 2) self.assertEqual(legend.legendSettings().columnCount(), 5)
def testDataDefinedTitle(self): layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() legend = QgsLayoutItemLegend(layout) layout.addLayoutItem(legend) legend.setTitle('original') self.assertEqual(legend.title(), 'original') self.assertEqual(legend.legendSettings().title(), 'original') legend.dataDefinedProperties().setProperty(QgsLayoutObject.LegendTitle, QgsProperty.fromExpression("'new'")) legend.refreshDataDefinedProperty() self.assertEqual(legend.title(), 'original') self.assertEqual(legend.legendSettings().title(), 'new')
def testCase(self): TEST_DATA_DIR = unitTestDataPath() vectorFileInfo = QFileInfo(TEST_DATA_DIR + "/france_parts.shp") mVectorLayer = QgsVectorLayer(vectorFileInfo.filePath(), vectorFileInfo.completeBaseName(), "ogr") QgsProject.instance().addMapLayers([mVectorLayer]) layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() label = QgsLayoutItemLabel(layout) layout.addLayoutItem(label) self.evaluation_test(layout, label) self.feature_evaluation_test(layout, label, mVectorLayer) self.page_evaluation_test(layout, label, mVectorLayer)
def testMapCrs(self): # create layout with layout map map_settings = QgsMapSettings() map_settings.setLayers([self.vector_layer]) layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() # check that new maps inherit project CRS QgsProject.instance().setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) map.setFrameEnabled(True) rectangle = QgsRectangle(-13838977, 2369660, -8672298, 6250909) map.setExtent(rectangle) map.setLayers([self.vector_layer]) layout.addLayoutItem(map) self.assertEqual(map.crs().authid(), 'EPSG:4326') self.assertFalse(map.presetCrs().isValid()) # overwrite CRS map.setCrs(QgsCoordinateReferenceSystem('EPSG:3857')) self.assertEqual(map.crs().authid(), 'EPSG:3857') self.assertEqual(map.presetCrs().authid(), 'EPSG:3857') checker = QgsLayoutChecker('composermap_crs3857', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) # overwrite CRS map.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) self.assertEqual(map.presetCrs().authid(), 'EPSG:4326') self.assertEqual(map.crs().authid(), 'EPSG:4326') rectangle = QgsRectangle(-124, 17, -78, 52) map.zoomToExtent(rectangle) checker = QgsLayoutChecker('composermap_crs4326', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) # change back to project CRS map.setCrs(QgsCoordinateReferenceSystem()) self.assertEqual(map.crs().authid(), 'EPSG:4326') self.assertFalse(map.presetCrs().isValid())
def test_StackingPosition(self): l = QgsLayout(QgsProject.instance()) l.initializeDefaults() overviewMap = QgsLayoutItemMap(l) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) l.addLayoutItem(overviewMap) overviewMap.overview().setStackingPosition(QgsLayoutItemMapItem.StackBelowMap) self.assertEqual(overviewMap.overview().stackingPosition(), QgsLayoutItemMapItem.StackBelowMap) overviewMap.overview().setStackingPosition(QgsLayoutItemMapItem.StackBelowMapLayer) self.assertEqual(overviewMap.overview().stackingPosition(), QgsLayoutItemMapItem.StackBelowMapLayer) overviewMap.overview().setStackingLayer(self.raster_layer) self.assertEqual(overviewMap.overview().stackingLayer(), self.raster_layer) overviewMap.overview().setStackingLayer(self.vector_layer) self.assertEqual(overviewMap.overview().stackingLayer(), self.vector_layer) overviewMap.overview().setStackingLayer(None) self.assertIsNone(overviewMap.overview().stackingLayer())
def testResizeDisabledCrop(self): """Test that if legend resizing is disabled, and legend is too small, then content is cropped""" point_path = os.path.join(TEST_DATA_DIR, 'points.shp') point_layer = QgsVectorLayer(point_path, 'points', 'ogr') QgsProject.instance().addMapLayers([point_layer]) s = QgsMapSettings() s.setLayers([point_layer]) layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(20, 20, 80, 80)) map.setFrameEnabled(True) map.setLayers([point_layer]) layout.addLayoutItem(map) map.setExtent(point_layer.extent()) legend = QgsLayoutItemLegend(layout) legend.setTitle("Legend") legend.attemptSetSceneRect(QRectF(120, 20, 20, 20)) legend.setFrameEnabled(True) legend.setFrameStrokeWidth(QgsLayoutMeasurement(2)) legend.setBackgroundColor(QColor(200, 200, 200)) legend.setTitle('') legend.setLegendFilterByMapEnabled(True) # disable auto resizing legend.setResizeToContents(False) layout.addLayoutItem(legend) legend.setLinkedMap(map) map.setExtent(QgsRectangle(-102.51, 41.16, -102.36, 41.30)) checker = QgsLayoutChecker( 'composer_legend_noresize_crop', layout) checker.setControlPathPrefix("composer_legend") result, message = checker.testLayout() self.assertTrue(result, message) QgsProject.instance().removeMapLayers([point_layer.id()])
def testCopyPaste(self): p = QgsProject() l = QgsLayout(p) # clear clipboard mime_data = QMimeData() mime_data.setData("text/xml", QByteArray()) clipboard = QApplication.clipboard() clipboard.setMimeData(mime_data) # add an item item1 = QgsLayoutItemLabel(l) item1.setText('label 1') l.addLayoutItem(item1) item1.setSelected(True) item2 = QgsLayoutItemLabel(l) item2.setText('label 2') l.addLayoutItem(item2) item2.setSelected(True) view = QgsLayoutView() view.setCurrentLayout(l) self.assertFalse(view.hasItemsInClipboard()) view.copySelectedItems(QgsLayoutView.ClipboardCopy) self.assertTrue(view.hasItemsInClipboard()) pasted = view.pasteItems(QgsLayoutView.PasteModeCursor) self.assertEqual(len(pasted), 2) self.assertIn(pasted[0], l.items()) self.assertIn(pasted[1], l.items()) self.assertIn(sip.cast(pasted[0], QgsLayoutItemLabel).text(), ('label 1', 'label 2')) self.assertIn(sip.cast(pasted[1], QgsLayoutItemLabel).text(), ('label 1', 'label 2')) # copy specific item view.copyItems([item2], QgsLayoutView.ClipboardCopy) l2 = QgsLayout(p) view2 = QgsLayoutView() view2.setCurrentLayout(l2) pasted = view2.pasteItems(QgsLayoutView.PasteModeCursor) self.assertEqual(len(pasted), 1) self.assertIn(pasted[0], l2.items()) self.assertEqual(sip.cast(pasted[0], QgsLayoutItemLabel).text(), 'label 2')
def testDeleteItems(self): p = QgsProject() l = QgsLayout(p) # add some items item1 = QgsLayoutItemLabel(l) item1.setText('label 1') l.addLayoutItem(item1) item2 = QgsLayoutItemLabel(l) item2.setText('label 2') l.addLayoutItem(item2) item3 = QgsLayoutItemLabel(l) item3.setText('label 2') l.addLayoutItem(item3) view = QgsLayoutView() view.setCurrentLayout(l) count_before = len(l.items()) view.deleteSelectedItems() self.assertEqual(len(l.items()), count_before) item2.setSelected(True) view.deleteSelectedItems() self.assertEqual(len(l.items()), count_before - 1) self.assertIn(item1, l.items()) self.assertIn(item3, l.items()) view.deleteItems([item3]) self.assertEqual(len(l.items()), count_before - 2) self.assertIn(item1, l.items())
def testOverviewStacking(self): l = QgsLayout(QgsProject.instance()) l.initializeDefaults() map = QgsLayoutItemMap(l) map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) map.setFrameEnabled(True) map.setLayers([self.raster_layer]) l.addLayoutItem(map) overviewMap = QgsLayoutItemMap(l) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) l.addLayoutItem(overviewMap) overviewMap.setFrameEnabled(True) overviewMap.setLayers([self.raster_layer]) # zoom in myRectangle = QgsRectangle(96, -152, 160, -120) map.setExtent(myRectangle) myRectangle2 = QgsRectangle(-20, -276, 276, 20) overviewMap.setExtent(myRectangle2) overviewMap.overview().setLinkedMap(map) overviewMap.overview().setInverted(True) overviewMap.overview().setStackingPosition(QgsLayoutItemMapItem.StackBelowMapLayer) overviewMap.overview().setStackingLayer(self.raster_layer) checker = QgsLayoutChecker('composermap_overview_belowmap', l) checker.setColorTolerance(6) checker.setControlPathPrefix("composer_mapoverview") myTestResult, myMessage = checker.testLayout() self.report += checker.report() self.assertTrue(myTestResult, myMessage) overviewMap.overview().setStackingPosition(QgsLayoutItemMapItem.StackAboveMapLayer) overviewMap.overview().setStackingLayer(self.raster_layer) checker = QgsLayoutChecker('composermap_overview_abovemap', l) checker.setColorTolerance(6) checker.setControlPathPrefix("composer_mapoverview") myTestResult, myMessage = checker.testLayout() self.report += checker.report() self.assertTrue(myTestResult, myMessage)
def testInitialSizeSymbolMapUnits(self): """Test initial size of legend with a symbol size in map units""" point_path = os.path.join(TEST_DATA_DIR, 'points.shp') point_layer = QgsVectorLayer(point_path, 'points', 'ogr') QgsProject.instance().addMapLayers([point_layer]) marker_symbol = QgsMarkerSymbol.createSimple({'color': '#ff0000', 'outline_style': 'no', 'size': '5', 'size_unit': 'MapUnit'}) point_layer.setRenderer(QgsSingleSymbolRenderer(marker_symbol)) s = QgsMapSettings() s.setLayers([point_layer]) layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(20, 20, 80, 80)) map.setFrameEnabled(True) map.setLayers([point_layer]) layout.addLayoutItem(map) map.setExtent(point_layer.extent()) legend = QgsLayoutItemLegend(layout) legend.attemptSetSceneRect(QRectF(120, 20, 80, 80)) legend.setFrameEnabled(True) legend.setFrameStrokeWidth(QgsLayoutMeasurement(2)) legend.setBackgroundColor(QColor(200, 200, 200)) legend.setTitle('') layout.addLayoutItem(legend) legend.setMap(map) checker = QgsLayoutChecker( 'composer_legend_mapunits', layout) checker.setControlPathPrefix("composer_legend") result, message = checker.testLayout() self.assertTrue(result, message) QgsProject.instance().removeMapLayers([point_layer.id()])
def testGridNorth(self): """Test syncing picture to grid north""" layout = QgsLayout(QgsProject.instance()) map = QgsLayoutItemMap(layout) map.setExtent(QgsRectangle(0, -256, 256, 0)) layout.addLayoutItem(map) picture = QgsLayoutItemPicture(layout) layout.addLayoutItem(picture) picture.setLinkedMap(map) self.assertEqual(picture.linkedMap(), map) picture.setNorthMode(QgsLayoutItemPicture.GridNorth) map.setMapRotation(45) self.assertEqual(picture.pictureRotation(), 45) # add an offset picture.setNorthOffset(-10) self.assertEqual(picture.pictureRotation(), 35)
def testExportWorldFile(self): l = QgsLayout(QgsProject.instance()) l.initializeDefaults() # add some items map = QgsLayoutItemMap(l) map.attemptSetSceneRect(QRectF(30, 60, 200, 100)) extent = QgsRectangle(2000, 2800, 2500, 2900) map.setExtent(extent) l.addLayoutItem(map) exporter = QgsLayoutExporter(l) # setup settings settings = QgsLayoutExporter.ImageExportSettings() settings.dpi = 80 settings.generateWorldFile = False rendered_file_path = os.path.join(self.basetestpath, 'test_exportwithworldfile.png') world_file_path = os.path.join(self.basetestpath, 'test_exportwithworldfile.pgw') self.assertEqual(exporter.exportToImage(rendered_file_path, settings), QgsLayoutExporter.Success) self.assertTrue(os.path.exists(rendered_file_path)) self.assertFalse(os.path.exists(world_file_path)) # with world file settings.generateWorldFile = True rendered_file_path = os.path.join(self.basetestpath, 'test_exportwithworldfile.png') self.assertEqual(exporter.exportToImage(rendered_file_path, settings), QgsLayoutExporter.Success) self.assertTrue(os.path.exists(rendered_file_path)) self.assertTrue(os.path.exists(world_file_path)) lines = tuple(open(world_file_path, 'r')) values = [float(f) for f in lines] self.assertAlmostEqual(values[0], 0.794117647059, 2) self.assertAlmostEqual(values[1], 0.0, 2) self.assertAlmostEqual(values[2], 0.0, 2) self.assertAlmostEqual(values[3], -0.794251134644, 2) self.assertAlmostEqual(values[4], 1925.000000000000, 2) self.assertAlmostEqual(values[5], 3050.000000000000, 2)
def testGrid(self): layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) map.setFrameEnabled(True) map.setBackgroundColor(QColor(150, 100, 100)) layout.addLayoutItem(map) """Test that we can create a grid for a map.""" myRectangle = QgsRectangle(781662.375, 3339523.125, 793062.375, 3345223.125) map.setExtent(myRectangle) map.grid().setEnabled(True) map.grid().setIntervalX(2000) map.grid().setIntervalY(2000) map.grid().setAnnotationEnabled(True) map.grid().setGridLineColor(QColor(0, 255, 0)) map.grid().setGridLineWidth(0.5) map.grid().setAnnotationFont(QgsFontUtils.getStandardTestFont()) map.grid().setAnnotationPrecision(0) map.grid().setAnnotationDisplay(QgsLayoutItemMapGrid.HideAll, QgsLayoutItemMapGrid.Left) map.grid().setAnnotationPosition(QgsLayoutItemMapGrid.OutsideMapFrame, QgsLayoutItemMapGrid.Right) map.grid().setAnnotationDisplay(QgsLayoutItemMapGrid.HideAll, QgsLayoutItemMapGrid.Top) map.grid().setAnnotationPosition(QgsLayoutItemMapGrid.OutsideMapFrame, QgsLayoutItemMapGrid.Bottom) map.grid().setAnnotationDirection(QgsLayoutItemMapGrid.Horizontal, QgsLayoutItemMapGrid.Right) map.grid().setAnnotationDirection(QgsLayoutItemMapGrid.Horizontal, QgsLayoutItemMapGrid.Bottom) map.grid().setAnnotationFontColor(QColor(255, 0, 0, 150)) map.grid().setBlendMode(QPainter.CompositionMode_Overlay) map.updateBoundingRect() checker = QgsLayoutChecker('composermap_grid', layout) checker.setControlPathPrefix("composer_mapgrid") myTestResult, myMessage = checker.testLayout() map.grid().setEnabled(False) map.grid().setAnnotationEnabled(False) assert myTestResult, myMessage
def renderAnnotationInLayout(self, test_name, annotation): pr = QgsProject() l = QgsLayout(pr) l.initializeDefaults() map = QgsLayoutItemMap(l) map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) map.setFrameEnabled(True) rectangle = QgsRectangle(0, 0, 18, 8) map.setExtent(rectangle) l.addLayoutItem(map) annotation.setMapPosition(QgsPointXY(1, 7)) annotation.setHasFixedMapPosition(True) pr.annotationManager().addAnnotation(annotation) checker = QgsLayoutChecker( test_name, l) checker.dots_per_meter = 2 * 96 / 25.4 * 1000 checker.size = QSize(1122 * 2, 794 * 2) checker.setControlPathPrefix("annotations") result, message = checker.testLayout() self.report += checker.report() return result
def testDeletePageWithItems2(self): p = QgsProject() l = QgsLayout(p) collection = l.pageCollection() # add a page page = QgsLayoutItemPage(l) page.setPageSize('A4') collection.addPage(page) page2 = QgsLayoutItemPage(l) page2.setPageSize('A4') collection.addPage(page2) page3 = QgsLayoutItemPage(l) page3.setPageSize('A4') collection.addPage(page3) # item on pages shape1 = QgsLayoutItemShape(l) shape1.attemptResize(QgsLayoutSize(90, 50)) shape1.attemptMove(QgsLayoutPoint(90, 50), page=0) l.addLayoutItem(shape1) shape2 = QgsLayoutItemShape(l) shape2.attemptResize(QgsLayoutSize(110, 50)) shape2.attemptMove(QgsLayoutPoint(100, 150), page=2) l.addLayoutItem(shape2) self.assertEqual(shape1.page(), 0) self.assertEqual(shape2.page(), 2) collection.deletePage(page2) # check item position self.assertEqual(shape1.page(), 0) self.assertEqual(shape1.pagePositionWithUnits(), QgsLayoutPoint(90, 50)) self.assertEqual(shape2.page(), 1) self.assertEqual(shape2.pagePositionWithUnits(), QgsLayoutPoint(100, 150))
def testVerticalLine(self): pr = QgsProject() l = QgsLayout(pr) l.initializeDefaults() p = QPolygonF() p.append(QPointF(100.0, 50.0)) p.append(QPointF(100.0, 100.0)) shape = QgsLayoutItemPolyline(p, l) l.addLayoutItem(shape) props = {} props["color"] = "0,0,0,255" props["width"] = "10.0" props["capstyle"] = "square" style = QgsLineSymbol.createSimple(props) shape.setSymbol(style) checker = QgsLayoutChecker( 'composerpolyline_vertline', l) checker.setControlPathPrefix("composer_polyline") myTestResult, myMessage = checker.testLayout() assert myTestResult, myMessage
class TestQgsLayoutPolygon(unittest.TestCase, LayoutItemTestCase): @classmethod def setUpClass(cls): cls.item_class = QgsLayoutItemPolygon def __init__(self, methodName): """Run once on class initialization.""" unittest.TestCase.__init__(self, methodName) # create composition self.layout = QgsLayout(QgsProject.instance()) self.layout.initializeDefaults() # create polygon = QPolygonF() polygon.append(QPointF(0.0, 0.0)) polygon.append(QPointF(100.0, 0.0)) polygon.append(QPointF(200.0, 100.0)) polygon.append(QPointF(100.0, 200.0)) self.polygon = QgsLayoutItemPolygon(polygon, self.layout) self.layout.addLayoutItem(self.polygon) # style props = {} props["color"] = "green" props["style"] = "solid" props["style_border"] = "solid" props["color_border"] = "black" props["width_border"] = "10.0" props["joinstyle"] = "miter" style = QgsFillSymbol.createSimple(props) self.polygon.setSymbol(style) def testNodes(self): polygon = QPolygonF() polygon.append(QPointF(0.0, 0.0)) polygon.append(QPointF(100.0, 0.0)) polygon.append(QPointF(200.0, 100.0)) polygon.append(QPointF(100.0, 200.0)) p = QgsLayoutItemPolygon(polygon, self.layout) self.assertEqual(p.nodes(), polygon) polygon = QPolygonF() polygon.append(QPointF(0.0, 0.0)) polygon.append(QPointF(1000.0, 0.0)) polygon.append(QPointF(2000.0, 100.0)) polygon.append(QPointF(1000.0, 200.0)) p.setNodes(polygon) self.assertEqual(p.nodes(), polygon) def testDisplayName(self): """Test if displayName is valid""" self.assertEqual(self.polygon.displayName(), "<Polygon>") def testType(self): """Test if type is valid""" self.assertEqual( self.polygon.type(), QgsLayoutItemRegistry.LayoutPolygon) def testDefaultStyle(self): """Test polygon rendering with default style.""" self.polygon.setDisplayNodes(False) checker = QgsLayoutChecker( 'composerpolygon_defaultstyle', self.layout) checker.setControlPathPrefix("composer_polygon") myTestResult, myMessage = checker.testLayout() assert myTestResult, myMessage def testDisplayNodes(self): """Test displayNodes method""" self.polygon.setDisplayNodes(True) checker = QgsLayoutChecker( 'composerpolygon_displaynodes', self.layout) checker.setControlPathPrefix("composer_polygon") myTestResult, myMessage = checker.testLayout() assert myTestResult, myMessage self.polygon.setDisplayNodes(False) checker = QgsLayoutChecker( 'composerpolygon_defaultstyle', self.layout) checker.setControlPathPrefix("composer_polygon") myTestResult, myMessage = checker.testLayout() assert myTestResult, myMessage def testSelectedNode(self): """Test selectedNode and deselectNode methods""" self.polygon.setDisplayNodes(True) self.polygon.setSelectedNode(3) checker = QgsLayoutChecker( 'composerpolygon_selectednode', self.layout) checker.setControlPathPrefix("composer_polygon") myTestResult, myMessage = checker.testLayout() assert myTestResult, myMessage self.polygon.deselectNode() self.polygon.setDisplayNodes(False) checker = QgsLayoutChecker( 'composerpolygon_defaultstyle', self.layout) checker.setControlPathPrefix("composer_polygon") myTestResult, myMessage = checker.testLayout() assert myTestResult, myMessage def testRemoveNode(self): """Test removeNode method""" rc = self.polygon.removeNode(100) self.assertEqual(rc, False) checker = QgsLayoutChecker( 'composerpolygon_defaultstyle', self.layout) checker.setControlPathPrefix("composer_polygon") myTestResult, myMessage = checker.testLayout() assert myTestResult, myMessage self.assertEqual(self.polygon.nodesSize(), 4) def testAddNode(self): """Test addNode method""" # default searching radius is 10 self.assertEqual(self.polygon.nodesSize(), 4) rc = self.polygon.addNode(QPointF(50.0, 10.0)) self.assertEqual(rc, False) # default searching radius is 10 self.assertEqual(self.polygon.nodesSize(), 4) rc = self.polygon.addNode(QPointF(50.0, 9.99)) self.assertEqual(rc, True) self.assertEqual(self.polygon.nodesSize(), 5) def testAddNodeCustomRadius(self): """Test addNode with custom radius""" # default searching radius is 10 self.assertEqual(self.polygon.nodesSize(), 4) rc = self.polygon.addNode(QPointF(50.0, 8.1), True, 8.0) self.assertEqual(rc, False) self.assertEqual(self.polygon.nodesSize(), 4) # default searching radius is 10 rc = self.polygon.addNode(QPointF(50.0, 7.9), True, 8.0) self.assertEqual(rc, True) self.assertEqual(self.polygon.nodesSize(), 5) def testAddNodeWithoutCheckingArea(self): """Test addNode without checking the maximum distance allowed""" # default searching radius is 10 self.assertEqual(self.polygon.nodesSize(), 4) rc = self.polygon.addNode(QPointF(50.0, 20.0)) self.assertEqual(rc, False) self.assertEqual(self.polygon.nodesSize(), 4) # default searching radius is 10 self.assertEqual(self.polygon.nodesSize(), 4) rc = self.polygon.addNode(QPointF(50.0, 20.0), False) self.assertEqual(rc, True) self.assertEqual(self.polygon.nodesSize(), 5) checker = QgsLayoutChecker( 'composerpolygon_addnode', self.layout) checker.setControlPathPrefix("composer_polygon") myTestResult, myMessage = checker.testLayout() assert myTestResult, myMessage def testMoveNode(self): """Test moveNode method""" rc = self.polygon.moveNode(30, QPointF(100.0, 300.0)) self.assertEqual(rc, False) rc = self.polygon.moveNode(3, QPointF(100.0, 150.0)) self.assertEqual(rc, True) checker = QgsLayoutChecker( 'composerpolygon_movenode', self.layout) checker.setControlPathPrefix("composer_polygon") myTestResult, myMessage = checker.testLayout() assert myTestResult, myMessage def testNodeAtPosition(self): """Test nodeAtPosition method""" p = QPolygonF() p.append(QPointF(0.0, 0.0)) p.append(QPointF(100.0, 0.0)) p.append(QPointF(200.0, 100.0)) p.append(QPointF(100.0, 200.0)) polygon = QgsLayoutItemPolygon(p, self.layout) # default searching radius is 10 rc = polygon.nodeAtPosition(QPointF(100.0, 210.0)) self.assertEqual(rc, -1) # default searching radius is 10 rc = polygon.nodeAtPosition( QPointF(100.0, 210.0), False) self.assertEqual(rc, 3) # default searching radius is 10 rc = polygon.nodeAtPosition( QPointF(100.0, 210.0), True, 10.1) self.assertEqual(rc, 3) def testReadWriteXml(self): pr = QgsProject() l = QgsLayout(pr) p = QPolygonF() p.append(QPointF(0.0, 0.0)) p.append(QPointF(100.0, 0.0)) p.append(QPointF(200.0, 100.0)) shape = QgsLayoutItemPolygon(p, l) props = {} props["color"] = "green" props["style"] = "solid" props["style_border"] = "solid" props["color_border"] = "red" props["width_border"] = "10.0" props["joinstyle"] = "miter" style = QgsFillSymbol.createSimple(props) shape.setSymbol(style) #save original item to xml doc = QDomDocument("testdoc") elem = doc.createElement("test") self.assertTrue(shape.writeXml(elem, doc, QgsReadWriteContext())) shape2 = QgsLayoutItemPolygon(l) self.assertTrue(shape2.readXml(elem.firstChildElement(), doc, QgsReadWriteContext())) self.assertEqual(shape2.nodes(), shape.nodes()) self.assertEqual(shape2.symbol().symbolLayer(0).color().name(), '#008000') self.assertEqual(shape2.symbol().symbolLayer(0).strokeColor().name(), '#ff0000') def testBounds(self): pr = QgsProject() l = QgsLayout(pr) p = QPolygonF() p.append(QPointF(50.0, 30.0)) p.append(QPointF(100.0, 10.0)) p.append(QPointF(200.0, 100.0)) shape = QgsLayoutItemPolygon(p, l) props = {} props["color"] = "green" props["style"] = "solid" props["style_border"] = "solid" props["color_border"] = "red" props["width_border"] = "6.0" props["joinstyle"] = "miter" style = QgsFillSymbol.createSimple(props) shape.setSymbol(style) # scene bounding rect should include symbol outline bounds = shape.sceneBoundingRect() self.assertEqual(bounds.left(), 47.0) self.assertEqual(bounds.right(), 203.0) self.assertEqual(bounds.top(), 7.0) self.assertEqual(bounds.bottom(), 103.0) # rectWithFrame should include symbol outline too bounds = shape.rectWithFrame() self.assertEqual(bounds.left(), -3.0) self.assertEqual(bounds.right(), 153.0) self.assertEqual(bounds.top(), -3.0) self.assertEqual(bounds.bottom(), 93.0)
def testAsMapLayer(self): l = QgsLayout(QgsProject.instance()) l.initializeDefaults() map = QgsLayoutItemMap(l) map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) l.addLayoutItem(map) overviewMap = QgsLayoutItemMap(l) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) l.addLayoutItem(overviewMap) # zoom in myRectangle = QgsRectangle(96, -152, 160, -120) map.setExtent(myRectangle) myRectangle2 = QgsRectangle(0, -256, 256, 0) overviewMap.setExtent(myRectangle2) overviewMap.overview().setLinkedMap(map) layer = overviewMap.overview().asMapLayer() self.assertIsNotNone(layer) self.assertTrue(layer.isValid()) self.assertEqual( [f.geometry().asWkt() for f in layer.getFeatures()], ['Polygon ((96 -120, 160 -120, 160 -152, 96 -152, 96 -120))']) # check that layer has correct renderer fill_symbol = QgsFillSymbol.createSimple({ 'color': '#00ff00', 'outline_color': '#ff0000', 'outline_width': '10' }) overviewMap.overview().setFrameSymbol(fill_symbol) layer = overviewMap.overview().asMapLayer() self.assertIsInstance(layer.renderer(), QgsSingleSymbolRenderer) self.assertEqual( layer.renderer().symbol().symbolLayer(0).properties()['color'], '0,255,0,255') self.assertEqual( layer.renderer().symbol().symbolLayer(0).properties() ['outline_color'], '255,0,0,255') # test layer blend mode self.assertEqual(layer.blendMode(), QPainter.CompositionMode_SourceOver) overviewMap.overview().setBlendMode(QPainter.CompositionMode_Clear) layer = overviewMap.overview().asMapLayer() self.assertEqual(layer.blendMode(), QPainter.CompositionMode_Clear) # should have no effect overviewMap.setMapRotation(45) layer = overviewMap.overview().asMapLayer() self.assertEqual( [f.geometry().asWkt() for f in layer.getFeatures()], ['Polygon ((96 -120, 160 -120, 160 -152, 96 -152, 96 -120))']) map.setMapRotation(15) layer = overviewMap.overview().asMapLayer() self.assertEqual( [f.geometry().asWkt(0) for f in layer.getFeatures()], ['Polygon ((93 -129, 155 -112, 163 -143, 101 -160, 93 -129))']) # with reprojection map.setCrs(QgsCoordinateReferenceSystem('EPSG:3875')) layer = overviewMap.overview().asMapLayer() self.assertEqual([ f.geometry().asWkt(0) for f in layer.getFeatures() ], [ 'Polygon ((93 -129, 96 -128, 99 -127, 102 -126, 105 -126, 108 -125, 111 -124, 114 -123, 116 -123, 119 -122, 122 -121, 125 -120, 128 -119, 131 -119, 134 -118, 137 -117, 140 -116, 143 -115, 146 -115, 149 -114, 152 -113, 155 -112, 155 -114, 156 -115, 156 -117, 156 -118, 157 -120, 157 -121, 158 -123, 158 -124, 158 -126, 159 -127, 159 -128, 160 -130, 160 -131, 160 -133, 161 -134, 161 -136, 161 -137, 162 -139, 162 -140, 163 -142, 163 -143, 160 -144, 157 -145, 154 -146, 151 -146, 148 -147, 145 -148, 142 -149, 140 -149, 137 -150, 134 -151, 131 -152, 128 -153, 125 -153, 122 -154, 119 -155, 116 -156, 113 -157, 110 -157, 107 -158, 104 -159, 101 -160, 101 -158, 100 -157, 100 -155, 100 -154, 99 -152, 99 -151, 98 -149, 98 -148, 98 -146, 97 -145, 97 -144, 96 -142, 96 -141, 96 -139, 95 -138, 95 -136, 95 -135, 94 -133, 94 -132, 93 -130, 93 -129))' ]) map.setCrs(overviewMap.crs()) # with invert overviewMap.overview().setInverted(True) layer = overviewMap.overview().asMapLayer() self.assertEqual([ f.geometry().asWkt(0) for f in layer.getFeatures() ], [ 'Polygon ((-53 -128, 128 53, 309 -128, 128 -309, -53 -128),(93 -129, 101 -160, 163 -143, 155 -112, 93 -129))' ])
class TestQgsLayoutMap(unittest.TestCase, LayoutItemTestCase): @classmethod def setUpClass(cls): cls.item_class = QgsLayoutItemMap def setUp(self): self.report = "<h1>Python QgsLayoutItemMap Tests</h1>\n" def tearDown(self): report_file_path = "%s/qgistest.html" % QDir.tempPath() with open(report_file_path, 'a') as report_file: report_file.write(self.report) def __init__(self, methodName): """Run once on class initialization.""" unittest.TestCase.__init__(self, methodName) myPath = os.path.join(TEST_DATA_DIR, 'rgb256x256.png') rasterFileInfo = QFileInfo(myPath) self.raster_layer = QgsRasterLayer(rasterFileInfo.filePath(), rasterFileInfo.completeBaseName()) rasterRenderer = QgsMultiBandColorRenderer( self.raster_layer.dataProvider(), 1, 2, 3) self.raster_layer.setRenderer(rasterRenderer) myPath = os.path.join(TEST_DATA_DIR, 'points.shp') vector_file_info = QFileInfo(myPath) self.vector_layer = QgsVectorLayer(vector_file_info.filePath(), vector_file_info.completeBaseName(), 'ogr') assert self.vector_layer.isValid() # pipe = mRasterLayer.pipe() # assert pipe.set(rasterRenderer), 'Cannot set pipe renderer' QgsProject.instance().addMapLayers([self.raster_layer, self.vector_layer]) # create layout with layout map self.layout = QgsLayout(QgsProject.instance()) self.layout.initializeDefaults() self.map = QgsLayoutItemMap(self.layout) self.map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) self.map.setFrameEnabled(True) self.map.setLayers([self.raster_layer]) self.layout.addLayoutItem(self.map) def testMapCrs(self): # create layout with layout map map_settings = QgsMapSettings() map_settings.setLayers([self.vector_layer]) layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() # check that new maps inherit project CRS QgsProject.instance().setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) map.setFrameEnabled(True) rectangle = QgsRectangle(-13838977, 2369660, -8672298, 6250909) map.setExtent(rectangle) map.setLayers([self.vector_layer]) layout.addLayoutItem(map) self.assertEqual(map.crs().authid(), 'EPSG:4326') self.assertFalse(map.presetCrs().isValid()) # overwrite CRS map.setCrs(QgsCoordinateReferenceSystem('EPSG:3857')) self.assertEqual(map.crs().authid(), 'EPSG:3857') self.assertEqual(map.presetCrs().authid(), 'EPSG:3857') checker = QgsLayoutChecker('composermap_crs3857', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) # overwrite CRS map.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) self.assertEqual(map.presetCrs().authid(), 'EPSG:4326') self.assertEqual(map.crs().authid(), 'EPSG:4326') rectangle = QgsRectangle(-124, 17, -78, 52) map.zoomToExtent(rectangle) checker = QgsLayoutChecker('composermap_crs4326', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) # change back to project CRS map.setCrs(QgsCoordinateReferenceSystem()) self.assertEqual(map.crs().authid(), 'EPSG:4326') self.assertFalse(map.presetCrs().isValid()) def testContainsAdvancedEffects(self): map_settings = QgsMapSettings() map_settings.setLayers([self.vector_layer]) layout = QgsLayout(QgsProject.instance()) map = QgsLayoutItemMap(layout) self.assertFalse(map.containsAdvancedEffects()) self.vector_layer.setBlendMode(QPainter.CompositionMode_Darken) result = map.containsAdvancedEffects() self.vector_layer.setBlendMode(QPainter.CompositionMode_SourceOver) self.assertTrue(result) def testRasterization(self): map_settings = QgsMapSettings() map_settings.setLayers([self.vector_layer]) layout = QgsLayout(QgsProject.instance()) map = QgsLayoutItemMap(layout) self.assertFalse(map.requiresRasterization()) self.vector_layer.setBlendMode(QPainter.CompositionMode_Darken) self.assertFalse(map.requiresRasterization()) self.assertTrue(map.containsAdvancedEffects()) map.setBackgroundEnabled(False) self.assertTrue(map.requiresRasterization()) map.setBackgroundEnabled(True) map.setBackgroundColor(QColor(1, 1, 1, 1)) self.assertTrue(map.requiresRasterization()) self.vector_layer.setBlendMode(QPainter.CompositionMode_SourceOver) def testLabelMargin(self): """ Test rendering map item with a label margin set """ format = QgsTextFormat() format.setFont(QgsFontUtils.getStandardTestFont("Bold")) format.setSize(20) format.setNamedStyle("Bold") format.setColor(QColor(0, 0, 0)) settings = QgsPalLayerSettings() settings.setFormat(format) settings.fieldName = "'X'" settings.isExpression = True settings.placement = QgsPalLayerSettings.OverPoint vl = QgsVectorLayer("Point?crs=epsg:4326&field=id:integer", "vl", "memory") vl.setRenderer(QgsNullSymbolRenderer()) f = QgsFeature(vl.fields(), 1) for x in range(15): for y in range(15): f.setGeometry(QgsPoint(x, y)) vl.dataProvider().addFeature(f) vl.setLabeling(QgsVectorLayerSimpleLabeling(settings)) vl.setLabelsEnabled(True) p = QgsProject() engine_settings = QgsLabelingEngineSettings() engine_settings.setFlag(QgsLabelingEngineSettings.UsePartialCandidates, False) engine_settings.setFlag(QgsLabelingEngineSettings.DrawLabelRectOnly, True) p.setLabelingEngineSettings(engine_settings) p.addMapLayer(vl) layout = QgsLayout(p) layout.initializeDefaults() p.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(10, 10, 180, 180)) map.setFrameEnabled(True) map.zoomToExtent(vl.extent()) map.setLayers([vl]) layout.addLayoutItem(map) checker = QgsLayoutChecker('composermap_label_nomargin', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) map.setLabelMargin(QgsLayoutMeasurement(15, QgsUnitTypes.LayoutMillimeters)) checker = QgsLayoutChecker('composermap_label_margin', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) map.setLabelMargin(QgsLayoutMeasurement(3, QgsUnitTypes.LayoutCentimeters)) checker = QgsLayoutChecker('composermap_label_cm_margin', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) map.setMapRotation(45) map.zoomToExtent(vl.extent()) map.setScale(map.scale() * 1.2) checker = QgsLayoutChecker('composermap_rotated_label_margin', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) # data defined map.setMapRotation(0) map.zoomToExtent(vl.extent()) map.dataDefinedProperties().setProperty(QgsLayoutObject.MapLabelMargin, QgsProperty.fromExpression('1+3')) map.refresh() checker = QgsLayoutChecker('composermap_dd_label_margin', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) def testPartialLabels(self): """ Test rendering map item with a show partial labels flag """ format = QgsTextFormat() format.setFont(QgsFontUtils.getStandardTestFont("Bold")) format.setSize(20) format.setNamedStyle("Bold") format.setColor(QColor(0, 0, 0)) settings = QgsPalLayerSettings() settings.setFormat(format) settings.fieldName = "'X'" settings.isExpression = True settings.placement = QgsPalLayerSettings.OverPoint vl = QgsVectorLayer("Point?crs=epsg:4326&field=id:integer", "vl", "memory") vl.setRenderer(QgsNullSymbolRenderer()) f = QgsFeature(vl.fields(), 1) for x in range(15): for y in range(15): f.setGeometry(QgsPoint(x, y)) vl.dataProvider().addFeature(f) vl.setLabeling(QgsVectorLayerSimpleLabeling(settings)) vl.setLabelsEnabled(True) p = QgsProject() engine_settings = QgsLabelingEngineSettings() engine_settings.setFlag(QgsLabelingEngineSettings.UsePartialCandidates, False) engine_settings.setFlag(QgsLabelingEngineSettings.DrawLabelRectOnly, True) p.setLabelingEngineSettings(engine_settings) p.addMapLayer(vl) layout = QgsLayout(p) layout.initializeDefaults() p.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(10, 10, 180, 180)) map.setFrameEnabled(True) map.zoomToExtent(vl.extent()) map.setLayers([vl]) layout.addLayoutItem(map) # default should always be to hide partial labels self.assertFalse(map.mapFlags() & QgsLayoutItemMap.ShowPartialLabels) # hiding partial labels (the default) map.setMapFlags(QgsLayoutItemMap.MapItemFlags()) checker = QgsLayoutChecker('composermap_label_nomargin', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) # showing partial labels map.setMapFlags(QgsLayoutItemMap.ShowPartialLabels) checker = QgsLayoutChecker('composermap_show_partial_labels', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) def testBlockingItems(self): """ Test rendering map item with blocking items """ format = QgsTextFormat() format.setFont(QgsFontUtils.getStandardTestFont("Bold")) format.setSize(20) format.setNamedStyle("Bold") format.setColor(QColor(0, 0, 0)) settings = QgsPalLayerSettings() settings.setFormat(format) settings.fieldName = "'X'" settings.isExpression = True settings.placement = QgsPalLayerSettings.OverPoint vl = QgsVectorLayer("Point?crs=epsg:4326&field=id:integer", "vl", "memory") vl.setRenderer(QgsNullSymbolRenderer()) f = QgsFeature(vl.fields(), 1) for x in range(15): for y in range(15): f.setGeometry(QgsPoint(x, y)) vl.dataProvider().addFeature(f) vl.setLabeling(QgsVectorLayerSimpleLabeling(settings)) vl.setLabelsEnabled(True) p = QgsProject() engine_settings = QgsLabelingEngineSettings() engine_settings.setFlag(QgsLabelingEngineSettings.DrawLabelRectOnly, True) p.setLabelingEngineSettings(engine_settings) p.addMapLayer(vl) layout = QgsLayout(p) layout.initializeDefaults() p.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(10, 10, 180, 180)) map.setFrameEnabled(True) map.zoomToExtent(vl.extent()) map.setLayers([vl]) map.setId('map') layout.addLayoutItem(map) map2 = QgsLayoutItemMap(layout) map2.attemptSetSceneRect(QRectF(0, 5, 50, 80)) map2.setFrameEnabled(True) map2.setBackgroundEnabled(False) map2.setId('map2') layout.addLayoutItem(map2) map3 = QgsLayoutItemMap(layout) map3.attemptSetSceneRect(QRectF(150, 160, 50, 50)) map3.setFrameEnabled(True) map3.setBackgroundEnabled(False) map3.setId('map3') layout.addLayoutItem(map3) map.addLabelBlockingItem(map2) map.addLabelBlockingItem(map3) map.setMapFlags(QgsLayoutItemMap.MapItemFlags()) checker = QgsLayoutChecker('composermap_label_blockers', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) doc = QDomDocument("testdoc") elem = layout.writeXml(doc, QgsReadWriteContext()) l2 = QgsLayout(p) self.assertTrue(l2.readXml(elem, doc, QgsReadWriteContext())) map_restore = [i for i in l2.items() if isinstance(i, QgsLayoutItemMap) and i.id() == 'map'][0] map2_restore = [i for i in l2.items() if isinstance(i, QgsLayoutItemMap) and i.id() == 'map2'][0] map3_restore = [i for i in l2.items() if isinstance(i, QgsLayoutItemMap) and i.id() == 'map3'][0] self.assertTrue(map_restore.isLabelBlockingItem(map2_restore)) self.assertTrue(map_restore.isLabelBlockingItem(map3_restore))
class TestQgsLayoutMap(unittest.TestCase, LayoutItemTestCase): @classmethod def setUpClass(cls): cls.item_class = QgsLayoutItemMap def setUp(self): self.report = "<h1>Python QgsLayoutItemMap Tests</h1>\n" def tearDown(self): report_file_path = "%s/qgistest.html" % QDir.tempPath() with open(report_file_path, 'a') as report_file: report_file.write(self.report) def __init__(self, methodName): """Run once on class initialization.""" unittest.TestCase.__init__(self, methodName) myPath = os.path.join(TEST_DATA_DIR, 'rgb256x256.png') rasterFileInfo = QFileInfo(myPath) self.raster_layer = QgsRasterLayer(rasterFileInfo.filePath(), rasterFileInfo.completeBaseName()) rasterRenderer = QgsMultiBandColorRenderer( self.raster_layer.dataProvider(), 1, 2, 3) self.raster_layer.setRenderer(rasterRenderer) myPath = os.path.join(TEST_DATA_DIR, 'points.shp') vector_file_info = QFileInfo(myPath) self.vector_layer = QgsVectorLayer(vector_file_info.filePath(), vector_file_info.completeBaseName(), 'ogr') assert self.vector_layer.isValid() QgsProject.instance().addMapLayers([self.raster_layer, self.vector_layer]) # create layout with layout map self.layout = QgsLayout(QgsProject.instance()) self.layout.initializeDefaults() self.map = QgsLayoutItemMap(self.layout) self.map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) self.map.setFrameEnabled(True) self.map.setLayers([self.raster_layer]) self.layout.addLayoutItem(self.map) def testOverviewMap(self): overviewMap = QgsLayoutItemMap(self.layout) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) overviewMap.setFrameEnabled(True) overviewMap.setLayers([self.raster_layer]) self.layout.addLayoutItem(overviewMap) # zoom in myRectangle = QgsRectangle(96, -152, 160, -120) self.map.setExtent(myRectangle) myRectangle2 = QgsRectangle(0, -256, 256, 0) overviewMap.setExtent(myRectangle2) overviewMap.overview().setLinkedMap(self.map) checker = QgsLayoutChecker('composermap_overview', self.layout) checker.setColorTolerance(6) checker.setControlPathPrefix("composer_mapoverview") myTestResult, myMessage = checker.testLayout() self.report += checker.report() self.layout.removeLayoutItem(overviewMap) self.assertTrue(myTestResult, myMessage) def testOverviewMapBlend(self): overviewMap = QgsLayoutItemMap(self.layout) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) overviewMap.setFrameEnabled(True) overviewMap.setLayers([self.raster_layer]) self.layout.addLayoutItem(overviewMap) # zoom in myRectangle = QgsRectangle(96, -152, 160, -120) self.map.setExtent(myRectangle) myRectangle2 = QgsRectangle(0, -256, 256, 0) overviewMap.setExtent(myRectangle2) overviewMap.overview().setLinkedMap(self.map) overviewMap.overview().setBlendMode(QPainter.CompositionMode_Multiply) checker = QgsLayoutChecker('composermap_overview_blending', self.layout) checker.setControlPathPrefix("composer_mapoverview") myTestResult, myMessage = checker.testLayout() self.report += checker.report() self.layout.removeLayoutItem(overviewMap) self.assertTrue(myTestResult, myMessage) def testOverviewMapInvert(self): overviewMap = QgsLayoutItemMap(self.layout) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) overviewMap.setFrameEnabled(True) overviewMap.setLayers([self.raster_layer]) self.layout.addLayoutItem(overviewMap) # zoom in myRectangle = QgsRectangle(96, -152, 160, -120) self.map.setExtent(myRectangle) myRectangle2 = QgsRectangle(0, -256, 256, 0) overviewMap.setExtent(myRectangle2) overviewMap.overview().setLinkedMap(self.map) overviewMap.overview().setInverted(True) checker = QgsLayoutChecker('composermap_overview_invert', self.layout) checker.setControlPathPrefix("composer_mapoverview") myTestResult, myMessage = checker.testLayout() self.report += checker.report() self.layout.removeLayoutItem(overviewMap) self.assertTrue(myTestResult, myMessage) def testOverviewMapCenter(self): overviewMap = QgsLayoutItemMap(self.layout) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) overviewMap.setFrameEnabled(True) overviewMap.setLayers([self.raster_layer]) self.layout.addLayoutItem(overviewMap) # zoom in myRectangle = QgsRectangle(192, -288, 320, -224) self.map.setExtent(myRectangle) myRectangle2 = QgsRectangle(0, -256, 256, 0) overviewMap.setExtent(myRectangle2) overviewMap.overview().setLinkedMap(self.map) overviewMap.overview().setInverted(False) overviewMap.overview().setCentered(True) checker = QgsLayoutChecker('composermap_overview_center', self.layout) checker.setControlPathPrefix("composer_mapoverview") myTestResult, myMessage = checker.testLayout() self.report += checker.report() self.layout.removeLayoutItem(overviewMap) self.assertTrue(myTestResult, myMessage) def testAsMapLayer(self): l = QgsLayout(QgsProject.instance()) l.initializeDefaults() map = QgsLayoutItemMap(l) map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) l.addLayoutItem(map) overviewMap = QgsLayoutItemMap(l) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) l.addLayoutItem(overviewMap) # zoom in myRectangle = QgsRectangle(96, -152, 160, -120) map.setExtent(myRectangle) myRectangle2 = QgsRectangle(0, -256, 256, 0) overviewMap.setExtent(myRectangle2) overviewMap.overview().setLinkedMap(map) layer = overviewMap.overview().asMapLayer() self.assertIsNotNone(layer) self.assertTrue(layer.isValid()) self.assertEqual([f.geometry().asWkt() for f in layer.getFeatures()], ['Polygon ((96 -120, 160 -120, 160 -152, 96 -152, 96 -120))']) # check that layer has correct renderer fill_symbol = QgsFillSymbol.createSimple({'color': '#00ff00', 'outline_color': '#ff0000', 'outline_width': '10'}) overviewMap.overview().setFrameSymbol(fill_symbol) layer = overviewMap.overview().asMapLayer() self.assertIsInstance(layer.renderer(), QgsSingleSymbolRenderer) self.assertEqual(layer.renderer().symbol().symbolLayer(0).properties()['color'], '0,255,0,255') self.assertEqual(layer.renderer().symbol().symbolLayer(0).properties()['outline_color'], '255,0,0,255') # test layer blend mode self.assertEqual(layer.blendMode(), QPainter.CompositionMode_SourceOver) overviewMap.overview().setBlendMode(QPainter.CompositionMode_Clear) layer = overviewMap.overview().asMapLayer() self.assertEqual(layer.blendMode(), QPainter.CompositionMode_Clear) # should have no effect overviewMap.setMapRotation(45) layer = overviewMap.overview().asMapLayer() self.assertEqual([f.geometry().asWkt() for f in layer.getFeatures()], ['Polygon ((96 -120, 160 -120, 160 -152, 96 -152, 96 -120))']) map.setMapRotation(15) layer = overviewMap.overview().asMapLayer() self.assertEqual([f.geometry().asWkt(0) for f in layer.getFeatures()], ['Polygon ((93 -129, 155 -112, 163 -143, 101 -160, 93 -129))']) # with reprojection map.setCrs(QgsCoordinateReferenceSystem('EPSG:3875')) layer = overviewMap.overview().asMapLayer() self.assertEqual([f.geometry().asWkt(0) for f in layer.getFeatures()], ['Polygon ((93 -129, 96 -128, 99 -127, 102 -126, 105 -126, 108 -125, 111 -124, 114 -123, 116 -123, 119 -122, 122 -121, 125 -120, 128 -119, 131 -119, 134 -118, 137 -117, 140 -116, 143 -115, 146 -115, 149 -114, 152 -113, 155 -112, 155 -114, 156 -115, 156 -117, 156 -118, 157 -120, 157 -121, 158 -123, 158 -124, 158 -126, 159 -127, 159 -128, 160 -130, 160 -131, 160 -133, 161 -134, 161 -136, 161 -137, 162 -139, 162 -140, 163 -142, 163 -143, 160 -144, 157 -145, 154 -146, 151 -146, 148 -147, 145 -148, 142 -149, 140 -149, 137 -150, 134 -151, 131 -152, 128 -153, 125 -153, 122 -154, 119 -155, 116 -156, 113 -157, 110 -157, 107 -158, 104 -159, 101 -160, 101 -158, 100 -157, 100 -155, 100 -154, 99 -152, 99 -151, 98 -149, 98 -148, 98 -146, 97 -145, 97 -144, 96 -142, 96 -141, 96 -139, 95 -138, 95 -136, 95 -135, 94 -133, 94 -132, 93 -130, 93 -129))']) map.setCrs(overviewMap.crs()) # with invert overviewMap.overview().setInverted(True) layer = overviewMap.overview().asMapLayer() self.assertEqual([f.geometry().asWkt(0) for f in layer.getFeatures()], ['Polygon ((-53 -128, 128 53, 309 -128, 128 -309, -53 -128),(93 -129, 101 -160, 163 -143, 155 -112, 93 -129))']) def test_StackingPosition(self): l = QgsLayout(QgsProject.instance()) l.initializeDefaults() overviewMap = QgsLayoutItemMap(l) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) l.addLayoutItem(overviewMap) overviewMap.overview().setStackingPosition(QgsLayoutItemMapItem.StackBelowMap) self.assertEqual(overviewMap.overview().stackingPosition(), QgsLayoutItemMapItem.StackBelowMap) overviewMap.overview().setStackingPosition(QgsLayoutItemMapItem.StackBelowMapLayer) self.assertEqual(overviewMap.overview().stackingPosition(), QgsLayoutItemMapItem.StackBelowMapLayer) overviewMap.overview().setStackingLayer(self.raster_layer) self.assertEqual(overviewMap.overview().stackingLayer(), self.raster_layer) overviewMap.overview().setStackingLayer(self.vector_layer) self.assertEqual(overviewMap.overview().stackingLayer(), self.vector_layer) overviewMap.overview().setStackingLayer(None) self.assertIsNone(overviewMap.overview().stackingLayer()) def test_ModifyMapLayerList(self): l = QgsLayout(QgsProject.instance()) l.initializeDefaults() overviewMap = QgsLayoutItemMap(l) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) l.addLayoutItem(overviewMap) map = QgsLayoutItemMap(l) map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) l.addLayoutItem(map) self.assertFalse(overviewMap.overviews().modifyMapLayerList([])) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [self.raster_layer, self.vector_layer]) overviewMap.overview().setLinkedMap(map) overviewMap.overview().setStackingPosition(QgsLayoutItemMapItem.StackBelowMap) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [self.raster_layer, self.vector_layer, overviewMap.overview().asMapLayer()]) overviewMap.overview().setStackingPosition(QgsLayoutItemMapItem.StackBelowMapLayer) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [self.raster_layer, self.vector_layer]) overviewMap.overview().setStackingLayer(self.raster_layer) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [self.raster_layer, overviewMap.overview().asMapLayer(), self.vector_layer]) overviewMap.overview().setStackingLayer(self.vector_layer) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [self.raster_layer, self.vector_layer, overviewMap.overview().asMapLayer()]) overviewMap.overview().setStackingPosition(QgsLayoutItemMapItem.StackAboveMapLayer) overviewMap.overview().setStackingLayer(None) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [self.raster_layer, self.vector_layer]) overviewMap.overview().setStackingLayer(self.raster_layer) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [overviewMap.overview().asMapLayer(), self.raster_layer, self.vector_layer]) overviewMap.overview().setStackingLayer(self.vector_layer) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [self.raster_layer, overviewMap.overview().asMapLayer(), self.vector_layer]) overviewMap.overview().setStackingPosition(QgsLayoutItemMapItem.StackBelowMapLabels) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [overviewMap.overview().asMapLayer(), self.raster_layer, self.vector_layer]) overviewMap.overview().setStackingPosition(QgsLayoutItemMapItem.StackAboveMapLabels) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [self.raster_layer, self.vector_layer]) # two overviews overviewMap.overview().setStackingPosition(QgsLayoutItemMapItem.StackBelowMap) overviewMap.overviews().addOverview(QgsLayoutItemMapOverview('x', overviewMap)) overviewMap.overviews().overview(1).setLinkedMap(map) overviewMap.overviews().overview(1).setStackingPosition(QgsLayoutItemMapItem.StackBelowMapLabels) self.assertEqual(overviewMap.overviews().modifyMapLayerList([self.raster_layer, self.vector_layer]), [overviewMap.overviews().overview(1).asMapLayer(), self.raster_layer, self.vector_layer, overviewMap.overview().asMapLayer()]) def testOverviewStacking(self): l = QgsLayout(QgsProject.instance()) l.initializeDefaults() map = QgsLayoutItemMap(l) map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) map.setFrameEnabled(True) map.setLayers([self.raster_layer]) l.addLayoutItem(map) overviewMap = QgsLayoutItemMap(l) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) l.addLayoutItem(overviewMap) overviewMap.setFrameEnabled(True) overviewMap.setLayers([self.raster_layer]) # zoom in myRectangle = QgsRectangle(96, -152, 160, -120) map.setExtent(myRectangle) myRectangle2 = QgsRectangle(-20, -276, 276, 20) overviewMap.setExtent(myRectangle2) overviewMap.overview().setLinkedMap(map) overviewMap.overview().setInverted(True) overviewMap.overview().setStackingPosition(QgsLayoutItemMapItem.StackBelowMapLayer) overviewMap.overview().setStackingLayer(self.raster_layer) checker = QgsLayoutChecker('composermap_overview_belowmap', l) checker.setColorTolerance(6) checker.setControlPathPrefix("composer_mapoverview") myTestResult, myMessage = checker.testLayout() self.report += checker.report() self.assertTrue(myTestResult, myMessage) overviewMap.overview().setStackingPosition(QgsLayoutItemMapItem.StackAboveMapLayer) overviewMap.overview().setStackingLayer(self.raster_layer) checker = QgsLayoutChecker('composermap_overview_abovemap', l) checker.setColorTolerance(6) checker.setControlPathPrefix("composer_mapoverview") myTestResult, myMessage = checker.testLayout() self.report += checker.report() self.assertTrue(myTestResult, myMessage)
def test_ModifyMapLayerList(self): l = QgsLayout(QgsProject.instance()) l.initializeDefaults() overviewMap = QgsLayoutItemMap(l) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) l.addLayoutItem(overviewMap) map = QgsLayoutItemMap(l) map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) l.addLayoutItem(map) self.assertFalse(overviewMap.overviews().modifyMapLayerList([])) self.assertEqual( overviewMap.overviews().modifyMapLayerList( [self.raster_layer, self.vector_layer]), [self.raster_layer, self.vector_layer]) overviewMap.overview().setLinkedMap(map) overviewMap.overview().setStackingPosition( QgsLayoutItemMapItem.StackBelowMap) self.assertEqual( overviewMap.overviews().modifyMapLayerList( [self.raster_layer, self.vector_layer]), [ self.raster_layer, self.vector_layer, overviewMap.overview().asMapLayer() ]) overviewMap.overview().setStackingPosition( QgsLayoutItemMapItem.StackBelowMapLayer) self.assertEqual( overviewMap.overviews().modifyMapLayerList( [self.raster_layer, self.vector_layer]), [self.raster_layer, self.vector_layer]) overviewMap.overview().setStackingLayer(self.raster_layer) self.assertEqual( overviewMap.overviews().modifyMapLayerList( [self.raster_layer, self.vector_layer]), [ self.raster_layer, overviewMap.overview().asMapLayer(), self.vector_layer ]) overviewMap.overview().setStackingLayer(self.vector_layer) self.assertEqual( overviewMap.overviews().modifyMapLayerList( [self.raster_layer, self.vector_layer]), [ self.raster_layer, self.vector_layer, overviewMap.overview().asMapLayer() ]) overviewMap.overview().setStackingPosition( QgsLayoutItemMapItem.StackAboveMapLayer) overviewMap.overview().setStackingLayer(None) self.assertEqual( overviewMap.overviews().modifyMapLayerList( [self.raster_layer, self.vector_layer]), [self.raster_layer, self.vector_layer]) overviewMap.overview().setStackingLayer(self.raster_layer) self.assertEqual( overviewMap.overviews().modifyMapLayerList( [self.raster_layer, self.vector_layer]), [ overviewMap.overview().asMapLayer(), self.raster_layer, self.vector_layer ]) overviewMap.overview().setStackingLayer(self.vector_layer) self.assertEqual( overviewMap.overviews().modifyMapLayerList( [self.raster_layer, self.vector_layer]), [ self.raster_layer, overviewMap.overview().asMapLayer(), self.vector_layer ]) overviewMap.overview().setStackingPosition( QgsLayoutItemMapItem.StackBelowMapLabels) self.assertEqual( overviewMap.overviews().modifyMapLayerList( [self.raster_layer, self.vector_layer]), [ overviewMap.overview().asMapLayer(), self.raster_layer, self.vector_layer ]) overviewMap.overview().setStackingPosition( QgsLayoutItemMapItem.StackAboveMapLabels) self.assertEqual( overviewMap.overviews().modifyMapLayerList( [self.raster_layer, self.vector_layer]), [self.raster_layer, self.vector_layer]) # two overviews overviewMap.overview().setStackingPosition( QgsLayoutItemMapItem.StackBelowMap) overviewMap.overviews().addOverview( QgsLayoutItemMapOverview('x', overviewMap)) overviewMap.overviews().overview(1).setLinkedMap(map) overviewMap.overviews().overview(1).setStackingPosition( QgsLayoutItemMapItem.StackBelowMapLabels) self.assertEqual( overviewMap.overviews().modifyMapLayerList( [self.raster_layer, self.vector_layer]), [ overviewMap.overviews().overview(1).asMapLayer(), self.raster_layer, self.vector_layer, overviewMap.overview().asMapLayer() ])
def testPartialLabels(self): """ Test rendering map item with a show partial labels flag """ format = QgsTextFormat() format.setFont(QgsFontUtils.getStandardTestFont("Bold")) format.setSize(20) format.setNamedStyle("Bold") format.setColor(QColor(0, 0, 0)) settings = QgsPalLayerSettings() settings.setFormat(format) settings.fieldName = "'X'" settings.isExpression = True settings.placement = QgsPalLayerSettings.OverPoint vl = QgsVectorLayer("Point?crs=epsg:4326&field=id:integer", "vl", "memory") vl.setRenderer(QgsNullSymbolRenderer()) f = QgsFeature(vl.fields(), 1) for x in range(15): for y in range(15): f.setGeometry(QgsPoint(x, y)) vl.dataProvider().addFeature(f) vl.setLabeling(QgsVectorLayerSimpleLabeling(settings)) vl.setLabelsEnabled(True) p = QgsProject() engine_settings = QgsLabelingEngineSettings() engine_settings.setFlag(QgsLabelingEngineSettings.UsePartialCandidates, False) engine_settings.setFlag(QgsLabelingEngineSettings.DrawLabelRectOnly, True) p.setLabelingEngineSettings(engine_settings) p.addMapLayer(vl) layout = QgsLayout(p) layout.initializeDefaults() p.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(10, 10, 180, 180)) map.setFrameEnabled(True) map.zoomToExtent(vl.extent()) map.setLayers([vl]) layout.addLayoutItem(map) # default should always be to hide partial labels self.assertFalse(map.mapFlags() & QgsLayoutItemMap.ShowPartialLabels) # hiding partial labels (the default) map.setMapFlags(QgsLayoutItemMap.MapItemFlags()) checker = QgsLayoutChecker('composermap_label_nomargin', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) # showing partial labels map.setMapFlags(QgsLayoutItemMap.ShowPartialLabels) checker = QgsLayoutChecker('composermap_show_partial_labels', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message)
def testCopyPaste(self): p = QgsProject() l = QgsLayout(p) # clear clipboard mime_data = QMimeData() mime_data.setData("text/xml", QByteArray()) clipboard = QApplication.clipboard() clipboard.setMimeData(mime_data) # add an item item1 = QgsLayoutItemLabel(l) item1.setText('label 1') l.addLayoutItem(item1) item1.setSelected(True) item2 = QgsLayoutItemLabel(l) item2.setText('label 2') l.addLayoutItem(item2) item2.setSelected(True) # multiframes multiframe1 = QgsLayoutItemHtml(l) multiframe1.setHtml('mf1') l.addMultiFrame(multiframe1) frame1 = QgsLayoutFrame(l, multiframe1) frame1.setId('frame1a') multiframe1.addFrame(frame1) frame1b = QgsLayoutFrame(l, multiframe1) frame1b.setId('frame1b') multiframe1.addFrame(frame1b) # not selected frame1c = QgsLayoutFrame(l, multiframe1) frame1c.setId('frame1b') multiframe1.addFrame(frame1c) # not selected multiframe2 = QgsLayoutItemHtml(l) multiframe2.setHtml('mf2') l.addMultiFrame(multiframe2) frame2 = QgsLayoutFrame(l, multiframe2) frame2.setId('frame2') multiframe2.addFrame(frame2) frame1.setSelected(True) frame2.setSelected(True) view = QgsLayoutView() view.setCurrentLayout(l) self.assertFalse(view.hasItemsInClipboard()) view.copySelectedItems(QgsLayoutView.ClipboardCopy) self.assertTrue(view.hasItemsInClipboard()) pasted = view.pasteItems(QgsLayoutView.PasteModeCursor) self.assertEqual(len(pasted), 4) new_multiframes = [ m for m in l.multiFrames() if m not in [multiframe1, multiframe2] ] self.assertEqual(len(new_multiframes), 2) self.assertIn(pasted[0], l.items()) self.assertIn(pasted[1], l.items()) labels = [ p for p in pasted if p.type() == QgsLayoutItemRegistry.LayoutLabel ] self.assertIn( sip.cast(labels[0], QgsLayoutItemLabel).text(), ('label 1', 'label 2')) self.assertIn( sip.cast(labels[1], QgsLayoutItemLabel).text(), ('label 1', 'label 2')) frames = [ p for p in pasted if p.type() == QgsLayoutItemRegistry.LayoutFrame ] pasted_frame1 = sip.cast(frames[0], QgsLayoutFrame) pasted_frame2 = sip.cast(frames[1], QgsLayoutFrame) self.assertIn(pasted_frame1.multiFrame(), new_multiframes) self.assertIn(new_multiframes[0].frames()[0].uuid(), (pasted_frame1.uuid(), pasted_frame2.uuid())) self.assertIn(pasted_frame2.multiFrame(), new_multiframes) self.assertIn(new_multiframes[1].frames()[0].uuid(), (pasted_frame1.uuid(), pasted_frame2.uuid())) self.assertEqual(frame1.multiFrame(), multiframe1) self.assertCountEqual(multiframe1.frames(), [frame1, frame1b, frame1c]) self.assertEqual(frame1b.multiFrame(), multiframe1) self.assertEqual(frame1c.multiFrame(), multiframe1) self.assertEqual(frame2.multiFrame(), multiframe2) self.assertCountEqual(multiframe2.frames(), [frame2]) # copy specific item view.copyItems([item2], QgsLayoutView.ClipboardCopy) l2 = QgsLayout(p) view2 = QgsLayoutView() view2.setCurrentLayout(l2) pasted = view2.pasteItems(QgsLayoutView.PasteModeCursor) self.assertEqual(len(pasted), 1) self.assertIn(pasted[0], l2.items()) self.assertEqual( sip.cast(pasted[0], QgsLayoutItemLabel).text(), 'label 2')
def testAddItemsFromXml(self): p = QgsProject() l = QgsLayout(p) # add some items item1 = QgsLayoutItemLabel(l) item1.setId('xxyyxx') item1.attemptMove(QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) item1.attemptResize(QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) l.addItem(item1) item2 = QgsLayoutItemLabel(l) item2.setId('zzyyzz') item2.attemptMove(QgsLayoutPoint(1.4, 1.8, QgsUnitTypes.LayoutCentimeters)) item2.attemptResize(QgsLayoutSize(2.8, 2.2, QgsUnitTypes.LayoutCentimeters)) l.addItem(item2) doc = QDomDocument("testdoc") # store in xml elem = l.writeXml(doc, QgsReadWriteContext()) l2 = QgsLayout(p) new_items = l2.addItemsFromXml(elem, doc, QgsReadWriteContext()) self.assertEqual(len(new_items), 2) items = l2.items() self.assertTrue([i for i in items if i.id() == 'xxyyxx']) self.assertTrue([i for i in items if i.id() == 'zzyyzz']) self.assertTrue(new_items[0] in l2.items()) self.assertTrue(new_items[1] in l2.items()) new_item1 = [i for i in items if i.id() == 'xxyyxx'][0] new_item2 = [i for i in items if i.id() == 'zzyyzz'][0] self.assertEqual(new_item1.positionWithUnits(), QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(new_item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(new_item2.positionWithUnits(), QgsLayoutPoint(1.4, 1.8, QgsUnitTypes.LayoutCentimeters)) self.assertEqual(new_item2.sizeWithUnits(), QgsLayoutSize(2.8, 2.2, QgsUnitTypes.LayoutCentimeters)) # test with a group group = QgsLayoutItemGroup(l) group.addItem(item1) group.addItem(item2) l.addLayoutItem(group) elem = l.writeXml(doc, QgsReadWriteContext()) l3 = QgsLayout(p) new_items = l3.addItemsFromXml(elem, doc, QgsReadWriteContext()) self.assertEqual(len(new_items), 3) items = l3.items() self.assertTrue([i for i in items if i.id() == 'xxyyxx']) self.assertTrue([i for i in items if i.id() == 'zzyyzz']) self.assertTrue(new_items[0] in l3.items()) self.assertTrue(new_items[1] in l3.items()) self.assertTrue(new_items[2] in l3.items()) # f*** you sip, I'll just manually cast new_group = sip.cast(l3.itemByUuid(group.uuid()), QgsLayoutItemGroup) self.assertIsNotNone(new_group) other_items = [i for i in new_items if i.type() != new_group.type()] self.assertCountEqual(new_group.items(), other_items) # test restoring at set position l3 = QgsLayout(p) new_items = l3.addItemsFromXml(elem, doc, QgsReadWriteContext(), QPointF(10, 30)) self.assertEqual(len(new_items), 3) items = l3.items() new_item1 = [i for i in items if i.id() == 'xxyyxx'][0] new_item2 = [i for i in items if i.id() == 'zzyyzz'][0] self.assertEqual(new_item1.positionWithUnits(), QgsLayoutPoint(10, 30, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(new_item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(new_item2.positionWithUnits(), QgsLayoutPoint(2.0, 4.0, QgsUnitTypes.LayoutCentimeters)) self.assertEqual(new_item2.sizeWithUnits(), QgsLayoutSize(2.8, 2.2, QgsUnitTypes.LayoutCentimeters)) # paste in place l4 = QgsLayout(p) page = QgsLayoutItemPage(l) page.setPageSize('A3') l4.pageCollection().addPage(page) page = QgsLayoutItemPage(l) page.setPageSize('A6') l4.pageCollection().addPage(page) new_items = l4.addItemsFromXml(elem, doc, QgsReadWriteContext(), QPointF(10, 30), True) self.assertEqual(len(new_items), 3) new_item1 = [i for i in new_items if i.id() == 'xxyyxx'][0] new_item2 = [i for i in new_items if i.id() == 'zzyyzz'][0] self.assertEqual(new_item1.pagePositionWithUnits(), QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(new_item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(new_item1.page(), 0) self.assertEqual(new_item2.pagePositionWithUnits(), QgsLayoutPoint(1.4, 1.8, QgsUnitTypes.LayoutCentimeters)) self.assertEqual(new_item2.sizeWithUnits(), QgsLayoutSize(2.8, 2.2, QgsUnitTypes.LayoutCentimeters)) self.assertEqual(new_item2.page(), 0) # paste in place, page 2 new_items = l4.addItemsFromXml(elem, doc, QgsReadWriteContext(), QPointF(10, 550), True) self.assertEqual(len(new_items), 3) new_item1 = [i for i in new_items if i.id() == 'xxyyxx'][0] new_item2 = [i for i in new_items if i.id() == 'zzyyzz'][0] self.assertEqual(new_item1.pagePositionWithUnits(), QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(new_item1.page(), 1) self.assertEqual(new_item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(new_item2.pagePositionWithUnits(), QgsLayoutPoint(1.4, 1.8, QgsUnitTypes.LayoutCentimeters)) self.assertEqual(new_item2.page(), 1) self.assertEqual(new_item2.sizeWithUnits(), QgsLayoutSize(2.8, 2.2, QgsUnitTypes.LayoutCentimeters))
def testLabelMargin(self): """ Test rendering map item with a label margin set """ format = QgsTextFormat() format.setFont(QgsFontUtils.getStandardTestFont("Bold")) format.setSize(20) format.setNamedStyle("Bold") format.setColor(QColor(0, 0, 0)) settings = QgsPalLayerSettings() settings.setFormat(format) settings.fieldName = "'X'" settings.isExpression = True settings.placement = QgsPalLayerSettings.OverPoint vl = QgsVectorLayer("Point?crs=epsg:4326&field=id:integer", "vl", "memory") vl.setRenderer(QgsNullSymbolRenderer()) f = QgsFeature(vl.fields(), 1) for x in range(15): for y in range(15): f.setGeometry(QgsPoint(x, y)) vl.dataProvider().addFeature(f) vl.setLabeling(QgsVectorLayerSimpleLabeling(settings)) vl.setLabelsEnabled(True) p = QgsProject() engine_settings = QgsLabelingEngineSettings() engine_settings.setFlag(QgsLabelingEngineSettings.UsePartialCandidates, False) engine_settings.setFlag(QgsLabelingEngineSettings.DrawLabelRectOnly, True) p.setLabelingEngineSettings(engine_settings) p.addMapLayer(vl) layout = QgsLayout(p) layout.initializeDefaults() p.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(10, 10, 180, 180)) map.setFrameEnabled(True) map.zoomToExtent(vl.extent()) map.setLayers([vl]) layout.addLayoutItem(map) checker = QgsLayoutChecker('composermap_label_nomargin', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) map.setLabelMargin( QgsLayoutMeasurement(15, QgsUnitTypes.LayoutMillimeters)) checker = QgsLayoutChecker('composermap_label_margin', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) map.setLabelMargin( QgsLayoutMeasurement(3, QgsUnitTypes.LayoutCentimeters)) checker = QgsLayoutChecker('composermap_label_cm_margin', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) map.setMapRotation(45) map.zoomToExtent(vl.extent()) map.setScale(map.scale() * 1.2) checker = QgsLayoutChecker('composermap_rotated_label_margin', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) # data defined map.setMapRotation(0) map.zoomToExtent(vl.extent()) map.dataDefinedProperties().setProperty( QgsLayoutObject.MapLabelMargin, QgsProperty.fromExpression('1+3')) map.refresh() checker = QgsLayoutChecker('composermap_dd_label_margin', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message)
class TestQgsComposerMap(unittest.TestCase): def __init__(self, methodName): """Run once on class initialization.""" unittest.TestCase.__init__(self, methodName) myPath = os.path.join(TEST_DATA_DIR, 'rgb256x256.png') rasterFileInfo = QFileInfo(myPath) self.raster_layer = QgsRasterLayer(rasterFileInfo.filePath(), rasterFileInfo.completeBaseName()) rasterRenderer = QgsMultiBandColorRenderer( self.raster_layer.dataProvider(), 1, 2, 3) self.raster_layer.setRenderer(rasterRenderer) myPath = os.path.join(TEST_DATA_DIR, 'points.shp') vector_file_info = QFileInfo(myPath) self.vector_layer = QgsVectorLayer(vector_file_info.filePath(), vector_file_info.completeBaseName(), 'ogr') assert self.vector_layer.isValid() # pipe = mRasterLayer.pipe() # assert pipe.set(rasterRenderer), 'Cannot set pipe renderer' QgsProject.instance().addMapLayers([self.raster_layer, self.vector_layer]) # create composition with composer map self.layout = QgsLayout(QgsProject.instance()) self.layout.initializeDefaults() self.map = QgsLayoutItemMap(self.layout) self.map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) self.map.setFrameEnabled(True) self.map.setLayers([self.raster_layer]) self.layout.addLayoutItem(self.map) def testOverviewMap(self): overviewMap = QgsLayoutItemMap(self.layout) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) overviewMap.setFrameEnabled(True) overviewMap.setLayers([self.raster_layer]) self.layout.addLayoutItem(overviewMap) # zoom in myRectangle = QgsRectangle(96, -152, 160, -120) self.map.setExtent(myRectangle) myRectangle2 = QgsRectangle(0, -256, 256, 0) overviewMap.setExtent(myRectangle2) overviewMap.overview().setFrameMap(self.map) checker = QgsLayoutChecker('composermap_overview', self.layout) checker.setColorTolerance(6) checker.setControlPathPrefix("composer_mapoverview") myTestResult, myMessage = checker.testLayout() self.layout.removeLayoutItem(overviewMap) assert myTestResult, myMessage def testOverviewMapBlend(self): overviewMap = QgsLayoutItemMap(self.layout) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) overviewMap.setFrameEnabled(True) overviewMap.setLayers([self.raster_layer]) self.layout.addLayoutItem(overviewMap) # zoom in myRectangle = QgsRectangle(96, -152, 160, -120) self.map.setExtent(myRectangle) myRectangle2 = QgsRectangle(0, -256, 256, 0) overviewMap.setExtent(myRectangle2) overviewMap.overview().setFrameMap(self.map) overviewMap.overview().setBlendMode(QPainter.CompositionMode_Multiply) checker = QgsLayoutChecker('composermap_overview_blending', self.layout) checker.setControlPathPrefix("composer_mapoverview") myTestResult, myMessage = checker.testLayout() self.layout.removeLayoutItem(overviewMap) assert myTestResult, myMessage def testOverviewMapInvert(self): overviewMap = QgsLayoutItemMap(self.layout) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) overviewMap.setFrameEnabled(True) overviewMap.setLayers([self.raster_layer]) self.layout.addLayoutItem(overviewMap) # zoom in myRectangle = QgsRectangle(96, -152, 160, -120) self.map.setExtent(myRectangle) myRectangle2 = QgsRectangle(0, -256, 256, 0) overviewMap.setExtent(myRectangle2) overviewMap.overview().setFrameMap(self.map) overviewMap.overview().setInverted(True) checker = QgsLayoutChecker('composermap_overview_invert', self.layout) checker.setControlPathPrefix("composer_mapoverview") myTestResult, myMessage = checker.testLayout() self.layout.removeLayoutItem(overviewMap) assert myTestResult, myMessage def testOverviewMapCenter(self): overviewMap = QgsLayoutItemMap(self.layout) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) overviewMap.setFrameEnabled(True) overviewMap.setLayers([self.raster_layer]) self.layout.addLayoutItem(overviewMap) # zoom in myRectangle = QgsRectangle(192, -288, 320, -224) self.map.setExtent(myRectangle) myRectangle2 = QgsRectangle(0, -256, 256, 0) overviewMap.setExtent(myRectangle2) overviewMap.overview().setFrameMap(self.map) overviewMap.overview().setInverted(False) overviewMap.overview().setCentered(True) checker = QgsLayoutChecker('composermap_overview_center', self.layout) checker.setControlPathPrefix("composer_mapoverview") myTestResult, myMessage = checker.testLayout() self.layout.removeLayoutItem(overviewMap) assert myTestResult, myMessage def testMapCrs(self): # create composition with composer map map_settings = QgsMapSettings() map_settings.setLayers([self.vector_layer]) layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() # check that new maps inherit project CRS QgsProject.instance().setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) map.setFrameEnabled(True) rectangle = QgsRectangle(-13838977, 2369660, -8672298, 6250909) map.setExtent(rectangle) map.setLayers([self.vector_layer]) layout.addLayoutItem(map) self.assertEqual(map.crs().authid(), 'EPSG:4326') self.assertFalse(map.presetCrs().isValid()) # overwrite CRS map.setCrs(QgsCoordinateReferenceSystem('EPSG:3857')) self.assertEqual(map.crs().authid(), 'EPSG:3857') self.assertEqual(map.presetCrs().authid(), 'EPSG:3857') checker = QgsLayoutChecker('composermap_crs3857', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.assertTrue(result, message) # overwrite CRS map.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) self.assertEqual(map.presetCrs().authid(), 'EPSG:4326') self.assertEqual(map.crs().authid(), 'EPSG:4326') rectangle = QgsRectangle(-124, 17, -78, 52) map.zoomToExtent(rectangle) checker = QgsLayoutChecker('composermap_crs4326', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.assertTrue(result, message) # change back to project CRS map.setCrs(QgsCoordinateReferenceSystem()) self.assertEqual(map.crs().authid(), 'EPSG:4326') self.assertFalse(map.presetCrs().isValid()) def testuniqueId(self): return doc = QDomDocument() documentElement = doc.createElement('ComposerItemClipboard') self.layout.writeXml(documentElement, doc) self.layout.addItemsFromXml(documentElement, doc) # test if both composer maps have different ids newMap = QgsComposerMap(self.layout, 0, 0, 10, 10) mapList = self.layout.composerMapItems() for mapIt in mapList: if mapIt != self.map: newMap = mapIt break oldId = self.map.id() newId = newMap.id() self.layout.removeComposerItem(newMap) myMessage = 'old: %s new: %s' % (oldId, newId) assert oldId != newId, myMessage def testWorldFileGeneration(self): return myRectangle = QgsRectangle(781662.375, 3339523.125, 793062.375, 3345223.125) self.map.setNewExtent(myRectangle) self.map.setMapRotation(30.0) self.layout.setGenerateWorldFile(True) self.layout.setReferenceMap(self.map) p = self.layout.computeWorldFileParameters() pexpected = (4.180480199790922, 2.4133064516129026, 779443.7612381146, 2.4136013686911886, -4.179969388427311, 3342408.5663611) ptolerance = (0.001, 0.001, 1, 0.001, 0.001, 1e+03) for i in range(0, 6): assert abs(p[i] - pexpected[i]) < ptolerance[i]
class TestQgsLayoutMap(unittest.TestCase, LayoutItemTestCase): @classmethod def setUpClass(cls): cls.item_class = QgsLayoutItemMap def setUp(self): self.report = "<h1>Python QgsLayoutItemMap Tests</h1>\n" def tearDown(self): report_file_path = "%s/qgistest.html" % QDir.tempPath() with open(report_file_path, 'a') as report_file: report_file.write(self.report) def __init__(self, methodName): """Run once on class initialization.""" unittest.TestCase.__init__(self, methodName) myPath = os.path.join(TEST_DATA_DIR, 'rgb256x256.png') rasterFileInfo = QFileInfo(myPath) self.raster_layer = QgsRasterLayer(rasterFileInfo.filePath(), rasterFileInfo.completeBaseName()) rasterRenderer = QgsMultiBandColorRenderer( self.raster_layer.dataProvider(), 1, 2, 3) self.raster_layer.setRenderer(rasterRenderer) myPath = os.path.join(TEST_DATA_DIR, 'points.shp') vector_file_info = QFileInfo(myPath) self.vector_layer = QgsVectorLayer(vector_file_info.filePath(), vector_file_info.completeBaseName(), 'ogr') assert self.vector_layer.isValid() # pipe = mRasterLayer.pipe() # assert pipe.set(rasterRenderer), 'Cannot set pipe renderer' QgsProject.instance().addMapLayers( [self.raster_layer, self.vector_layer]) # create layout with layout map self.layout = QgsLayout(QgsProject.instance()) self.layout.initializeDefaults() self.map = QgsLayoutItemMap(self.layout) self.map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) self.map.setFrameEnabled(True) self.map.setLayers([self.raster_layer]) self.layout.addLayoutItem(self.map) def testOverviewMap(self): overviewMap = QgsLayoutItemMap(self.layout) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) overviewMap.setFrameEnabled(True) overviewMap.setLayers([self.raster_layer]) self.layout.addLayoutItem(overviewMap) # zoom in myRectangle = QgsRectangle(96, -152, 160, -120) self.map.setExtent(myRectangle) myRectangle2 = QgsRectangle(0, -256, 256, 0) overviewMap.setExtent(myRectangle2) overviewMap.overview().setLinkedMap(self.map) checker = QgsLayoutChecker('composermap_overview', self.layout) checker.setColorTolerance(6) checker.setControlPathPrefix("composer_mapoverview") myTestResult, myMessage = checker.testLayout() self.report += checker.report() self.layout.removeLayoutItem(overviewMap) assert myTestResult, myMessage def testOverviewMapBlend(self): overviewMap = QgsLayoutItemMap(self.layout) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) overviewMap.setFrameEnabled(True) overviewMap.setLayers([self.raster_layer]) self.layout.addLayoutItem(overviewMap) # zoom in myRectangle = QgsRectangle(96, -152, 160, -120) self.map.setExtent(myRectangle) myRectangle2 = QgsRectangle(0, -256, 256, 0) overviewMap.setExtent(myRectangle2) overviewMap.overview().setLinkedMap(self.map) overviewMap.overview().setBlendMode(QPainter.CompositionMode_Multiply) checker = QgsLayoutChecker('composermap_overview_blending', self.layout) checker.setControlPathPrefix("composer_mapoverview") myTestResult, myMessage = checker.testLayout() self.report += checker.report() self.layout.removeLayoutItem(overviewMap) assert myTestResult, myMessage def testOverviewMapInvert(self): overviewMap = QgsLayoutItemMap(self.layout) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) overviewMap.setFrameEnabled(True) overviewMap.setLayers([self.raster_layer]) self.layout.addLayoutItem(overviewMap) # zoom in myRectangle = QgsRectangle(96, -152, 160, -120) self.map.setExtent(myRectangle) myRectangle2 = QgsRectangle(0, -256, 256, 0) overviewMap.setExtent(myRectangle2) overviewMap.overview().setLinkedMap(self.map) overviewMap.overview().setInverted(True) checker = QgsLayoutChecker('composermap_overview_invert', self.layout) checker.setControlPathPrefix("composer_mapoverview") myTestResult, myMessage = checker.testLayout() self.report += checker.report() self.layout.removeLayoutItem(overviewMap) assert myTestResult, myMessage def testOverviewMapCenter(self): overviewMap = QgsLayoutItemMap(self.layout) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) overviewMap.setFrameEnabled(True) overviewMap.setLayers([self.raster_layer]) self.layout.addLayoutItem(overviewMap) # zoom in myRectangle = QgsRectangle(192, -288, 320, -224) self.map.setExtent(myRectangle) myRectangle2 = QgsRectangle(0, -256, 256, 0) overviewMap.setExtent(myRectangle2) overviewMap.overview().setLinkedMap(self.map) overviewMap.overview().setInverted(False) overviewMap.overview().setCentered(True) checker = QgsLayoutChecker('composermap_overview_center', self.layout) checker.setControlPathPrefix("composer_mapoverview") myTestResult, myMessage = checker.testLayout() self.report += checker.report() self.layout.removeLayoutItem(overviewMap) assert myTestResult, myMessage def testMapCrs(self): # create layout with layout map map_settings = QgsMapSettings() map_settings.setLayers([self.vector_layer]) layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() # check that new maps inherit project CRS QgsProject.instance().setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) map.setFrameEnabled(True) rectangle = QgsRectangle(-13838977, 2369660, -8672298, 6250909) map.setExtent(rectangle) map.setLayers([self.vector_layer]) layout.addLayoutItem(map) self.assertEqual(map.crs().authid(), 'EPSG:4326') self.assertFalse(map.presetCrs().isValid()) # overwrite CRS map.setCrs(QgsCoordinateReferenceSystem('EPSG:3857')) self.assertEqual(map.crs().authid(), 'EPSG:3857') self.assertEqual(map.presetCrs().authid(), 'EPSG:3857') checker = QgsLayoutChecker('composermap_crs3857', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) # overwrite CRS map.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) self.assertEqual(map.presetCrs().authid(), 'EPSG:4326') self.assertEqual(map.crs().authid(), 'EPSG:4326') rectangle = QgsRectangle(-124, 17, -78, 52) map.zoomToExtent(rectangle) checker = QgsLayoutChecker('composermap_crs4326', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) # change back to project CRS map.setCrs(QgsCoordinateReferenceSystem()) self.assertEqual(map.crs().authid(), 'EPSG:4326') self.assertFalse(map.presetCrs().isValid()) def testContainsAdvancedEffects(self): map_settings = QgsMapSettings() map_settings.setLayers([self.vector_layer]) layout = QgsLayout(QgsProject.instance()) map = QgsLayoutItemMap(layout) self.assertFalse(map.containsAdvancedEffects()) self.vector_layer.setBlendMode(QPainter.CompositionMode_Darken) result = map.containsAdvancedEffects() self.vector_layer.setBlendMode(QPainter.CompositionMode_SourceOver) self.assertTrue(result) def testRasterization(self): map_settings = QgsMapSettings() map_settings.setLayers([self.vector_layer]) layout = QgsLayout(QgsProject.instance()) map = QgsLayoutItemMap(layout) self.assertFalse(map.requiresRasterization()) self.vector_layer.setBlendMode(QPainter.CompositionMode_Darken) self.assertFalse(map.requiresRasterization()) self.assertTrue(map.containsAdvancedEffects()) map.setBackgroundEnabled(False) self.assertTrue(map.requiresRasterization()) map.setBackgroundEnabled(True) map.setBackgroundColor(QColor(1, 1, 1, 1)) self.assertTrue(map.requiresRasterization()) self.vector_layer.setBlendMode(QPainter.CompositionMode_SourceOver)
def testResizeToContents(self): p = QgsProject() l = QgsLayout(p) shape1 = QgsLayoutItemShape(l) shape1.attemptResize(QgsLayoutSize(90, 50)) shape1.attemptMove(QgsLayoutPoint(90, 50)) shape1.setItemRotation(45, False) l.addLayoutItem(shape1) shape2 = QgsLayoutItemShape(l) shape2.attemptResize(QgsLayoutSize(110, 50)) shape2.attemptMove(QgsLayoutPoint(100, 150), True, False, 0) l.addLayoutItem(shape2) shape3 = QgsLayoutItemShape(l) l.addLayoutItem(shape3) shape3.attemptResize(QgsLayoutSize(50, 100)) shape3.attemptMove(QgsLayoutPoint(210, 250), True, False, 0) shape4 = QgsLayoutItemShape(l) l.addLayoutItem(shape4) shape4.attemptResize(QgsLayoutSize(50, 30)) shape4.attemptMove(QgsLayoutPoint(10, 340), True, False, 0) shape4.setVisibility(False) # resize with no existing pages l.pageCollection().resizeToContents(QgsMargins(1, 2, 3, 4), QgsUnitTypes.LayoutCentimeters) self.assertEqual(l.pageCollection().pageCount(), 1) self.assertAlmostEqual( l.pageCollection().page(0).sizeWithUnits().width(), 290.3, 2) self.assertAlmostEqual( l.pageCollection().page(0).sizeWithUnits().height(), 380.36, 2) self.assertAlmostEqual( l.pageCollection().page(0).sizeWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertAlmostEqual(shape1.positionWithUnits().x(), 90.15, 2) self.assertAlmostEqual(shape1.positionWithUnits().y(), 20.21, 2) self.assertAlmostEqual(shape2.positionWithUnits().x(), 100.15, 2) self.assertAlmostEqual(shape2.positionWithUnits().y(), 120.21, 2) self.assertAlmostEqual(shape3.positionWithUnits().x(), 210.15, 2) self.assertAlmostEqual(shape3.positionWithUnits().y(), 220.21, 2) self.assertAlmostEqual(shape4.positionWithUnits().x(), 10.15, 2) self.assertAlmostEqual(shape4.positionWithUnits().y(), 310.21, 2) # add a second page page2 = QgsLayoutItemPage(l) page2.setPageSize("A4", QgsLayoutItemPage.Landscape) l.pageCollection().addPage(page2) # add some guides g1 = QgsLayoutGuide( Qt.Horizontal, QgsLayoutMeasurement(2.5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0)) l.guides().addGuide(g1) g2 = QgsLayoutGuide( Qt.Vertical, QgsLayoutMeasurement(4.5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0)) l.guides().addGuide(g2) # second page should be removed l.pageCollection().resizeToContents(QgsMargins(0, 0, 0, 0), QgsUnitTypes.LayoutCentimeters) self.assertEqual(l.pageCollection().pageCount(), 1) self.assertAlmostEqual( l.pageCollection().page(0).sizeWithUnits().width(), 250.3, 2) self.assertAlmostEqual( l.pageCollection().page(0).sizeWithUnits().height(), 320.36, 2) self.assertAlmostEqual( l.pageCollection().page(0).sizeWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertAlmostEqual(g1.position().length(), 0.5, 2) self.assertAlmostEqual(g2.position().length(), 3.5, 2)
class TestQgsLayoutPolygon(unittest.TestCase, LayoutItemTestCase): @classmethod def setUpClass(cls): cls.item_class = QgsLayoutItemPolygon def __init__(self, methodName): """Run once on class initialization.""" unittest.TestCase.__init__(self, methodName) # create composition self.layout = QgsLayout(QgsProject.instance()) self.layout.initializeDefaults() # create polygon = QPolygonF() polygon.append(QPointF(0.0, 0.0)) polygon.append(QPointF(100.0, 0.0)) polygon.append(QPointF(200.0, 100.0)) polygon.append(QPointF(100.0, 200.0)) self.polygon = QgsLayoutItemPolygon(polygon, self.layout) self.layout.addLayoutItem(self.polygon) # style props = {} props["color"] = "green" props["style"] = "solid" props["style_border"] = "solid" props["color_border"] = "black" props["width_border"] = "10.0" props["joinstyle"] = "miter" style = QgsFillSymbol.createSimple(props) self.polygon.setSymbol(style) def testNodes(self): polygon = QPolygonF() polygon.append(QPointF(0.0, 0.0)) polygon.append(QPointF(100.0, 0.0)) polygon.append(QPointF(200.0, 100.0)) polygon.append(QPointF(100.0, 200.0)) p = QgsLayoutItemPolygon(polygon, self.layout) self.assertEqual(p.nodes(), polygon) polygon = QPolygonF() polygon.append(QPointF(0.0, 0.0)) polygon.append(QPointF(1000.0, 0.0)) polygon.append(QPointF(2000.0, 100.0)) polygon.append(QPointF(1000.0, 200.0)) p.setNodes(polygon) self.assertEqual(p.nodes(), polygon) def testDisplayName(self): """Test if displayName is valid""" self.assertEqual(self.polygon.displayName(), "<Polygon>") def testType(self): """Test if type is valid""" self.assertEqual(self.polygon.type(), QgsLayoutItemRegistry.LayoutPolygon) def testDefaultStyle(self): """Test polygon rendering with default style.""" self.polygon.setDisplayNodes(False) checker = QgsLayoutChecker('composerpolygon_defaultstyle', self.layout) checker.setControlPathPrefix("composer_polygon") myTestResult, myMessage = checker.testLayout() assert myTestResult, myMessage def testDisplayNodes(self): """Test displayNodes method""" self.polygon.setDisplayNodes(True) checker = QgsLayoutChecker('composerpolygon_displaynodes', self.layout) checker.setControlPathPrefix("composer_polygon") myTestResult, myMessage = checker.testLayout() assert myTestResult, myMessage self.polygon.setDisplayNodes(False) checker = QgsLayoutChecker('composerpolygon_defaultstyle', self.layout) checker.setControlPathPrefix("composer_polygon") myTestResult, myMessage = checker.testLayout() assert myTestResult, myMessage def testSelectedNode(self): """Test selectedNode and deselectNode methods""" self.polygon.setDisplayNodes(True) self.polygon.setSelectedNode(3) checker = QgsLayoutChecker('composerpolygon_selectednode', self.layout) checker.setControlPathPrefix("composer_polygon") myTestResult, myMessage = checker.testLayout() assert myTestResult, myMessage self.polygon.deselectNode() self.polygon.setDisplayNodes(False) checker = QgsLayoutChecker('composerpolygon_defaultstyle', self.layout) checker.setControlPathPrefix("composer_polygon") myTestResult, myMessage = checker.testLayout() assert myTestResult, myMessage def testRemoveNode(self): """Test removeNode method""" rc = self.polygon.removeNode(100) self.assertEqual(rc, False) checker = QgsLayoutChecker('composerpolygon_defaultstyle', self.layout) checker.setControlPathPrefix("composer_polygon") myTestResult, myMessage = checker.testLayout() assert myTestResult, myMessage self.assertEqual(self.polygon.nodesSize(), 4) def testAddNode(self): """Test addNode method""" # default searching radius is 10 self.assertEqual(self.polygon.nodesSize(), 4) rc = self.polygon.addNode(QPointF(50.0, 10.0)) self.assertEqual(rc, False) # default searching radius is 10 self.assertEqual(self.polygon.nodesSize(), 4) rc = self.polygon.addNode(QPointF(50.0, 9.99)) self.assertEqual(rc, True) self.assertEqual(self.polygon.nodesSize(), 5) def testAddNodeCustomRadius(self): """Test addNode with custom radius""" # default searching radius is 10 self.assertEqual(self.polygon.nodesSize(), 4) rc = self.polygon.addNode(QPointF(50.0, 8.1), True, 8.0) self.assertEqual(rc, False) self.assertEqual(self.polygon.nodesSize(), 4) # default searching radius is 10 rc = self.polygon.addNode(QPointF(50.0, 7.9), True, 8.0) self.assertEqual(rc, True) self.assertEqual(self.polygon.nodesSize(), 5) def testAddNodeWithoutCheckingArea(self): """Test addNode without checking the maximum distance allowed""" # default searching radius is 10 self.assertEqual(self.polygon.nodesSize(), 4) rc = self.polygon.addNode(QPointF(50.0, 20.0)) self.assertEqual(rc, False) self.assertEqual(self.polygon.nodesSize(), 4) # default searching radius is 10 self.assertEqual(self.polygon.nodesSize(), 4) rc = self.polygon.addNode(QPointF(50.0, 20.0), False) self.assertEqual(rc, True) self.assertEqual(self.polygon.nodesSize(), 5) checker = QgsLayoutChecker('composerpolygon_addnode', self.layout) checker.setControlPathPrefix("composer_polygon") myTestResult, myMessage = checker.testLayout() assert myTestResult, myMessage def testMoveNode(self): """Test moveNode method""" rc = self.polygon.moveNode(30, QPointF(100.0, 300.0)) self.assertEqual(rc, False) rc = self.polygon.moveNode(3, QPointF(100.0, 150.0)) self.assertEqual(rc, True) checker = QgsLayoutChecker('composerpolygon_movenode', self.layout) checker.setControlPathPrefix("composer_polygon") myTestResult, myMessage = checker.testLayout() assert myTestResult, myMessage def testNodeAtPosition(self): """Test nodeAtPosition method""" p = QPolygonF() p.append(QPointF(0.0, 0.0)) p.append(QPointF(100.0, 0.0)) p.append(QPointF(200.0, 100.0)) p.append(QPointF(100.0, 200.0)) polygon = QgsLayoutItemPolygon(p, self.layout) # default searching radius is 10 rc = polygon.nodeAtPosition(QPointF(100.0, 210.0)) self.assertEqual(rc, -1) # default searching radius is 10 rc = polygon.nodeAtPosition(QPointF(100.0, 210.0), False) self.assertEqual(rc, 3) # default searching radius is 10 rc = polygon.nodeAtPosition(QPointF(100.0, 210.0), True, 10.1) self.assertEqual(rc, 3) def testReadWriteXml(self): pr = QgsProject() l = QgsLayout(pr) p = QPolygonF() p.append(QPointF(0.0, 0.0)) p.append(QPointF(100.0, 0.0)) p.append(QPointF(200.0, 100.0)) shape = QgsLayoutItemPolygon(p, l) props = {} props["color"] = "green" props["style"] = "solid" props["style_border"] = "solid" props["color_border"] = "red" props["width_border"] = "10.0" props["joinstyle"] = "miter" style = QgsFillSymbol.createSimple(props) shape.setSymbol(style) #save original item to xml doc = QDomDocument("testdoc") elem = doc.createElement("test") self.assertTrue(shape.writeXml(elem, doc, QgsReadWriteContext())) shape2 = QgsLayoutItemPolygon(l) self.assertTrue( shape2.readXml(elem.firstChildElement(), doc, QgsReadWriteContext())) self.assertEqual(shape2.nodes(), shape.nodes()) self.assertEqual(shape2.symbol().symbolLayer(0).color().name(), '#008000') self.assertEqual(shape2.symbol().symbolLayer(0).strokeColor().name(), '#ff0000') def testBounds(self): pr = QgsProject() l = QgsLayout(pr) p = QPolygonF() p.append(QPointF(50.0, 30.0)) p.append(QPointF(100.0, 10.0)) p.append(QPointF(200.0, 100.0)) shape = QgsLayoutItemPolygon(p, l) props = {} props["color"] = "green" props["style"] = "solid" props["style_border"] = "solid" props["color_border"] = "red" props["width_border"] = "6.0" props["joinstyle"] = "miter" style = QgsFillSymbol.createSimple(props) shape.setSymbol(style) # scene bounding rect should include symbol outline bounds = shape.sceneBoundingRect() self.assertEqual(bounds.left(), 47.0) self.assertEqual(bounds.right(), 203.0) self.assertEqual(bounds.top(), 7.0) self.assertEqual(bounds.bottom(), 103.0) # rectWithFrame should include symbol outline too bounds = shape.rectWithFrame() self.assertEqual(bounds.left(), -3.0) self.assertEqual(bounds.right(), 153.0) self.assertEqual(bounds.top(), -3.0) self.assertEqual(bounds.bottom(), 93.0)
def testStacking(self): p = QgsProject() l = QgsLayout(p) # add some items item1 = QgsLayoutItemPicture(l) l.addLayoutItem(item1) item2 = QgsLayoutItemPicture(l) l.addLayoutItem(item2) item3 = QgsLayoutItemPicture(l) l.addLayoutItem(item3) view = QgsLayoutView() view.setCurrentLayout(l) self.assertEqual(item1.zValue(), 1) self.assertEqual(item2.zValue(), 2) self.assertEqual(item3.zValue(), 3) # no effect interactions view.raiseSelectedItems() view.lowerSelectedItems() view.moveSelectedItemsToTop() view.moveSelectedItemsToBottom() self.assertEqual(item1.zValue(), 1) self.assertEqual(item2.zValue(), 2) self.assertEqual(item3.zValue(), 3) # raising item3.setSelected(True) view.raiseSelectedItems() self.assertEqual(item1.zValue(), 1) self.assertEqual(item2.zValue(), 2) self.assertEqual(item3.zValue(), 3) item3.setSelected(False) item2.setSelected(True) view.raiseSelectedItems() self.assertEqual(item1.zValue(), 1) self.assertEqual(item2.zValue(), 3) self.assertEqual(item3.zValue(), 2) view.raiseSelectedItems() self.assertEqual(item1.zValue(), 1) self.assertEqual(item2.zValue(), 3) self.assertEqual(item3.zValue(), 2) item2.setSelected(False) item1.setSelected(True) view.raiseSelectedItems() self.assertEqual(item1.zValue(), 2) self.assertEqual(item2.zValue(), 3) self.assertEqual(item3.zValue(), 1) # lower item1.setSelected(False) item3.setSelected(True) view.lowerSelectedItems() self.assertEqual(item1.zValue(), 2) self.assertEqual(item2.zValue(), 3) self.assertEqual(item3.zValue(), 1) item3.setSelected(False) item2.setSelected(True) view.lowerSelectedItems() self.assertEqual(item1.zValue(), 3) self.assertEqual(item2.zValue(), 2) self.assertEqual(item3.zValue(), 1) view.lowerSelectedItems() self.assertEqual(item1.zValue(), 3) self.assertEqual(item2.zValue(), 1) self.assertEqual(item3.zValue(), 2) # raise to top item2.setSelected(False) item1.setSelected(True) view.moveSelectedItemsToTop() self.assertEqual(item1.zValue(), 3) self.assertEqual(item2.zValue(), 1) self.assertEqual(item3.zValue(), 2) item1.setSelected(False) item3.setSelected(True) view.moveSelectedItemsToTop() self.assertEqual(item1.zValue(), 2) self.assertEqual(item2.zValue(), 1) self.assertEqual(item3.zValue(), 3) item3.setSelected(False) item2.setSelected(True) view.moveSelectedItemsToTop() self.assertEqual(item1.zValue(), 1) self.assertEqual(item2.zValue(), 3) self.assertEqual(item3.zValue(), 2) # move to bottom item2.setSelected(False) item1.setSelected(True) view.moveSelectedItemsToBottom() self.assertEqual(item1.zValue(), 1) self.assertEqual(item2.zValue(), 3) self.assertEqual(item3.zValue(), 2) item1.setSelected(False) item3.setSelected(True) view.moveSelectedItemsToBottom() self.assertEqual(item1.zValue(), 2) self.assertEqual(item2.zValue(), 3) self.assertEqual(item3.zValue(), 1) item3.setSelected(False) item2.setSelected(True) view.moveSelectedItemsToBottom() self.assertEqual(item1.zValue(), 3) self.assertEqual(item2.zValue(), 1) self.assertEqual(item3.zValue(), 2)
class TestQgsLayoutMap(unittest.TestCase, LayoutItemTestCase): @classmethod def setUpClass(cls): cls.item_class = QgsLayoutItemMap def setUp(self): self.report = "<h1>Python QgsLayoutItemMap Tests</h1>\n" def tearDown(self): report_file_path = "%s/qgistest.html" % QDir.tempPath() with open(report_file_path, 'a') as report_file: report_file.write(self.report) def __init__(self, methodName): """Run once on class initialization.""" unittest.TestCase.__init__(self, methodName) myPath = os.path.join(TEST_DATA_DIR, 'rgb256x256.png') rasterFileInfo = QFileInfo(myPath) self.raster_layer = QgsRasterLayer(rasterFileInfo.filePath(), rasterFileInfo.completeBaseName()) rasterRenderer = QgsMultiBandColorRenderer( self.raster_layer.dataProvider(), 1, 2, 3) self.raster_layer.setRenderer(rasterRenderer) myPath = os.path.join(TEST_DATA_DIR, 'points.shp') vector_file_info = QFileInfo(myPath) self.vector_layer = QgsVectorLayer(vector_file_info.filePath(), vector_file_info.completeBaseName(), 'ogr') assert self.vector_layer.isValid() # pipe = mRasterLayer.pipe() # assert pipe.set(rasterRenderer), 'Cannot set pipe renderer' QgsProject.instance().addMapLayers( [self.raster_layer, self.vector_layer]) # create layout with layout map self.layout = QgsLayout(QgsProject.instance()) self.layout.initializeDefaults() self.map = QgsLayoutItemMap(self.layout) self.map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) self.map.setFrameEnabled(True) self.map.setLayers([self.raster_layer]) self.layout.addLayoutItem(self.map) def testMapCrs(self): # create layout with layout map map_settings = QgsMapSettings() map_settings.setLayers([self.vector_layer]) layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() # check that new maps inherit project CRS QgsProject.instance().setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) map.setFrameEnabled(True) rectangle = QgsRectangle(-13838977, 2369660, -8672298, 6250909) map.setExtent(rectangle) map.setLayers([self.vector_layer]) layout.addLayoutItem(map) self.assertEqual(map.crs().authid(), 'EPSG:4326') self.assertFalse(map.presetCrs().isValid()) # overwrite CRS map.setCrs(QgsCoordinateReferenceSystem('EPSG:3857')) self.assertEqual(map.crs().authid(), 'EPSG:3857') self.assertEqual(map.presetCrs().authid(), 'EPSG:3857') checker = QgsLayoutChecker('composermap_crs3857', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) # overwrite CRS map.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) self.assertEqual(map.presetCrs().authid(), 'EPSG:4326') self.assertEqual(map.crs().authid(), 'EPSG:4326') rectangle = QgsRectangle(-124, 17, -78, 52) map.zoomToExtent(rectangle) checker = QgsLayoutChecker('composermap_crs4326', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) # change back to project CRS map.setCrs(QgsCoordinateReferenceSystem()) self.assertEqual(map.crs().authid(), 'EPSG:4326') self.assertFalse(map.presetCrs().isValid()) def testContainsAdvancedEffects(self): map_settings = QgsMapSettings() map_settings.setLayers([self.vector_layer]) layout = QgsLayout(QgsProject.instance()) map = QgsLayoutItemMap(layout) self.assertFalse(map.containsAdvancedEffects()) self.vector_layer.setBlendMode(QPainter.CompositionMode_Darken) result = map.containsAdvancedEffects() self.vector_layer.setBlendMode(QPainter.CompositionMode_SourceOver) self.assertTrue(result) def testRasterization(self): map_settings = QgsMapSettings() map_settings.setLayers([self.vector_layer]) layout = QgsLayout(QgsProject.instance()) map = QgsLayoutItemMap(layout) self.assertFalse(map.requiresRasterization()) self.vector_layer.setBlendMode(QPainter.CompositionMode_Darken) self.assertFalse(map.requiresRasterization()) self.assertTrue(map.containsAdvancedEffects()) map.setBackgroundEnabled(False) self.assertTrue(map.requiresRasterization()) map.setBackgroundEnabled(True) map.setBackgroundColor(QColor(1, 1, 1, 1)) self.assertTrue(map.requiresRasterization()) self.vector_layer.setBlendMode(QPainter.CompositionMode_SourceOver) def testLabelMargin(self): """ Test rendering map item with a label margin set """ format = QgsTextFormat() format.setFont(QgsFontUtils.getStandardTestFont("Bold")) format.setSize(20) format.setNamedStyle("Bold") format.setColor(QColor(0, 0, 0)) settings = QgsPalLayerSettings() settings.setFormat(format) settings.fieldName = "'X'" settings.isExpression = True settings.placement = QgsPalLayerSettings.OverPoint vl = QgsVectorLayer("Point?crs=epsg:4326&field=id:integer", "vl", "memory") vl.setRenderer(QgsNullSymbolRenderer()) f = QgsFeature(vl.fields(), 1) for x in range(15): for y in range(15): f.setGeometry(QgsPoint(x, y)) vl.dataProvider().addFeature(f) vl.setLabeling(QgsVectorLayerSimpleLabeling(settings)) vl.setLabelsEnabled(True) p = QgsProject() engine_settings = QgsLabelingEngineSettings() engine_settings.setFlag(QgsLabelingEngineSettings.UsePartialCandidates, False) engine_settings.setFlag(QgsLabelingEngineSettings.DrawLabelRectOnly, True) p.setLabelingEngineSettings(engine_settings) p.addMapLayer(vl) layout = QgsLayout(p) layout.initializeDefaults() p.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(10, 10, 180, 180)) map.setFrameEnabled(True) map.zoomToExtent(vl.extent()) map.setLayers([vl]) layout.addLayoutItem(map) checker = QgsLayoutChecker('composermap_label_nomargin', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) map.setLabelMargin( QgsLayoutMeasurement(15, QgsUnitTypes.LayoutMillimeters)) checker = QgsLayoutChecker('composermap_label_margin', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) map.setLabelMargin( QgsLayoutMeasurement(3, QgsUnitTypes.LayoutCentimeters)) checker = QgsLayoutChecker('composermap_label_cm_margin', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) map.setMapRotation(45) map.zoomToExtent(vl.extent()) map.setScale(map.scale() * 1.2) checker = QgsLayoutChecker('composermap_rotated_label_margin', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) # data defined map.setMapRotation(0) map.zoomToExtent(vl.extent()) map.dataDefinedProperties().setProperty( QgsLayoutObject.MapLabelMargin, QgsProperty.fromExpression('1+3')) map.refresh() checker = QgsLayoutChecker('composermap_dd_label_margin', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) def testPartialLabels(self): """ Test rendering map item with a show partial labels flag """ format = QgsTextFormat() format.setFont(QgsFontUtils.getStandardTestFont("Bold")) format.setSize(20) format.setNamedStyle("Bold") format.setColor(QColor(0, 0, 0)) settings = QgsPalLayerSettings() settings.setFormat(format) settings.fieldName = "'X'" settings.isExpression = True settings.placement = QgsPalLayerSettings.OverPoint vl = QgsVectorLayer("Point?crs=epsg:4326&field=id:integer", "vl", "memory") vl.setRenderer(QgsNullSymbolRenderer()) f = QgsFeature(vl.fields(), 1) for x in range(15): for y in range(15): f.setGeometry(QgsPoint(x, y)) vl.dataProvider().addFeature(f) vl.setLabeling(QgsVectorLayerSimpleLabeling(settings)) vl.setLabelsEnabled(True) p = QgsProject() engine_settings = QgsLabelingEngineSettings() engine_settings.setFlag(QgsLabelingEngineSettings.UsePartialCandidates, False) engine_settings.setFlag(QgsLabelingEngineSettings.DrawLabelRectOnly, True) p.setLabelingEngineSettings(engine_settings) p.addMapLayer(vl) layout = QgsLayout(p) layout.initializeDefaults() p.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(10, 10, 180, 180)) map.setFrameEnabled(True) map.zoomToExtent(vl.extent()) map.setLayers([vl]) layout.addLayoutItem(map) # default should always be to hide partial labels self.assertFalse(map.mapFlags() & QgsLayoutItemMap.ShowPartialLabels) # hiding partial labels (the default) map.setMapFlags(QgsLayoutItemMap.MapItemFlags()) checker = QgsLayoutChecker('composermap_label_nomargin', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) # showing partial labels map.setMapFlags(QgsLayoutItemMap.ShowPartialLabels) checker = QgsLayoutChecker('composermap_show_partial_labels', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) def testBlockingItems(self): """ Test rendering map item with blocking items """ format = QgsTextFormat() format.setFont(QgsFontUtils.getStandardTestFont("Bold")) format.setSize(20) format.setNamedStyle("Bold") format.setColor(QColor(0, 0, 0)) settings = QgsPalLayerSettings() settings.setFormat(format) settings.fieldName = "'X'" settings.isExpression = True settings.placement = QgsPalLayerSettings.OverPoint vl = QgsVectorLayer("Point?crs=epsg:4326&field=id:integer", "vl", "memory") vl.setRenderer(QgsNullSymbolRenderer()) f = QgsFeature(vl.fields(), 1) for x in range(15): for y in range(15): f.setGeometry(QgsPoint(x, y)) vl.dataProvider().addFeature(f) vl.setLabeling(QgsVectorLayerSimpleLabeling(settings)) vl.setLabelsEnabled(True) p = QgsProject() engine_settings = QgsLabelingEngineSettings() engine_settings.setFlag(QgsLabelingEngineSettings.DrawLabelRectOnly, True) p.setLabelingEngineSettings(engine_settings) p.addMapLayer(vl) layout = QgsLayout(p) layout.initializeDefaults() p.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(10, 10, 180, 180)) map.setFrameEnabled(True) map.zoomToExtent(vl.extent()) map.setLayers([vl]) map.setId('map') layout.addLayoutItem(map) map2 = QgsLayoutItemMap(layout) map2.attemptSetSceneRect(QRectF(0, 5, 50, 80)) map2.setFrameEnabled(True) map2.setBackgroundEnabled(False) map2.setId('map2') layout.addLayoutItem(map2) map3 = QgsLayoutItemMap(layout) map3.attemptSetSceneRect(QRectF(150, 160, 50, 50)) map3.setFrameEnabled(True) map3.setBackgroundEnabled(False) map3.setId('map3') layout.addLayoutItem(map3) map.addLabelBlockingItem(map2) map.addLabelBlockingItem(map3) map.setMapFlags(QgsLayoutItemMap.MapItemFlags()) checker = QgsLayoutChecker('composermap_label_blockers', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) doc = QDomDocument("testdoc") elem = layout.writeXml(doc, QgsReadWriteContext()) l2 = QgsLayout(p) self.assertTrue(l2.readXml(elem, doc, QgsReadWriteContext())) map_restore = [ i for i in l2.items() if isinstance(i, QgsLayoutItemMap) and i.id() == 'map' ][0] map2_restore = [ i for i in l2.items() if isinstance(i, QgsLayoutItemMap) and i.id() == 'map2' ][0] map3_restore = [ i for i in l2.items() if isinstance(i, QgsLayoutItemMap) and i.id() == 'map3' ][0] self.assertTrue(map_restore.isLabelBlockingItem(map2_restore)) self.assertTrue(map_restore.isLabelBlockingItem(map3_restore))
class TestQgsLayoutMap(unittest.TestCase, LayoutItemTestCase): @classmethod def setUpClass(cls): cls.item_class = QgsLayoutItemMap def setUp(self): self.report = "<h1>Python QgsLayoutItemMap Tests</h1>\n" def tearDown(self): report_file_path = "%s/qgistest.html" % QDir.tempPath() with open(report_file_path, 'a') as report_file: report_file.write(self.report) def __init__(self, methodName): """Run once on class initialization.""" unittest.TestCase.__init__(self, methodName) myPath = os.path.join(TEST_DATA_DIR, 'rgb256x256.png') rasterFileInfo = QFileInfo(myPath) self.raster_layer = QgsRasterLayer(rasterFileInfo.filePath(), rasterFileInfo.completeBaseName()) rasterRenderer = QgsMultiBandColorRenderer( self.raster_layer.dataProvider(), 1, 2, 3) self.raster_layer.setRenderer(rasterRenderer) myPath = os.path.join(TEST_DATA_DIR, 'points.shp') vector_file_info = QFileInfo(myPath) self.vector_layer = QgsVectorLayer(vector_file_info.filePath(), vector_file_info.completeBaseName(), 'ogr') assert self.vector_layer.isValid() # pipe = mRasterLayer.pipe() # assert pipe.set(rasterRenderer), 'Cannot set pipe renderer' QgsProject.instance().addMapLayers([self.raster_layer, self.vector_layer]) # create layout with layout map self.layout = QgsLayout(QgsProject.instance()) self.layout.initializeDefaults() self.map = QgsLayoutItemMap(self.layout) self.map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) self.map.setFrameEnabled(True) self.map.setLayers([self.raster_layer]) self.layout.addLayoutItem(self.map) def testOverviewMap(self): overviewMap = QgsLayoutItemMap(self.layout) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) overviewMap.setFrameEnabled(True) overviewMap.setLayers([self.raster_layer]) self.layout.addLayoutItem(overviewMap) # zoom in myRectangle = QgsRectangle(96, -152, 160, -120) self.map.setExtent(myRectangle) myRectangle2 = QgsRectangle(0, -256, 256, 0) overviewMap.setExtent(myRectangle2) overviewMap.overview().setLinkedMap(self.map) checker = QgsLayoutChecker('composermap_overview', self.layout) checker.setColorTolerance(6) checker.setControlPathPrefix("composer_mapoverview") myTestResult, myMessage = checker.testLayout() self.report += checker.report() self.layout.removeLayoutItem(overviewMap) assert myTestResult, myMessage def testOverviewMapBlend(self): overviewMap = QgsLayoutItemMap(self.layout) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) overviewMap.setFrameEnabled(True) overviewMap.setLayers([self.raster_layer]) self.layout.addLayoutItem(overviewMap) # zoom in myRectangle = QgsRectangle(96, -152, 160, -120) self.map.setExtent(myRectangle) myRectangle2 = QgsRectangle(0, -256, 256, 0) overviewMap.setExtent(myRectangle2) overviewMap.overview().setLinkedMap(self.map) overviewMap.overview().setBlendMode(QPainter.CompositionMode_Multiply) checker = QgsLayoutChecker('composermap_overview_blending', self.layout) checker.setControlPathPrefix("composer_mapoverview") myTestResult, myMessage = checker.testLayout() self.report += checker.report() self.layout.removeLayoutItem(overviewMap) assert myTestResult, myMessage def testOverviewMapInvert(self): overviewMap = QgsLayoutItemMap(self.layout) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) overviewMap.setFrameEnabled(True) overviewMap.setLayers([self.raster_layer]) self.layout.addLayoutItem(overviewMap) # zoom in myRectangle = QgsRectangle(96, -152, 160, -120) self.map.setExtent(myRectangle) myRectangle2 = QgsRectangle(0, -256, 256, 0) overviewMap.setExtent(myRectangle2) overviewMap.overview().setLinkedMap(self.map) overviewMap.overview().setInverted(True) checker = QgsLayoutChecker('composermap_overview_invert', self.layout) checker.setControlPathPrefix("composer_mapoverview") myTestResult, myMessage = checker.testLayout() self.report += checker.report() self.layout.removeLayoutItem(overviewMap) assert myTestResult, myMessage def testOverviewMapCenter(self): overviewMap = QgsLayoutItemMap(self.layout) overviewMap.attemptSetSceneRect(QRectF(20, 130, 70, 70)) overviewMap.setFrameEnabled(True) overviewMap.setLayers([self.raster_layer]) self.layout.addLayoutItem(overviewMap) # zoom in myRectangle = QgsRectangle(192, -288, 320, -224) self.map.setExtent(myRectangle) myRectangle2 = QgsRectangle(0, -256, 256, 0) overviewMap.setExtent(myRectangle2) overviewMap.overview().setLinkedMap(self.map) overviewMap.overview().setInverted(False) overviewMap.overview().setCentered(True) checker = QgsLayoutChecker('composermap_overview_center', self.layout) checker.setControlPathPrefix("composer_mapoverview") myTestResult, myMessage = checker.testLayout() self.report += checker.report() self.layout.removeLayoutItem(overviewMap) assert myTestResult, myMessage def testMapCrs(self): # create layout with layout map map_settings = QgsMapSettings() map_settings.setLayers([self.vector_layer]) layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() # check that new maps inherit project CRS QgsProject.instance().setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(20, 20, 200, 100)) map.setFrameEnabled(True) rectangle = QgsRectangle(-13838977, 2369660, -8672298, 6250909) map.setExtent(rectangle) map.setLayers([self.vector_layer]) layout.addLayoutItem(map) self.assertEqual(map.crs().authid(), 'EPSG:4326') self.assertFalse(map.presetCrs().isValid()) # overwrite CRS map.setCrs(QgsCoordinateReferenceSystem('EPSG:3857')) self.assertEqual(map.crs().authid(), 'EPSG:3857') self.assertEqual(map.presetCrs().authid(), 'EPSG:3857') checker = QgsLayoutChecker('composermap_crs3857', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) # overwrite CRS map.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) self.assertEqual(map.presetCrs().authid(), 'EPSG:4326') self.assertEqual(map.crs().authid(), 'EPSG:4326') rectangle = QgsRectangle(-124, 17, -78, 52) map.zoomToExtent(rectangle) checker = QgsLayoutChecker('composermap_crs4326', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) # change back to project CRS map.setCrs(QgsCoordinateReferenceSystem()) self.assertEqual(map.crs().authid(), 'EPSG:4326') self.assertFalse(map.presetCrs().isValid()) def testContainsAdvancedEffects(self): map_settings = QgsMapSettings() map_settings.setLayers([self.vector_layer]) layout = QgsLayout(QgsProject.instance()) map = QgsLayoutItemMap(layout) self.assertFalse(map.containsAdvancedEffects()) self.vector_layer.setBlendMode(QPainter.CompositionMode_Darken) result = map.containsAdvancedEffects() self.vector_layer.setBlendMode(QPainter.CompositionMode_SourceOver) self.assertTrue(result) def testRasterization(self): map_settings = QgsMapSettings() map_settings.setLayers([self.vector_layer]) layout = QgsLayout(QgsProject.instance()) map = QgsLayoutItemMap(layout) self.assertFalse(map.requiresRasterization()) self.vector_layer.setBlendMode(QPainter.CompositionMode_Darken) self.assertFalse(map.requiresRasterization()) self.assertTrue(map.containsAdvancedEffects()) map.setBackgroundEnabled(False) self.assertTrue(map.requiresRasterization()) map.setBackgroundEnabled(True) map.setBackgroundColor(QColor(1, 1, 1, 1)) self.assertTrue(map.requiresRasterization()) self.vector_layer.setBlendMode(QPainter.CompositionMode_SourceOver)
def testBlockingItems(self): """ Test rendering map item with blocking items """ format = QgsTextFormat() format.setFont(QgsFontUtils.getStandardTestFont("Bold")) format.setSize(20) format.setNamedStyle("Bold") format.setColor(QColor(0, 0, 0)) settings = QgsPalLayerSettings() settings.setFormat(format) settings.fieldName = "'X'" settings.isExpression = True settings.placement = QgsPalLayerSettings.OverPoint vl = QgsVectorLayer("Point?crs=epsg:4326&field=id:integer", "vl", "memory") vl.setRenderer(QgsNullSymbolRenderer()) f = QgsFeature(vl.fields(), 1) for x in range(15): for y in range(15): f.setGeometry(QgsPoint(x, y)) vl.dataProvider().addFeature(f) vl.setLabeling(QgsVectorLayerSimpleLabeling(settings)) vl.setLabelsEnabled(True) p = QgsProject() engine_settings = QgsLabelingEngineSettings() engine_settings.setFlag(QgsLabelingEngineSettings.DrawLabelRectOnly, True) p.setLabelingEngineSettings(engine_settings) p.addMapLayer(vl) layout = QgsLayout(p) layout.initializeDefaults() p.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(10, 10, 180, 180)) map.setFrameEnabled(True) map.zoomToExtent(vl.extent()) map.setLayers([vl]) map.setId('map') layout.addLayoutItem(map) map2 = QgsLayoutItemMap(layout) map2.attemptSetSceneRect(QRectF(0, 5, 50, 80)) map2.setFrameEnabled(True) map2.setBackgroundEnabled(False) map2.setId('map2') layout.addLayoutItem(map2) map3 = QgsLayoutItemMap(layout) map3.attemptSetSceneRect(QRectF(150, 160, 50, 50)) map3.setFrameEnabled(True) map3.setBackgroundEnabled(False) map3.setId('map3') layout.addLayoutItem(map3) map.addLabelBlockingItem(map2) map.addLabelBlockingItem(map3) map.setMapFlags(QgsLayoutItemMap.MapItemFlags()) checker = QgsLayoutChecker('composermap_label_blockers', layout) checker.setControlPathPrefix("composer_map") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) doc = QDomDocument("testdoc") elem = layout.writeXml(doc, QgsReadWriteContext()) l2 = QgsLayout(p) self.assertTrue(l2.readXml(elem, doc, QgsReadWriteContext())) map_restore = [ i for i in l2.items() if isinstance(i, QgsLayoutItemMap) and i.id() == 'map' ][0] map2_restore = [ i for i in l2.items() if isinstance(i, QgsLayoutItemMap) and i.id() == 'map2' ][0] map3_restore = [ i for i in l2.items() if isinstance(i, QgsLayoutItemMap) and i.id() == 'map3' ][0] self.assertTrue(map_restore.isLabelBlockingItem(map2_restore)) self.assertTrue(map_restore.isLabelBlockingItem(map3_restore))
def testStacking(self): p = QgsProject() l = QgsLayout(p) # add some items item1 = QgsLayoutItemMap(l) l.addLayoutItem(item1) item2 = QgsLayoutItemMap(l) l.addLayoutItem(item2) item3 = QgsLayoutItemMap(l) l.addLayoutItem(item3) self.assertEqual(item1.zValue(), 1) self.assertEqual(item2.zValue(), 2) self.assertEqual(item3.zValue(), 3) # no effect interactions self.assertFalse(l.raiseItem(None)) self.assertFalse(l.lowerItem(None)) self.assertFalse(l.moveItemToTop(None)) self.assertFalse(l.moveItemToBottom(None)) self.assertEqual(item1.zValue(), 1) self.assertEqual(item2.zValue(), 2) self.assertEqual(item3.zValue(), 3) # raising self.assertFalse(l.raiseItem(item3)) self.assertEqual(item1.zValue(), 1) self.assertEqual(item2.zValue(), 2) self.assertEqual(item3.zValue(), 3) self.assertTrue(l.raiseItem(item2)) self.assertEqual(item1.zValue(), 1) self.assertEqual(item2.zValue(), 3) self.assertEqual(item3.zValue(), 2) self.assertFalse(l.raiseItem(item2)) self.assertEqual(item1.zValue(), 1) self.assertEqual(item2.zValue(), 3) self.assertEqual(item3.zValue(), 2) self.assertTrue(l.raiseItem(item1)) self.assertEqual(item1.zValue(), 2) self.assertEqual(item2.zValue(), 3) self.assertEqual(item3.zValue(), 1) # lower self.assertFalse(l.lowerItem(item3)) self.assertEqual(item1.zValue(), 2) self.assertEqual(item2.zValue(), 3) self.assertEqual(item3.zValue(), 1) self.assertTrue(l.lowerItem(item2)) self.assertEqual(item1.zValue(), 3) self.assertEqual(item2.zValue(), 2) self.assertEqual(item3.zValue(), 1) self.assertTrue(l.lowerItem(item2)) self.assertEqual(item1.zValue(), 3) self.assertEqual(item2.zValue(), 1) self.assertEqual(item3.zValue(), 2) # raise to top self.assertFalse(l.moveItemToTop(item1)) self.assertEqual(item1.zValue(), 3) self.assertEqual(item2.zValue(), 1) self.assertEqual(item3.zValue(), 2) self.assertTrue(l.moveItemToTop(item3)) self.assertEqual(item1.zValue(), 2) self.assertEqual(item2.zValue(), 1) self.assertEqual(item3.zValue(), 3) self.assertTrue(l.moveItemToTop(item2)) self.assertEqual(item1.zValue(), 1) self.assertEqual(item2.zValue(), 3) self.assertEqual(item3.zValue(), 2) # move to bottom self.assertFalse(l.moveItemToBottom(item1)) self.assertEqual(item1.zValue(), 1) self.assertEqual(item2.zValue(), 3) self.assertEqual(item3.zValue(), 2) self.assertTrue(l.moveItemToBottom(item3)) self.assertEqual(item1.zValue(), 2) self.assertEqual(item2.zValue(), 3) self.assertEqual(item3.zValue(), 1) self.assertTrue(l.moveItemToBottom(item2)) self.assertEqual(item1.zValue(), 3) self.assertEqual(item2.zValue(), 1) self.assertEqual(item3.zValue(), 2)
def testInitialSizeSymbolMapUnits(self): """Test initial size of legend with a symbol size in map units""" point_path = os.path.join(TEST_DATA_DIR, 'points.shp') point_layer = QgsVectorLayer(point_path, 'points', 'ogr') QgsProject.instance().addMapLayers([point_layer]) marker_symbol = QgsMarkerSymbol.createSimple({ 'color': '#ff0000', 'outline_style': 'no', 'size': '5', 'size_unit': 'MapUnit' }) point_layer.setRenderer(QgsSingleSymbolRenderer(marker_symbol)) s = QgsMapSettings() s.setLayers([point_layer]) layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(20, 20, 80, 80)) map.setFrameEnabled(True) map.setLayers([point_layer]) layout.addLayoutItem(map) map.setExtent(point_layer.extent()) legend = QgsLayoutItemLegend(layout) legend.attemptSetSceneRect(QRectF(120, 20, 80, 80)) legend.setFrameEnabled(True) legend.setFrameStrokeWidth(QgsLayoutMeasurement(2)) legend.setBackgroundColor(QColor(200, 200, 200)) legend.setTitle('') layout.addLayoutItem(legend) legend.setLinkedMap(map) checker = QgsLayoutChecker('composer_legend_mapunits', layout) checker.setControlPathPrefix("composer_legend") result, message = checker.testLayout() self.assertTrue(result, message) # resize with non-top-left reference point legend.setResizeToContents(False) legend.setReferencePoint(QgsLayoutItem.LowerRight) legend.attemptMove(QgsLayoutPoint(120, 90)) legend.attemptResize(QgsLayoutSize(50, 60)) self.assertEqual(legend.positionWithUnits().x(), 120.0) self.assertEqual(legend.positionWithUnits().y(), 90.0) self.assertAlmostEqual(legend.pos().x(), 70, -1) self.assertAlmostEqual(legend.pos().y(), 30, -1) legend.setResizeToContents(True) legend.updateLegend() self.assertEqual(legend.positionWithUnits().x(), 120.0) self.assertEqual(legend.positionWithUnits().y(), 90.0) self.assertAlmostEqual(legend.pos().x(), 91, -1) self.assertAlmostEqual(legend.pos().y(), 71, -1) QgsProject.instance().removeMapLayers([point_layer.id()])
def test_layout_exports(self): """Test mask effects in a layout export at 300 dpi""" # modify labeling settings label_settings = self.polys_layer.labeling().settings() fmt = label_settings.format() # enable a mask fmt.mask().setEnabled(True) fmt.mask().setSize(4.0) # and mask other symbol layers underneath fmt.mask().setMaskedSymbolLayers([ # the black part of roads QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0)), # the black jets QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("B52", 0)), QgsSymbolLayerReference(self.points_layer.id(), QgsSymbolLayerId("Jet", 0)) ]) # add an outer glow effect to the mask blur = QgsOuterGlowEffect.create({ "enabled": "1", "blur_level": "6.445", "blur_unit": "MM", "opacity": "1", "spread": "0.6", "spread_unit": "MM", "color1": "0,0,255,255", "draw_mode": "2" }) fmt.mask().setPaintEffect(blur) label_settings.setFormat(fmt) self.polys_layer.labeling().setSettings(label_settings) layout = QgsLayout(QgsProject.instance()) page = QgsLayoutItemPage(layout) page.setPageSize(QgsLayoutSize(50, 33)) layout.pageCollection().addPage(page) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(1, 1, 48, 32)) map.setFrameEnabled(True) layout.addLayoutItem(map) map.setExtent(self.lines_layer.extent()) map.setLayers([self.points_layer, self.lines_layer, self.polys_layer]) image = QImage(591, 591, QImage.Format_RGB32) image.setDotsPerMeterX(300 / 25.3 * 1000) image.setDotsPerMeterY(300 / 25.3 * 1000) image.fill(0) p = QPainter(image) exporter = QgsLayoutExporter(layout) exporter.renderPage(p, 0) p.end() tmp = getTempfilePath('png') image.save(tmp) control_name = "layout_export" self.checker.setControlName(control_name) self.checker.setRenderedImage(tmp) res = self.checker.compareImages(control_name) self.report += self.checker.report() self.assertTrue(res)
class TestQgsLayoutPicture(unittest.TestCase, LayoutItemTestCase): @classmethod def setUpClass(cls): cls.item_class = QgsLayoutItemPicture # Bring up a simple HTTP server, for remote picture tests os.chdir(unitTestDataPath() + '') handler = http.server.SimpleHTTPRequestHandler cls.httpd = socketserver.TCPServer(('localhost', 0), handler) cls.port = cls.httpd.server_address[1] cls.httpd_thread = threading.Thread(target=cls.httpd.serve_forever) cls.httpd_thread.setDaemon(True) cls.httpd_thread.start() def __init__(self, methodName): """Run once on class initialization.""" unittest.TestCase.__init__(self, methodName) TEST_DATA_DIR = unitTestDataPath() self.pngImage = TEST_DATA_DIR + "/sample_image.png" self.svgImage = TEST_DATA_DIR + "/sample_svg.svg" # create composition self.layout = QgsLayout(QgsProject.instance()) self.layout.initializeDefaults() self.picture = QgsLayoutItemPicture(self.layout) self.picture.setPicturePath(self.pngImage) self.picture.attemptSetSceneRect(QRectF(70, 70, 100, 100)) self.picture.setFrameEnabled(True) self.layout.addLayoutItem(self.picture) def setUp(self): self.report = "<h1>Python QgsLayoutItemPicture Tests</h1>\n" def tearDown(self): report_file_path = "%s/qgistest.html" % QDir.tempPath() with open(report_file_path, 'a') as report_file: report_file.write(self.report) def testMode(self): pic = QgsLayoutItemPicture(self.layout) # should default to unknown self.assertEquals(pic.mode(), QgsLayoutItemPicture.FormatUnknown) spy = QSignalSpy(pic.changed) pic.setMode(QgsLayoutItemPicture.FormatRaster) self.assertEquals(pic.mode(), QgsLayoutItemPicture.FormatRaster) self.assertEqual(len(spy), 1) pic.setMode(QgsLayoutItemPicture.FormatRaster) self.assertEqual(len(spy), 1) pic.setMode(QgsLayoutItemPicture.FormatSVG) self.assertEqual(len(spy), 3) # ideally only 2! self.assertEquals(pic.mode(), QgsLayoutItemPicture.FormatSVG) # set picture path without explicit format pic.setPicturePath(self.pngImage) self.assertEquals(pic.mode(), QgsLayoutItemPicture.FormatRaster) pic.setPicturePath(self.svgImage) self.assertEquals(pic.mode(), QgsLayoutItemPicture.FormatSVG) # forced format pic.setPicturePath(self.pngImage, QgsLayoutItemPicture.FormatSVG) self.assertEquals(pic.mode(), QgsLayoutItemPicture.FormatSVG) pic.setPicturePath(self.pngImage, QgsLayoutItemPicture.FormatRaster) self.assertEquals(pic.mode(), QgsLayoutItemPicture.FormatRaster) pic.setPicturePath(self.svgImage, QgsLayoutItemPicture.FormatSVG) self.assertEquals(pic.mode(), QgsLayoutItemPicture.FormatSVG) pic.setPicturePath(self.svgImage, QgsLayoutItemPicture.FormatRaster) self.assertEquals(pic.mode(), QgsLayoutItemPicture.FormatRaster) def testReadWriteXml(self): pr = QgsProject() l = QgsLayout(pr) pic = QgsLayoutItemPicture(l) # mode should be saved/restored pic.setMode(QgsLayoutItemPicture.FormatRaster) #save original item to xml doc = QDomDocument("testdoc") elem = doc.createElement("test") self.assertTrue(pic.writeXml(elem, doc, QgsReadWriteContext())) pic2 = QgsLayoutItemPicture(l) self.assertTrue( pic2.readXml(elem.firstChildElement(), doc, QgsReadWriteContext())) self.assertEqual(pic2.mode(), QgsLayoutItemPicture.FormatRaster) pic.setMode(QgsLayoutItemPicture.FormatSVG) elem = doc.createElement("test2") self.assertTrue(pic.writeXml(elem, doc, QgsReadWriteContext())) pic3 = QgsLayoutItemPicture(l) self.assertTrue( pic3.readXml(elem.firstChildElement(), doc, QgsReadWriteContext())) self.assertEqual(pic3.mode(), QgsLayoutItemPicture.FormatSVG) def testResizeZoom(self): """Test picture resize zoom mode.""" self.picture.setResizeMode(QgsLayoutItemPicture.Zoom) checker = QgsLayoutChecker('composerpicture_resize_zoom', self.layout) checker.setControlPathPrefix("composer_picture") testResult, message = checker.testLayout() self.report += checker.report() assert testResult, message def testRemoteImage(self): """Test fetching remote picture.""" self.picture.setPicturePath('http://localhost:' + str(TestQgsLayoutPicture.port) + '/qgis_local_server/logo.png') checker = QgsLayoutChecker('composerpicture_remote', self.layout) checker.setControlPathPrefix("composer_picture") testResult, message = checker.testLayout() self.report += checker.report() self.picture.setPicturePath(self.pngImage) assert testResult, message def testNorthArrowWithMapItemRotation(self): """Test picture rotation when map item is also rotated""" layout = QgsLayout(QgsProject.instance()) map = QgsLayoutItemMap(layout) map.setExtent(QgsRectangle(0, -256, 256, 0)) layout.addLayoutItem(map) picture = QgsLayoutItemPicture(layout) layout.addLayoutItem(picture) picture.setLinkedMap(map) self.assertEqual(picture.linkedMap(), map) picture.setNorthMode(QgsLayoutItemPicture.GridNorth) map.setItemRotation(45) self.assertEqual(picture.pictureRotation(), 45) map.setMapRotation(-34) self.assertEqual(picture.pictureRotation(), 11) # add an offset picture.setNorthOffset(-10) self.assertEqual(picture.pictureRotation(), 1) map.setItemRotation(55) self.assertEqual(picture.pictureRotation(), 11) def testGridNorth(self): """Test syncing picture to grid north""" layout = QgsLayout(QgsProject.instance()) map = QgsLayoutItemMap(layout) map.setExtent(QgsRectangle(0, -256, 256, 0)) layout.addLayoutItem(map) picture = QgsLayoutItemPicture(layout) layout.addLayoutItem(picture) picture.setLinkedMap(map) self.assertEqual(picture.linkedMap(), map) picture.setNorthMode(QgsLayoutItemPicture.GridNorth) map.setMapRotation(45) self.assertEqual(picture.pictureRotation(), 45) # add an offset picture.setNorthOffset(-10) self.assertEqual(picture.pictureRotation(), 35) def testTrueNorth(self): """Test syncing picture to true north""" layout = QgsLayout(QgsProject.instance()) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(0, 0, 10, 10)) map.setCrs(QgsCoordinateReferenceSystem.fromEpsgId(3575)) map.setExtent( QgsRectangle(-2126029.962, -2200807.749, -119078.102, -757031.156)) layout.addLayoutItem(map) picture = QgsLayoutItemPicture(layout) layout.addLayoutItem(picture) picture.setLinkedMap(map) self.assertEqual(picture.linkedMap(), map) picture.setNorthMode(QgsLayoutItemPicture.TrueNorth) self.assertAlmostEqual(picture.pictureRotation(), 37.20, 1) # shift map map.setExtent( QgsRectangle(2120672.293, -3056394.691, 2481640.226, -2796718.780)) self.assertAlmostEqual(picture.pictureRotation(), -38.18, 1) # rotate map map.setMapRotation(45) self.assertAlmostEqual(picture.pictureRotation(), -38.18 + 45, 1) # add an offset picture.setNorthOffset(-10) self.assertAlmostEqual(picture.pictureRotation(), -38.18 + 35, 1)
class TestQgsLayoutPicture(unittest.TestCase, LayoutItemTestCase): @classmethod def setUpClass(cls): cls.item_class = QgsLayoutItemPicture # Bring up a simple HTTP server, for remote picture tests os.chdir(unitTestDataPath() + '') handler = http.server.SimpleHTTPRequestHandler cls.httpd = socketserver.TCPServer(('localhost', 0), handler) cls.port = cls.httpd.server_address[1] cls.httpd_thread = threading.Thread(target=cls.httpd.serve_forever) cls.httpd_thread.setDaemon(True) cls.httpd_thread.start() def __init__(self, methodName): """Run once on class initialization.""" unittest.TestCase.__init__(self, methodName) TEST_DATA_DIR = unitTestDataPath() self.pngImage = TEST_DATA_DIR + "/sample_image.png" # create composition self.layout = QgsLayout(QgsProject.instance()) self.layout.initializeDefaults() self.picture = QgsLayoutItemPicture(self.layout) self.picture.setPicturePath(self.pngImage) self.picture.attemptSetSceneRect(QRectF(70, 70, 100, 100)) self.picture.setFrameEnabled(True) self.layout.addLayoutItem(self.picture) def testResizeZoom(self): """Test picture resize zoom mode.""" self.picture.setResizeMode(QgsLayoutItemPicture.Zoom) checker = QgsLayoutChecker('composerpicture_resize_zoom', self.layout) checker.setControlPathPrefix("composer_picture") testResult, message = checker.testLayout() assert testResult, message @unittest.skip('test is broken for qt5/python3 - feature works') def testRemoteImage(self): """Test fetching remote picture.""" self.picture.setPicturePath('http://localhost:' + str(TestQgsLayoutPicture.port) + '/qgis_local_server/logo.png') checker = QgsLayoutChecker('picture_remote', self.layout) checker.setControlPathPrefix("composer_picture") testResult, message = checker.testLayout() self.picture.setPicturePath(self.pngImage) assert testResult, message def testGridNorth(self): """Test syncing picture to grid north""" layout = QgsLayout(QgsProject.instance()) map = QgsLayoutItemMap(layout) map.setExtent(QgsRectangle(0, -256, 256, 0)) layout.addLayoutItem(map) picture = QgsLayoutItemPicture(layout) layout.addLayoutItem(picture) picture.setLinkedMap(map) self.assertEqual(picture.linkedMap(), map) picture.setNorthMode(QgsLayoutItemPicture.GridNorth) map.setMapRotation(45) self.assertEqual(picture.pictureRotation(), 45) # add an offset picture.setNorthOffset(-10) self.assertEqual(picture.pictureRotation(), 35) def testTrueNorth(self): """Test syncing picture to true north""" layout = QgsLayout(QgsProject.instance()) map = QgsLayoutItemMap(layout) map.attemptSetSceneRect(QRectF(0, 0, 10, 10)) map.setCrs(QgsCoordinateReferenceSystem.fromEpsgId(3575)) map.setExtent( QgsRectangle(-2126029.962, -2200807.749, -119078.102, -757031.156)) layout.addLayoutItem(map) picture = QgsLayoutItemPicture(layout) layout.addLayoutItem(picture) picture.setLinkedMap(map) self.assertEqual(picture.linkedMap(), map) picture.setNorthMode(QgsLayoutItemPicture.TrueNorth) self.assertAlmostEqual(picture.pictureRotation(), 37.20, 1) # shift map map.setExtent( QgsRectangle(2120672.293, -3056394.691, 2481640.226, -2796718.780)) self.assertAlmostEqual(picture.pictureRotation(), -38.18, 1) # rotate map map.setMapRotation(45) self.assertAlmostEqual(picture.pictureRotation(), -38.18 + 45, 1) # add an offset picture.setNorthOffset(-10) self.assertAlmostEqual(picture.pictureRotation(), -38.18 + 35, 1)
class TestLayoutBase(TestQgsPalLabeling): layer = None """:type: QgsVectorLayer""" @classmethod def setUpClass(cls): if not cls._BaseSetup: TestQgsPalLabeling.setUpClass() # the blue background (set via layer style) to match renderchecker's TestQgsPalLabeling.loadFeatureLayer('background', True) cls._TestKind = 0 # OutputKind.(Img|Svg|Pdf) @classmethod def tearDownClass(cls): """Run after all tests""" TestQgsPalLabeling.tearDownClass() cls.removeMapLayer(cls.layer) cls.layer = None # avoid crash on finish, probably related to https://bugreports.qt.io/browse/QTBUG-35760 QThreadPool.globalInstance().waitForDone() def setUp(self): """Run before each test.""" super(TestLayoutBase, self).setUp() self._TestImage = '' # ensure per test map settings stay encapsulated self._TestMapSettings = self.cloneMapSettings(self._MapSettings) self._Mismatch = 0 self._ColorTol = 0 self._Mismatches.clear() self._ColorTols.clear() def _set_up_composition(self, width, height, dpi, engine_settings): # set up layout and add map self._c = QgsLayout(QgsProject.instance()) """:type: QgsLayout""" # self._c.setUseAdvancedEffects(False) self._c.renderContext().setDpi(dpi) # 600 x 400 px = 211.67 x 141.11 mm @ 72 dpi paperw = width * 25.4 / dpi paperh = height * 25.4 / dpi page = QgsLayoutItemPage(self._c) page.attemptResize(QgsLayoutSize(paperw, paperh)) self._c.pageCollection().addPage(page) # NOTE: do not use QgsLayoutItemMap(self._c, 0, 0, paperw, paperh) since # it only takes integers as parameters and the composition will grow # larger based upon union of item scene rectangles and a slight buffer # see end of QgsComposition::compositionBounds() # add map as small graphics item first, then set its scene QRectF later self._cmap = QgsLayoutItemMap(self._c) self._cmap.attemptSetSceneRect(QRectF(10, 10, 10, 10)) """:type: QgsLayoutItemMap""" self._cmap.setFrameEnabled(False) self._cmap.setLayers(self._TestMapSettings.layers()) self._c.addLayoutItem(self._cmap) # now expand map to fill page and set its extent self._cmap.attemptSetSceneRect(QRectF(0, 0, paperw, paperw)) self._cmap.setExtent(self.aoiExtent()) # self._cmap.updateCachedImage() # composition takes labeling engine settings from project QgsProject.instance().setLabelingEngineSettings(engine_settings) # noinspection PyUnusedLocal def _get_layout_image(self, width, height, dpi): image = QImage(QSize(width, height), self._TestMapSettings.outputImageFormat()) image.fill(QColor(152, 219, 249).rgb()) image.setDotsPerMeterX(dpi / 25.4 * 1000) image.setDotsPerMeterY(dpi / 25.4 * 1000) p = QPainter(image) p.setRenderHint( QPainter.Antialiasing, self._TestMapSettings.testFlag(QgsMapSettings.Antialiasing)) exporter = QgsLayoutExporter(self._c) exporter.renderPage(p, 0) p.end() # image = self._c.printPageAsRaster(0) # """:type: QImage""" if image.isNull(): return False, '' filepath = getTempfilePath('png') res = image.save(filepath, 'png') if not res: os.unlink(filepath) filepath = '' return res, filepath def _get_layout_svg_image(self, width, height, dpi): svgpath = getTempfilePath('svg') temp_size = os.path.getsize(svgpath) svg_g = QSvgGenerator() # noinspection PyArgumentList svg_g.setTitle(QgsProject.instance().title()) svg_g.setFileName(svgpath) svg_g.setSize(QSize(width, height)) svg_g.setViewBox(QRect(0, 0, width, height)) svg_g.setResolution(dpi) sp = QPainter(svg_g) exporter = QgsLayoutExporter(self._c) exporter.renderPage(sp, 0) sp.end() if temp_size == os.path.getsize(svgpath): return False, '' image = QImage(width, height, self._TestMapSettings.outputImageFormat()) image.fill(QColor(152, 219, 249).rgb()) image.setDotsPerMeterX(dpi / 25.4 * 1000) image.setDotsPerMeterY(dpi / 25.4 * 1000) svgr = QSvgRenderer(svgpath) p = QPainter(image) p.setRenderHint( QPainter.Antialiasing, self._TestMapSettings.testFlag(QgsMapSettings.Antialiasing)) p.setRenderHint(QPainter.TextAntialiasing) svgr.render(p) p.end() filepath = getTempfilePath('png') res = image.save(filepath, 'png') if not res: os.unlink(filepath) filepath = '' # TODO: remove .svg file as well? return res, filepath def _get_layout_pdf_image(self, width, height, dpi): pdfpath = getTempfilePath('pdf') temp_size = os.path.getsize(pdfpath) p = QPrinter() p.setOutputFormat(QPrinter.PdfFormat) p.setOutputFileName(pdfpath) p.setPaperSize( QSizeF(self._c.pageCollection().page(0).sizeWithUnits().width(), self._c.pageCollection().page(0).sizeWithUnits().height()), QPrinter.Millimeter) p.setFullPage(True) p.setColorMode(QPrinter.Color) p.setResolution(self._c.renderContext().dpi()) pdf_p = QPainter(p) # page_mm = p.pageRect(QPrinter.Millimeter) # page_px = p.pageRect(QPrinter.DevicePixel) # self._c.render(pdf_p, page_px, page_mm) exporter = QgsLayoutExporter(self._c) exporter.renderPage(pdf_p, 0) pdf_p.end() if temp_size == os.path.getsize(pdfpath): return False, '' filepath = getTempfilePath('png') # Poppler (pdftocairo or pdftoppm): # PDFUTIL -png -singlefile -r 72 -x 0 -y 0 -W 420 -H 280 in.pdf pngbase # muPDF (mudraw): # PDFUTIL -c rgb[a] -r 72 -w 420 -h 280 -o out.png in.pdf if PDFUTIL.strip().endswith('pdftocairo'): filebase = os.path.join( os.path.dirname(filepath), os.path.splitext(os.path.basename(filepath))[0]) call = [ PDFUTIL, '-png', '-singlefile', '-r', str(dpi), '-x', '0', '-y', '0', '-W', str(width), '-H', str(height), pdfpath, filebase ] elif PDFUTIL.strip().endswith('mudraw'): call = [ PDFUTIL, '-c', 'rgba', '-r', str(dpi), '-w', str(width), '-h', str(height), # '-b', '8', '-o', filepath, pdfpath ] else: return False, '' qDebug("_get_layout_pdf_image call: {0}".format(' '.join(call))) res = False try: subprocess.check_call(call) res = True except subprocess.CalledProcessError as e: qDebug("_get_layout_pdf_image failed!\n" "cmd: {0}\n" "returncode: {1}\n" "message: {2}".format(e.cmd, e.returncode, e.message)) if not res: os.unlink(filepath) filepath = '' return res, filepath def get_layout_output(self, kind): ms = self._TestMapSettings osize = ms.outputSize() width, height, dpi = osize.width(), osize.height(), ms.outputDpi() self._set_up_composition(width, height, dpi, ms.labelingEngineSettings()) if kind == OutputKind.Svg: return self._get_layout_svg_image(width, height, dpi) elif kind == OutputKind.Pdf: return self._get_layout_pdf_image(width, height, dpi) else: # OutputKind.Img return self._get_layout_image(width, height, dpi) # noinspection PyUnusedLocal def checkTest(self, **kwargs): self.layer.setLabeling(QgsVectorLayerSimpleLabeling(self.lyr)) ms = self._MapSettings # class settings settings_type = 'Class' if self._TestMapSettings is not None: ms = self._TestMapSettings # per test settings settings_type = 'Test' if 'PAL_VERBOSE' in os.environ: qDebug('MapSettings type: {0}'.format(settings_type)) qDebug(mapSettingsString(ms)) res_m, self._TestImage = self.get_layout_output(self._TestKind) self.assertTrue(res_m, 'Failed to retrieve/save output from layout') self.saveControlImage(self._TestImage) mismatch = 0 if 'PAL_NO_MISMATCH' not in os.environ: # some mismatch expected mismatch = self._Mismatch if self._Mismatch else 20 if self._TestGroup in self._Mismatches: mismatch = self._Mismatches[self._TestGroup] colortol = 0 if 'PAL_NO_COLORTOL' not in os.environ: colortol = self._ColorTol if self._ColorTol else 0 if self._TestGroup in self._ColorTols: colortol = self._ColorTols[self._TestGroup] self.assertTrue(*self.renderCheck( mismatch=mismatch, colortol=colortol, imgpath=self._TestImage))
def testCopyPaste(self): p = QgsProject() l = QgsLayout(p) # clear clipboard mime_data = QMimeData() mime_data.setData("text/xml", QByteArray()) clipboard = QApplication.clipboard() clipboard.setMimeData(mime_data) # add an item item1 = QgsLayoutItemLabel(l) item1.setText('label 1') l.addLayoutItem(item1) item1.setSelected(True) item2 = QgsLayoutItemLabel(l) item2.setText('label 2') l.addLayoutItem(item2) item2.setSelected(True) # multiframes multiframe1 = QgsLayoutItemHtml(l) multiframe1.setHtml('mf1') l.addMultiFrame(multiframe1) frame1 = QgsLayoutFrame(l, multiframe1) frame1.setId('frame1a') multiframe1.addFrame(frame1) frame1b = QgsLayoutFrame(l, multiframe1) frame1b.setId('frame1b') multiframe1.addFrame(frame1b) # not selected frame1c = QgsLayoutFrame(l, multiframe1) frame1c.setId('frame1b') multiframe1.addFrame(frame1c) # not selected multiframe2 = QgsLayoutItemHtml(l) multiframe2.setHtml('mf2') l.addMultiFrame(multiframe2) frame2 = QgsLayoutFrame(l, multiframe2) frame2.setId('frame2') multiframe2.addFrame(frame2) frame1.setSelected(True) frame2.setSelected(True) view = QgsLayoutView() view.setCurrentLayout(l) self.assertFalse(view.hasItemsInClipboard()) view.copySelectedItems(QgsLayoutView.ClipboardCopy) self.assertTrue(view.hasItemsInClipboard()) pasted = view.pasteItems(QgsLayoutView.PasteModeCursor) self.assertEqual(len(pasted), 4) new_multiframes = [m for m in l.multiFrames() if m not in [multiframe1, multiframe2]] self.assertEqual(len(new_multiframes), 2) self.assertIn(pasted[0], l.items()) self.assertIn(pasted[1], l.items()) labels = [p for p in pasted if p.type() == QgsLayoutItemRegistry.LayoutLabel] self.assertIn(sip.cast(labels[0], QgsLayoutItemLabel).text(), ('label 1', 'label 2')) self.assertIn(sip.cast(labels[1], QgsLayoutItemLabel).text(), ('label 1', 'label 2')) frames = [p for p in pasted if p.type() == QgsLayoutItemRegistry.LayoutFrame] pasted_frame1 = sip.cast(frames[0], QgsLayoutFrame) pasted_frame2 = sip.cast(frames[1], QgsLayoutFrame) self.assertIn(pasted_frame1.multiFrame(), new_multiframes) self.assertIn(new_multiframes[0].frames()[0].uuid(), (pasted_frame1.uuid(), pasted_frame2.uuid())) self.assertIn(pasted_frame2.multiFrame(), new_multiframes) self.assertIn(new_multiframes[1].frames()[0].uuid(), (pasted_frame1.uuid(), pasted_frame2.uuid())) self.assertEqual(frame1.multiFrame(), multiframe1) self.assertCountEqual(multiframe1.frames(), [frame1, frame1b, frame1c]) self.assertEqual(frame1b.multiFrame(), multiframe1) self.assertEqual(frame1c.multiFrame(), multiframe1) self.assertEqual(frame2.multiFrame(), multiframe2) self.assertCountEqual(multiframe2.frames(), [frame2]) # copy specific item view.copyItems([item2], QgsLayoutView.ClipboardCopy) l2 = QgsLayout(p) view2 = QgsLayoutView() view2.setCurrentLayout(l2) pasted = view2.pasteItems(QgsLayoutView.PasteModeCursor) self.assertEqual(len(pasted), 1) self.assertIn(pasted[0], l2.items()) self.assertEqual(sip.cast(pasted[0], QgsLayoutItemLabel).text(), 'label 2')
def testLegendRenderWithMapTheme(self): """Test rendering legends linked to map themes""" QgsProject.instance().removeAllMapLayers() point_path = os.path.join(TEST_DATA_DIR, 'points.shp') point_layer = QgsVectorLayer(point_path, 'points', 'ogr') line_path = os.path.join(TEST_DATA_DIR, 'lines.shp') line_layer = QgsVectorLayer(line_path, 'lines', 'ogr') QgsProject.instance().clear() QgsProject.instance().addMapLayers([point_layer, line_layer]) marker_symbol = QgsMarkerSymbol.createSimple({ 'color': '#ff0000', 'outline_style': 'no', 'size': '5' }) point_layer.setRenderer(QgsSingleSymbolRenderer(marker_symbol)) point_layer.styleManager().addStyleFromLayer("red") line_symbol = QgsLineSymbol.createSimple({ 'color': '#ff0000', 'line_width': '2' }) line_layer.setRenderer(QgsSingleSymbolRenderer(line_symbol)) line_layer.styleManager().addStyleFromLayer("red") red_record = QgsMapThemeCollection.MapThemeRecord() point_red_record = QgsMapThemeCollection.MapThemeLayerRecord( point_layer) point_red_record.usingCurrentStyle = True point_red_record.currentStyle = 'red' red_record.addLayerRecord(point_red_record) line_red_record = QgsMapThemeCollection.MapThemeLayerRecord(line_layer) line_red_record.usingCurrentStyle = True line_red_record.currentStyle = 'red' red_record.addLayerRecord(line_red_record) QgsProject.instance().mapThemeCollection().insert('red', red_record) marker_symbol1 = QgsMarkerSymbol.createSimple({ 'color': '#0000ff', 'outline_style': 'no', 'size': '5' }) marker_symbol2 = QgsMarkerSymbol.createSimple({ 'color': '#0000ff', 'name': 'diamond', 'outline_style': 'no', 'size': '5' }) marker_symbol3 = QgsMarkerSymbol.createSimple({ 'color': '#0000ff', 'name': 'rectangle', 'outline_style': 'no', 'size': '5' }) point_layer.setRenderer( QgsCategorizedSymbolRenderer('Class', [ QgsRendererCategory('B52', marker_symbol1, ''), QgsRendererCategory('Biplane', marker_symbol2, ''), QgsRendererCategory('Jet', marker_symbol3, ''), ])) point_layer.styleManager().addStyleFromLayer("blue") line_symbol = QgsLineSymbol.createSimple({ 'color': '#0000ff', 'line_width': '2' }) line_layer.setRenderer(QgsSingleSymbolRenderer(line_symbol)) line_layer.styleManager().addStyleFromLayer("blue") blue_record = QgsMapThemeCollection.MapThemeRecord() point_blue_record = QgsMapThemeCollection.MapThemeLayerRecord( point_layer) point_blue_record.usingCurrentStyle = True point_blue_record.currentStyle = 'blue' blue_record.addLayerRecord(point_blue_record) line_blue_record = QgsMapThemeCollection.MapThemeLayerRecord( line_layer) line_blue_record.usingCurrentStyle = True line_blue_record.currentStyle = 'blue' blue_record.addLayerRecord(line_blue_record) QgsProject.instance().mapThemeCollection().insert('blue', blue_record) layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() map1 = QgsLayoutItemMap(layout) map1.attemptSetSceneRect(QRectF(20, 20, 80, 80)) map1.setFrameEnabled(True) map1.setLayers([point_layer, line_layer]) layout.addLayoutItem(map1) map1.setExtent(point_layer.extent()) map1.setFollowVisibilityPreset(True) map1.setFollowVisibilityPresetName('red') map2 = QgsLayoutItemMap(layout) map2.attemptSetSceneRect(QRectF(20, 120, 80, 80)) map2.setFrameEnabled(True) map2.setLayers([point_layer, line_layer]) layout.addLayoutItem(map2) map2.setExtent(point_layer.extent()) map2.setFollowVisibilityPreset(True) map2.setFollowVisibilityPresetName('blue') legend = QgsLayoutItemLegend(layout) legend.setTitle("Legend") legend.attemptSetSceneRect(QRectF(120, 20, 80, 80)) legend.setFrameEnabled(True) legend.setFrameStrokeWidth(QgsLayoutMeasurement(2)) legend.setBackgroundColor(QColor(200, 200, 200)) legend.setTitle('') layout.addLayoutItem(legend) legend.setLinkedMap(map1) legend2 = QgsLayoutItemLegend(layout) legend2.setTitle("Legend") legend2.attemptSetSceneRect(QRectF(120, 120, 80, 80)) legend2.setFrameEnabled(True) legend2.setFrameStrokeWidth(QgsLayoutMeasurement(2)) legend2.setBackgroundColor(QColor(200, 200, 200)) legend2.setTitle('') layout.addLayoutItem(legend2) legend2.setLinkedMap(map2) checker = QgsLayoutChecker('composer_legend_theme', layout) checker.setControlPathPrefix("composer_legend") result, message = checker.testLayout() self.report += checker.report() self.assertTrue(result, message) QgsProject.instance().clear()