Skip to content

Instantly share code, notes, and snippets.

@datagistips
Last active February 27, 2020 11:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save datagistips/5766073f532bcfd85cb9f8bccacb1c76 to your computer and use it in GitHub Desktop.
Save datagistips/5766073f532bcfd85cb9f8bccacb1c76 to your computer and use it in GitHub Desktop.
Processing python code to get the print layout extents as a layer in qgis map
# -*- coding: utf-8 -*-
from PyQt5.QtCore import QCoreApplication, QVariant
from qgis.core import (QgsVectorLayer, QgsFields, QgsField, QgsProject,QgsLayoutItemMap,QgsFeature,QgsGeometry,QgsCoordinateReferenceSystem,
QgsProcessing,
QgsFeatureSink,
QgsProcessingAlgorithm,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink)
class ExampleProcessingAlgorithm(QgsProcessingAlgorithm):
"""
This is an example algorithm that takes a vector layer and
creates a new identical one.
It is meant to be used as an example of how to create your own
algorithms and explain methods and variables used to do it. An
algorithm like this will be available in all elements, and there
is not need for additional work.
All Processing algorithms should extend the QgsProcessingAlgorithm
class.
"""
INPUT = 'INPUT'
OUTPUT = 'OUTPUT'
def tr(self, string):
"""
Returns a translatable string with the self.tr() function.
"""
return QCoreApplication.translate('Processing', string)
def createInstance(self):
return ExampleProcessingAlgorithm()
def name(self):
"""
Returns the algorithm name, used for identifying the algorithm. This
string should be fixed for the algorithm, and must not be localised.
The name should be unique within each provider. Names should contain
lowercase alphanumeric characters only and no spaces or other
formatting characters.
"""
return 'createExtentsFromPrintLayouts'
def displayName(self):
"""
Returns the translated algorithm name, which should be used for any
user-visible display of the algorithm name.
"""
return self.tr('Create Extents FromPrint Layouts')
def group(self):
"""
Returns the name of the group this algorithm belongs to. This string
should be localised.
"""
return self.tr('My Scripts')
def groupId(self):
"""
Returns the unique ID of the group this algorithm belongs to. This
string should be fixed for the algorithm, and must not be localised.
The group id should be unique within each provider. Group id should
contain lowercase alphanumeric characters only and no spaces or other
formatting characters.
"""
return 'myscripts'
def initAlgorithm(self, config=None):
"""
Here we define the inputs and output of the algorithm, along
with some other properties.
"""
# We add a feature sink in which to store our processed features (this
# usually takes the form of a newly created vector layer when the
# algorithm is run in QGIS).
self.addParameter(
QgsProcessingParameterFeatureSink(
self.OUTPUT,
self.tr('Output layer')
)
)
def fGetExtentFeaturesFromPrintLayouts(self) :
"""
This is a function that iterates over print layouts to return a list of features, each of which represents the extent of the composer map
As attribute, we have the name of the print layout
"""
# get compositions
projectInstance= QgsProject.instance()
projectLayoutManager = projectInstance.layoutManager()
comps = projectLayoutManager.printLayouts()
# iterate over compositions
feats = list()
for comp in comps :
layoutName = comp.name()
for item in comp.items() :
if (isinstance(item, QgsLayoutItemMap)) :
# geometry
e = item.extent()
feat = QgsFeature()
feat.setGeometry(QgsGeometry.fromRect(e))
# attributes
feat.initAttributes(1)
feat.setAttribute(0, QVariant(layoutName))
feats.append(feat)
return(feats)
def processAlgorithm(self, parameters, context, feedback):
"""
Here is where the processing itself takes place.
"""
# defining CRS
crs = QgsCoordinateReferenceSystem()
crs.createFromId(2154)
# fields
flds = QgsFields()
flds.append(QgsField("name", QVariant.String))
# sink params
(sink, dest_id) = self.parameterAsSink(
parameters,
self.OUTPUT,
context,
flds,
3,
crs
)
# get features from print layouts
feats = self.fGetExtentFeaturesFromPrintLayouts()
for feat in feats :
sink.addFeature(feat, QgsFeatureSink.FastInsert)
# Return the results of the algorithm
return {self.OUTPUT: dest_id}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment