Skip to content

Commit 2abc3f7

Browse files
committed
[processing] update raster layer histogram
1 parent e0131a7 commit 2abc3f7

File tree

2 files changed

+13
-31
lines changed

2 files changed

+13
-31
lines changed

python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,14 @@ def __init__(self):
277277
#~ ])
278278
if hasPlotly:
279279
from .VectorLayerHistogram import VectorLayerHistogram
280-
#~ from .RasterLayerHistogram import RasterLayerHistogram
280+
from .RasterLayerHistogram import RasterLayerHistogram
281281
from .VectorLayerScatterplot import VectorLayerScatterplot
282282
#~ from .MeanAndStdDevPlot import MeanAndStdDevPlot
283283
from .BarPlot import BarPlot
284284
#~ from .PolarPlot import PolarPlot
285285

286-
self.alglist.extend([
287-
VectorLayerHistogram(), VectorLayerScatterplot(),
288-
BarPlot()])
286+
self.alglist.extend([VectorLayerHistogram(), RasterLayerHistogram(),
287+
VectorLayerScatterplot(), BarPlot()])
289288

290289
self.externalAlgs = [] # to store algs added by 3rd party plugins as scripts
291290

python/plugins/processing/algs/qgis/RasterLayerHistogram.py

+10-27
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,21 @@
2727

2828
__revision__ = '$Format:%H$'
2929

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
3532

3633
from processing.core.GeoAlgorithm import GeoAlgorithm
3734
from processing.core.parameters import ParameterNumber
3835
from processing.core.parameters import ParameterRaster
39-
from processing.core.outputs import OutputTable
4036
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
4338

4439

4540
class RasterLayerHistogram(GeoAlgorithm):
4641

4742
INPUT = 'INPUT'
48-
PLOT = 'PLOT'
49-
TABLE = 'TABLE'
5043
BINS = 'BINS'
44+
PLOT = 'PLOT'
5145

5246
def defineCharacteristics(self):
5347
self.name, self.i18n_name = self.trAlgorithm('Raster layer histogram')
@@ -59,33 +53,22 @@ def defineCharacteristics(self):
5953
self.tr('Number of bins'), 2, None, 10))
6054

6155
self.addOutput(OutputHTML(self.PLOT, self.tr('Histogram')))
62-
self.addOutput(OutputTable(self.TABLE, self.tr('Table')))
6356

6457
def processAlgorithm(self, feedback):
6558
layer = dataobjects.getObjectFromUri(
6659
self.getParameterValue(self.INPUT))
6760
nbins = self.getParameterValue(self.BINS)
6861

69-
outputplot = self.getOutputValue(self.PLOT)
70-
outputtable = self.getOutputFromName(self.TABLE)
62+
output = self.getOutputValue(self.PLOT)
7163

64+
# ALERT: this is potentially blocking if the layer is too big
7265
values = raster.scanraster(layer, feedback)
7366

74-
# ALERT: this is potentially blocking if the layer is too big
75-
plt.close()
7667
valueslist = []
7768
for v in values:
7869
if v is not None:
7970
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

Comments
 (0)