Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Port gdal aspect alg to new API
  • Loading branch information
nyalldawson committed Aug 13, 2017
1 parent 2e7db48 commit 1cbbb8a
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 117 deletions.
16 changes: 9 additions & 7 deletions python/plugins/processing/algs/gdal/GdalAlgorithmProvider.py
Expand Up @@ -33,9 +33,10 @@
from processing.core.ProcessingConfig import ProcessingConfig, Setting
from .GdalUtils import GdalUtils

from .aspect import aspect
from .warp import warp
# from .nearblack import nearblack
# from .information import information
from .warp import warp
# from .rgb2pct import rgb2pct
# from .translate import translate
# from .pct2rgb import pct2rgb
Expand All @@ -54,7 +55,7 @@
# from .gdal2xyz import gdal2xyz
# from .hillshade import hillshade
# from .slope import slope
# from .aspect import aspect

# from .tri import tri
# from .tpi import tpi
# from .roughness import roughness
Expand All @@ -70,12 +71,13 @@
# from .gdal2tiles import gdal2tiles
# from .AssignProjection import AssignProjection
#
from .ogr2ogrpointsonlines import Ogr2OgrPointsOnLines
from .ogr2ogrtopostgis import Ogr2OgrToPostGis

# from .ogr2ogr import Ogr2Ogr
# from .ogr2ogrclip import Ogr2OgrClip
# from .ogr2ogrclipextent import Ogr2OgrClipExtent
# from .ogr2ogrtopostgis import Ogr2OgrToPostGis
# from .ogr2ogrtopostgislist import Ogr2OgrToPostGisList
from .ogr2ogrpointsonlines import Ogr2OgrPointsOnLines
# from .ogr2ogrbuffer import Ogr2OgrBuffer
# from .ogr2ogrdissolve import Ogr2OgrDissolve
# from .onesidebuffer import OneSideBuffer
Expand Down Expand Up @@ -141,6 +143,7 @@ def loadAlgorithms(self):
self.algs = [
# nearblack(),
# information(),
aspect(),
warp(),
# translate(),
# rgb2pct(),
Expand All @@ -160,7 +163,6 @@ def loadAlgorithms(self):
# gdal2xyz(),
# hillshade(),
# slope(),
# aspect(),
# tri(),
# tpi(),
# roughness(),
Expand All @@ -176,13 +178,13 @@ def loadAlgorithms(self):
# gdal2tiles(),
# AssignProjection(),
# ----- OGR tools -----
Ogr2OgrPointsOnLines(),
Ogr2OgrToPostGis(),
# OgrInfo(),
# Ogr2Ogr(),
# Ogr2OgrClip(),
# Ogr2OgrClipExtent(),
# Ogr2OgrToPostGis(),
# Ogr2OgrToPostGisList(),
Ogr2OgrPointsOnLines(),
# Ogr2OgrBuffer(),
# Ogr2OgrDissolve(),
# OneSideBuffer(),
Expand Down
56 changes: 29 additions & 27 deletions python/plugins/processing/algs/gdal/aspect.py
Expand Up @@ -28,11 +28,11 @@

import os

from qgis.core import (QgsProcessingParameterRasterLayer,
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 @@ -52,20 +52,20 @@ 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.TRIG_ANGLE,
self.tr('Return trigonometric angle (instead of azimuth)'), False))
self.addParameter(ParameterBoolean(self.ZERO_FLAT,
self.tr('Return 0 for flat (instead of -9999)'), False))

self.addOutput(OutputRaster(self.OUTPUT, self.tr('Aspect')))
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer')))
self.addParameter(QgsProcessingParameterNumber(
self.BAND, self.tr('Band number'), minValue=1, maxValue=99, defaultValue=1))
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.TRIG_ANGLE,
self.tr('Return trigonometric angle (instead of azimuth)'), defaultValue=False))
self.addParameter(QgsProcessingParameterBoolean(self.ZERO_FLAT,
self.tr('Return 0 for flat (instead of -9999)'), defaultValue=False))

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

def name(self):
return 'aspect'
Expand All @@ -78,27 +78,29 @@ def group(self):

def getConsoleCommands(self, parameters, context, feedback):
arguments = ['aspect']
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(GdalUtils.getFormatShortNameFromFilename(out))

arguments.append('-b')
arguments.append(str(self.getParameterValue(self.BAND)))
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.TRIG_ANGLE):
if self.parameterAsBool(parameters, self.TRIG_ANGLE, context):
arguments.append('-trigonometric')

if self.getParameterValue(self.ZERO_FLAT):
if self.parameterAsBool(parameters, self.ZERO_FLAT, context):
arguments.append('-zero_for_flat')

return ['gdaldem', GdalUtils.escapeAndJoin(arguments)]
2 changes: 0 additions & 2 deletions python/plugins/processing/algs/gdal/warp.py
Expand Up @@ -37,7 +37,6 @@
QgsProcessingParameterBoolean,
QgsProcessingParameterExtent,
QgsProcessingParameterRasterDestination,
QgsProcessingOutputRasterLayer,
QgsProcessingUtils)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils
Expand Down Expand Up @@ -113,7 +112,6 @@ def initAlgorithm(self, config=None):
self.TYPE, defaultValue=5))

