Skip to content

Commit

Permalink
Port remaining plotly based algs to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 29, 2017
1 parent 9464ab7 commit 184899e
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 190 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ tests/testdata/grass/wgs84/test/.gislock
tests/testdata/grass/wgs84/test6/.gislock
tests/testdata/grass/wgs84/test7/.gislock
tests/testdata/*.aux.xml
tests/testdata/landsat-int16-b1.tif.aux.xml
tests/testdata/raster/*.aux.xml
tests/testdata/raster/band1_byte_noct_epsg4326.tif.aux.xml
tests/testdata/raster/band1_float32_noct_epsg4326.tif.aux.xml
Expand Down
7 changes: 2 additions & 5 deletions python/plugins/processing/algs/qgis/BarPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
import plotly.graph_objs as go


from qgis.core import (QgsApplication,
QgsFeatureSink,
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
from qgis.core import (QgsProcessingParameterFeatureSource,
QgsProcessingParameterField,
QgsProcessingParameterFileDestination,
QgsProcessingOutputHtml)
Expand Down Expand Up @@ -63,7 +60,7 @@ def initAlgorithm(self, config=None):
self.tr('Value field'),
None, self.INPUT, QgsProcessingParameterField.Numeric))

self.addParameter(QgsProcessingParameterFileDestination(self.OUTPUT, self.tr('Added'), self.tr('HTML files (*.html)')))
self.addParameter(QgsProcessingParameterFileDestination(self.OUTPUT, self.tr('Bar plot'), self.tr('HTML files (*.html)')))

self.addOutput(QgsProcessingOutputHtml(self.OUTPUT, self.tr('Bar plot')))

Expand Down
56 changes: 30 additions & 26 deletions python/plugins/processing/algs/qgis/BoxPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@
import plotly as plt
import plotly.graph_objs as go

from qgis.core import (QgsApplication,
QgsFeatureSink,
QgsProcessingUtils)
from processing.core.parameters import ParameterTable
from processing.core.parameters import ParameterTableField
from processing.core.parameters import ParameterSelection
from qgis.core import (QgsProcessingParameterFeatureSource,
QgsProcessingParameterField,
QgsProcessingParameterEnum,
QgsProcessingParameterFileDestination,
QgsProcessingOutputHtml,
QgsFeatureRequest)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.outputs import OutputHTML
from processing.tools import vector


Expand All @@ -54,25 +53,27 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterTable(self.INPUT, self.tr('Input table')))
self.addParameter(ParameterTableField(self.NAME_FIELD,
self.tr('Category name field'),
self.INPUT,
ParameterTableField.DATA_TYPE_ANY))
self.addParameter(ParameterTableField(self.VALUE_FIELD,
self.tr('Value field'),
self.INPUT,
ParameterTableField.DATA_TYPE_NUMBER))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Input layer')))
self.addParameter(QgsProcessingParameterField(self.NAME_FIELD,
self.tr('Category name field'),
parentLayerParameterName=self.INPUT,
type=QgsProcessingParameterField.Any))
self.addParameter(QgsProcessingParameterField(self.VALUE_FIELD,
self.tr('Value field'),
parentLayerParameterName=self.INPUT,
type=QgsProcessingParameterField.Numeric))
msd = [self.tr('Show Mean'),
self.tr('Show Standard Deviation'),
self.tr('Don\'t show Mean and Standard Deviation')
]
self.addParameter(ParameterSelection(
self.addParameter(QgsProcessingParameterEnum(
self.MSD,
self.tr('Additional Statistic Lines'),
msd, default=0))
options=msd, defaultValue=0))

self.addOutput(OutputHTML(self.OUTPUT, self.tr('Box plot')))
self.addParameter(QgsProcessingParameterFileDestination(self.OUTPUT, self.tr('Box plot'), self.tr('HTML files (*.html)')))
self.addOutput(QgsProcessingOutputHtml(self.OUTPUT, self.tr('Box plot')))

def name(self):
return 'boxplot'
Expand All @@ -81,17 +82,18 @@ def displayName(self):
return self.tr('Box plot')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
namefieldname = self.getParameterValue(self.NAME_FIELD)
valuefieldname = self.getParameterValue(self.VALUE_FIELD)
source = self.parameterAsSource(parameters, self.INPUT, context)
namefieldname = self.parameterAsString(parameters, self.NAME_FIELD, context)
valuefieldname = self.parameterAsString(parameters, self.VALUE_FIELD, context)

output = self.getOutputValue(self.OUTPUT)
output = self.parameterAsFileOutput(parameters, self.OUTPUT, context)

values = vector.values(layer, valuefieldname)
values = vector.values(source, valuefieldname)

x_var = [i[namefieldname] for i in layer.getFeatures()]
x_index = source.fields().lookupField(namefieldname)
x_var = [i[namefieldname] for i in source.getFeatures(QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry).setSubsetOfAttributes([x_index]))]

msdIndex = self.getParameterValue(self.MSD)
msdIndex = self.parameterAsEnum(parameters, self.MSD, context)
msd = True

if msdIndex == 1:
Expand All @@ -105,3 +107,5 @@ def processAlgorithm(self, parameters, context, feedback):
boxmean=msd)]

plt.offline.plot(data, filename=output, auto_open=False)

return {self.OUTPUT: output}
40 changes: 21 additions & 19 deletions python/plugins/processing/algs/qgis/MeanAndStdDevPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@
import plotly as plt
import plotly.graph_objs as go

from qgis.core import (QgsApplication,
QgsFeatureSink,
QgsProcessingUtils)
from qgis.core import (QgsProcessingParameterFeatureSource,
QgsProcessingParameterField,
QgsProcessingUtils,
QgsProcessingParameterFileDestination,
QgsProcessingOutputHtml)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterTable
from processing.core.parameters import ParameterTableField
from processing.core.outputs import OutputHTML

from processing.tools import vector

Expand All @@ -53,15 +52,16 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterTable(self.INPUT,
self.tr('Input table')))
self.addParameter(ParameterTableField(self.NAME_FIELD,
self.tr('Category name field'), self.INPUT,
ParameterTableField.DATA_TYPE_ANY))
self.addParameter(ParameterTableField(self.VALUE_FIELD,
self.tr('Value field'), self.INPUT))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Input table')))
self.addParameter(QgsProcessingParameterField(self.NAME_FIELD,
self.tr('Category name field'), parentLayerParameterName=self.INPUT,
type=QgsProcessingParameterField.Any))
self.addParameter(QgsProcessingParameterField(self.VALUE_FIELD,
self.tr('Value field'), parentLayerParameterName=self.INPUT))

self.addOutput(OutputHTML(self.OUTPUT, self.tr('Plot')))
self.addParameter(QgsProcessingParameterFileDestination(self.OUTPUT, self.tr('Plot'), self.tr('HTML files (*.html)')))
self.addOutput(QgsProcessingOutputHtml(self.OUTPUT, self.tr('Plot')))

def name(self):
return 'meanandstandarddeviationplot'
Expand All @@ -70,13 +70,13 @@ def displayName(self):
return self.tr('Mean and standard deviation plot')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
namefieldname = self.getParameterValue(self.NAME_FIELD)
valuefieldname = self.getParameterValue(self.VALUE_FIELD)
source = self.parameterAsSource(parameters, self.INPUT, context)
namefieldname = self.parameterAsString(parameters, self.NAME_FIELD, context)
valuefieldname = self.parameterAsString(parameters, self.VALUE_FIELD, context)

output = self.getOutputValue(self.OUTPUT)
output = self.parameterAsFileOutput(parameters, self.OUTPUT, context)

values = vector.values(layer, namefieldname, valuefieldname)
values = vector.values(source, namefieldname, valuefieldname)

d = {}
for i in range(len(values[namefieldname])):
Expand All @@ -93,3 +93,5 @@ def processAlgorithm(self, parameters, context, feedback):
name=k
))
plt.offline.plot(data, filename=output, auto_open=False)

return {self.OUTPUT: output}
36 changes: 19 additions & 17 deletions python/plugins/processing/algs/qgis/PolarPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@
import plotly.graph_objs as go
import numpy as np

from qgis.core import (QgsApplication,
QgsProcessingUtils)
from qgis.core import (QgsProcessingParameterFeatureSource,
QgsProcessingParameterField,
QgsProcessingParameterFileDestination,
QgsProcessingOutputHtml)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterTable
from processing.core.parameters import ParameterTableField
from processing.core.outputs import OutputHTML
from processing.tools import vector


Expand All @@ -52,14 +51,15 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterTable(self.INPUT,
self.tr('Input table')))
self.addParameter(ParameterTableField(self.NAME_FIELD,
self.tr('Category name field'), self.INPUT)) # FIXME unused?
self.addParameter(ParameterTableField(self.VALUE_FIELD,
self.tr('Value field'), self.INPUT))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Input layer')))
self.addParameter(QgsProcessingParameterField(self.NAME_FIELD,
self.tr('Category name field'), parentLayerParameterName=self.INPUT)) # FIXME unused?
self.addParameter(QgsProcessingParameterField(self.VALUE_FIELD,
self.tr('Value field'), parentLayerParameterName=self.INPUT))

self.addOutput(OutputHTML(self.OUTPUT, self.tr('Polar plot')))
self.addParameter(QgsProcessingParameterFileDestination(self.OUTPUT, self.tr('Polar plot'), self.tr('HTML files (*.html)')))
self.addOutput(QgsProcessingOutputHtml(self.OUTPUT, self.tr('Polar plot')))

def name(self):
return 'polarplot'
Expand All @@ -68,14 +68,16 @@ def displayName(self):
return self.tr('Polar plot')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
namefieldname = self.getParameterValue(self.NAME_FIELD) # NOQA FIXME unused?
valuefieldname = self.getParameterValue(self.VALUE_FIELD)
source = self.parameterAsSource(parameters, self.INPUT, context)
namefieldname = self.parameterAsString(parameters, self.NAME_FIELD, context) # NOQA FIXME unused?
valuefieldname = self.parameterAsString(parameters, self.VALUE_FIELD, context)

output = self.getOutputValue(self.OUTPUT)
output = self.parameterAsFileOutput(parameters, self.OUTPUT, context)

values = vector.values(layer, valuefieldname)
values = vector.values(source, valuefieldname)

data = [go.Area(r=values[valuefieldname],
t=np.degrees(np.arange(0.0, 2 * np.pi, 2 * np.pi / len(values[valuefieldname]))))]
plt.offline.plot(data, filename=output, auto_open=False)

return {self.OUTPUT: output}
29 changes: 16 additions & 13 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,20 +323,23 @@ def getAlgs(self):
]

if hasPlotly:
# from .VectorLayerHistogram import VectorLayerHistogram
# from .RasterLayerHistogram import RasterLayerHistogram
# from .VectorLayerScatterplot import VectorLayerScatterplot
# from .MeanAndStdDevPlot import MeanAndStdDevPlot
from .BarPlot import BarPlot
# from .PolarPlot import PolarPlot
# from .BoxPlot import BoxPlot
# from .VectorLayerScatterplot3D import VectorLayerScatterplot3D
#
algs.extend([BarPlot()])
#[VectorLayerHistogram(), RasterLayerHistogram(),
# VectorLayerScatterplot(), MeanAndStdDevPlot(),
# BarPlot(), PolarPlot(), BoxPlot(),
# VectorLayerScatterplot3D()])
from .BoxPlot import BoxPlot
from .MeanAndStdDevPlot import MeanAndStdDevPlot
from .PolarPlot import PolarPlot
from .RasterLayerHistogram import RasterLayerHistogram
from .VectorLayerHistogram import VectorLayerHistogram
from .VectorLayerScatterplot import VectorLayerScatterplot
from .VectorLayerScatterplot3D import VectorLayerScatterplot3D

algs.extend([BarPlot(),
BoxPlot(),
MeanAndStdDevPlot(),
PolarPlot(),
RasterLayerHistogram(),
VectorLayerHistogram(),
VectorLayerScatterplot(),
VectorLayerScatterplot3D()])

# to store algs added by 3rd party plugins as scripts
folder = os.path.join(os.path.dirname(__file__), 'scripts')
Expand Down
41 changes: 25 additions & 16 deletions python/plugins/processing/algs/qgis/RasterLayerHistogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@
import plotly as plt
import plotly.graph_objs as go

from qgis.core import (QgsApplication,
QgsProcessingUtils)
from qgis.core import (QgsProcessingParameterRasterLayer,
QgsProcessingParameterBand,
QgsProcessingParameterNumber,
QgsProcessingParameterFileDestination,
QgsProcessingOutputHtml)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterRaster
from processing.core.outputs import OutputHTML
from processing.tools import raster


class RasterLayerHistogram(QgisAlgorithm):

INPUT = 'INPUT'
BINS = 'BINS'
PLOT = 'PLOT'
OUTPUT = 'OUTPUT'
BAND = 'BAND'

def group(self):
return self.tr('Graphics')
Expand All @@ -50,12 +51,17 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterRaster(self.INPUT,
self.tr('Input layer')))
self.addParameter(ParameterNumber(self.BINS,
self.tr('Number of bins'), 2, None, 10))

self.addOutput(OutputHTML(self.PLOT, self.tr('Histogram')))
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,
self.tr('Input layer')))
self.addParameter(QgsProcessingParameterBand(self.BAND,
self.tr('Band number'),
1,
self.INPUT))
self.addParameter(QgsProcessingParameterNumber(self.BINS,
self.tr('number of bins'), minValue=2, defaultValue=10))

self.addParameter(QgsProcessingParameterFileDestination(self.OUTPUT, self.tr('Histogram'), self.tr('HTML files (*.html)')))
self.addOutput(QgsProcessingOutputHtml(self.OUTPUT, self.tr('Histogram')))

def name(self):
return 'rasterlayerhistogram'
Expand All @@ -64,13 +70,14 @@ def displayName(self):
return self.tr('Raster layer histogram')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
nbins = self.getParameterValue(self.BINS)
layer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
band = self.parameterAsInt(parameters, self.BAND, context)
nbins = self.parameterAsInt(parameters, self.BINS, context)

output = self.getOutputValue(self.PLOT)
output = self.parameterAsFileOutput(parameters, self.OUTPUT, context)

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

valueslist = []
for v in values:
Expand All @@ -80,3 +87,5 @@ def processAlgorithm(self, parameters, context, feedback):
data = [go.Histogram(x=valueslist,
nbinsx=nbins)]
plt.offline.plot(data, filename=output, auto_open=False)

return {self.OUTPUT: output}
Loading

0 comments on commit 184899e

Please sign in to comment.