Skip to content

Commit

Permalink
[processing] enable algs that require 3rd party Python modules when
Browse files Browse the repository at this point in the history
possible
  • Loading branch information
alexbruy committed Jan 17, 2015
1 parent 737c9cc commit fdd1751
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 126 deletions.
37 changes: 21 additions & 16 deletions python/plugins/processing/algs/qgis/BarPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@
import matplotlib.pyplot as plt
import matplotlib.pylab as lab
import numpy as np

from PyQt4.QtCore import *
from qgis.core import *

from processing.core.parameters import ParameterTable
from processing.core.parameters import ParameterTableField
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.outputs import OutputHTML
from processing.tools import *
from processing.tools import vector
from processing.tools import dataobjects


class BarPlot(GeoAlgorithm):
Expand All @@ -44,33 +47,35 @@ class BarPlot(GeoAlgorithm):
NAME_FIELD = 'NAME_FIELD'
VALUE_FIELD = 'VALUE_FIELD'

def defineCharacteristics(self):
self.name = 'Bar plot'
self.group = 'Graphics'

self.addParameter(ParameterTable(self.INPUT, self.tr('Input table')))
self.addParameter(ParameterTableField(self.NAME_FIELD,
self.tr('Category name field'), self.INPUT))
self.addParameter(ParameterTableField(self.VALUE_FIELD,
self.tr('Value field'), self.INPUT))

self.addOutput(OutputHTML(self.OUTPUT, self.tr('Output')))

def processAlgorithm(self, progress):
uri = self.getParameterValue(self.INPUT)
layer = getObjectFromUri(uri)
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT))
namefieldname = self.getParameterValue(self.NAME_FIELD)
valuefieldname = self.getParameterValue(self.VALUE_FIELD)

output = self.getOutputValue(self.OUTPUT)
values = vector.getAttributeValues(layer, namefieldname,
valuefieldname)

values = vector.values(layer, namefieldname, valuefieldname)
plt.close()

ind = np.arange(len(values[namefieldname]))
width = 0.8
plt.bar(ind, values[valuefieldname], width, color='r')

plt.xticks(ind, values[namefieldname], rotation=45)
plotFilename = output + '.png'
lab.savefig(plotFilename)
f = open(output, 'w')
f.write('<img src="' + plotFilename + '"/>')
f.close()

def defineCharacteristics(self):
self.name = 'Bar plot'
self.group = 'Graphics'
self.addParameter(ParameterTable(self.INPUT, self.tr('Input table')))
self.addParameter(ParameterTableField(self.NAME_FIELD,
self.tr('Category name field'), self.INPUT))
self.addParameter(ParameterTableField(self.VALUE_FIELD,
self.tr('Value field'), self.INPUT))
self.addOutput(OutputHTML(self.OUTPUT, self.tr('Output')))
32 changes: 19 additions & 13 deletions python/plugins/processing/algs/qgis/CreateConstantRaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@

from PyQt4.QtCore import *
from qgis.core import *
from processing.tools.raster import RasterWriter

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterNumber
from processing.core.outputs import OutputRaster
from processing.tools.raster import RasterWriter
from processing.tools import dataobjects


Expand All @@ -41,13 +42,28 @@ class CreateConstantRaster(GeoAlgorithm):
OUTPUT = 'OUTPUT'
NUMBER = 'NUMBER'

def defineCharacteristics(self):
self.name = 'Create constant raster layer'
self.group = 'Raster tools'

self.addParameter(ParameterRaster(self.INPUT,
self.tr('Reference layer')))
self.addParameter(ParameterNumber(self.NUMBER,
self.tr('Constant value'), default=1.0))

self.addOutput(OutputRaster(self.OUTPUT,
self.tr('Output layer')))

def processAlgorithm(self, progress):
output = self.getOutputFromName(self.OUTPUT)
value = self.getOutputValue(self.NUMBER)
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT))
value = self.getOutputValue(self.NUMBER)

output = self.getOutputFromName(self.OUTPUT)

cellsize = (layer.extent().xMaximum() - layer.extent().xMinimum()) \
/ layer.width()

w = RasterWriter(output.getCompatibleFileName(self),
layer.extent().xMinimum(),
layer.extent().yMinimum(),
Expand All @@ -59,13 +75,3 @@ def processAlgorithm(self, progress):
)
w.matrix[:] = value
w.close()

