BarPlot.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """
  2. ***************************************************************************
  3. BarPlot.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 (QgsFeatureRequest,
  22. QgsProcessingParameterFeatureSource,
  23. QgsProcessingParameterField,
  24. QgsProcessingException,
  25. QgsProcessingParameterFileDestination)
  26. from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
  27. from processing.tools import vector
  28. from qgis.PyQt.QtCore import QCoreApplication
  29. class BarPlot(QgisAlgorithm):
  30. INPUT = 'INPUT'
  31. OUTPUT = 'OUTPUT'
  32. NAME_FIELD = 'NAME_FIELD'
  33. VALUE_FIELD = 'VALUE_FIELD'
  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(QgsProcessingParameterFeatureSource(self.INPUT,
  42. self.tr('Input layer')))
  43. self.addParameter(QgsProcessingParameterField(self.NAME_FIELD,
  44. self.tr('Category name field'),
  45. None, self.INPUT, QgsProcessingParameterField.Any))
  46. self.addParameter(QgsProcessingParameterField(self.VALUE_FIELD,
  47. self.tr('Value field'),
  48. None, self.INPUT, QgsProcessingParameterField.Numeric))
  49. self.addParameter(QgsProcessingParameterFileDestination(self.OUTPUT, self.tr('Bar plot'), self.tr('HTML files (*.html)')))
  50. def name(self):
  51. return 'barplot'
  52. def displayName(self):
  53. return self.tr('Bar plot')
  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('BarPlot', 'This algorithm requires the Python “plotly” library. Please install this library and try again.'))
  64. source = self.parameterAsSource(parameters, self.INPUT, context)
  65. if source is None:
  66. raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
  67. namefieldname = self.parameterAsString(parameters, self.NAME_FIELD, context)
  68. valuefieldname = self.parameterAsString(parameters, self.VALUE_FIELD, context)
  69. output = self.parameterAsFileOutput(parameters, self.OUTPUT, context)
  70. values = vector.values(source, valuefieldname)
  71. x_var = vector.convert_nulls([i[namefieldname] for i in source.getFeatures(QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry))], '<NULL>')
  72. data = [go.Bar(x=x_var,
  73. y=values[valuefieldname])]
  74. plt.offline.plot(data, filename=output, auto_open=False)
  75. return {self.OUTPUT: output}