pansharp.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. """
  2. ***************************************************************************
  3. pansharp.py
  4. ---------------------
  5. Date : March 2019
  6. Copyright : (C) 2019 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__ = 'March 2019'
  19. __copyright__ = '(C) 2019, Alexander Bruy'
  20. import os
  21. from qgis.PyQt.QtGui import QIcon
  22. from qgis.core import (QgsRasterFileWriter,
  23. QgsProcessingException,
  24. QgsProcessingParameterDefinition,
  25. QgsProcessingParameterRasterLayer,
  26. QgsProcessingParameterEnum,
  27. QgsProcessingParameterString,
  28. QgsProcessingParameterRasterDestination)
  29. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  30. from processing.algs.gdal.GdalUtils import GdalUtils
  31. from processing.tools.system import isWindows
  32. pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
  33. class pansharp(GdalAlgorithm):
  34. SPECTRAL = 'SPECTRAL'
  35. PANCHROMATIC = 'PANCHROMATIC'
  36. RESAMPLING = 'RESAMPLING'
  37. OPTIONS = 'OPTIONS'
  38. EXTRA = 'EXTRA'
  39. OUTPUT = 'OUTPUT'
  40. def __init__(self):
  41. super().__init__()
  42. def initAlgorithm(self, config=None):
  43. self.methods = ((self.tr('Nearest Neighbour'), 'nearest'),
  44. (self.tr('Bilinear (2x2 Kernel)'), 'bilinear'),
  45. (self.tr('Cubic (4x4 Kernel)'), 'cubic'),
  46. (self.tr('Cubic B-Spline (4x4 Kernel)'), 'cubicspline'),
  47. (self.tr('Lanczos (6x6 Kernel)'), 'lanczos'),
  48. (self.tr('Average'), 'average'))
  49. self.addParameter(QgsProcessingParameterRasterLayer(self.SPECTRAL,
  50. self.tr('Spectral dataset')))
  51. self.addParameter(QgsProcessingParameterRasterLayer(self.PANCHROMATIC,
  52. self.tr('Panchromatic dataset')))
  53. resampling_param = QgsProcessingParameterEnum(self.RESAMPLING,
  54. self.tr('Resampling algorithm'),
  55. options=[i[0] for i in self.methods],
  56. defaultValue=2)
  57. resampling_param.setFlags(resampling_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  58. self.addParameter(resampling_param)
  59. options_param = QgsProcessingParameterString(self.OPTIONS,
  60. self.tr('Additional creation options'),
  61. defaultValue='',
  62. optional=True)
  63. options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  64. options_param.setMetadata({
  65. 'widget_wrapper': {
  66. 'class': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper'}})
  67. self.addParameter(options_param)
  68. extra_param = QgsProcessingParameterString(self.EXTRA,
  69. self.tr('Additional command-line parameters'),
  70. defaultValue=None,
  71. optional=True)
  72. extra_param.setFlags(extra_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  73. self.addParameter(extra_param)
  74. self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT,
  75. self.tr('Output')))
  76. def name(self):
  77. return 'pansharp'
  78. def displayName(self):
  79. return self.tr('Pansharpening')
  80. def group(self):
  81. return self.tr('Raster miscellaneous')
  82. def groupId(self):
  83. return 'rastermiscellaneous'
  84. def commandName(self):
  85. return 'gdal_pansharpen'
  86. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  87. spectral = self.parameterAsRasterLayer(parameters, self.SPECTRAL, context)
  88. if spectral is None:
  89. raise QgsProcessingException(self.invalidRasterError(parameters, self.SPECTRAL))
  90. panchromatic = self.parameterAsRasterLayer(parameters, self.PANCHROMATIC, context)
  91. if panchromatic is None:
  92. raise QgsProcessingException(self.invalidRasterError(parameters, self.PANCHROMATIC))
  93. out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
  94. self.setOutputValue(self.OUTPUT, out)
  95. output_format = QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
  96. if not output_format:
  97. raise QgsProcessingException(self.tr('Output format is invalid'))
  98. arguments = [
  99. panchromatic.source(),
  100. spectral.source(),
  101. out,
  102. '-r',
  103. self.methods[self.parameterAsEnum(parameters, self.RESAMPLING, context)][1],
  104. '-of',
  105. output_format
  106. ]
  107. options = self.parameterAsString(parameters, self.OPTIONS, context)
  108. if options:
  109. arguments.extend(GdalUtils.parseCreationOptions(options))
  110. if self.EXTRA in parameters and parameters[self.EXTRA] not in (None, ''):
  111. extra = self.parameterAsString(parameters, self.EXTRA, context)
  112. arguments.append(extra)
  113. return [self.commandName() + ('.bat' if isWindows() else '.py'), GdalUtils.escapeAndJoin(arguments)]