本文整理汇总了Python中qgis.core.QgsPrintLayout.addLayoutItem方法的典型用法代码示例。如果您正苦于以下问题:Python QgsPrintLayout.addLayoutItem方法的具体用法?Python QgsPrintLayout.addLayoutItem怎么用?Python QgsPrintLayout.addLayoutItem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qgis.core.QgsPrintLayout
的用法示例。
在下文中一共展示了QgsPrintLayout.addLayoutItem方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testExpressionInText
# 需要导入模块: from qgis.core import QgsPrintLayout [as 别名]
# 或者: from qgis.core.QgsPrintLayout import addLayoutItem [as 别名]
def testExpressionInText(self):
"""Test expressions embedded in legend node text"""
point_path = os.path.join(TEST_DATA_DIR, 'points.shp')
point_layer = QgsVectorLayer(point_path, 'points', 'ogr')
layout = QgsPrintLayout(QgsProject.instance())
layout.setName('LAYOUT')
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, 100, 100))
legend.setFrameEnabled(True)
legend.setFrameStrokeWidth(QgsLayoutMeasurement(2))
legend.setBackgroundColor(QColor(200, 200, 200))
legend.setTitle('')
legend.setLegendFilterByMapEnabled(False)
legend.setStyleFont(QgsLegendStyle.Title, QgsFontUtils.getStandardTestFont('Bold', 16))
legend.setStyleFont(QgsLegendStyle.Group, QgsFontUtils.getStandardTestFont('Bold', 16))
legend.setStyleFont(QgsLegendStyle.Subgroup, QgsFontUtils.getStandardTestFont('Bold', 16))
legend.setStyleFont(QgsLegendStyle.Symbol, QgsFontUtils.getStandardTestFont('Bold', 16))
legend.setStyleFont(QgsLegendStyle.SymbolLabel, QgsFontUtils.getStandardTestFont('Bold', 16))
legend.setAutoUpdateModel(False)
QgsProject.instance().addMapLayers([point_layer])
s = QgsMapSettings()
s.setLayers([point_layer])
group = legend.model().rootGroup().addGroup("Group [% 1 + 5 %] [% @layout_name %]")
layer_tree_layer = group.addLayer(point_layer)
layer_tree_layer.setCustomProperty("legend/title-label", 'bbbb [% 1+2 %] xx [% @layout_name %] [% @layer_name %]')
QgsMapLayerLegendUtils.setLegendNodeUserLabel(layer_tree_layer, 0, 'xxxx')
legend.model().refreshLayerLegend(layer_tree_layer)
legend.model().layerLegendNodes(layer_tree_layer)[0].setUserLabel('bbbb [% 1+2 %] xx [% @layout_name %] [% @layer_name %]')
layout.addLayoutItem(legend)
legend.setLinkedMap(map)
map.setExtent(QgsRectangle(-102.51, 41.16, -102.36, 41.30))
checker = QgsLayoutChecker(
'composer_legend_expressions', layout)
checker.setControlPathPrefix("composer_legend")
result, message = checker.testLayout()
self.assertTrue(result, message)
QgsProject.instance().removeMapLayers([point_layer.id()])
示例2: testCase
# 需要导入模块: from qgis.core import QgsPrintLayout [as 别名]
# 或者: from qgis.core.QgsPrintLayout import addLayoutItem [as 别名]
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 = QgsPrintLayout(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)
示例3: rotation_test
# 需要导入模块: from qgis.core import QgsPrintLayout [as 别名]
# 或者: from qgis.core.QgsPrintLayout import addLayoutItem [as 别名]
def rotation_test(self):
# We will create a polygon layer with a rotated rectangle.
# Then we will make it the object layer for the atlas,
# rotate the map and test that the bounding rectangle
# is smaller than the bounds without rotation.
polygonLayer = QgsVectorLayer('Polygon', 'test_polygon', 'memory')
poly = QgsFeature(polygonLayer.fields())
points = [(10, 15), (15, 10), (45, 40), (40, 45)]
poly.setGeometry(QgsGeometry.fromPolygonXY([[QgsPointXY(x[0], x[1]) for x in points]]))
polygonLayer.dataProvider().addFeatures([poly])
QgsProject.instance().addMapLayer(polygonLayer)
# Recreating the layout locally
composition = QgsPrintLayout(QgsProject.instance())
composition.initializeDefaults()
# the atlas map
atlasMap = QgsLayoutItemMap(composition)
atlasMap.attemptSetSceneRect(QRectF(20, 20, 130, 130))
atlasMap.setFrameEnabled(True)
atlasMap.setLayers([polygonLayer])
atlasMap.setExtent(QgsRectangle(0, 0, 100, 50))
composition.addLayoutItem(atlasMap)
# the atlas
atlas = composition.atlas()
atlas.setCoverageLayer(polygonLayer)
atlas.setEnabled(True)
atlasMap.setAtlasDriven(True)
atlasMap.setAtlasScalingMode(QgsLayoutItemMap.Auto)
atlasMap.setAtlasMargin(0.0)
# Testing
atlasMap.setMapRotation(0.0)
atlas.beginRender()
atlas.first()
nonRotatedExtent = QgsRectangle(atlasMap.extent())
atlasMap.setMapRotation(45.0)
atlas.first()
rotatedExtent = QgsRectangle(atlasMap.extent())
self.assertLess(rotatedExtent.width(), nonRotatedExtent.width() * 0.9)
self.assertLess(rotatedExtent.height(), nonRotatedExtent.height() * 0.9)
QgsProject.instance().removeMapLayer(polygonLayer)
示例4: test_datadefined_margin
# 需要导入模块: from qgis.core import QgsPrintLayout [as 别名]
# 或者: from qgis.core.QgsPrintLayout import addLayoutItem [as 别名]
def test_datadefined_margin(self):
polygonLayer = QgsVectorLayer('Polygon?field=margin:int', 'test_polygon', 'memory')
poly = QgsFeature(polygonLayer.fields())
poly.setAttributes([0])
poly.setGeometry(QgsGeometry.fromWkt('Polygon((30 30, 40 30, 40 40, 30 40, 30 30))'))
polygonLayer.dataProvider().addFeatures([poly])
poly = QgsFeature(polygonLayer.fields())
poly.setAttributes([10])
poly.setGeometry(QgsGeometry.fromWkt('Polygon((10 10, 20 10, 20 20, 10 20, 10 10))'))
polygonLayer.dataProvider().addFeatures([poly])
poly = QgsFeature(polygonLayer.fields())
poly.setAttributes([20])
poly.setGeometry(QgsGeometry.fromWkt('Polygon((50 50, 60 50, 60 60, 50 60, 50 50))'))
polygonLayer.dataProvider().addFeatures([poly])
QgsProject.instance().addMapLayer(polygonLayer)
layout = QgsPrintLayout(QgsProject.instance())
map = QgsLayoutItemMap(layout)
map.setCrs(polygonLayer.crs())
map.attemptSetSceneRect(QRectF(20, 20, 130, 130))
map.setFrameEnabled(True)
map.setLayers([polygonLayer])
map.setExtent(QgsRectangle(0, 0, 100, 50))
layout.addLayoutItem(map)
atlas = layout.atlas()
atlas.setCoverageLayer(polygonLayer)
atlas.setEnabled(True)
map.setAtlasDriven(True)
map.setAtlasScalingMode(QgsLayoutItemMap.Auto)
map.setAtlasMargin(77.0)
map.dataDefinedProperties().setProperty(QgsLayoutObject.MapAtlasMargin, QgsProperty.fromExpression('margin/2'))
atlas.beginRender()
atlas.first()
self.assertEqual(map.extent(), QgsRectangle(25, 30, 45, 40))
self.assertTrue(atlas.next())
self.assertEqual(map.extent(), QgsRectangle(4.5, 9.75, 25.5, 20.25))
self.assertTrue(atlas.next())
self.assertEqual(map.extent(), QgsRectangle(44, 49.5, 66, 60.5))
QgsProject.instance().removeMapLayer(polygonLayer)
示例5: prepareIteratorLayout
# 需要导入模块: from qgis.core import QgsPrintLayout [as 别名]
# 或者: from qgis.core.QgsPrintLayout import addLayoutItem [as 别名]
def prepareIteratorLayout(self):
layer_path = os.path.join(TEST_DATA_DIR, 'france_parts.shp')
layer = QgsVectorLayer(layer_path, 'test', "ogr")
project = QgsProject()
project.addMapLayers([layer])
# select epsg:2154
crs = QgsCoordinateReferenceSystem()
crs.createFromSrid(2154)
project.setCrs(crs)
layout = QgsPrintLayout(project)
layout.initializeDefaults()
# fix the renderer, fill with green
props = {"color": "0,127,0", "outline_width": "4", "outline_color": '255,255,255'}
fillSymbol = QgsFillSymbol.createSimple(props)
renderer = QgsSingleSymbolRenderer(fillSymbol)
layer.setRenderer(renderer)
# the atlas map
atlas_map = QgsLayoutItemMap(layout)
atlas_map.attemptSetSceneRect(QRectF(20, 20, 130, 130))
atlas_map.setFrameEnabled(True)
atlas_map.setLayers([layer])
layout.addLayoutItem(atlas_map)
# the atlas
atlas = layout.atlas()
atlas.setCoverageLayer(layer)
atlas.setEnabled(True)
atlas_map.setExtent(
QgsRectangle(332719.06221504929, 6765214.5887386119, 560957.85090677091, 6993453.3774303338))
atlas_map.setAtlasDriven(True)
atlas_map.setAtlasScalingMode(QgsLayoutItemMap.Auto)
atlas_map.setAtlasMargin(0.10)
return project, layout
示例6: TestQgsLayoutAtlas
# 需要导入模块: from qgis.core import QgsPrintLayout [as 别名]
# 或者: from qgis.core.QgsPrintLayout import addLayoutItem [as 别名]
class TestQgsLayoutAtlas(unittest.TestCase):
def setUp(self):
self.report = "<h1>Python QgsLayoutAtlas 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 testCase(self):
self.TEST_DATA_DIR = unitTestDataPath()
tmppath = tempfile.mkdtemp()
for file in glob.glob(os.path.join(self.TEST_DATA_DIR, 'france_parts.*')):
shutil.copy(os.path.join(self.TEST_DATA_DIR, file), tmppath)
vectorFileInfo = QFileInfo(tmppath + "/france_parts.shp")
mVectorLayer = QgsVectorLayer(vectorFileInfo.filePath(), vectorFileInfo.completeBaseName(), "ogr")
QgsProject.instance().addMapLayers([mVectorLayer])
self.layers = [mVectorLayer]
# create layout with layout map
# select epsg:2154
crs = QgsCoordinateReferenceSystem()
crs.createFromSrid(2154)
QgsProject.instance().setCrs(crs)
self.layout = QgsPrintLayout(QgsProject.instance())
self.layout.initializeDefaults()
# fix the renderer, fill with green
props = {"color": "0,127,0", 'outline_color': 'black'}
fillSymbol = QgsFillSymbol.createSimple(props)
renderer = QgsSingleSymbolRenderer(fillSymbol)
mVectorLayer.setRenderer(renderer)
# the atlas map
self.atlas_map = QgsLayoutItemMap(self.layout)
self.atlas_map.attemptSetSceneRect(QRectF(20, 20, 130, 130))
self.atlas_map.setFrameEnabled(True)
self.atlas_map.setLayers([mVectorLayer])
self.layout.addLayoutItem(self.atlas_map)
# the atlas
self.atlas = self.layout.atlas()
self.atlas.setCoverageLayer(mVectorLayer)
self.atlas.setEnabled(True)
# an overview
self.overview = QgsLayoutItemMap(self.layout)
self.overview.attemptSetSceneRect(QRectF(180, 20, 50, 50))
self.overview.setFrameEnabled(True)
self.overview.overview().setLinkedMap(self.atlas_map)
self.overview.setLayers([mVectorLayer])
self.layout.addLayoutItem(self.overview)
nextent = QgsRectangle(49670.718, 6415139.086, 699672.519, 7065140.887)
self.overview.setExtent(nextent)
# set the fill symbol of the overview map
props2 = {"color": "127,0,0,127", 'outline_color': 'black'}
fillSymbol2 = QgsFillSymbol.createSimple(props2)
self.overview.overview().setFrameSymbol(fillSymbol2)
# header label
self.mLabel1 = QgsLayoutItemLabel(self.layout)
self.layout.addLayoutItem(self.mLabel1)
self.mLabel1.setText("[% \"NAME_1\" %] area")
self.mLabel1.setFont(QgsFontUtils.getStandardTestFont())
self.mLabel1.adjustSizeToText()
self.mLabel1.attemptSetSceneRect(QRectF(150, 5, 60, 15))
self.mLabel1.setMarginX(1)
self.mLabel1.setMarginY(1)
# feature number label
self.mLabel2 = QgsLayoutItemLabel(self.layout)
self.layout.addLayoutItem(self.mLabel2)
self.mLabel2.setText("# [%@atlas_featurenumber || ' / ' || @atlas_totalfeatures%]")
self.mLabel2.setFont(QgsFontUtils.getStandardTestFont())
self.mLabel2.adjustSizeToText()
self.mLabel2.attemptSetSceneRect(QRectF(150, 200, 60, 15))
self.mLabel2.setMarginX(1)
self.mLabel2.setMarginY(1)
self.filename_test()
self.autoscale_render_test()
self.fixedscale_render_test()
self.predefinedscales_render_test()
self.hidden_render_test()
self.legend_test()
self.rotation_test()
shutil.rmtree(tmppath, True)
def testReadWriteXml(self):
p = QgsProject()
vectorFileInfo = QFileInfo(unitTestDataPath() + "/france_parts.shp")
vector_layer = QgsVectorLayer(vectorFileInfo.filePath(), vectorFileInfo.completeBaseName(), "ogr")
self.assertTrue(vector_layer.isValid())
p.addMapLayer(vector_layer)
#.........这里部分代码省略.........
示例7: testCombo
# 需要导入模块: from qgis.core import QgsPrintLayout [as 别名]
# 或者: from qgis.core.QgsPrintLayout import addLayoutItem [as 别名]
def testCombo(self):
project = QgsProject()
layout = QgsPrintLayout(project)
combo = QgsLayoutItemComboBox(None)
spy = QSignalSpy(combo.itemChanged)
self.assertEqual(combo.count(), 0)
self.assertIsNone(combo.currentLayout())
self.assertIsNone(combo.currentItem())
self.assertIsNone(combo.item(0))
self.assertIsNone(combo.item(-1))
self.assertIsNone(combo.item(1))
combo.setItem(None)
self.assertIsNone(combo.currentItem())
self.assertEqual(combo.currentIndex(), -1)
self.assertEqual(len(spy), 0)
combo.setCurrentLayout(layout)
self.assertEqual(combo.currentIndex(), -1)
self.assertEqual(len(spy), 0)
self.assertEqual(combo.currentLayout(), layout)
self.assertEqual(combo.count(), 0)
self.assertIsNone(combo.currentItem())
self.assertIsNone(combo.item(0))
self.assertIsNone(combo.item(-1))
self.assertIsNone(combo.item(1))
combo.setItem(None)
self.assertIsNone(combo.currentItem())
self.assertEqual(len(spy), 0)
combo.setAllowEmptyItem(True)
self.assertEqual(combo.currentIndex(), 0)
self.assertEqual(combo.count(), 1)
self.assertEqual(len(spy), 2)
self.assertEqual(combo.currentLayout(), layout)
self.assertIsNone(combo.currentItem())
self.assertIsNone(combo.item(0))
self.assertIsNone(combo.item(-1))
self.assertIsNone(combo.item(1))
combo.setItem(None)
self.assertIsNone(combo.currentItem())
self.assertEqual(len(spy), 2)
self.assertEqual(combo.currentIndex(), 0)
self.assertEqual(combo.itemText(0), '')
combo.setAllowEmptyItem(False)
self.assertEqual(combo.currentIndex(), -1)
self.assertEqual(combo.count(), 0)
self.assertEqual(len(spy), 4)
self.assertEqual(combo.currentLayout(), layout)
self.assertIsNone(combo.currentItem())
self.assertIsNone(combo.item(0))
self.assertIsNone(combo.item(-1))
self.assertIsNone(combo.item(1))
combo.setItem(None)
self.assertIsNone(combo.currentItem())
self.assertEqual(len(spy), 4)
self.assertEqual(combo.currentIndex(), -1)
label1 = QgsLayoutItemLabel(layout)
label1.setId('llll')
# don't add to layout yet!
combo.setItem(label1)
self.assertIsNone(combo.currentItem())
self.assertEqual(len(spy), 4)
self.assertEqual(combo.currentIndex(), -1)
combo.setAllowEmptyItem(True)
combo.setItem(label1)
self.assertIsNone(combo.currentItem())
self.assertEqual(len(spy), 6)
self.assertEqual(combo.currentIndex(), 0)
combo.setAllowEmptyItem(False)
layout.addLayoutItem(label1)
self.assertEqual(combo.currentIndex(), 0)
self.assertEqual(combo.count(), 1)
self.assertEqual(combo.itemText(0), 'llll')
self.assertEqual(len(spy), 10)
self.assertEqual(combo.currentLayout(), layout)
self.assertEqual(combo.currentItem(), label1)
self.assertEqual(combo.item(0), label1)
self.assertIsNone(combo.item(-1))
self.assertIsNone(combo.item(1))
combo.setItem(None)
self.assertIsNone(combo.currentItem())
self.assertEqual(len(spy), 11)
self.assertEqual(combo.currentIndex(), -1)
combo.setItem(label1)
self.assertEqual(combo.currentItem(), label1)
self.assertEqual(len(spy), 12)
self.assertEqual(combo.currentIndex(), 0)
combo.setAllowEmptyItem(True)
self.assertEqual(combo.currentIndex(), 1)
self.assertEqual(combo.count(), 2)
self.assertEqual(combo.itemText(0), '')
self.assertEqual(combo.itemText(1), 'llll')
self.assertEqual(len(spy), 13)
self.assertEqual(combo.currentLayout(), layout)
self.assertEqual(combo.currentItem(), label1)
#.........这里部分代码省略.........