Is there some way to set/edit user defined project variables with a model or a calculator so their values can be read by all the layouts and reports in a project? I want to calculate a project reference point whose values can appear in all title blocks in various scale maps in the project. And, the variable names will remain in the project template and recalculated for the next project etc.
-
Have a look at the QGIS variable : north-road.com/2015/12/02/… , that may be what you're looking for– J.RCommented Apr 24, 2020 at 22:07
-
Can you be more explicit on your needs? It's not clear to me. Indeed, you can look to variables, you can also have a look to project template?– etrimailleCommented Apr 25, 2020 at 15:53
-
I have rephrased the question above. I hope that helps.– MikeorammaCommented Apr 29, 2020 at 15:08
Add a comment
|
1 Answer
It's not possible out-of-the-box, but here's a simple little script based algorithm you could add which will give you a new modeler tool to set project variables:
# -*- coding: utf-8 -*-
"""
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
QgsExpressionContextUtils,
QgsProcessingException,
QgsProcessingAlgorithm,
QgsProcessingParameterString)
class SetProjectVariableAlgorithm(QgsProcessingAlgorithm):
NAME = 'NAME'
VALUE = 'VALUE'
def createInstance(self):
return SetProjectVariableAlgorithm()
def name(self):
return 'setprojectvariable'
def displayName(self):
return 'Set project variable'
def group(self):
return 'Modeler tools'
def groupId(self):
return 'modelertools'
def shortHelpString(self):
return "Sets an expression variable for the current project"
def flags(self):
return QgsProcessingAlgorithm.FlagHideFromToolbox | QgsProcessingAlgorithm.FlagSkipGenericModelLogging
def initAlgorithm(self, config=None):
self.addParameter(
QgsProcessingParameterString(
self.NAME,
'Variable name'
)
)
self.addParameter(
QgsProcessingParameterString(
self.VALUE,
'Variable value'
)
)
def prepareAlgorithm(self, parameters, context, feedback):
# this is all nice and quick, we can (and should) do it in the main thread without issue
name = self.parameterAsString(
parameters,
self.NAME,
context
)
value = self.parameterAsString(
parameters,
self.VALUE,
context
)
if not name:
raise QgsProcessingException('Variable name cannot be empty')
QgsExpressionContextUtils.setProjectVariable(context.project(), name, value)
feedback.pushInfo('Set variable \'{}\' to \'{}\''.format(name,value))
return True
def processAlgorithm(self, parameters, context, feedback):
return {}
-
Nyall. Thanks for getting this added to the next build! m Commented Oct 23, 2020 at 21:17