RasterLayerHistogram.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. """
  2. ***************************************************************************
  3. RasterLayerHistogram.py
  4. ---------------------
  5. Date : January 2013
  6. Copyright : (C) 2013 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__ = 'January 2013'
  19. __copyright__ = '(C) 2013, Victor Olaya'
  20. import warnings
  21. from qgis.core import (QgsProcessingParameterRasterLayer,
  22. QgsProcessingParameterBand,
  23. QgsProcessingParameterNumber,
  24. QgsProcessingParameterFileDestination,
  25. QgsProcessingException)
  26. from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
  27. from processing.tools import raster
  28. from qgis.PyQt.QtCore import QCoreApplication
  29. class RasterLayerHistogram(QgisAlgorithm):
  30. INPUT = 'INPUT'
  31. BINS = 'BINS'
  32. OUTPUT = 'OUTPUT'
  33. BAND = 'BAND'
  34. def group(self):
  35. return self.tr('Plots')
  36. def groupId(self):
  37. return 'plots'
  38. def __init__(self):
  39. super().__init__()
  40. def initAlgorithm(self, config=None):
  41. self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,
  42. self.tr('Input layer')))
  43. self.addParameter(QgsProcessingParameterBand(self.BAND,
  44. self.tr('Band number'),
  45. 1,
  46. self.INPUT))
  47. self.addParameter(QgsProcessingParameterNumber(self.BINS,
  48. self.tr('number of bins'), minValue=2, defaultValue=10))
  49. self.addParameter(QgsProcessingParameterFileDestination(self.OUTPUT, self.tr('Histogram'), self.tr('HTML files (*.html)')))
  50. def name(self):
  51. return 'rasterlayerhistogram'
  52. def displayName(self):
  53. return self.tr('Raster layer histogram')
  54. def processAlgorithm(self, parameters, context, feedback):
  55. try:
  56. # importing plotly throws Python warnings from within the library - filter these out
  57. with warnings.catch_warnings():
  58. warnings.filterwarnings("ignore", category=ResourceWarning)
  59. warnings.filterwarnings("ignore", category=ImportWarning)
  60. import plotly as plt
  61. import plotly.graph_objs as go
  62. except ImportError:
  63. raise QgsProcessingException(QCoreApplication.translate('RasterLayerHistogram', 'This algorithm requires the Python “plotly” library. Please install this library and try again.'))
  64. layer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
  65. band = self.parameterAsInt(parameters, self.BAND, context)
  66. nbins = self.parameterAsInt(parameters, self.BINS, context)
  67. output = self.parameterAsFileOutput(parameters, self.OUTPUT, context)
  68. # ALERT: this is potentially blocking if the layer is too big
  69. values = raster.scanraster(layer, feedback, band)
  70. valueslist = [
  71. v
  72. for v in values
  73. if v is not None
  74. ]
  75. data = [go.Histogram(x=valueslist,
  76. nbinsx=nbins)]
  77. plt.offline.plot(data, filename=output, auto_open=False)
  78. return {self.OUTPUT: output}