本文整理汇总了Python中qgis.core.QgsLayoutItemMap.extent方法的典型用法代码示例。如果您正苦于以下问题:Python QgsLayoutItemMap.extent方法的具体用法?Python QgsLayoutItemMap.extent怎么用?Python QgsLayoutItemMap.extent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qgis.core.QgsLayoutItemMap
的用法示例。
在下文中一共展示了QgsLayoutItemMap.extent方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: rotation_test
# 需要导入模块: from qgis.core import QgsLayoutItemMap [as 别名]
# 或者: from qgis.core.QgsLayoutItemMap import extent [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)
示例2: test_datadefined_margin
# 需要导入模块: from qgis.core import QgsLayoutItemMap [as 别名]
# 或者: from qgis.core.QgsLayoutItemMap import extent [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)