Skip to content

Commit

Permalink
[needs-docs][processing] Use input layer data type for gdal algorithms
Browse files Browse the repository at this point in the history
when appropriate:
- warp (i.e. reproject)
- translate (i.e. file format conversion)
- rearrange bands
- clip by extent/mask
  • Loading branch information
nirvn committed Sep 4, 2018
1 parent 8bae311 commit 8398aae
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 47 deletions.
9 changes: 5 additions & 4 deletions python/plugins/processing/algs/gdal/ClipRasterByExtent.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ClipRasterByExtent(GdalAlgorithm):
DATA_TYPE = 'DATA_TYPE'
OUTPUT = 'OUTPUT'

TYPES = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']
TYPES = ['Use input layer data type', 'Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']

def __init__(self):
super().__init__()
Expand Down Expand Up @@ -84,7 +84,7 @@ def initAlgorithm(self, config=None):
self.tr('Output data type'),
self.TYPES,
allowMultiple=False,
defaultValue=5)
defaultValue=0)
dataType_param.setFlags(dataType_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(dataType_param)

Expand Down Expand Up @@ -132,8 +132,9 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
if nodata is not None:
arguments.append('-a_nodata {}'.format(nodata))

arguments.append('-ot')
arguments.append(self.TYPES[self.parameterAsEnum(parameters, self.DATA_TYPE, context)])
data_type = self.parameterAsEnum(parameters, self.DATA_TYPE, context)
if data_type:
arguments.append('-ot ' + self.TYPES[data_type])

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
Expand Down
10 changes: 6 additions & 4 deletions python/plugins/processing/algs/gdal/ClipRasterByMask.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class ClipRasterByMask(GdalAlgorithm):
DATA_TYPE = 'DATA_TYPE'
OUTPUT = 'OUTPUT'

TYPES = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']
TYPES = ['Use input layer data type', 'Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']

def __init__(self):
super().__init__()
Expand Down Expand Up @@ -98,7 +98,7 @@ def initAlgorithm(self, config=None):
self.tr('Output data type'),
self.TYPES,
allowMultiple=False,
defaultValue=5)
defaultValue=0)
dataType_param.setFlags(dataType_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(dataType_param)

Expand Down Expand Up @@ -138,8 +138,10 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)

arguments = []
arguments.append('-ot')
arguments.append(self.TYPES[self.parameterAsEnum(parameters, self.DATA_TYPE, context)])

data_type = self.parameterAsEnum(parameters, self.DATA_TYPE, context)
if data_type:
arguments.append('-ot ' + self.TYPES[data_type])

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
Expand Down
9 changes: 5 additions & 4 deletions python/plugins/processing/algs/gdal/rearrange_bands.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class rearrange_bands(GdalAlgorithm):
DATA_TYPE = 'DATA_TYPE'
OUTPUT = 'OUTPUT'

TYPES = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']
TYPES = ['Use input layer data type', 'Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']

def __init__(self):
super().__init__()
Expand All @@ -79,7 +79,7 @@ def initAlgorithm(self, config=None):
self.tr('Output data type'),
self.TYPES,
allowMultiple=False,
defaultValue=5)
defaultValue=0)
dataType_param.setFlags(dataType_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(dataType_param)

Expand Down Expand Up @@ -121,8 +121,9 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
for band in bands:
arguments.append('-b {}'.format(band))

arguments.append('-ot')
arguments.append(self.TYPES[self.parameterAsEnum(parameters, self.DATA_TYPE, context)])
data_type = self.parameterAsEnum(parameters, self.DATA_TYPE, context)
if data_type:
arguments.append('-ot ' + self.TYPES[data_type])

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
Expand Down
9 changes: 5 additions & 4 deletions python/plugins/processing/algs/gdal/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class translate(GdalAlgorithm):
DATA_TYPE = 'DATA_TYPE'
OUTPUT = 'OUTPUT'

TYPES = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']
TYPES = ['Use input layer data type', 'Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']

def __init__(self):
super().__init__()
Expand Down Expand Up @@ -89,7 +89,7 @@ def initAlgorithm(self, config=None):
self.tr('Output data type'),
self.TYPES,
allowMultiple=False,
defaultValue=5)
defaultValue=0)
dataType_param.setFlags(dataType_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(dataType_param)

Expand Down Expand Up @@ -139,8 +139,9 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
if self.parameterAsBool(parameters, self.COPY_SUBDATASETS, context):
arguments.append('-sds')

arguments.append('-ot')
arguments.append(self.TYPES[self.parameterAsEnum(parameters, self.DATA_TYPE, context)])
data_type = self.parameterAsEnum(parameters, self.DATA_TYPE, context)
if data_type:
arguments.append('-ot ' + self.TYPES[data_type])

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))
Expand Down
9 changes: 5 additions & 4 deletions python/plugins/processing/algs/gdal/warp.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class warp(GdalAlgorithm):
MULTITHREADING = 'MULTITHREADING'
OUTPUT = 'OUTPUT'

TYPES = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']
TYPES = ['Use input layer data type', 'Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64', 'CInt16', 'CInt32', 'CFloat32', 'CFloat64']

def __init__(self):
super().__init__()
Expand Down Expand Up @@ -117,7 +117,7 @@ def initAlgorithm(self, config=None):
self.tr('Output data type'),
self.TYPES,
allowMultiple=False,
defaultValue=5)
defaultValue=0)
dataType_param.setFlags(dataType_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(dataType_param)

Expand Down Expand Up @@ -216,8 +216,9 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
if self.parameterAsBool(parameters, self.MULTITHREADING, context):
arguments.append('-multi')

arguments.append('-ot')
arguments.append(self.TYPES[self.parameterAsEnum(parameters, self.DATA_TYPE, context)])
data_type = self.parameterAsEnum(parameters, self.DATA_TYPE, context)
if data_type:
arguments.append('-ot ' + self.TYPES[data_type])

out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
arguments.append('-of')
Expand Down

0 comments on commit 8398aae

Please sign in to comment.