Skip to content

Commit

Permalink
Merge pull request #2390 from NaturalGIS/processing_gdal_clip_raster
Browse files Browse the repository at this point in the history
Processing advanced parameters for gdal clip raster
  • Loading branch information
volaya committed Oct 24, 2015
2 parents 07bd3ff + ebd51b2 commit fd26acd
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 6 deletions.
67 changes: 65 additions & 2 deletions python/plugins/processing/algs/gdal/ClipByExtent.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
from processing.core.parameters import ParameterExtent
from processing.core.parameters import ParameterString
from processing.core.outputs import OutputRaster
from processing.core.parameters import ParameterSelection
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterBoolean

from processing.algs.gdal.GdalUtils import GdalUtils

Expand All @@ -42,7 +45,18 @@ class ClipByExtent(GdalAlgorithm):
NO_DATA = 'NO_DATA'
PROJWIN = 'PROJWIN'
EXTRA = 'EXTRA'

RTYPE = 'RTYPE'
TYPE = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64']
TILED = 'TILED'
COMPRESS = 'COMPRESS'
JPEGCOMPRESSION = 'JPEGCOMPRESSION'
PREDICTOR = 'PREDICTOR'
ZLEVEL = 'ZLEVEL'
BIGTIFF = 'BIGTIFF'
BIGTIFFTYPE = ['', 'YES', 'NO', 'IF_NEEDED', 'IF_SAFER']
COMPRESSTYPE = ['NONE', 'JPEG', 'LZW', 'PACKBITS', 'DEFLATE']
TFW = 'TFW'

