tpi.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. """
  2. ***************************************************************************
  3. tpi.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 (QgsProcessingException,
  22. QgsProcessingParameterDefinition,
  23. QgsProcessingParameterRasterLayer,
  24. QgsProcessingParameterBand,
  25. QgsProcessingParameterString,
  26. QgsProcessingParameterBoolean,
  27. QgsProcessingParameterRasterDestination)
  28. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  29. from processing.algs.gdal.GdalUtils import GdalUtils
  30. pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
  31. class tpi(GdalAlgorithm):
  32. INPUT = 'INPUT'
  33. BAND = 'BAND'
  34. COMPUTE_EDGES = 'COMPUTE_EDGES'
  35. OPTIONS = 'OPTIONS'
  36. OUTPUT = 'OUTPUT'
  37. def __init__(self):
  38. super().__init__()
  39. def initAlgorithm(self, config=None):
  40. self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer')))
  41. self.addParameter(QgsProcessingParameterBand(self.BAND,
  42. self.tr('Band number'),
  43. 1,
  44. parentLayerParameterName=self.INPUT))
  45. self.addParameter(QgsProcessingParameterBoolean(self.COMPUTE_EDGES,
  46. self.tr('Compute edges'),
  47. defaultValue=False))
  48. options_param = QgsProcessingParameterString(self.OPTIONS,
  49. self.tr('Additional creation options'),
  50. defaultValue='',
  51. optional=True)
  52. options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  53. options_param.setMetadata({
  54. 'widget_wrapper': {
  55. 'class': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper'}})
  56. self.addParameter(options_param)
  57. self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Topographic Position Index')))
  58. def name(self):
  59. return 'tpitopographicpositionindex'
  60. def displayName(self):
  61. return self.tr('Topographic Position Index (TPI)')
  62. def group(self):
  63. return self.tr('Raster analysis')
  64. def groupId(self):
  65. return 'rasteranalysis'
  66. def commandName(self):
  67. return 'gdaldem'
  68. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  69. arguments = ['TPI']
  70. inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
  71. if inLayer is None:
  72. raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))
  73. arguments.append(inLayer.source())
  74. out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
  75. self.setOutputValue(self.OUTPUT, out)
  76. arguments.append(out)
  77. arguments.append('-b')
  78. arguments.append(str(self.parameterAsInt(parameters, self.BAND, context)))
  79. if self.parameterAsBoolean(parameters, self.COMPUTE_EDGES, context):
  80. arguments.append('-compute_edges')
  81. options = self.parameterAsString(parameters, self.OPTIONS, context)
  82. if options:
  83. arguments.extend(GdalUtils.parseCreationOptions(options))
  84. return [self.commandName(), GdalUtils.escapeAndJoin(arguments)]