Nyall Dawson 1e13d733c2 Move declaration of algorithm parameters/outputs to a new virtual
initAlgorithm() method

This allows 2 benefits:
- algorithms can be subclassed and have subclasses add additional
parameters/outputs to the algorithm. With the previous approach
of declaring parameters/outputs in the constructor, it's not
possible to call virtual methods to add additional parameters/
outputs (since you can't call virtual methods from a constructor).

- initAlgorithm takes a variant map argument, allowing the algorithm
to dynamically adjust its declared parameters and outputs according
to this configuration map. This potentially allows model algorithms which
can be configured to have variable numbers of parameters and
outputs at run time. E.g. a "router" algorithm which directs
features to one of any number of output sinks depending on some
user configured criteria.
2017-07-10 16:31:14 +10:00

82 lines
2.8 KiB
Python

# -*- coding: utf-8 -*-
"""
***************************************************************************
Slope.py
---------------------
Date : October 2016
Copyright : (C) 2016 by Alexander Bruy
Email : alexander dot bruy at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Alexander Bruy'
__date__ = 'October 2016'
__copyright__ = '(C) 2016, Alexander Bruy'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import os
from qgis.PyQt.QtGui import QIcon
from qgis.analysis import QgsSlopeFilter
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterNumber
from processing.core.outputs import OutputRaster
from processing.tools import raster
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
class Slope(QgisAlgorithm):
INPUT_LAYER = 'INPUT_LAYER'
Z_FACTOR = 'Z_FACTOR'
OUTPUT_LAYER = 'OUTPUT_LAYER'
def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'dem.png'))
def group(self):
return self.tr('Raster terrain analysis')
def __init__(self):
super().__init__()
def initAlgorithm(self, config=None):
self.addParameter(ParameterRaster(self.INPUT_LAYER,
self.tr('Elevation layer')))
self.addParameter(ParameterNumber(self.Z_FACTOR,
self.tr('Z factor'), 1.0, 999999.99, 1.0))
self.addOutput(OutputRaster(self.OUTPUT_LAYER,
self.tr('Slope')))
def name(self):
return 'slope'
def displayName(self):
return self.tr('Slope')
def processAlgorithm(self, parameters, context, feedback):
inputFile = self.getParameterValue(self.INPUT_LAYER)
zFactor = self.getParameterValue(self.Z_FACTOR)
outputFile = self.getOutputValue(self.OUTPUT_LAYER)
outputFormat = raster.formatShortNameFromFileName(outputFile)
slope = QgsSlopeFilter(inputFile, outputFile, outputFormat)
slope.setZFactor(zFactor)
slope.processRaster(None)