def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Clip raster by extent')
self.group, self.i18n_group = self.trAlgorithm('[GDAL] Extraction')
Expand All @@ -52,19 +66,54 @@ def defineCharacteristics(self):
self.tr("Nodata value, leave blank to take the nodata value from input"),
''))
self.addParameter(ParameterExtent(self.PROJWIN, self.tr('Clipping extent')))
self.addParameter(ParameterString(self.EXTRA,

params = []
params.append(ParameterSelection(self.RTYPE,
self.tr('Output raster type'), self.TYPE, 5))
params.append(ParameterSelection(self.COMPRESS,
self.tr('GeoTIFF options. Compression type:'), self.COMPRESSTYPE, 4))
params.append(ParameterNumber(self.JPEGCOMPRESSION,
self.tr('Set the JPEG compression level'),
1, 100, 75))
params.append(ParameterNumber(self.ZLEVEL,
self.tr('Set the DEFLATE compression level'),
1, 9, 6))
params.append(ParameterNumber(self.PREDICTOR,
self.tr('Set the predictor for LZW or DEFLATE compression'),
1, 3, 1))
params.append(ParameterBoolean(self.TILED,
self.tr('Create tiled output (only used for the GTiff format)'), False))
params.append(ParameterSelection(self.BIGTIFF,
self.tr('Control whether the created file is a BigTIFF or a classic TIFF'), self.BIGTIFFTYPE, 0))
params.append(ParameterBoolean(self.TFW,
self.tr('Force the generation of an associated ESRI world file (.tfw))'), False))
params.append(ParameterString(self.EXTRA,
self.tr('Additional creation parameters'), '', optional=True))

for param in params:
param.isAdvanced = True
self.addParameter(param)

self.addOutput(OutputRaster(self.OUTPUT, self.tr('Clipped (extent)')))

def getConsoleCommands(self):
out = self.getOutputValue(self.OUTPUT)
noData = unicode(self.getParameterValue(self.NO_DATA))
projwin = unicode(self.getParameterValue(self.PROJWIN))
extra = unicode(self.getParameterValue(self.EXTRA))
jpegcompression = unicode(self.getParameterValue(self.JPEGCOMPRESSION))
predictor = unicode(self.getParameterValue(self.PREDICTOR))
zlevel = unicode(self.getParameterValue(self.ZLEVEL))
tiled = unicode(self.getParameterValue(self.TILED))
compress = self.COMPRESSTYPE[self.getParameterValue(self.COMPRESS)]
bigtiff = self.BIGTIFFTYPE[self.getParameterValue(self.BIGTIFF)]
tfw = unicode(self.getParameterValue(self.TFW))

arguments = []
arguments.append('-of')
arguments.append(GdalUtils.getFormatShortNameFromFilename(out))
arguments.append('-ot')
arguments.append(self.TYPE[self.getParameterValue(self.RTYPE)])
if len(noData) > 0:
arguments.append('-a_nodata')
arguments.append(noData)
Expand All @@ -78,6 +127,20 @@ def getConsoleCommands(self):

if len(extra) > 0:
arguments.append(extra)
if GdalUtils.getFormatShortNameFromFilename(out) == "GTiff":
arguments.append("-co COMPRESS=" + compress)
if compress == 'JPEG':
arguments.append("-co JPEG_QUALITY=" + jpegcompression)
elif (compress == 'LZW') or (compress == 'DEFLATE'):
arguments.append("-co PREDICTOR=" + predictor)
if compress == 'DEFLATE':
arguments.append("-co ZLEVEL=" + zlevel)
if tiled == "True":
arguments.append("-co TILED=YES")
if tfw == "True":
arguments.append("-co TFW=YES")
if len(bigtiff) > 0:
arguments.append("-co BIGTIFF=" + bigtiff)

arguments.append(self.getParameterValue(self.INPUT))
arguments.append(out)
Expand Down
72 changes: 68 additions & 4 deletions python/plugins/processing/algs/gdal/ClipByMask.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterSelection
from processing.core.parameters import ParameterNumber

from processing.core.outputs import OutputRaster

from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils


class ClipByMask(GdalAlgorithm):
class ClipByMask(GdalAlgorithm,OgrAlgorithm):

INPUT = 'INPUT'
OUTPUT = 'OUTPUT'
Expand All @@ -48,7 +51,18 @@ class ClipByMask(GdalAlgorithm):
ALPHA_BAND = 'ALPHA_BAND'
KEEP_RESOLUTION = 'KEEP_RESOLUTION'
EXTRA = 'EXTRA'

RTYPE = 'RTYPE'
TYPE = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64']
TILED = 'TILED'
COMPRESS = 'COMPRESS'
JPEGCOMPRESSION = 'JPEGCOMPRESSION'
PREDICTOR = 'PREDICTOR'
ZLEVEL = 'ZLEVEL'
BIGTIFF = 'BIGTIFF'
BIGTIFFTYPE = ['', 'YES', 'NO', 'IF_NEEDED', 'IF_SAFER']
COMPRESSTYPE = ['NONE', 'JPEG', 'LZW', 'PACKBITS', 'DEFLATE']
TFW = 'TFW'

def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Clip raster by mask layer')
self.group, self.i18n_group = self.trAlgorithm('[GDAL] Extraction')
Expand All @@ -62,19 +76,55 @@ def defineCharacteristics(self):
self.tr('Create and output alpha band'), False))
self.addParameter(ParameterBoolean(self.KEEP_RESOLUTION,
self.tr('Keep resolution of output raster'), False))
self.addParameter(ParameterString(self.EXTRA,

params = []
params.append(ParameterSelection(self.RTYPE,
self.tr('Output raster type'), self.TYPE, 5))
params.append(ParameterSelection(self.COMPRESS,
self.tr('GeoTIFF options. Compression type:'), self.COMPRESSTYPE, 4))
params.append(ParameterNumber(self.JPEGCOMPRESSION,
self.tr('Set the JPEG compression level'),
1, 100, 75))
params.append(ParameterNumber(self.ZLEVEL,
self.tr('Set the DEFLATE compression level'),
1, 9, 6))
params.append(ParameterNumber(self.PREDICTOR,
self.tr('Set the predictor for LZW or DEFLATE compression'),
1, 3, 1))
params.append(ParameterBoolean(self.TILED,
self.tr('Create tiled output (only used for the GTiff format)'), False))
params.append(ParameterSelection(self.BIGTIFF,
self.tr('Control whether the created file is a BigTIFF or a classic TIFF'), self.BIGTIFFTYPE, 0))
params.append(ParameterBoolean(self.TFW,
self.tr('Force the generation of an associated ESRI world file (.tfw))'), False))
params.append(ParameterString(self.EXTRA,
self.tr('Additional creation parameters'), '', optional=True))

for param in params:
param.isAdvanced = True
self.addParameter(param)

self.addOutput(OutputRaster(self.OUTPUT, self.tr('Clipped (mask)')))

def getConsoleCommands(self):
out = self.getOutputValue(self.OUTPUT)
mask = self.getParameterValue(self.MASK)
ogrMask = self.ogrConnectionString(mask)[1:-1]
noData = unicode(self.getParameterValue(self.NO_DATA))
addAlphaBand = self.getParameterValue(self.ALPHA_BAND)
keepResolution = self.getParameterValue(self.KEEP_RESOLUTION)
extra = unicode(self.getParameterValue(self.EXTRA))
jpegcompression = unicode(self.getParameterValue(self.JPEGCOMPRESSION))
predictor = unicode(self.getParameterValue(self.PREDICTOR))
zlevel = unicode(self.getParameterValue(self.ZLEVEL))
tiled = unicode(self.getParameterValue(self.TILED))
compress = self.COMPRESSTYPE[self.getParameterValue(self.COMPRESS)]
bigtiff = self.BIGTIFFTYPE[self.getParameterValue(self.BIGTIFF)]
tfw = unicode(self.getParameterValue(self.TFW))

arguments = []
arguments.append('-ot')
arguments.append(self.TYPE[self.getParameterValue(self.RTYPE)])
arguments.append('-q')
arguments.append('-of')
arguments.append(GdalUtils.getFormatShortNameFromFilename(out))
Expand All @@ -92,14 +142,28 @@ def getConsoleCommands(self):
arguments.append('-tap')

arguments.append('-cutline')
arguments.append(mask)
arguments.append(ogrMask)
arguments.append('-crop_to_cutline')

if addAlphaBand:
arguments.append('-dstalpha')

if len(extra) > 0:
arguments.append(extra)
if GdalUtils.getFormatShortNameFromFilename(out) == "GTiff":
arguments.append("-co COMPRESS=" + compress)
if compress == 'JPEG':
arguments.append("-co JPEG_QUALITY=" + jpegcompression)
elif (compress == 'LZW') or (compress == 'DEFLATE'):
arguments.append("-co PREDICTOR=" + predictor)
if compress == 'DEFLATE':
arguments.append("-co ZLEVEL=" + zlevel)
if tiled == "True":
arguments.append("-co TILED=YES")
if tfw == "True":
arguments.append("-co TFW=YES")
if len(bigtiff) > 0:
arguments.append("-co BIGTIFF=" + bigtiff)

arguments.append(self.getParameterValue(self.INPUT))
arguments.append(out)
Expand Down

0 comments on commit fd26acd

Please sign in to comment.