Last active
February 20, 2020 12:39
-
-
Save gpprojekt-marcin/fcc6002afa65b0b080acc2651b50d2d9 to your computer and use it in GitHub Desktop.
PyQGIS Composer Map Extends in layer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from qgis.core import * | |
from PyQt5.QtCore import QVariant | |
project = QgsProject.instance() | |
projectLayoutManager = project.layoutManager() | |
layout = projectLayoutManager.layoutByName('KIERUNKI_A0+') | |
treeRoot = project.layerTreeRoot() | |
temp = QgsVectorLayer("Polygon?crs=epsg:2180", "map_extends", "memory") | |
temp_data = temp.dataProvider() | |
temp.startEditing() | |
temp_data.addAttributes([QgsField('ID', QVariant.String)]) | |
temp.updateFields() | |
for item in layout.items(): | |
if type(item) == QgsLayoutItemMap: | |
feat = QgsFeature() | |
feat.setGeometry(QgsGeometry.fromRect(item.extent())) | |
feat.initAttributes(1) | |
feat.setAttribute(0,item.id()) | |
temp.addFeature(feat) | |
temp.updateExtents() | |
temp.commitChanges() | |
treeRoot.addLayer(temp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment