Skip to content

Commit

Permalink
[processing] restore near black and hillshade gdal algs (#5028)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Aug 16, 2017
1 parent 19efee8 commit a9d8d39
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 123 deletions.
8 changes: 4 additions & 4 deletions python/plugins/processing/algs/gdal/GdalAlgorithmProvider.py
Expand Up @@ -38,7 +38,7 @@
from .ColorRelief import ColorRelief
from .tri import tri
from .warp import warp
# from .nearblack import nearblack
from .nearblack import nearblack
# from .information import information
# from .rgb2pct import rgb2pct
# from .translate import translate
Expand All @@ -56,7 +56,7 @@
# from .fillnodata import fillnodata
# from .extractprojection import ExtractProjection
# from .gdal2xyz import gdal2xyz
# from .hillshade import hillshade
from .hillshade import hillshade
# from .slope import slope
# from .tpi import tpi
# from .roughness import roughness
Expand Down Expand Up @@ -140,7 +140,7 @@ def svgIconPath(self):

def loadAlgorithms(self):
self.algs = [
# nearblack(),
nearblack(),
# information(),
AssignProjection(),
aspect(),
Expand All @@ -163,7 +163,7 @@ def loadAlgorithms(self):
# fillnodata(),
# ExtractProjection(),
# gdal2xyz(),
# hillshade(),
hillshade(),
# slope(),
# tpi(),
# roughness(),
Expand Down
68 changes: 35 additions & 33 deletions python/plugins/processing/algs/gdal/hillshade.py
Expand Up @@ -29,11 +29,13 @@

import os

from qgis.core import (QgsProcessingParameterRasterLayer,
QgsProcessingParameterBand,
QgsProcessingParameterBoolean,
QgsProcessingParameterNumber,
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 @@ -55,26 +57,26 @@ 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(ParameterNumber(self.Z_FACTOR,
self.tr('Z factor (vertical exaggeration)'),
0.0, 99999999.999999, 1.0))
self.addParameter(ParameterNumber(self.SCALE,
self.tr('Scale (ratio of vert. units to horiz.)'),
0.0, 99999999.999999, 1.0))
self.addParameter(ParameterNumber(self.AZIMUTH,
self.tr('Azimuth of the light'), 0.0, 359.0, 315.0))
self.addParameter(ParameterNumber(self.ALTITUDE,
self.tr('Altitude of the light'), 0.0, 99999999.999999, 45.0))

self.addOutput(OutputRaster(self.OUTPUT, self.tr('Hillshade')))
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(QgsProcessingParameterNumber(self.Z_FACTOR,
self.tr('Z factor (vertical exaggeration)'),
type=QgsProcessingParameterNumber.Double, minValue=0.0, maxValue=99999999.999999, defaultValue=1.0))
self.addParameter(QgsProcessingParameterNumber(self.SCALE,
self.tr('Scale (ratio of vert. units to horiz.)'),
type=QgsProcessingParameterNumber.Double, minValue=0.0, maxValue=99999999.999999, defaultValue=1.0))
self.addParameter(QgsProcessingParameterNumber(self.AZIMUTH,
self.tr('Azimuth of the light'),
type=QgsProcessingParameterNumber.Double, minValue=0.0, maxValue=359.9, defaultValue=315.0))
self.addParameter(QgsProcessingParameterNumber(self.ALTITUDE,
self.tr('Altitude of the light'),
type=QgsProcessingParameterNumber.Double, minValue=0.0, maxValue=99999999.999999, defaultValue=45.0))
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Hillshade')))

def name(self):
return 'hillshade'
Expand All @@ -87,24 +89,24 @@ def group(self):

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

arguments.append('-b')
arguments.append(str(self.getParameterValue(self.BAND)))
arguments.append(str(self.parameterAsInt(parameters, self.BAND, context)))
arguments.append('-z')
arguments.append(str(self.getParameterValue(self.Z_FACTOR)))
arguments.append(str(self.parameterAsDouble(parameters, self.Z_FACTOR, context)))
arguments.append('-s')
arguments.append(str(self.getParameterValue(self.SCALE)))
arguments.append(str(self.parameterAsDouble(parameters, self.SCALE, context)))
arguments.append('-az')
arguments.append(str(self.getParameterValue(self.AZIMUTH)))
arguments.append(str(self.parameterAsDouble(parameters, self.AZIMUTH, context)))
arguments.append('-alt')
arguments.append(str(self.getParameterValue(self.ALTITUDE)))
arguments.append(str(self.parameterAsDouble(parameters, self.ALTITUDE, 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')

Expand Down
39 changes: 21 additions & 18 deletions python/plugins/processing/algs/gdal/nearblack.py
Expand Up @@ -28,13 +28,14 @@

import os

from qgis.core import (QgsProcessingParameterRasterLayer,
QgsProcessingParameterBoolean,
QgsProcessingParameterNumber,
QgsProcessingParameterRasterDestination)

from qgis.PyQt.QtGui import QIcon

from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterBoolean
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 @@ -54,14 +55,14 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterRaster(self.INPUT,
self.tr('Input layer'), False))
self.addParameter(ParameterNumber(self.NEAR,
self.tr('How far from black (white)'), 0, None, 15))
self.addParameter(ParameterBoolean(self.WHITE,
self.tr('Search for nearly white pixels instead of nearly black'),
False))
self.addOutput(OutputRaster(self.OUTPUT, self.tr('Nearblack')))
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer'), False))
self.addParameter(QgsProcessingParameterNumber(self.NEAR,
self.tr('How far from black (white)'),
type=QgsProcessingParameterNumber.Integer, minValue=0, maxValue=15, defaultValue=None))
self.addParameter(QgsProcessingParameterBoolean(self.WHITE,
self.tr('Search for nearly white pixels instead of nearly black'),
defaultValue=False))
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Nearblack')))

def name(self):
return 'nearblack'
Expand All @@ -73,15 +74,17 @@ def group(self):
return self.tr('Raster analysis')

def getConsoleCommands(self, parameters, context, feedback):
out = str(self.parameterAsOutputLayer(parameters, self.OUTPUT, context))

arguments = []
arguments.append('-o')
output = str(self.getOutputValue(self.OUTPUT))
arguments.append(output)
arguments.append(out)
arguments.append('-of')
arguments.append(GdalUtils.getFormatShortNameFromFilename(output))
arguments.append(GdalUtils.getFormatShortNameFromFilename(out))
arguments.append('-near')
arguments.append(str(self.getParameterValue(self.NEAR)))
if self.getParameterValue(self.WHITE):
arguments.append(str(self.parameterAsInt(parameters, self.NEAR, context)))
if self.parameterAsBool(parameters, self.WHITE, context):
arguments.append('-white')
arguments.append(self.getParameterValue(self.INPUT))
arguments.append(self.parameterAsRasterLayer(parameters, self.INPUT, context).source())

return ['nearblack', GdalUtils.escapeAndJoin(arguments)]
136 changes: 68 additions & 68 deletions python/plugins/processing/tests/testdata/gdal_algorithm_tests.yaml
Expand Up @@ -339,61 +339,61 @@ tests:
# OUTPUT:
# hash: 8f69431c6ff7db5174c1ca8e879ae39582fd4ce86f9b53edae0f38ce
# type: rasterhash
#
# - algorithm: gdal:hillshade
# name: Hillshade
# params:
# ALTITUDE: 45.0
# AZIMUTH: 315.0
# BAND: 1
# COMPUTE_EDGES: false
# INPUT:
# name: dem.tif
# type: raster
# SCALE: 1.0
# ZEVENBERGEN: false
# Z_FACTOR: 1.0
# results:
# OUTPUT:
# hash: e5cf91b875b918682e456463165eddc4af4ae8855b143068e198ce2a
# type: rasterhash
#
# - algorithm: gdal:hillshade
# name: Hillshade with edges calculation
# params:
# ALTITUDE: 45.0
# AZIMUTH: 315.0
# BAND: 1
# COMPUTE_EDGES: true
# INPUT:
# name: dem.tif
# type: raster
# SCALE: 1.0
# ZEVENBERGEN: false
# Z_FACTOR: 1.0
# results:
# OUTPUT:
# hash: 570671408b020c9cc817e876f0e8081e0b11920c7d297abfe1cb3368
# type: rasterhash
#
# - algorithm: gdal:hillshade
# name: Hillshade with Zevenbergen formula
# params:
# ALTITUDE: 45.0
# AZIMUTH: 315.0
# BAND: 1
# COMPUTE_EDGES: false
# INPUT:
# name: dem.tif
# type: raster
# SCALE: 1.0
# ZEVENBERGEN: true
# Z_FACTOR: 1.0
# results:
# OUTPUT:
# hash: ce2174ab155023367d38785fb867456c6a1ae3535ea0ec5ad7a694d5
# type: rasterhash
#

- algorithm: gdal:hillshade
name: Hillshade
params:
ALTITUDE: 45.0
AZIMUTH: 315.0
BAND: 1
COMPUTE_EDGES: false
INPUT:
name: dem.tif
type: raster
SCALE: 1.0
ZEVENBERGEN: false
Z_FACTOR: 1.0
results:
OUTPUT:
hash: e5cf91b875b918682e456463165eddc4af4ae8855b143068e198ce2a
type: rasterhash

- algorithm: gdal:hillshade
name: Hillshade with edges calculation
params:
ALTITUDE: 45.0
AZIMUTH: 315.0
BAND: 1
COMPUTE_EDGES: true
INPUT:
name: dem.tif
type: raster
SCALE: 1.0
ZEVENBERGEN: false
Z_FACTOR: 1.0
results:
OUTPUT:
hash: 570671408b020c9cc817e876f0e8081e0b11920c7d297abfe1cb3368
type: rasterhash

- algorithm: gdal:hillshade
name: Hillshade with Zevenbergen formula
params:
ALTITUDE: 45.0
AZIMUTH: 315.0
BAND: 1
COMPUTE_EDGES: false
INPUT:
name: dem.tif
type: raster
SCALE: 1.0
ZEVENBERGEN: true
Z_FACTOR: 1.0
results:
OUTPUT:
hash: ce2174ab155023367d38785fb867456c6a1ae3535ea0ec5ad7a694d5
type: rasterhash

# - algorithm: gdal:slope
# name: Slope
# params:
Expand Down Expand Up @@ -510,19 +510,19 @@ tests:
hash: 07376c74de869323125ceacbb5e34438655ff73f63d4b3ff3e268f7f
type: rasterhash

# - algorithm: gdal:nearblack
# name: standard near black
# params:
# INPUT:
# name: dem.tif
# type: raster
# NEAR: 15
# WHITE: false
# results:
# OUTPUT:
# hash: fff4a08498e93494f3f2cf1a9074451e6fd68341849aedc9e2c45e6a
# type: rasterhash
#
- algorithm: gdal:nearblack
name: standard near black
params:
INPUT:
name: dem.tif
type: raster
NEAR: 15
WHITE: false
results:
OUTPUT:
hash: fff4a08498e93494f3f2cf1a9074451e6fd68341849aedc9e2c45e6a
type: rasterhash

# - algorithm: gdal:tpitopographicpositionindex
# name: standard TPI
# params:
Expand Down

0 comments on commit a9d8d39

Please sign in to comment.