本文整理汇总了Python中processing.script.ScriptAlgorithm.ScriptAlgorithm类的典型用法代码示例。如果您正苦于以下问题:Python ScriptAlgorithm类的具体用法?Python ScriptAlgorithm怎么用?Python ScriptAlgorithm使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ScriptAlgorithm类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getAlgorithmFromHookFile
def getAlgorithmFromHookFile(self, hookFile):
if hookFile.endswith('py'):
script = ScriptAlgorithm(hookFile)
script.provider = Providers.providers['script']
return script
elif hookFile.endswith('model'):
model = ModelerAlgorithm()
model.openModel(hookFile)
model.provider = Providers.providers['model']
return model
else:
raise Exception ("Wrong hook file")
示例2: getAlgorithmFromHookFile
def getAlgorithmFromHookFile(self, hookFile):
if hookFile.endswith("py"):
script = ScriptAlgorithm(hookFile)
script.provider = ModelerUtils.providers["script"]
return script
elif hookFile.endswith("model"):
model = ModelerAlgorithm()
model.openModel(hookFile)
model.provider = ModelerUtils.providers["model"]
return model
else:
raise Exception("Wrong hook file")
示例3: check_algorithm
def check_algorithm(self, name, defs):
"""
Will run an algorithm definition and check if it generates the expected result
:param name: The identifier name used in the test output heading
:param defs: A python dict containing a test algorithm definition
"""
QgsProject.instance().removeAllMapLayers()
params = self.load_params(defs['params'])
if defs['algorithm'].startswith('script:'):
filePath = os.path.join(processingTestDataPath(), 'scripts', '{}.py'.format(defs['algorithm'][len('script:'):]))
alg = ScriptAlgorithm(filePath)
else:
alg = QgsApplication.processingRegistry().algorithmById(defs['algorithm'])
parameters = {}
if isinstance(params, list):
for param in zip(alg.parameterDefinitions(), params):
parameters[param[0].name()] = param[1]
else:
for k, p in list(params.items()):
parameters[k] = p
for r, p in list(defs['results'].items()):
if not 'in_place_result' in p or not p['in_place_result']:
parameters[r] = self.load_result_param(p)
expectFailure = False
if 'expectedFailure' in defs:
exec(('\n'.join(defs['expectedFailure'][:-1])), globals(), locals())
expectFailure = eval(defs['expectedFailure'][-1])
# ignore user setting for invalid geometry handling
context = QgsProcessingContext()
context.setProject(QgsProject.instance())
feedback = QgsProcessingFeedback()
if expectFailure:
try:
results, ok = alg.run(parameters, context, feedback)
self.check_results(results, context, defs['params'], defs['results'])
if ok:
raise _UnexpectedSuccess
except Exception:
pass
else:
results, ok = alg.run(parameters, context, feedback)
self.assertTrue(ok, parameters)
self.check_results(results, context, defs['params'], defs['results'])
示例4: runAlgorithm
def runAlgorithm(self):
if self.algType == self.SCRIPT_PYTHON:
alg = ScriptAlgorithm(None, self.editor.text())
dlg = alg.createCustomParametersWidget(self)
if not dlg:
dlg = AlgorithmDialog(alg)
canvas = iface.mapCanvas()
prevMapTool = canvas.mapTool()
dlg.show()
dlg.exec_()
if canvas.mapTool() != prevMapTool:
try:
canvas.mapTool().reset()
except:
pass
canvas.setMapTool(prevMapTool)
示例5: loadFromFolder
def loadFromFolder(folder):
if not os.path.exists(folder):
return []
algs = []
for path, subdirs, files in os.walk(folder):
for descriptionFile in files:
if descriptionFile.endswith('py'):
try:
fullpath = os.path.join(path, descriptionFile)
alg = ScriptAlgorithm(fullpath)
if alg.name().strip() != '':
algs.append(alg)
except WrongScriptException as e:
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, e.msg)
except Exception as e:
ProcessingLog.addToLog(
ProcessingLog.LOG_ERROR,
QCoreApplication.translate('Processing', 'Could not load script: {0}\n{1}').format(descriptionFile, str(e))
)
return algs
示例6: runAlgorithm
def runAlgorithm(self):
if self.algType == self.SCRIPT_PYTHON:
alg = ScriptAlgorithm(None, unicode(self.editor.text()))
alg.provider = Providers.providers["script"]
if self.algType == self.SCRIPT_R:
alg = RAlgorithm(None, unicode(self.editor.text()))
alg.provider = Providers.providers["r"]
dlg = alg.getCustomParametersDialog()
if not dlg:
dlg = ParametersDialog(alg)
canvas = iface.mapCanvas()
prevMapTool = canvas.mapTool()
dlg.show()
dlg.exec_()
if canvas.mapTool() != prevMapTool:
try:
canvas.mapTool().reset()
except:
pass
canvas.setMapTool(prevMapTool)
示例7: runAlgorithm
def runAlgorithm(self):
if self.algType == self.SCRIPT_PYTHON:
alg = ScriptAlgorithm(None, self.editor.text())
dlg = alg.createCustomParametersWidget(self)
if not dlg:
dlg = AlgorithmDialog(alg)
canvas = iface.mapCanvas()
prevMapTool = canvas.mapTool()
dlg.show()
dlg.exec_()
# have to manually delete the dialog - otherwise it's owned by the
# iface mainWindow and never deleted
dlg.deleteLater()
if canvas.mapTool() != prevMapTool:
try:
canvas.mapTool().reset()
except:
pass
canvas.setMapTool(prevMapTool)
示例8: check_algorithm
def check_algorithm(self, name, defs):
"""
Will run an algorithm definition and check if it generates the expected result
:param name: The identifier name used in the test output heading
:param defs: A python dict containing a test algorithm definition
"""
QgsProject.instance().removeAllMapLayers()
params = self.load_params(defs['params'])
if defs['algorithm'].startswith('script:'):
filePath = os.path.join(processingTestDataPath(), 'scripts', '{}.py'.format(defs['algorithm'][len('script:'):]))
alg = ScriptAlgorithm(filePath)
else:
alg = QgsApplication.processingRegistry().algorithmById(defs['algorithm'])
if isinstance(params, list):
for param in zip(alg.parameters, params):
param[0].setValue(param[1])
else:
for k, p in list(params.items()):
alg.setParameterValue(k, p)
for r, p in list(defs['results'].items()):
alg.setOutputValue(r, self.load_result_param(p))
expectFailure = False
if 'expectedFailure' in defs:
exec(('\n'.join(defs['expectedFailure'][:-1])), globals(), locals())
expectFailure = eval(defs['expectedFailure'][-1])
if expectFailure:
try:
alg.execute()
self.check_results(alg.getOutputValuesAsDictionary(), defs['params'], defs['results'])
except Exception:
pass
else:
raise _UnexpectedSuccess
else:
alg.execute()
self.check_results(alg.getOutputValuesAsDictionary(), defs['params'], defs['results'])