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 Diff line number Diff line change
Expand Up @@ -277,15 +277,14 @@ def __init__(self):
#~ ])
if hasPlotly:
from .VectorLayerHistogram import VectorLayerHistogram
#~ from .RasterLayerHistogram import RasterLayerHistogram
from .RasterLayerHistogram import RasterLayerHistogram
from .VectorLayerScatterplot import VectorLayerScatterplot
#~ from .MeanAndStdDevPlot import MeanAndStdDevPlot
from .BarPlot import BarPlot
#~ from .PolarPlot import PolarPlot

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

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 Diff line number Diff line change
Expand Up @@ -27,27 +27,21 @@

__revision__ = '$Format:%H$'

import matplotlib.pyplot as plt
import matplotlib.pylab as lab

from qgis.PyQt.QtCore import QVariant
from qgis.core import QgsField
import plotly as plt
import plotly.graph_objs as go

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


class RasterLayerHistogram(GeoAlgorithm):

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

def defineCharacteristics(self):
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.addOutput(OutputHTML(self.PLOT, self.tr('Histogram')))
self.addOutput(OutputTable(self.TABLE, self.tr('Table')))

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

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

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

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

fields = [QgsField('CENTER_VALUE', QVariant.Double),
QgsField('NUM_ELEM', QVariant.Double)]
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>')

data = [go.Histogram(x=valueslist,
nbinsx=nbins)]
plt.offline.plot(data, filename=output)

0 comments on commit 2abc3f7

Please sign in to comment.