Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing] homogenize gdal algorithms
-consistent parameters naming
-use additional creation options where possible
-remove duplicated functionality
  • Loading branch information
alexbruy committed Oct 11, 2017
1 parent 10baf9a commit 0f22664
Show file tree
Hide file tree
Showing 29 changed files with 800 additions and 514 deletions.
7 changes: 5 additions & 2 deletions python/plugins/processing/algs/gdal/AssignProjection.py
Expand Up @@ -41,6 +41,7 @@


class AssignProjection(GdalAlgorithm):

INPUT = 'INPUT'
CRS = 'CRS'
OUTPUT = 'OUTPUT'
Expand All @@ -49,11 +50,13 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer'), optional=False))
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,
self.tr('Input layer')))
self.addParameter(QgsProcessingParameterCrs(self.CRS,
self.tr('Desired CRS')))

self.addOutput(QgsProcessingOutputRasterLayer(self.OUTPUT, self.tr('Layer with projection')))
self.addOutput(QgsProcessingOutputRasterLayer(self.OUTPUT,
self.tr('Layer with projection')))

def name(self):
return 'assignprojection'
Expand Down
14 changes: 7 additions & 7 deletions python/plugins/processing/algs/gdal/ClipByExtent.py
Expand Up @@ -66,8 +66,6 @@ def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterNumber(self.NODATA,
self.tr('Assign a specified nodata value to output bands'),
type=QgsProcessingParameterNumber.Double,
minValue=-99999999.999999,
maxValue=99999999.999999,
defaultValue=0.0,
optional=True))

Expand All @@ -81,11 +79,13 @@ def initAlgorithm(self, config=None):
'class': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper'}})
self.addParameter(options_param)

