本文整理汇总了Python中qgis.core.QgsExpressionContextUtils.globalScope方法的典型用法代码示例。如果您正苦于以下问题:Python QgsExpressionContextUtils.globalScope方法的具体用法?Python QgsExpressionContextUtils.globalScope怎么用?Python QgsExpressionContextUtils.globalScope使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qgis.core.QgsExpressionContextUtils
的用法示例。
在下文中一共展示了QgsExpressionContextUtils.globalScope方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: processAlgorithm
# 需要导入模块: from qgis.core import QgsExpressionContextUtils [as 别名]
# 或者: from qgis.core.QgsExpressionContextUtils import globalScope [as 别名]
def processAlgorithm(self, progress):
ns = {}
ns["progress"] = progress
ns["scriptDescriptionFile"] = self.descriptionFile
for param in self.parameters:
ns[param.name] = param.value
for out in self.outputs:
ns[out.name] = out.value
variables = re.findall("@[a-zA-Z0-9_]*", self.script)
script = "import processing\n"
script += self.script
context = QgsExpressionContext()
context.appendScope(QgsExpressionContextUtils.globalScope())
context.appendScope(QgsExpressionContextUtils.projectScope())
for var in variables:
varname = var[1:]
if context.hasVariable(varname):
script = script.replace(var, context.variable(varname))
else:
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING, "Cannot find variable: %s" % varname)
exec((script), ns)
for out in self.outputs:
out.setValue(ns[out.name])
示例2: processAlgorithm
# 需要导入模块: from qgis.core import QgsExpressionContextUtils [as 别名]
# 或者: from qgis.core.QgsExpressionContextUtils import globalScope [as 别名]
def processAlgorithm(self, progress):
layer = layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT))
expression = self.getParameterValue(self.EXPRESSION)
qExp = QgsExpression(expression)
if qExp.hasParserError():
raise GeoAlgorithmExecutionException(qExp.parserErrorString())
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
layer.fields(), layer.wkbType(), layer.crs())
context = QgsExpressionContext()
context.appendScope(QgsExpressionContextUtils.globalScope())
context.appendScope(QgsExpressionContextUtils.projectScope())
context.appendScope(QgsExpressionContextUtils.layerScope(layer))
count = layer.featureCount()
step = 100.0 / count if count else 1
request = QgsFeatureRequest(qExp, context)
for current, f in enumerate(layer.getFeatures(request)):
writer.addFeature(f)
progress.setPercentage(int(current * step))
del writer
示例3: processAlgorithm
# 需要导入模块: from qgis.core import QgsExpressionContextUtils [as 别名]
# 或者: from qgis.core.QgsExpressionContextUtils import globalScope [as 别名]
def processAlgorithm(self, parameters, context, feedback):
ns = {}
ns['feedback'] = feedback
ns['scriptDescriptionFile'] = self.descriptionFile
ns['context'] = context
for param in self.parameterDefinitions():
ns[param.name] = parameters[param.name()]
for out in self.outputs:
ns[out.name] = out.value
variables = re.findall('@[a-zA-Z0-9_]*', self.script)
script = 'import processing\n'
script += self.script
context = QgsExpressionContext()
context.appendScope(QgsExpressionContextUtils.globalScope())
context.appendScope(QgsExpressionContextUtils.projectScope(QgsProject.instance()))
for var in variables:
varname = var[1:]
if context.hasVariable(varname):
script = script.replace(var, context.variable(varname))
else:
QgsMessageLog.logMessage(self.tr('Cannot find variable: {0}').format(varname), self.tr('Processing'), QgsMessageLog.WARNING)
exec((script), ns)
for out in self.outputs:
out.setValue(ns[out.name])
示例4: processAlgorithm
# 需要导入模块: from qgis.core import QgsExpressionContextUtils [as 别名]
# 或者: from qgis.core.QgsExpressionContextUtils import globalScope [as 别名]
def processAlgorithm(self, progress):
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT_LAYER))
geometry_type = self.getParameterValue(self.OUTPUT_GEOMETRY)
wkb_type = None
if geometry_type == 0:
wkb_type = QgsWkbTypes.Polygon
elif geometry_type == 1:
wkb_type = QgsWkbTypes.LineString
else:
wkb_type = QgsWkbTypes.Point
if self.getParameterValue(self.WITH_Z):
wkb_type = QgsWkbTypes.addZ(wkb_type)
if self.getParameterValue(self.WITH_M):
wkb_type = QgsWkbTypes.addM(wkb_type)
writer = self.getOutputFromName(
self.OUTPUT_LAYER).getVectorWriter(
layer.fields(),
wkb_type,
layer.crs())
expression = QgsExpression(self.getParameterValue(self.EXPRESSION))
if expression.hasParserError():
raise GeoAlgorithmExecutionException(expression.parserErrorString())
exp_context = QgsExpressionContext()
exp_context.appendScope(QgsExpressionContextUtils.globalScope())
exp_context.appendScope(QgsExpressionContextUtils.projectScope())
exp_context.appendScope(QgsExpressionContextUtils.layerScope(layer))
if not expression.prepare(exp_context):
raise GeoAlgorithmExecutionException(
self.tr('Evaluation error: %s' % expression.evalErrorString()))
features = vector.features(layer)
total = 100.0 / len(features)
for current, input_feature in enumerate(features):
output_feature = input_feature
exp_context.setFeature(input_feature)
value = expression.evaluate(exp_context)
if expression.hasEvalError():
raise GeoAlgorithmExecutionException(
self.tr('Evaluation error: %s' % expression.evalErrorString()))
if not value:
output_feature.setGeometry(QgsGeometry())
else:
if not isinstance(value, QgsGeometry):
raise GeoAlgorithmExecutionException(
self.tr('{} is not a geometry').format(value))
output_feature.setGeometry(value)
writer.addFeature(output_feature)
progress.setPercentage(int(current * total))
del writer
示例5: initContext
# 需要导入模块: from qgis.core import QgsExpressionContextUtils [as 别名]
# 或者: from qgis.core.QgsExpressionContextUtils import globalScope [as 别名]
def initContext(self):
exp_context = self.builder.expressionContext()
exp_context.appendScope(QgsExpressionContextUtils.globalScope())
exp_context.appendScope(QgsExpressionContextUtils.projectScope())
exp_context.appendScope(QgsExpressionContextUtils.layerScope(self.layer))
exp_context.lastScope().setVariable("row_number", 1)
exp_context.setHighlightedVariables(["row_number"])
self.builder.setExpressionContext(exp_context)
示例6: _expressionContext
# 需要导入模块: from qgis.core import QgsExpressionContextUtils [as 别名]
# 或者: from qgis.core.QgsExpressionContextUtils import globalScope [as 别名]
def _expressionContext(alg):
context = QgsExpressionContext()
context.appendScope(QgsExpressionContextUtils.globalScope())
context.appendScope(QgsExpressionContextUtils.projectScope())
processingScope = QgsExpressionContextScope()
for param in alg.parameters:
processingScope.setVariable('%s_value' % param.name, '')
context.appendScope(processingScope)
return context
示例7: expressionContext
# 需要导入模块: from qgis.core import QgsExpressionContextUtils [as 别名]
# 或者: from qgis.core.QgsExpressionContextUtils import globalScope [as 别名]
def expressionContext(self):
context = QgsExpressionContext()
context.appendScope(QgsExpressionContextUtils.globalScope())
context.appendScope(QgsExpressionContextUtils.projectScope())
processingScope = QgsExpressionContextScope()
for param in self.alg.parameters:
processingScope.setVariable("%s_value" % param.name, "")
context.appendScope(processingScope)
return context