Skip to content
Permalink
Browse files
[processing] Add the missing optional extent param to the gdal clip r…
…aster by mask aglorithm
  • Loading branch information
nirvn committed Dec 30, 2021
1 parent 6e60731 commit 17b9bb8a60ff6c6f8adce6df70144ab40ae27cfa
Showing with 29 additions and 0 deletions.
  1. +17 −0 python/plugins/processing/algs/gdal/ClipRasterByMask.py
  2. +12 −0 python/plugins/processing/tests/GdalAlgorithmsRasterTest.py
@@ -33,6 +33,7 @@
QgsProcessingParameterRasterLayer,
QgsProcessingParameterCrs,
QgsProcessingParameterEnum,
QgsProcessingParameterExtent,
QgsProcessingParameterString,
QgsProcessingParameterNumber,
QgsProcessingParameterBoolean,
@@ -48,6 +49,7 @@ class ClipRasterByMask(GdalAlgorithm):
MASK = 'MASK'
SOURCE_CRS = 'SOURCE_CRS'
TARGET_CRS = 'TARGET_CRS'
EXTENT = 'TARGET_EXTENT'
NODATA = 'NODATA'
ALPHA_BAND = 'ALPHA_BAND'
CROP_TO_CUTLINE = 'CROP_TO_CUTLINE'
@@ -79,6 +81,9 @@ def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterCrs(self.TARGET_CRS,
self.tr('Target CRS'),
optional=True))
self.addParameter(QgsProcessingParameterExtent(self.EXTENT,
self.tr('Target extent'),
optional=True))
self.addParameter(QgsProcessingParameterNumber(self.NODATA,
self.tr('Assign a specified nodata value to output bands'),
type=QgsProcessingParameterNumber.Double,
@@ -169,6 +174,9 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
sourceCrs = self.parameterAsCrs(parameters, self.SOURCE_CRS, context)
targetCrs = self.parameterAsCrs(parameters, self.TARGET_CRS, context)

bbox = self.parameterAsExtent(parameters, self.EXTENT, context)
bboxCrs = self.parameterAsExtentCrs(parameters, self.EXTENT, context)

if self.NODATA in parameters and parameters[self.NODATA] is not None:
nodata = self.parameterAsDouble(parameters, self.NODATA, context)
else:
@@ -187,6 +195,15 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
arguments.append('-t_srs')
arguments.append(GdalUtils.gdal_crs_string(targetCrs))

if not bbox.isNull():
arguments.append('-te')
arguments.append(str(bbox.xMinimum()))
arguments.append(str(bbox.yMinimum()))
arguments.append(str(bbox.xMaximum()))
arguments.append(str(bbox.yMaximum()))
arguments.append('-te_srs')
arguments.append(GdalUtils.gdal_crs_string(bboxCrs))

data_type = self.parameterAsEnum(parameters, self.DATA_TYPE, context)
if data_type:
arguments.append('-ot ' + self.TYPES[data_type])
@@ -30,6 +30,7 @@
QgsProcessingException,
QgsProcessingFeedback,
QgsRectangle,
QgsReferencedRectangle,
QgsRasterLayer,
QgsProject,
QgsProjUtils,
@@ -393,6 +394,7 @@ def testClipRasterByMask(self):
feedback = QgsProcessingFeedback()
source = os.path.join(testDataPath, 'dem.tif')
mask = os.path.join(testDataPath, 'polys.gml')
extent = QgsReferencedRectangle(QgsRectangle(1, 2, 3, 4), QgsCoordinateReferenceSystem('EPSG:4236'))
alg = ClipRasterByMask()
alg.initAlgorithm()

@@ -460,6 +462,16 @@ def testClipRasterByMask(self):
mask + ' -cl polys2 -crop_to_cutline -multi -nosrcalpha -wm 2048 -nomd ' +
source + ' ' +
outdir + '/check.jpg'])
# with target extent value
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'MASK': mask,
'TARGET_EXTENT': extent,
'OUTPUT': outdir + '/check.jpg'}, context, feedback),
['gdalwarp',
'-overwrite -te 1.0 2.0 3.0 4.0 -te_srs EPSG:4236 -of JPEG -cutline ' +
mask + ' -cl polys2 -crop_to_cutline ' + source + ' ' +
outdir + '/check.jpg'])

def testContourPolygon(self):
context = QgsProcessingContext()

0 comments on commit 17b9bb8

Please sign in to comment.