Skip to content
Permalink
Browse files

Merge pull request #9446 from NaturalGIS/gdal_fix_improve_gdal_clip_r…

…aster_by_mask_36

Improves the GDAL clip by mask layer tool
  • Loading branch information
m-kuhn committed Mar 10, 2019
2 parents cf1f0d8 + 534c069 commit 56ec3cfc35d7dd50d9e95cd52dd730a4e8a05f5f
Showing with 51 additions and 32 deletions.
  1. +51 −32 python/plugins/processing/algs/gdal/ClipRasterByMask.py
@@ -34,12 +34,11 @@
QgsProcessingException,
QgsProcessingParameterDefinition,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterCrs,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterCrs,
QgsProcessingParameterEnum,
QgsProcessingParameterString,
QgsProcessingParameterNumber,
QgsProcessingParameterExtent,
QgsProcessingParameterBoolean,
QgsProcessingParameterRasterDestination)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
@@ -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'

@@ -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,
@@ -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='',
@@ -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:
@@ -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])
@@ -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)

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

0 comments on commit 56ec3cf

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