roughness.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """
  2. ***************************************************************************
  3. roughness.py
  4. ---------------------
  5. Date : October 2013
  6. Copyright : (C) 2013 by Alexander Bruy
  7. Email : alexander dot bruy 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__ = 'Alexander Bruy'
  18. __date__ = 'October 2013'
  19. __copyright__ = '(C) 2013, Alexander Bruy'
  20. import os
  21. from qgis.core import (QgsRasterFileWriter,
  22. QgsProcessingException,
  23. QgsProcessingParameterDefinition,
  24. QgsProcessingParameterRasterLayer,
  25. QgsProcessingParameterBand,
  26. QgsProcessingParameterString,
  27. QgsProcessingParameterBoolean,
  28. QgsProcessingParameterRasterDestination)
  29. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  30. from processing.algs.gdal.GdalUtils import GdalUtils
  31. pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
  32. class roughness(GdalAlgorithm):
  33. INPUT = 'INPUT'
  34. BAND = 'BAND'
  35. COMPUTE_EDGES = 'COMPUTE_EDGES'
  36. OPTIONS = 'OPTIONS'
  37. OUTPUT = 'OUTPUT'
  38. def __init__(self):
  39. super().__init__()
  40. def initAlgorithm(self, config=None):
  41. self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer')))
  42. self.addParameter(QgsProcessingParameterBand(self.BAND,
  43. self.tr('Band number'),
  44. 1,
  45. parentLayerParameterName=self.INPUT))
  46. self.addParameter(QgsProcessingParameterBoolean(self.COMPUTE_EDGES,
  47. self.tr('Compute edges'),
  48. defaultValue=False))
  49. options_param = QgsProcessingParameterString(self.OPTIONS,
  50. self.tr('Additional creation options'),
  51. defaultValue='',
  52. optional=True)
  53. options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  54. options_param.setMetadata({
  55. 'widget_wrapper': {
  56. 'class': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper'}})
  57. self.addParameter(options_param)
  58. self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Roughness')))
  59. def name(self):
  60. return 'roughness'
  61. def displayName(self):
  62. return self.tr('Roughness')
  63. def group(self):
  64. return self.tr('Raster analysis')
  65. def groupId(self):
  66. return 'rasteranalysis'
  67. def commandName(self):
  68. return 'gdaldem'
  69. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  70. inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
  71. if inLayer is None:
  72. raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))
  73. out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
  74. self.setOutputValue(self.OUTPUT, out)
  75. output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
  76. if not output_format:
  77. raise QgsProcessingException(self.tr('Output format is invalid'))
  78. arguments = [
  79. 'roughness',
  80. inLayer.source(),
  81. out,
  82. '-of',
  83. output_format,
  84. '-b',
  85. str(self.parameterAsInt(parameters, self.BAND, context))
  86. ]
  87. if self.parameterAsBoolean(parameters, self.COMPUTE_EDGES, context):
  88. arguments.append('-compute_edges')
  89. options = self.parameterAsString(parameters, self.OPTIONS, context)
  90. if options:
  91. arguments.extend(GdalUtils.parseCreationOptions(options))
  92. return [self.commandName(), GdalUtils.escapeAndJoin(arguments)]