Skip to content

Commit

Permalink
[processing] update raster layer histogram
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Feb 15, 2017
1 parent e0131a7 commit 2abc3f7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 31 deletions.
7 changes: 3 additions & 4 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -277,15 +277,14 @@ def __init__(self):
#~ ]) #~ ])
if hasPlotly: if hasPlotly:
from .VectorLayerHistogram import VectorLayerHistogram from .VectorLayerHistogram import VectorLayerHistogram
#~ from .RasterLayerHistogram import RasterLayerHistogram from .RasterLayerHistogram import RasterLayerHistogram
from .VectorLayerScatterplot import VectorLayerScatterplot from .VectorLayerScatterplot import VectorLayerScatterplot
#~ from .MeanAndStdDevPlot import MeanAndStdDevPlot #~ from .MeanAndStdDevPlot import MeanAndStdDevPlot
from .BarPlot import BarPlot from .BarPlot import BarPlot
#~ from .PolarPlot import PolarPlot #~ from .PolarPlot import PolarPlot


self.alglist.extend([ self.alglist.extend([VectorLayerHistogram(), RasterLayerHistogram(),
VectorLayerHistogram(), VectorLayerScatterplot(), VectorLayerScatterplot(), BarPlot()])
BarPlot()])


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


Expand Down
37 changes: 10 additions & 27 deletions python/plugins/processing/algs/qgis/RasterLayerHistogram.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,27 +27,21 @@


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


import matplotlib.pyplot as plt import plotly as plt
import matplotlib.pylab as lab import plotly.graph_objs as go

from qgis.PyQt.QtCore import QVariant
from qgis.core import QgsField


from processing.core.GeoAlgorithm import GeoAlgorithm from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterNumber from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterRaster from processing.core.parameters import ParameterRaster
from processing.core.outputs import OutputTable
from processing.core.outputs import OutputHTML from processing.core.outputs import OutputHTML
from processing.tools import dataobjects from processing.tools import dataobjects, raster
from processing.tools import raster




class RasterLayerHistogram(GeoAlgorithm): class RasterLayerHistogram(GeoAlgorithm):


INPUT = 'INPUT' INPUT = 'INPUT'
PLOT = 'PLOT'
TABLE = 'TABLE'
BINS = 'BINS' BINS = 'BINS'
PLOT = 'PLOT'


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


self.addOutput(OutputHTML(self.PLOT, self.tr('Histogram'))) self.addOutput(OutputHTML(self.PLOT, self.tr('Histogram')))
self.addOutput(OutputTable(self.TABLE, self.tr('Table')))


def processAlgorithm(self, feedback): def processAlgorithm(self, feedback):
layer = dataobjects.getObjectFromUri( layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT)) self.getParameterValue(self.INPUT))
nbins = self.getParameterValue(self.BINS) nbins = self.getParameterValue(self.BINS)


outputplot = self.getOutputValue(self.PLOT) output = self.getOutputValue(self.PLOT)
outputtable = self.getOutputFromName(self.TABLE)


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


# ALERT: this is potentially blocking if the layer is too big
plt.close()
valueslist = [] valueslist = []
for v in values: for v in values:
if v is not None: if v is not None:
valueslist.append(v) valueslist.append(v)
(n, bins, values) = plt.hist(valueslist, nbins)

data = [go.Histogram(x=valueslist,
fields = [QgsField('CENTER_VALUE', QVariant.Double), nbinsx=nbins)]
QgsField('NUM_ELEM', QVariant.Double)] plt.offline.plot(data, filename=output)
writer = outputtable.getTableWriter(fields)
for i in range(len(values)):
writer.addRecord([str(bins[i]) + '-' + str(bins[i + 1]), n[i]])

plotFilename = outputplot + '.png'
lab.savefig(plotFilename)
with open(outputplot, 'w') as f:
f.write('<html><img src="' + plotFilename + '"/></html>')

0 comments on commit 2abc3f7

Please sign in to comment.