Skip to content
Permalink
Browse files
[processing] Add an optional output extent to the gdal raster calcula…
…tor algorithm
  • Loading branch information
nirvn committed Jan 2, 2022
1 parent d339d16 commit 406520f754273bd478427bed63abef64cf1448df
Showing with 36 additions and 0 deletions.
  1. +25 −0 python/plugins/processing/algs/gdal/gdalcalc.py
  2. +11 −0 python/plugins/processing/tests/GdalAlgorithmsRasterTest.py
@@ -27,6 +27,7 @@
QgsProcessingParameterBand,
QgsProcessingParameterNumber,
QgsProcessingParameterEnum,
QgsProcessingParameterExtent,
QgsProcessingParameterString,
QgsProcessingParameterRasterDestination)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
@@ -49,6 +50,7 @@ class gdalcalc(GdalAlgorithm):
BAND_E = 'BAND_E'
BAND_F = 'BAND_F'
FORMULA = 'FORMULA'
EXTENT = 'PROJWIN'
OUTPUT = 'OUTPUT'
NO_DATA = 'NO_DATA'
OPTIONS = 'OPTIONS'
@@ -138,6 +140,14 @@ def initAlgorithm(self, config=None):
type=QgsProcessingParameterNumber.Double,
defaultValue=None,
optional=True))

if GdalUtils.version() >= 3030000:
extent_param = QgsProcessingParameterExtent(self.EXTENT,
self.tr('Output extent'),
optional=True)
extent_param.setHelp(self.tr('Custom extent of the output raster'))
self.addParameter(extent_param)

self.addParameter(
QgsProcessingParameterEnum(
self.RTYPE,
@@ -182,7 +192,13 @@ def groupId(self):
def commandName(self):
return 'gdal_calc'

def processAlgorithm(self, parameters, context, feedback):
if GdalUtils.version() < 3030000 and self.EXTENT in parameters.keys():
raise QgsProcessingException(self.tr('The output extent option is only available on GDAL 3.3 or later'))
return GdalAlgorithm.processAlgorithm(self, parameters, context, feedback)

def getConsoleCommands(self, parameters, context, feedback, executing=True):

out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, out)
formula = self.parameterAsString(parameters, self.FORMULA, context)
@@ -206,6 +222,15 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
layer = self.parameterAsRasterLayer(parameters, self.INPUT_A, context)
if layer is None:
raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT_A))

bbox = self.parameterAsExtent(parameters, self.EXTENT, context, layer.crs())
if not bbox.isNull():
arguments.append('--projwin')
arguments.append(str(bbox.xMinimum()))
arguments.append(str(bbox.yMaximum()))
arguments.append(str(bbox.xMaximum()))
arguments.append(str(bbox.yMinimum()))

arguments.append('-A')
arguments.append(layer.source())
if self.parameterAsString(parameters, self.BAND_A, context):
@@ -674,6 +674,17 @@ def testGdalCalc(self):
['gdal_calc.py',
'--overwrite --calc "{}" --format JPEG --type Float32 -A {} --A_band 1 --outfile {}'.format(formula, source, output)])

if GdalUtils.version() >= 3030000:
extent = QgsReferencedRectangle(QgsRectangle(1, 2, 3, 4), QgsCoordinateReferenceSystem('EPSG:4326'))
self.assertEqual(
alg.getConsoleCommands({'INPUT_A': source,
'BAND_A': 1,
'FORMULA': formula,
'PROJWIN': extent,
'OUTPUT': output}, context, feedback),
['gdal_calc.py',
'--overwrite --calc "{}" --format JPEG --type Float32 --projwin 1.0 4.0 3.0 2.0 -A {} --A_band 1 --outfile {}'.format(formula, source, output)])

# check that formula is not escaped and formula is returned as it is
formula = 'A * 2' # <--- add spaces in the formula
self.assertEqual(

0 comments on commit 406520f

Please sign in to comment.