Skip to content
Permalink
Browse files

Processing - GDAL - Raster clip by extent - override CRS

  • Loading branch information
rldhont committed Oct 27, 2020
1 parent cf37cb9 commit 90e9eda8183fb426750aa8f9aab3bc730fda8bcd
@@ -33,6 +33,7 @@
QgsProcessingParameterExtent,
QgsProcessingParameterString,
QgsProcessingParameterNumber,
QgsProcessingParameterBoolean,
QgsProcessingParameterRasterDestination)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils
@@ -43,6 +44,7 @@
class ClipRasterByExtent(GdalAlgorithm):
INPUT = 'INPUT'
EXTENT = 'PROJWIN'
OVERCRS = 'OVERCRS'
NODATA = 'NODATA'
OPTIONS = 'OPTIONS'
DATA_TYPE = 'DATA_TYPE'
@@ -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,
@@ -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:
@@ -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))

@@ -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
@@ -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

0 comments on commit 90e9eda

Please sign in to comment.
You can’t perform that action at this time.