Skip to content

Commit

Permalink
[processing] restore slope algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Oct 11, 2017
1 parent c9ea889 commit f663ceb
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 98 deletions.
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/gdal/GdalAlgorithmProvider.py
Expand Up @@ -43,6 +43,7 @@
from .pct2rgb import pct2rgb
from .polygonize import polygonize
from .rgb2pct import rgb2pct
from .slope import slope
from .translate import translate
from .tpi import tpi
from .tri import tri
Expand All @@ -59,7 +60,6 @@
# from .fillnodata import fillnodata
# from .extractprojection import ExtractProjection
# from .gdal2xyz import gdal2xyz
# from .slope import slope
# from .roughness import roughness
# from .GridInvDist import GridInvDist
# from .GridAverage import GridAverage
Expand Down Expand Up @@ -144,6 +144,7 @@ def loadAlgorithms(self):
pct2rgb(),
polygonize(),
rgb2pct(),
slope(),
translate(),
tpi(),
tri(),
Expand All @@ -159,7 +160,6 @@ def loadAlgorithms(self):
# fillnodata(),
# ExtractProjection(),
# gdal2xyz(),
# slope(),
# roughness(),
# GridInvDist(),
# GridAverage(),
Expand Down
71 changes: 39 additions & 32 deletions python/plugins/processing/algs/gdal/slope.py
Expand Up @@ -29,11 +29,13 @@

import os

from qgis.core import (QgsRasterFileWriter,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterBand,
QgsProcessingParameterNumber,
QgsProcessingParameterBoolean,
QgsProcessingParameterRasterDestination)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterNumber
from processing.core.outputs import OutputRaster
from processing.algs.gdal.GdalUtils import GdalUtils

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
Expand All @@ -49,57 +51,62 @@ class slope(GdalAlgorithm):
SCALE = 'SCALE'
OUTPUT = 'OUTPUT'

def group(self):
return self.tr('Raster analysis')

def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterRaster(self.INPUT, self.tr('Input layer')))
self.addParameter(ParameterNumber(self.BAND,
self.tr('Band number'), 1, 99, 1))
self.addParameter(ParameterBoolean(self.COMPUTE_EDGES,
self.tr('Compute edges'), False))
self.addParameter(ParameterBoolean(self.ZEVENBERGEN,
self.tr("Use Zevenbergen&Thorne formula (instead of the Horn's one)"),
False))
self.addParameter(ParameterBoolean(self.AS_PERCENT,
self.tr('Slope expressed as percent (instead of degrees)'), False))
self.addParameter(ParameterNumber(self.SCALE,
self.tr('Scale (ratio of vert. units to horiz.)'),
0.0, 99999999.999999, 1.0))

self.addOutput(OutputRaster(self.OUTPUT, self.tr('Slope')))
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer')))
self.addParameter(QgsProcessingParameterBand(
self.BAND, self.tr('Band number'), parentLayerParameterName=self.INPUT))
self.addParameter(QgsProcessingParameterBoolean(
self.COMPUTE_EDGES, self.tr('Compute edges'), defaultValue=False))
self.addParameter(QgsProcessingParameterBoolean(
self.ZEVENBERGEN, self.tr("Use Zevenbergen&Thorne formula (instead of the Horn's one)"),
defaultValue=False))
self.addParameter(QgsProcessingParameterBoolean(
self.AS_PERCENT, self.tr('Slope expressed as percent (instead of degrees)'),
defaultValue=False))
self.addParameter(QgsProcessingParameterNumber(
self.SCALE, self.tr('Ratio of vertical units to horizontal'),
type=QgsProcessingParameterNumber.Double,
minValue=0.0, maxValue=99999999.999999, defaultValue=1.0))

self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Slope')))

def name(self):
return 'slope'

def displayName(self):
return self.tr('Slope')

def group(self):
return self.tr('Raster analysis')

def getConsoleCommands(self, parameters, context, feedback):
arguments = ['slope']
arguments.append(str(self.getParameterValue(self.INPUT)))
output = str(self.getOutputValue(self.OUTPUT))
arguments.append(output)
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
arguments.append(inLayer.source())

out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
arguments.append(out)

arguments.append('-of')
arguments.append(GdalUtils.getFormatShortNameFromFilename(output))
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))

arguments.append('-b')
arguments.append(str(self.getParameterValue(self.BAND)))
arguments.append('-s')
arguments.append(str(self.getParameterValue(self.SCALE)))
arguments.append(str(self.parameterAsInt(parameters, self.BAND, context)))

