Skip to content

Commit

Permalink
Merge pull request #38841 from rldhont/processing-gdal-clipbyextent
Browse files Browse the repository at this point in the history
[Processing][GDAL] Clip Raster by extent - override CRS
  • Loading branch information
alexbruy committed Oct 28, 2020
2 parents ad01045 + 90e9eda commit 41dc20c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
10 changes: 10 additions & 0 deletions python/plugins/processing/algs/gdal/ClipRasterByExtent.py
Expand Up @@ -33,6 +33,7 @@
QgsProcessingParameterExtent,
QgsProcessingParameterString,
QgsProcessingParameterNumber,
QgsProcessingParameterBoolean,
QgsProcessingParameterRasterDestination)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils
Expand All @@ -43,6 +44,7 @@
class ClipRasterByExtent(GdalAlgorithm):
INPUT = 'INPUT'
EXTENT = 'PROJWIN'
OVERCRS = 'OVERCRS'
NODATA = 'NODATA'
OPTIONS = 'OPTIONS'
DATA_TYPE = 'DATA_TYPE'
Expand All @@ -60,6 +62,9 @@ def initAlgorithm(self, config=None):
self.tr('Input layer')))
self.addParameter(QgsProcessingParameterExtent(self.EXTENT,
self.tr('Clipping extent')))
self.addParameter(QgsProcessingParameterBoolean(self.OVERCRS,
self.tr('Override the projection for the output file'),
defaultValue=False))
self.addParameter(QgsProcessingParameterNumber(self.NODATA,
self.tr('Assign a specified nodata value to output bands'),
type=QgsProcessingParameterNumber.Double,
Expand Down Expand Up @@ -118,6 +123,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
raise QgsProcessingException('Invalid input layer {}'.format(parameters[self.INPUT] if self.INPUT in parameters else 'INPUT'))

bbox = self.parameterAsExtent(parameters, self.EXTENT, context, inLayer.crs())
override_crs = self.parameterAsBoolean(parameters, self.OVERCRS, context)
if self.NODATA in parameters and parameters[self.NODATA] is not None:
nodata = self.parameterAsDouble(parameters, self.NODATA, context)
else:
Expand All @@ -133,6 +139,10 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
arguments.append(str(bbox.xMaximum()))
arguments.append(str(bbox.yMinimum()))

crs = inLayer.crs()
if override_crs and crs.isValid():
arguments.append('-a_srs {}'.format(GdalUtils.gdal_crs_string(crs)))

if nodata is not None:
arguments.append('-a_nodata {}'.format(nodata))

Expand Down
Expand Up @@ -24,6 +24,7 @@ tests:
INPUT:
name: dem.tif
type: raster
OVERCRS: false
NODATA: 0.0
OPTIONS: ''
PROJWIN: 18.673038221977773,18.699957975064194,45.782253906735804,45.80350236352593
Expand All @@ -39,6 +40,7 @@ tests:
INPUT:
name: dem.tif
type: raster
OVERCRS: false
NODATA: 0.0
OPTIONS: ''
PROJWIN: 18.674136950224096,18.70011285383855,45.78239420868433,45.80342583519035
Expand Down

0 comments on commit 41dc20c

Please sign in to comment.