self.addParameter(QgsProcessingParameterEnum(self.DATA_TYPE,
self.tr('Output data type'),
self.TYPES,
allowMultiple=False,
defaultValue=5))
dataType_param = QgsProcessingParameterEnum(self.DATA_TYPE,
self.tr('Output data type'),
self.TYPES,
allowMultiple=False,
defaultValue=5)
dataType_param.setFlags(dataType_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(dataType_param)

self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT,
self.tr('Clipped (extent)')))
Expand Down
14 changes: 7 additions & 7 deletions python/plugins/processing/algs/gdal/ClipByMask.py
Expand Up @@ -71,8 +71,6 @@ def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterNumber(self.NODATA,
self.tr('Assign a specified nodata value to output bands'),
type=QgsProcessingParameterNumber.Double,
minValue=-99999999.999999,
maxValue=99999999.999999,
defaultValue=0.0,
optional=True))
self.addParameter(QgsProcessingParameterBoolean(self.ALPHA_BAND,
Expand All @@ -95,11 +93,13 @@ def initAlgorithm(self, config=None):
'class': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper'}})
self.addParameter(options_param)

self.addParameter(QgsProcessingParameterEnum(self.DATA_TYPE,
self.tr('Output data type'),
self.TYPES,
allowMultiple=False,
defaultValue=5))
dataType_param = QgsProcessingParameterEnum(self.DATA_TYPE,
self.tr('Output data type'),
self.TYPES,
allowMultiple=False,
defaultValue=5)
dataType_param.setFlags(dataType_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(dataType_param)

self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT,
self.tr('Clipped (mask)')))
Expand Down
53 changes: 34 additions & 19 deletions python/plugins/processing/algs/gdal/ColorRelief.py
Expand Up @@ -16,7 +16,6 @@
* *
***************************************************************************
"""
from builtins import str

__author__ = 'Alexander Bruy'
__date__ = 'October 2013'
Expand All @@ -26,11 +25,14 @@

__revision__ = '$Format:%H$'

from qgis.core import (QgsProcessingParameterRasterLayer,
from qgis.core import (QgsRasterFileWriter,
QgsProcessingParameterDefinition,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterBand,
QgsProcessingParameterBoolean,
QgsProcessingParameterEnum,
QgsProcessingParameterFile,
QgsProcessingParameterString,
QgsProcessingParameterRasterDestination)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils
Expand All @@ -43,23 +45,40 @@ class ColorRelief(GdalAlgorithm):
COMPUTE_EDGES = 'COMPUTE_EDGES'
COLOR_TABLE = 'COLOR_TABLE'
MATCH_MODE = 'MATCH_MODE'
OPTIONS = 'OPTIONS'
OUTPUT = 'OUTPUT'

MATCHING_MODES = ['"0,0,0,0" RGBA', 'Exact color', 'Nearest color']

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

def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer')))
self.addParameter(QgsProcessingParameterBand(
self.BAND, self.tr('Band number'), parentLayerParameterName=self.INPUT))
self.modes = ((self.tr('Use strict color matching'), '-exact_color_entry'),
(self.tr('Use closest RGBA quadruplet'), '-nearest_color_entry'))

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.tr('Compute edges'),
defaultValue=False))
self.addParameter(QgsProcessingParameterFile(self.COLOR_TABLE,
self.tr('Color configuration file'), optional=False))
self.tr('Color configuration file')))
self.addParameter(QgsProcessingParameterEnum(self.MATCH_MODE,
self.tr('Matching mode'), options=self.MATCHING_MODES, defaultValue=0))
self.tr('Matching mode'),
options=[i[0] for i in self.modes],
defaultValue=0))
options_param = QgsProcessingParameterString(self.OPTIONS,
self.tr('Additional creation parameters'),
defaultValue='',
optional=True)

options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
options_param.setMetadata({
'widget_wrapper': {
'class': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper'}})
self.addParameter(options_param)

self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Color relief')))

Expand All @@ -77,23 +96,19 @@ def getConsoleCommands(self, parameters, context, feedback):
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
arguments.append(inLayer.source())
arguments.append(self.parameterAsFile(parameters, self.COLOR_TABLE, context))
#filePath = unicode(self.getParameterValue(self.COLOR_TABLE))
#if filePath is None or filePath == '':
# filePath = os.path.join(os.path.dirname(__file__), 'terrain.txt')
#arguments.append(filePath)

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

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

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

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

mode = self.parameterAsEnum(parameters, self.MATCH_MODE, context)
if mode == 1:
arguments.append('-exact_color_entry')
elif mode == 2:
arguments.append('-nearest_color_entry')
arguments.append(self.modes[self.parameterAsEnum(parameters, self.MATCH_MODE, context)][1])

return ['gdaldem', GdalUtils.escapeAndJoin(arguments)]
39 changes: 27 additions & 12 deletions python/plugins/processing/algs/gdal/GridAverage.py
Expand Up @@ -36,6 +36,7 @@
QgsProcessingParameterEnum,
QgsProcessingParameterField,
QgsProcessingParameterNumber,
QgsProcessingParameterString,
QgsProcessingParameterRasterDestination)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils
Expand All @@ -52,10 +53,11 @@ class GridAverage(GdalAlgorithm):
MIN_POINTS = 'MIN_POINTS'
ANGLE = 'ANGLE'
NODATA = 'NODATA'
OPTIONS = 'OPTIONS'
DATA_TYPE = 'DATA_TYPE'
OUTPUT = 'OUTPUT'

TYPE = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']
TYPES = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']

def __init__(self):
super().__init__()
Expand All @@ -78,13 +80,11 @@ def initAlgorithm(self, config=None):
self.tr('The first radius of search ellipse'),
type=QgsProcessingParameterNumber.Double,
minValue=0.0,
maxValue=99999999.999999,
defaultValue=0.0))
self.addParameter(QgsProcessingParameterNumber(self.RADIUS_2,
self.tr('The second radius of search ellipse'),
type=QgsProcessingParameterNumber.Double,
minValue=0.0,
maxValue=99999999.999999,
defaultValue=0.0))
self.addParameter(QgsProcessingParameterNumber(self.ANGLE,
self.tr('Angle of search ellipse rotation in degrees (counter clockwise)'),
Expand All @@ -96,19 +96,29 @@ def initAlgorithm(self, config=None):
self.tr('Minimum number of data points to use'),
type=QgsProcessingParameterNumber.Integer,
minValue=0,
maxValue=99999999,
defaultValue=0))
self.addParameter(QgsProcessingParameterNumber(self.NODATA,
self.tr('NODATA marker to fill empty points'),
type=QgsProcessingParameterNumber.Double,
minValue=-99999999.999999,
maxValue=99999999.999999,
defaultValue=0.0))
self.addParameter(QgsProcessingParameterEnum(self.DATA_TYPE,
self.tr('Output data type'),
self.TYPE,
allowMultiple=False,
defaultValue=5))

options_param = QgsProcessingParameterString(self.OPTIONS,
self.tr('Additional creation parameters'),
defaultValue='',
optional=True)
options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
options_param.setMetadata({
'widget_wrapper': {
'class': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper'}})
self.addParameter(options_param)

dataType_param = QgsProcessingParameterEnum(self.DATA_TYPE,
self.tr('Output data type'),
self.TYPES,
allowMultiple=False,
defaultValue=5)
dataType_param.setFlags(dataType_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(dataType_param)

self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT,
self.tr('Interpolated (moving average)')))
Expand Down Expand Up @@ -147,12 +157,17 @@ def getConsoleCommands(self, parameters, context, feedback):
arguments.append('-a')
arguments.append(params)
arguments.append('-ot')
arguments.append(self.TYPE[self.parameterAsEnum(parameters, self.DATA_TYPE, context)])
arguments.append(self.TYPES[self.parameterAsEnum(parameters, self.DATA_TYPE, context)])

out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))

options = self.parameterAsString(parameters, self.OPTIONS, context)
if options:
arguments.append('-co')
arguments.append(options)

arguments.append(connectionString)
arguments.append(out)

Expand Down

0 comments on commit 0f22664

Please sign in to comment.