1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
"""
***************************************************************************
general.py
---------------------
Date : April 2013
Copyright : (C) 2013 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = "Victor Olaya"
__date__ = "April 2013"
__copyright__ = "(C) 2013, Victor Olaya"
from qgis.core import (
QgsApplication,
QgsProcessingAlgorithm,
QgsProcessingParameterDefinition,
QgsProcessingParameterEnum,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterVectorDestination,
QgsProcessingParameterRasterDestination,
QgsProcessingOutputLayerDefinition,
QgsProject,
)
from processing.core.Processing import Processing
from processing.gui.Postprocessing import handleAlgorithmResults
from processing.gui.AlgorithmDialog import AlgorithmDialog
from qgis.utils import iface
# changing this signature? make sure you update the signature in
# python/processing/__init__.py too!
# Docstring for this function is in python/processing/__init__.py
def algorithmHelp(id: str) -> None:
alg = QgsApplication.processingRegistry().algorithmById(id)
if alg is not None:
print(f"{alg.displayName()} ({alg.id()})\n")
if alg.shortDescription():
print(alg.shortDescription() + "\n")
if alg.shortHelpString():
print(alg.shortHelpString() + "\n")
print("\n----------------")
print("Input parameters")
print("----------------")
for p in alg.parameterDefinitions():
if p.flags() & QgsProcessingParameterDefinition.Flag.FlagHidden:
continue
print(f"\n{p.name()}: {p.description()}")
if p.help():
print(f"\n\t{p.help()}")
print(f"\n\tParameter type:\t{p.__class__.__name__}")
if isinstance(p, QgsProcessingParameterEnum):
opts = []
for i, o in enumerate(p.options()):
opts.append(f"\t\t- {i}: {o}")
print("\n\tAvailable values:\n{}".format("\n".join(opts)))
parameter_type = QgsApplication.processingRegistry().parameterType(p.type())
accepted_types = (
parameter_type.acceptedPythonTypes()
if parameter_type is not None
else []
)
if accepted_types:
opts = []
for t in accepted_types:
opts.append(f"\t\t- {t}")
print("\n\tAccepted data types:")
print("\n".join(opts))
print("\n----------------")
print("Outputs")
print("----------------")
for o in alg.outputDefinitions():
print(f"\n{o.name()}: <{o.__class__.__name__}>")
if o.description():
print("\t" + o.description())
else:
print(f'Algorithm "{id}" not found.')
# changing this signature? make sure you update the signature in
# python/processing/__init__.py too!
# Docstring for this function is in python/processing/__init__.py
def run(
algOrName,
parameters,
onFinish=None,
feedback=None,
context=None,
is_child_algorithm=False,
):
if onFinish or not is_child_algorithm:
return Processing.runAlgorithm(
algOrName, parameters, onFinish, feedback, context
)
else:
# for child algorithms, we disable to default post-processing step where layer ownership
# is transferred from the context to the caller. In this case, we NEED the ownership to remain
# with the context, so that further steps in the algorithm have guaranteed access to the layer.
def post_process(_alg, _context, _feedback):
return
return Processing.runAlgorithm(
algOrName,
parameters,
onFinish=post_process,
feedback=feedback,
context=context,
)
# changing this signature? make sure you update the signature in
# python/processing/__init__.py too!
# Docstring for this function is in python/processing/__init__.py
def runAndLoadResults(algOrName, parameters, feedback=None, context=None):
if isinstance(algOrName, QgsProcessingAlgorithm):
alg = algOrName
else:
alg = QgsApplication.processingRegistry().createAlgorithmById(algOrName)
# output destination parameters to point to current project
for param in alg.parameterDefinitions():
if not param.name() in parameters:
continue
if isinstance(
param,
(
QgsProcessingParameterFeatureSink,
QgsProcessingParameterVectorDestination,
QgsProcessingParameterRasterDestination,
),
):
p = parameters[param.name()]
if not isinstance(p, QgsProcessingOutputLayerDefinition):
parameters[param.name()] = QgsProcessingOutputLayerDefinition(
p, QgsProject.instance()
)
else:
p.destinationProject = QgsProject.instance()
parameters[param.name()] = p
return Processing.runAlgorithm(
alg,
parameters=parameters,
onFinish=handleAlgorithmResults,
feedback=feedback,
context=context,
)
# changing this signature? make sure you update the signature in
# python/processing/__init__.py too!
# Docstring for this function is in python/processing/__init__.py
def createAlgorithmDialog(algOrName, parameters={}):
if isinstance(algOrName, QgsProcessingAlgorithm):
alg = algOrName.create()
else:
alg = QgsApplication.processingRegistry().createAlgorithmById(algOrName)
if alg is None:
return None
dlg = alg.createCustomParametersWidget(iface.mainWindow())
if not dlg:
dlg = AlgorithmDialog(alg, parent=iface.mainWindow())
dlg.setParameters(parameters)
return dlg
# changing this signature? make sure you update the signature in
# python/processing/__init__.py too!
# Docstring for this function is in python/processing/__init__.py
def execAlgorithmDialog(algOrName, parameters={}):
dlg = createAlgorithmDialog(algOrName, parameters)
if dlg is None:
return {}
canvas = iface.mapCanvas()
prevMapTool = canvas.mapTool()
dlg.show()
dlg.exec()
if canvas.mapTool() != prevMapTool:
try:
canvas.mapTool().reset()
except:
pass
canvas.setMapTool(prevMapTool)
results = dlg.results()
# make sure the dialog is destroyed and not only hidden on pressing Esc
dlg.close()
return results
|