def defineCharacteristics(self):
self.name = 'Create constant raster layer'
self.group = 'Raster tools'
self.addParameter(ParameterRaster(self.INPUT,
self.tr('Reference layer')))
self.addParameter(ParameterNumber(self.NUMBER,
self.tr('Constant value'), default=1.0))
self.addOutput(OutputRaster(self.OUTPUT,
self.tr('Output layer')))
45 changes: 25 additions & 20 deletions python/plugins/processing/algs/qgis/MeanAndStdDevPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@
import matplotlib.pyplot as plt
import matplotlib.pylab as lab
import numpy as np

from PyQt4.QtCore import *
from qgis.core import *

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterTable
from processing.core.parameters import ParameterTableField
from processing.core.outputs import OutputHTML
from processing.tools import *

from processing.tools import vector
from processing.tools import dataobjects


class MeanAndStdDevPlot(GeoAlgorithm):
Expand All @@ -46,17 +49,33 @@ class MeanAndStdDevPlot(GeoAlgorithm):
MEAN_FIELD = 'MEAN_FIELD'
STDDEV_FIELD = 'STDDEV_FIELD'

def defineCharacteristics(self):
self.name = 'Mean and standard deviation plot'
self.group = 'Graphics'

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.MEAN_FIELD,
self.tr('Mean field'), self.INPUT))
self.addParameter(ParameterTableField(self.STDDEV_FIELD,
self.tr('StdDev field'), self.INPUT))

self.addOutput(OutputHTML(self.OUTPUT, self.tr('Output')))

def processAlgorithm(self, progress):
uri = self.getParameterValue(self.INPUT)
layer = dataobjects.getObjectFromUri(uri)
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT))
namefieldname = self.getParameterValue(self.NAME_FIELD)
meanfieldname = self.getParameterValue(self.MEAN_FIELD)
stddevfieldname = self.getParameterValue(self.STDDEV_FIELD)

output = self.getOutputValue(self.OUTPUT)
values = vector.getAttributeValues(layer, namefieldname,
meanfieldname, stddevfieldname)
plt.close()

values = vector.values(layer, namefieldname, meanfieldname, stddevfieldname)
plt.close()
ind = np.arange(len(values[namefieldname]))
width = 0.8
plt.bar(ind, values[meanfieldname], width, color='r',
Expand All @@ -70,17 +89,3 @@ def processAlgorithm(self, progress):
f = open(output, 'w')
f.write('<img src="' + plotFilename + '"/>')
f.close()

def defineCharacteristics(self):
self.name = 'Mean and standard deviation plot'
self.group = 'Graphics'
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.MEAN_FIELD,
self.tr('Mean field'), self.INPUT))
self.addParameter(ParameterTableField(self.STDDEV_FIELD,
self.tr('StdDev field'), self.INPUT))
self.addOutput(OutputHTML(self.OUTPUT, self.tr('Output')))
39 changes: 22 additions & 17 deletions python/plugins/processing/algs/qgis/PolarPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@
import matplotlib.cm as cm
from matplotlib.pyplot import figure, show, rc
import numpy as np

from PyQt4.QtCore import *
from qgis.core import *

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterTable
from processing.core.parameters import ParameterTableField
from processing.core.outputs import OutputHTML
from processing.tools import *
from processing.tools import vector
from processing.tools import dataobjects


class PolarPlot(GeoAlgorithm):
Expand All @@ -46,16 +49,29 @@ class PolarPlot(GeoAlgorithm):
NAME_FIELD = 'NAME_FIELD'
VALUE_FIELD = 'VALUE_FIELD'

def defineCharacteristics(self):
self.name = 'Polar plot'
self.group = 'Graphics'

self.addParameter(ParameterTable(self.INPUT,
self.tr('Input table')))
self.addParameter(ParameterTableField(self.NAME_FIELD,
self.tr('Category name field'), self.INPUT))
self.addParameter(ParameterTableField(self.VALUE_FIELD,
self.tr('Value field'), self.INPUT))

self.addOutput(OutputHTML(self.OUTPUT, self.tr('Output')))

def processAlgorithm(self, progress):
uri = self.getParameterValue(self.INPUT)
layer = getObjectFromUri(uri)
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT))
namefieldname = self.getParameterValue(self.NAME_FIELD)
valuefieldname = self.getParameterValue(self.VALUE_FIELD)

