Skip to content

Instantly share code, notes, and snippets.

@gpprojekt-marcin
Last active February 20, 2020 12:39
Show Gist options
  • Save gpprojekt-marcin/fcc6002afa65b0b080acc2651b50d2d9 to your computer and use it in GitHub Desktop.
Save gpprojekt-marcin/fcc6002afa65b0b080acc2651b50d2d9 to your computer and use it in GitHub Desktop.
PyQGIS Composer Map Extends in layer
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