VectorLayerScatterplot.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """
  2. ***************************************************************************
  3. EquivalentNumField.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 (QgsProcessingException,
  22. QgsProcessingParameterFeatureSource,
  23. QgsProcessingParameterField,
  24. QgsProcessingParameterFileDestination)
  25. from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
  26. from processing.tools import vector
  27. from qgis.PyQt.QtCore import QCoreApplication
  28. class VectorLayerScatterplot(QgisAlgorithm):
  29. INPUT = 'INPUT'
  30. OUTPUT = 'OUTPUT'
  31. XFIELD = 'XFIELD'
  32. YFIELD = 'YFIELD'
  33. def group(self):
  34. return self.tr('Plots')
  35. def groupId(self):
  36. return 'plots'
  37. def __init__(self):
  38. super().__init__()
  39. def initAlgorithm(self, config=None):
  40. self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
  41. self.tr('Input layer')))
  42. self.addParameter(QgsProcessingParameterField(self.XFIELD,
  43. self.tr('X attribute'),
  44. parentLayerParameterName=self.INPUT,
  45. type=QgsProcessingParameterField.Numeric))
  46. self.addParameter(QgsProcessingParameterField(self.YFIELD,
  47. self.tr('Y attribute'),
  48. parentLayerParameterName=self.INPUT,
  49. type=QgsProcessingParameterField.Numeric))
  50. self.addParameter(QgsProcessingParameterFileDestination(self.OUTPUT, self.tr('Scatterplot'), self.tr('HTML files (*.html)')))
  51. def name(self):
  52. return 'vectorlayerscatterplot'
  53. def displayName(self):
  54. return self.tr('Vector layer scatterplot')
  55. def processAlgorithm(self, parameters, context, feedback):
  56. try:
  57. # importing plotly throws Python warnings from within the library - filter these out
  58. with warnings.catch_warnings():
  59. warnings.filterwarnings("ignore", category=ResourceWarning)
  60. warnings.filterwarnings("ignore", category=ImportWarning)
  61. import plotly as plt
  62. import plotly.graph_objs as go
  63. except ImportError:
  64. raise QgsProcessingException(QCoreApplication.translate('VectorLayerScatterplot', 'This algorithm requires the Python “plotly” library. Please install this library and try again.'))
  65. source = self.parameterAsSource(parameters, self.INPUT, context)
  66. if source is None:
  67. raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
  68. xfieldname = self.parameterAsString(parameters, self.XFIELD, context)
  69. yfieldname = self.parameterAsString(parameters, self.YFIELD, context)
  70. output = self.parameterAsFileOutput(parameters, self.OUTPUT, context)
  71. values = vector.values(source, xfieldname, yfieldname)
  72. data = [go.Scatter(x=values[xfieldname],
  73. y=values[yfieldname],
  74. mode='markers')]
  75. plt.offline.plot(data, filename=output, auto_open=False)
  76. return {self.OUTPUT: output}