output = self.getOutputValue(self.OUTPUT)
values = vector.getAttributeValues(layer, namefieldname,
valuefieldname)
plt.close()

values = vector.values(layer, namefieldname, valuefieldname)
plt.close()
fig = figure(figsize=(8, 8))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True)
N = len(values[valuefieldname])
Expand All @@ -68,14 +84,3 @@ def processAlgorithm(self, progress):
f = open(output, 'w')
f.write('<img src="' + plotFilename + '"/>')
f.close()

def defineCharacteristics(self):
self.name = 'Polar plot'
self.group = 'Graphics'
self.addParameter(ParameterTable(self.INPUT,
self.tr('Input table')))
self.addParameter(ParameterTableField(self.NAME_FIELD,
self.tr('Category name field'), self.INPUT))
self.addParameter(ParameterTableField(self.VALUE_FIELD,
self.tr('Value field'), self.INPUT))
self.addOutput(OutputHTML(self.OUTPUT, self.tr('Output')))
33 changes: 20 additions & 13 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@

import os

try:
import matplotlib.pyplot
hasMatplotlib = True
except:
hasMatplotlib = False

from PyQt4.QtGui import *

from processing.core.AlgorithmProvider import AlgorithmProvider
Expand Down Expand Up @@ -119,12 +125,6 @@
from SelectByExpression import SelectByExpression
from HypsometricCurves import HypsometricCurves
from SplitLinesWithLines import SplitLinesWithLines
# from VectorLayerHistogram import VectorLayerHistogram
# from VectorLayerScatterplot import VectorLayerScatterplot
# from MeanAndStdDevPlot import MeanAndStdDevPlot
# from BarPlot import BarPlot
# from PolarPlot import PolarPlot
# from RasterLayerHistogram import RasterLayerHistogram

import processing.resources_rc

Expand Down Expand Up @@ -170,15 +170,22 @@ def __init__(self):
PostGISExecuteSQL(), ImportIntoPostGIS(),
SetVectorStyle(), SetRasterStyle(),
SelectByExpression(), HypsometricCurves(),
SplitLinesWithLines()
# ------ raster ------
# CreateConstantRaster(),
# ------ graphics ------
# VectorLayerHistogram(), VectorLayerScatterplot(),
# RasterLayerHistogram(), MeanAndStdDevPlot(),
# BarPlot(), PolarPlot()
SplitLinesWithLines(), CreateConstantRaster(),
]

if hasMatplotlib:
from VectorLayerHistogram import VectorLayerHistogram
from RasterLayerHistogram import RasterLayerHistogram
from VectorLayerScatterplot import VectorLayerScatterplot
from MeanAndStdDevPlot import MeanAndStdDevPlot
from BarPlot import BarPlot
from PolarPlot import PolarPlot

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

folder = os.path.join(os.path.dirname(__file__), 'scripts')
scripts = ScriptUtils.loadFromFolder(folder)
for script in scripts:
Expand Down
35 changes: 22 additions & 13 deletions python/plugins/processing/algs/qgis/RasterLayerHistogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@

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

from PyQt4.QtCore import *
from qgis.core import *

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterRaster
Expand All @@ -45,13 +47,28 @@ class RasterLayerHistogram(GeoAlgorithm):
TABLE = 'TABLE'
BINS = 'BINS'

def defineCharacteristics(self):
self.name = 'Raster layer histogram'
self.group = 'Graphics'

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('Output plot')))
self.addOutput(OutputTable(self.TABLE, self.tr('Output table')))


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

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

values = raster.scanraster(layer, progress)
nbins = self.getParameterValue(self.BINS)

# ALERT: this is potentially blocking if the layer is too big
plt.close()
Expand All @@ -60,23 +77,15 @@ def processAlgorithm(self, progress):
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 xrange(len(values)):
writer.addRecord([str(bins[i]) + '-' + str(bins[i + 1]), n[i]])

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

def defineCharacteristics(self):
self.name = 'Raster layer histogram'
self.group = 'Graphics'
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('Output plot')))
self.addOutput(OutputTable(self.TABLE, self.tr('Output table')))
Loading

0 comments on commit fdd1751

Please sign in to comment.