if self.getParameterValue(self.COMPUTE_EDGES):
if self.parameterAsBool(parameters, self.COMPUTE_EDGES, context):
arguments.append('-compute_edges')

if self.getParameterValue(self.ZEVENBERGEN):
if self.parameterAsBool(parameters, self.ZEVENBERGEN, context):
arguments.append('-alg')
arguments.append('ZevenbergenThorne')

if self.getParameterValue(self.AS_PERCENT):
if self.parameterAsBool(parameters, self.AS_PERCENT, context):
arguments.append('-p')

arguments.append('-s')
arguments.append(str(self.parameterAsDouble(parameters, self.SCALE, context)))

return ['gdaldem', GdalUtils.escapeAndJoin(arguments)]
128 changes: 64 additions & 64 deletions python/plugins/processing/tests/testdata/gdal_algorithm_tests.yaml
Expand Up @@ -392,70 +392,70 @@ tests:
hash: ce2174ab155023367d38785fb867456c6a1ae3535ea0ec5ad7a694d5
type: rasterhash

# - algorithm: gdal:slope
# name: Slope
# params:
# AS_PERCENT: false
# BAND: 1
# COMPUTE_EDGES: false
# INPUT:
# name: dem.tif
# type: raster
# SCALE: 1.0
# ZEVENBERGEN: false
# results:
# OUTPUT:
# hash: 319c470de3315f440371d5df8a6e478a8ecaf1cf904a013dbf1b3a6a
# type: rasterhash
#
# - algorithm: gdal:slope
# name: Slope with edges
# params:
# AS_PERCENT: false
# BAND: 1
# COMPUTE_EDGES: true
# INPUT:
# name: dem.tif
# type: raster
# SCALE: 1.0
# ZEVENBERGEN: false
# results:
# OUTPUT:
# hash: f7f8df8b6517fd8660304f7f2fbd6ade2ae68035f4dd9a224c80b465
# type: rasterhash
#
# - algorithm: gdal:slope
# name: Slope with Zevenbergen formula
# params:
# AS_PERCENT: false
# BAND: 1
# COMPUTE_EDGES: false
# INPUT:
# name: dem.tif
# type: raster
# SCALE: 1.0
# ZEVENBERGEN: true
# results:
# OUTPUT:
# hash: 90e42b1bc7be9cf7b4a729c6db44dde0bba39dd33012ade6bc8080e7
# type: rasterhash
#
# - algorithm: gdal:slope
# name: Slope with percent instead of degree
# params:
# AS_PERCENT: true
# BAND: 1
# COMPUTE_EDGES: false
# INPUT:
# name: dem.tif
# type: raster
# SCALE: 1.0
# ZEVENBERGEN: false
# results:
# OUTPUT:
# hash: c9dc888254a571e7fbf66691fb72b35f030f87decf59ce67e32ad89d
# type: rasterhash
#
- algorithm: gdal:slope
name: Slope
params:
AS_PERCENT: false
BAND: 1
COMPUTE_EDGES: false
INPUT:
name: dem.tif
type: raster
SCALE: 1.0
ZEVENBERGEN: false
results:
OUTPUT:
hash: 319c470de3315f440371d5df8a6e478a8ecaf1cf904a013dbf1b3a6a
type: rasterhash

- algorithm: gdal:slope
name: Slope with edges
params:
AS_PERCENT: false
BAND: 1
COMPUTE_EDGES: true
INPUT:
name: dem.tif
type: raster
SCALE: 1.0
ZEVENBERGEN: false
results:
OUTPUT:
hash: f7f8df8b6517fd8660304f7f2fbd6ade2ae68035f4dd9a224c80b465
type: rasterhash

- algorithm: gdal:slope
name: Slope with Zevenbergen formula
params:
AS_PERCENT: false
BAND: 1
COMPUTE_EDGES: false
INPUT:
name: dem.tif
type: raster
SCALE: 1.0
ZEVENBERGEN: true
results:
OUTPUT:
hash: 90e42b1bc7be9cf7b4a729c6db44dde0bba39dd33012ade6bc8080e7
type: rasterhash

- algorithm: gdal:slope
name: Slope with percent instead of degree
params:
AS_PERCENT: true
BAND: 1
COMPUTE_EDGES: false
INPUT:
name: dem.tif
type: raster
SCALE: 1.0
ZEVENBERGEN: false
results:
OUTPUT:
hash: c9dc888254a571e7fbf66691fb72b35f030f87decf59ce67e32ad89d
type: rasterhash

# - algorithm: gdal:roughness
# name: standard roughness
# params:
Expand Down

0 comments on commit f663ceb

Please sign in to comment.