self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Reprojected')))
self.addOutput(QgsProcessingOutputRasterLayer(self.OUTPUT, self.tr('Reprojected')))

def name(self):
return 'warpreproject'
Expand Down
162 changes: 81 additions & 81 deletions python/plugins/processing/tests/testdata/gdal_algorithm_tests.yaml
Expand Up @@ -139,87 +139,87 @@ tests:
# compare:
# geometry:
# precision: 7
#
# - algorithm: gdal:aspect
# name: Aspect with standard parameters
# params:
# BAND: 1
# COMPUTE_EDGES: false
# INPUT:
# name: dem.tif
# type: raster
# TRIG_ANGLE: false
# ZERO_FLAT: false
# ZEVENBERGEN: false
# results:
# OUTPUT:
# hash: 8436df662a44a00762aa29768e5d6ecfaf2d42e9a4da02d8afc6e3f6
# type: rasterhash
#
# - algorithm: gdal:aspect
# name: Aspect without NULL (-9999) values (0 instead)
# params:
# BAND: 1
# COMPUTE_EDGES: false
# INPUT:
# name: dem.tif
# type: raster
# TRIG_ANGLE: false
# ZERO_FLAT: true
# ZEVENBERGEN: false
# results:
# OUTPUT:
# hash: 43cccb440c7febb0095103eee3509b740e9f1bf2b3ad3b8a4c25622e
# type: rasterhash
#
# - algorithm: gdal:aspect
# name: Aspect with trigonometric angle
# params:
# BAND: 1
# COMPUTE_EDGES: false
# INPUT:
# name: dem.tif
# type: raster
# TRIG_ANGLE: true
# ZERO_FLAT: false
# ZEVENBERGEN: false
# results:
# OUTPUT:
# hash: a95e8a09a613b551d3f33dfb4975c430f599dc55f761063ae9529124
# type: rasterhash
#
# - algorithm: gdal:aspect
# name: Aspect zevenbergen
# params:
# BAND: 1
# COMPUTE_EDGES: false
# INPUT:
# name: dem.tif
# type: raster
# TRIG_ANGLE: false
# ZERO_FLAT: false
# ZEVENBERGEN: true
# results:
# OUTPUT:
# hash: 2cd5868b21efbd286f4977795143c89df77ac8976f8bc2a2c4e310d8
# type: rasterhash
#
# - algorithm: gdal:aspect
# name: Aspect with edges
# params:
# BAND: 1
# COMPUTE_EDGES: true
# INPUT:
# name: dem.tif
# type: raster
# TRIG_ANGLE: false
# ZERO_FLAT: false
# ZEVENBERGEN: false
# results:
# OUTPUT:
# hash: d3a354c6e5f207779bb58f9bd23fd89a9f90a77d81aafc661d0ae077
# type: rasterhash
#

- algorithm: gdal:aspect
name: Aspect with standard parameters
params:
BAND: 1
COMPUTE_EDGES: false
INPUT:
name: dem.tif
type: raster
TRIG_ANGLE: false
ZERO_FLAT: false
ZEVENBERGEN: false
results:
OUTPUT:
hash: 8436df662a44a00762aa29768e5d6ecfaf2d42e9a4da02d8afc6e3f6
type: rasterhash

- algorithm: gdal:aspect
name: Aspect without NULL (-9999) values (0 instead)
params:
BAND: 1
COMPUTE_EDGES: false
INPUT:
name: dem.tif
type: raster
TRIG_ANGLE: false
ZERO_FLAT: true
ZEVENBERGEN: false
results:
OUTPUT:
hash: 43cccb440c7febb0095103eee3509b740e9f1bf2b3ad3b8a4c25622e
type: rasterhash

- algorithm: gdal:aspect
name: Aspect with trigonometric angle
params:
BAND: 1
COMPUTE_EDGES: false
INPUT:
name: dem.tif
type: raster
TRIG_ANGLE: true
ZERO_FLAT: false
ZEVENBERGEN: false
results:
OUTPUT:
hash: a95e8a09a613b551d3f33dfb4975c430f599dc55f761063ae9529124
type: rasterhash

- algorithm: gdal:aspect
name: Aspect zevenbergen
params:
BAND: 1
COMPUTE_EDGES: false
INPUT:
name: dem.tif
type: raster
TRIG_ANGLE: false
ZERO_FLAT: false
ZEVENBERGEN: true
results:
OUTPUT:
hash: 2cd5868b21efbd286f4977795143c89df77ac8976f8bc2a2c4e310d8
type: rasterhash

- algorithm: gdal:aspect
name: Aspect with edges
params:
BAND: 1
COMPUTE_EDGES: true
INPUT:
name: dem.tif
type: raster
TRIG_ANGLE: false
ZERO_FLAT: false
ZEVENBERGEN: false
results:
OUTPUT:
hash: d3a354c6e5f207779bb58f9bd23fd89a9f90a77d81aafc661d0ae077
type: rasterhash

# - algorithm: gdal:cliprasterbyextent
# name: Clip raster by extent
# params:
Expand Down

0 comments on commit 1cbbb8a

Please sign in to comment.