BatchAlgorithmDialog.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. """
  2. ***************************************************************************
  3. BatchAlgorithmDialog.py
  4. ---------------------
  5. Date : August 2012
  6. Copyright : (C) 2012 by Victor Olaya
  7. Email : volayaf at gmail dot com
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Victor Olaya'
  18. __date__ = 'August 2012'
  19. __copyright__ = '(C) 2012, Victor Olaya'
  20. import codecs
  21. import time
  22. from qgis.core import (QgsProcessingOutputHtml,
  23. QgsProcessingOutputNumber,
  24. QgsProcessingOutputString,
  25. QgsProcessingOutputBoolean,
  26. QgsProject)
  27. from qgis.gui import QgsProcessingBatchAlgorithmDialogBase
  28. from qgis.utils import iface
  29. from processing.core.ProcessingResults import resultsList
  30. from processing.gui.BatchPanel import BatchPanel
  31. from processing.gui.Postprocessing import handleAlgorithmResults
  32. from processing.tools import dataobjects
  33. from processing.tools.system import getTempFilename
  34. class BatchAlgorithmDialog(QgsProcessingBatchAlgorithmDialogBase):
  35. def __init__(self, alg, parent=None):
  36. super().__init__(parent)
  37. self.setAlgorithm(alg)
  38. self.setWindowTitle(self.tr('Batch Processing - {0}').format(self.algorithm().displayName()))
  39. self.setMainWidget(BatchPanel(self, self.algorithm()))
  40. self.context = None
  41. self.hideShortHelp()
  42. def runAsSingle(self):
  43. self.close()
  44. from processing.gui.AlgorithmDialog import AlgorithmDialog
  45. dlg = AlgorithmDialog(self.algorithm().create(), parent=iface.mainWindow())
  46. dlg.show()
  47. dlg.exec_()
  48. def processingContext(self):
  49. if self.context is None:
  50. self.feedback = self.createFeedback()
  51. self.context = dataobjects.createContext(self.feedback)
  52. self.context.setLogLevel(self.logLevel())
  53. return self.context
  54. def createContext(self, feedback):
  55. return dataobjects.createContext(feedback)
  56. def runAlgorithm(self):
  57. alg_parameters = []
  58. load_layers = self.mainWidget().checkLoadLayersOnCompletion.isChecked()
  59. project = QgsProject.instance() if load_layers else None
  60. for row in range(self.mainWidget().batchRowCount()):
  61. parameters, ok = self.mainWidget().parametersForRow(
  62. row=row,
  63. context=self.processingContext(),
  64. destinationProject=project,
  65. warnOnInvalid=True)
  66. if ok:
  67. alg_parameters.append(parameters)
  68. if not alg_parameters:
  69. return
  70. self.execute(alg_parameters)
  71. def handleAlgorithmResults(self, algorithm, context, feedback, parameters):
  72. handleAlgorithmResults(algorithm, context, feedback, parameters)
  73. def loadHtmlResults(self, results, num):
  74. for out in self.algorithm().outputDefinitions():
  75. if isinstance(out, QgsProcessingOutputHtml) and out.name() in results and results[out.name()]:
  76. resultsList.addResult(icon=self.algorithm().icon(), name=f'{out.description()} [{num}]',
  77. result=results[out.name()])
  78. def createSummaryTable(self, algorithm_results, errors):
  79. createTable = False
  80. for out in self.algorithm().outputDefinitions():
  81. if isinstance(out, (QgsProcessingOutputNumber, QgsProcessingOutputString, QgsProcessingOutputBoolean)):
  82. createTable = True
  83. break
  84. if not createTable and not errors:
  85. return
  86. outputFile = getTempFilename('html')
  87. with codecs.open(outputFile, 'w', encoding='utf-8') as f:
  88. if createTable:
  89. for i, res in enumerate(algorithm_results):
  90. results = res['results']
  91. params = res['parameters']
  92. if i > 0:
  93. f.write('<hr>\n')
  94. f.write(self.tr('<h3>Parameters</h3>\n'))
  95. f.write('<table>\n')
  96. for param in self.algorithm().parameterDefinitions():
  97. if not param.isDestination():
  98. if param.name() in params:
  99. f.write('<tr><th>{}</th><td>{}</td></tr>\n'.format(param.description(),
  100. params[param.name()]))
  101. f.write('</table>\n')
  102. f.write(self.tr('<h3>Results</h3>\n'))
  103. f.write('<table>\n')
  104. for out in self.algorithm().outputDefinitions():
  105. if out.name() in results:
  106. f.write(f'<tr><th>{out.description()}</th><td>{results[out.name()]}</td></tr>\n')
  107. f.write('</table>\n')
  108. if errors:
  109. f.write('<h2 style="color: red">{}</h2>\n'.format(self.tr('Errors')))
  110. for i, res in enumerate(errors):
  111. errors = res['errors']
  112. params = res['parameters']
  113. if i > 0:
  114. f.write('<hr>\n')
  115. f.write(self.tr('<h3>Parameters</h3>\n'))
  116. f.write('<table>\n')
  117. for param in self.algorithm().parameterDefinitions():
  118. if not param.isDestination():
  119. if param.name() in params:
  120. f.write(
  121. f'<tr><th>{param.description()}</th><td>{params[param.name()]}</td></tr>\n')
  122. f.write('</table>\n')
  123. f.write('<h3>{}</h3>\n'.format(self.tr('Error')))
  124. f.write('<p style="color: red">{}</p>\n'.format('<br>'.join(errors)))
  125. resultsList.addResult(icon=self.algorithm().icon(),
  126. name=f'{self.algorithm().name()} [summary]', timestamp=time.localtime(),
  127. result=outputFile)