当前位置: 首页>>代码示例>>Python>>正文


Python QgsLayoutItemMap.extent方法代码示例

本文整理汇总了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)
开发者ID:sbrunner,项目名称:QGIS,代码行数:49,代码来源:test_qgslayoutatlas.py

示例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)
开发者ID:aaime,项目名称:QGIS,代码行数:46,代码来源:test_qgslayoutatlas.py


注:本文中的qgis.core.QgsLayoutItemMap.extent方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。