Skip to content
Permalink
Browse files

Port remaining plotly based algs to new API

  • Loading branch information
nyalldawson committed Aug 29, 2017
1 parent 9464ab7 commit 184899ec30cc5fb48900f242b083671ac5a7c890
@@ -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
@@ -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)
@@ -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')))

@@ -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


@@ -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'
@@ -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:
@@ -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}
@@ -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

@@ -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'
@@ -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])):
@@ -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}
@@ -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


@@ -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'
@@ -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}
@@ -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')
@@ -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')
@@ -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'
@@ -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:
@@ -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}

0 comments on commit 184899e

Please sign in to comment.
You can’t perform that action at this time.