27
27
28
28
__revision__ = '$Format:%H$'
29
29
30
- import matplotlib .pyplot as plt
31
- import matplotlib .pylab as lab
32
-
33
- from qgis .PyQt .QtCore import QVariant
34
- from qgis .core import QgsField
30
+ import plotly as plt
31
+ import plotly .graph_objs as go
35
32
36
33
from processing .core .GeoAlgorithm import GeoAlgorithm
37
34
from processing .core .parameters import ParameterNumber
38
35
from processing .core .parameters import ParameterRaster
39
- from processing .core .outputs import OutputTable
40
36
from processing .core .outputs import OutputHTML
41
- from processing .tools import dataobjects
42
- from processing .tools import raster
37
+ from processing .tools import dataobjects , raster
43
38
44
39
45
40
class RasterLayerHistogram (GeoAlgorithm ):
46
41
47
42
INPUT = 'INPUT'
48
- PLOT = 'PLOT'
49
- TABLE = 'TABLE'
50
43
BINS = 'BINS'
44
+ PLOT = 'PLOT'
51
45
52
46
def defineCharacteristics (self ):
53
47
self .name , self .i18n_name = self .trAlgorithm ('Raster layer histogram' )
@@ -59,33 +53,22 @@ def defineCharacteristics(self):
59
53
self .tr ('Number of bins' ), 2 , None , 10 ))
60
54
61
55
self .addOutput (OutputHTML (self .PLOT , self .tr ('Histogram' )))
62
- self .addOutput (OutputTable (self .TABLE , self .tr ('Table' )))
63
56
64
57
def processAlgorithm (self , feedback ):
65
58
layer = dataobjects .getObjectFromUri (
66
59
self .getParameterValue (self .INPUT ))
67
60
nbins = self .getParameterValue (self .BINS )
68
61
69
- outputplot = self .getOutputValue (self .PLOT )
70
- outputtable = self .getOutputFromName (self .TABLE )
62
+ output = self .getOutputValue (self .PLOT )
71
63
64
+ # ALERT: this is potentially blocking if the layer is too big
72
65
values = raster .scanraster (layer , feedback )
73
66
74
- # ALERT: this is potentially blocking if the layer is too big
75
- plt .close ()
76
67
valueslist = []
77
68
for v in values :
78
69
if v is not None :
79
70
valueslist .append (v )
80
- (n , bins , values ) = plt .hist (valueslist , nbins )
81
-
82
- fields = [QgsField ('CENTER_VALUE' , QVariant .Double ),
83
- QgsField ('NUM_ELEM' , QVariant .Double )]
84
- writer = outputtable .getTableWriter (fields )
85
- for i in range (len (values )):
86
- writer .addRecord ([str (bins [i ]) + '-' + str (bins [i + 1 ]), n [i ]])
87
-
88
- plotFilename = outputplot + '.png'
89
- lab .savefig (plotFilename )
90
- with open (outputplot , 'w' ) as f :
91
- f .write ('<html><img src="' + plotFilename + '"/></html>' )
71
+
72
+ data = [go .Histogram (x = valueslist ,
73
+ nbinsx = nbins )]
74
+ plt .offline .plot (data , filename = output )
0 commit comments