Skip to content

Commit

Permalink
Merge pull request #9444 from NaturalGIS/gdal_fix_improve_gdal_clip_r…
Browse files Browse the repository at this point in the history
…aster_by_mask

Fixes/improves the GDAL clip by mask layer tool
  • Loading branch information
m-kuhn authored Mar 10, 2019
2 parents bdf0805 + 78cc5b2 commit a91ecd5
Showing 1 changed file with 51 additions and 32 deletions.
83 changes: 51 additions & 32 deletions python/plugins/processing/algs/gdal/ClipRasterByMask.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@
QgsProcessingException,
QgsProcessingParameterDefinition,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterCrs,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterCrs,
QgsProcessingParameterEnum,
QgsProcessingParameterString,
QgsProcessingParameterNumber,
QgsProcessingParameterExtent,
QgsProcessingParameterBoolean,
QgsProcessingParameterRasterDestination)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
Expand All @@ -52,14 +51,17 @@ class ClipRasterByMask(GdalAlgorithm):

INPUT = 'INPUT'
MASK = 'MASK'
SOURCE_CRS = 'SOURCE_CRS'
TARGET_CRS = 'TARGET_CRS'
NODATA = 'NODATA'
ALPHA_BAND = 'ALPHA_BAND'
CROP_TO_CUTLINE = 'CROP_TO_CUTLINE'
KEEP_RESOLUTION = 'KEEP_RESOLUTION'
SET_RESOLUTION = 'SET_RESOLUTION'
X_RESOLUTION = 'X_RESOLUTION'
Y_RESOLUTION = 'Y_RESOLUTION'
OPTIONS = 'OPTIONS'
DATA_TYPE = 'DATA_TYPE'
TARGET_EXTENT = 'TARGET_EXTENT'
TARGET_EXTENT_CRS = 'TARGET_EXTENT_CRS'
MULTITHREADING = 'MULTITHREADING'
OUTPUT = 'OUTPUT'

Expand All @@ -74,6 +76,12 @@ def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterFeatureSource(self.MASK,
self.tr('Mask layer'),
[QgsProcessing.TypeVectorPolygon]))
self.addParameter(QgsProcessingParameterCrs(self.SOURCE_CRS,
self.tr('Source CRS'),
optional=True))
self.addParameter(QgsProcessingParameterCrs(self.TARGET_CRS,
self.tr('Target CRS'),
optional=True))
self.addParameter(QgsProcessingParameterNumber(self.NODATA,
self.tr('Assign a specified nodata value to output bands'),
type=QgsProcessingParameterNumber.Double,
Expand All @@ -86,27 +94,26 @@ def initAlgorithm(self, config=None):
self.tr('Match the extent of the clipped raster to the extent of the mask layer'),
defaultValue=True))
self.addParameter(QgsProcessingParameterBoolean(self.KEEP_RESOLUTION,
self.tr('Keep resolution of output raster'),
self.tr('Keep resolution of input raster'),
defaultValue=False))

target_extent_param = QgsProcessingParameterExtent(self.TARGET_EXTENT,
self.tr('Georeferenced extents of output file to be created'),
optional=True)
target_extent_param.setFlags(target_extent_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(target_extent_param)

target_extent_crs_param = QgsProcessingParameterCrs(self.TARGET_EXTENT_CRS,
self.tr('CRS of the target raster extent'),
optional=True)
target_extent_crs_param.setFlags(target_extent_crs_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(target_extent_crs_param)

self.addParameter(QgsProcessingParameterBoolean(self.SET_RESOLUTION,
self.tr('Set output file resolution'),
defaultValue=False))
self.addParameter(QgsProcessingParameterNumber(self.X_RESOLUTION,
self.tr('X Resolution to output bands'),
type=QgsProcessingParameterNumber.Double,
defaultValue=None,
optional=True))
self.addParameter(QgsProcessingParameterNumber(self.Y_RESOLUTION,
self.tr('Y Resolution to output bands'),
type=QgsProcessingParameterNumber.Double,
defaultValue=None,
optional=True))
multithreading_param = QgsProcessingParameterBoolean(self.MULTITHREADING,
self.tr('Use multithreaded warping implementation'),
defaultValue=False)
multithreading_param.setFlags(multithreading_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(multithreading_param)

options_param = QgsProcessingParameterString(self.OPTIONS,
self.tr('Additional creation options'),
defaultValue='',
Expand Down Expand Up @@ -153,6 +160,9 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

maskLayer, maskLayerName = self.getOgrCompatibleSource(self.MASK, parameters, context, feedback, executing)

sourceCrs = self.parameterAsCrs(parameters, self.SOURCE_CRS, context)
targetCrs = self.parameterAsCrs(parameters, self.TARGET_CRS, context)

if self.NODATA in parameters and parameters[self.NODATA] is not None:
nodata = self.parameterAsDouble(parameters, self.NODATA, context)
else:
Expand All @@ -163,6 +173,14 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

arguments = []

if sourceCrs.isValid():
arguments.append('-s_srs')
arguments.append(GdalUtils.gdal_crs_string(sourceCrs))

if targetCrs.isValid():
arguments.append('-t_srs')
arguments.append(GdalUtils.gdal_crs_string(targetCrs))

data_type = self.parameterAsEnum(parameters, self.DATA_TYPE, context)
if data_type:
arguments.append('-ot ' + self.TYPES[data_type])
Expand All @@ -176,6 +194,20 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
arguments.append(str(-inLayer.rasterUnitsPerPixelY()))
arguments.append('-tap')

if self.parameterAsBool(parameters, self.SET_RESOLUTION, context):
arguments.append('-tr')
if self.X_RESOLUTION in parameters and parameters[self.X_RESOLUTION] is not None:
xres = self.parameterAsDouble(parameters, self.X_RESOLUTION, context)
arguments.append('{}'.format(xres))
else:
arguments.append(str(inLayer.rasterUnitsPerPixelX()))
if self.Y_RESOLUTION in parameters and parameters[self.Y_RESOLUTION] is not None:
yres = self.parameterAsDouble(parameters, self.Y_RESOLUTION, context)
arguments.append('{}'.format(yres))
else:
arguments.append(str(-inLayer.rasterUnitsPerPixelY()))
arguments.append('-tap')

arguments.append('-cutline')
arguments.append(maskLayer)

Expand All @@ -188,19 +220,6 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
if nodata is not None:
arguments.append('-dstnodata {}'.format(nodata))

extent = self.parameterAsExtent(parameters, self.TARGET_EXTENT, context)
if not extent.isNull():
arguments.append('-te')
arguments.append(extent.xMinimum())
arguments.append(extent.yMinimum())
arguments.append(extent.xMaximum())
arguments.append(extent.yMaximum())

extentCrs = self.parameterAsCrs(parameters, self.TARGET_EXTENT_CRS, context)
if extentCrs:
arguments.append('-te_srs')
arguments.append(extentCrs.authid())

if self.parameterAsBool(parameters, self.MULTITHREADING, context):
arguments.append('-multi')

Expand Down

0 comments on commit a91ecd5

Please sign in to comment.