Skip to content
Permalink
Browse files

[processing] use custom options widget in GDAL algorithms

  • Loading branch information
alexbruy committed Jan 7, 2017
1 parent 60cb517 commit f9680ddc0cb940812b347ff497b9d6836359a823
@@ -1,3 +1,5 @@
FILE(GLOB PY_FILES *.py)

ADD_SUBDIRECTORY(ui)

PLUGIN_INSTALL(processing ./algs/gdal ${PY_FILES})
@@ -32,13 +32,11 @@

from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm

from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterExtent
from processing.core.parameters import ParameterString
from processing.core.parameters import (ParameterRaster,
ParameterExtent,
ParameterString,
ParameterSelection)
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

@@ -51,77 +49,41 @@ class ClipByExtent(GdalAlgorithm):
OUTPUT = 'OUTPUT'
NO_DATA = 'NO_DATA'
PROJWIN = 'PROJWIN'
EXTRA = 'EXTRA'
OPTIONS = 'OPTIONS'
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 getIcon(self):
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'raster-clip.png'))

def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Clip raster by extent')
self.group, self.i18n_group = self.trAlgorithm('[GDAL] Extraction')
self.addParameter(ParameterRaster(
self.INPUT, self.tr('Input layer'), False))

self.addParameter(ParameterRaster(self.INPUT, self.tr('Input layer')))
self.addParameter(ParameterString(self.NO_DATA,
self.tr("Nodata value, leave blank to take the nodata value from input"),
'', optional=True))
'',
optional=True))
self.addParameter(ParameterExtent(self.PROJWIN, self.tr('Clipping extent')))

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.addParameter(ParameterString(self.OPTIONS,
self.tr('Additional creation options'),
optional=True,
metadata={'widget_wrapper': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper'}))
self.addParameter(ParameterSelection(self.RTYPE,
self.tr('Output raster type'),
self.TYPE, 5))

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

def getConsoleCommands(self):
out = self.getOutputValue(self.OUTPUT)
noData = self.getParameterValue(self.NO_DATA)
opts = self.getParameterValue(self.OPTIONS)
projwin = self.getParameterValue(self.PROJWIN)

if noData is not None:
noData = str(noData)
projwin = str(self.getParameterValue(self.PROJWIN))
extra = self.getParameterValue(self.EXTRA)
if extra is not None:
extra = str(extra)
jpegcompression = str(self.getParameterValue(self.JPEGCOMPRESSION))
predictor = str(self.getParameterValue(self.PREDICTOR))
zlevel = str(self.getParameterValue(self.ZLEVEL))
tiled = str(self.getParameterValue(self.TILED))
compress = self.COMPRESSTYPE[self.getParameterValue(self.COMPRESS)]
bigtiff = self.BIGTIFFTYPE[self.getParameterValue(self.BIGTIFF)]
tfw = str(self.getParameterValue(self.TFW))

arguments = []
arguments.append('-of')
@@ -139,22 +101,9 @@ def getConsoleCommands(self):
arguments.append(regionCoords[1])
arguments.append(regionCoords[2])

if extra and 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)
if opts:
arguments.append('-co')
arguments.append(opts)

arguments.append(self.getParameterValue(self.INPUT))
arguments.append(out)
@@ -32,12 +32,11 @@

from osgeo import gdal

from processing.core.parameters import ParameterRaster
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.parameters import (ParameterRaster,
ParameterVector,
ParameterBoolean,
ParameterString,
ParameterSelection)

from processing.core.outputs import OutputRaster

@@ -59,64 +58,39 @@ class ClipByMask(GdalAlgorithm):
ALPHA_BAND = 'ALPHA_BAND'
CROP_TO_CUTLINE = 'CROP_TO_CUTLINE'
KEEP_RESOLUTION = 'KEEP_RESOLUTION'
EXTRA = 'EXTRA'
OPTIONS = 'OPTIONS'
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 getIcon(self):
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'raster-clip.png'))

def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Clip raster by mask layer')
self.group, self.i18n_group = self.trAlgorithm('[GDAL] Extraction')

self.addParameter(ParameterRaster(self.INPUT, self.tr('Input layer'), False))
self.addParameter(ParameterVector(self.MASK, self.tr('Mask layer'),
[dataobjects.TYPE_VECTOR_POLYGON]))
self.addParameter(ParameterString(self.NO_DATA,
self.tr("Nodata value, leave blank to take the nodata value from input"),
'', optional=True))
self.addParameter(ParameterBoolean(self.ALPHA_BAND,
self.tr('Create and output alpha band'), False))
self.tr('Create and output alpha band'),
False))
self.addParameter(ParameterBoolean(self.CROP_TO_CUTLINE,
self.tr('Crop the extent of the target dataset to the extent of the cutline'), True))
self.tr('Crop the extent of the target dataset to the extent of the cutline'),
True))
self.addParameter(ParameterBoolean(self.KEEP_RESOLUTION,
self.tr('Keep resolution of output raster'), False))

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.tr('Keep resolution of output raster'),
False))
self.addParameter(ParameterString(self.OPTIONS,
self.tr('Additional creation options'),
optional=True,
metadata={'widget_wrapper': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper'}))
self.addParameter(ParameterSelection(self.RTYPE,
self.tr('Output raster type'),
self.TYPE, 5))

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

@@ -127,21 +101,14 @@ def getConsoleCommands(self):
self.getParameterValue(self.MASK))
ogrMask = ogrConnectionString(mask)[1:-1]
noData = self.getParameterValue(self.NO_DATA)
opts = self.getParameterValue(self.OPTIONS)

if noData is not None:
noData = str(noData)

addAlphaBand = self.getParameterValue(self.ALPHA_BAND)
cropToCutline = self.getParameterValue(self.CROP_TO_CUTLINE)
keepResolution = self.getParameterValue(self.KEEP_RESOLUTION)
extra = self.getParameterValue(self.EXTRA)
if extra is not None:
extra = str(extra)
jpegcompression = str(self.getParameterValue(self.JPEGCOMPRESSION))
predictor = str(self.getParameterValue(self.PREDICTOR))
zlevel = str(self.getParameterValue(self.ZLEVEL))
tiled = str(self.getParameterValue(self.TILED))
compress = self.COMPRESSTYPE[self.getParameterValue(self.COMPRESS)]
bigtiff = self.BIGTIFFTYPE[self.getParameterValue(self.BIGTIFF)]
tfw = str(self.getParameterValue(self.TFW))

arguments = []
arguments.append('-ot')
@@ -174,24 +141,9 @@ def getConsoleCommands(self):
if addAlphaBand:
arguments.append('-dstalpha')

if extra and 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("-wo OPTIMIZE_SIZE=TRUE")
if opts:
arguments.append('-co')
arguments.append(opts)

if GdalUtils.version() in [2010000, 2010100]:
arguments.append("--config GDALWARP_IGNORE_BAD_CUTLINE YES")
@@ -175,6 +175,7 @@ def getFormatShortNameFromFilename(filename):
def escapeAndJoin(strList):
joined = ''
for s in strList:
print(s)
if s[0] != '-' and ' ' in s:
escaped = '"' + s.replace('\\', '\\\\').replace('"', '\\"') \
+ '"'
@@ -30,12 +30,14 @@
from qgis.PyQt.QtGui import QIcon

from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.core.parameters import (ParameterBoolean,
ParameterString,
ParameterSelection,
ParameterMultipleInput)
from processing.core.outputs import OutputRaster
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterMultipleInput
from processing.core.parameters import ParameterSelection
from processing.tools.system import isWindows
from processing.tools import dataobjects

from processing.algs.gdal.GdalUtils import GdalUtils

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
@@ -44,10 +46,11 @@
class merge(GdalAlgorithm):

INPUT = 'INPUT'
OUTPUT = 'OUTPUT'
OPTIONS = 'OPTIONS'
PCT = 'PCT'
SEPARATE = 'SEPARATE'
RTYPE = 'RTYPE'
OUTPUT = 'OUTPUT'

TYPE = ['Byte', 'Int16', 'UInt16', 'UInt32', 'Int32', 'Float32', 'Float64']

@@ -57,31 +60,44 @@ def getIcon(self):
def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Merge')
self.group, self.i18n_group = self.trAlgorithm('[GDAL] Miscellaneous')
self.addParameter(ParameterMultipleInput(merge.INPUT,
self.tr('Input layers'), dataobjects.TYPE_RASTER))
self.addParameter(ParameterBoolean(merge.PCT,
self.tr('Grab pseudocolor table from first layer'), False))
self.addParameter(ParameterBoolean(merge.SEPARATE,
self.tr('Place each input file into a separate band'), False))
self.addParameter(ParameterMultipleInput(self.INPUT,
self.tr('Input layers'),
dataobjects.TYPE_RASTER))
self.addParameter(ParameterString(self.OPTIONS,
self.tr('Additional creation options'),
optional=True,
metadata={'widget_wrapper': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper'}))
self.addParameter(ParameterBoolean(self.PCT,
self.tr('Grab pseudocolor table from first layer'),
False))
self.addParameter(ParameterBoolean(self.SEPARATE,
self.tr('Place each input file into a separate band'),
False))
self.addParameter(ParameterSelection(self.RTYPE,
self.tr('Output raster type'), self.TYPE, 5))
self.tr('Output raster type'),
self.TYPE, 5))

self.addOutput(OutputRaster(merge.OUTPUT, self.tr('Merged')))
self.addOutput(OutputRaster(self.OUTPUT, self.tr('Merged')))

def getConsoleCommands(self):
arguments = []
arguments.append('-ot')
arguments.append(self.TYPE[self.getParameterValue(self.RTYPE)])
if self.getParameterValue(merge.SEPARATE):
if self.getParameterValue(self.SEPARATE):
arguments.append('-separate')
if self.getParameterValue(merge.PCT):
if self.getParameterValue(self.PCT):
arguments.append('-pct')
opts = self.getParameterValue(self.OPTIONS)
if opts:
arguments.append('-co')
arguments.append(opts)

arguments.append('-o')
out = self.getOutputValue(merge.OUTPUT)
out = self.getOutputValue(self.OUTPUT)
arguments.append(out)
arguments.append('-of')
arguments.append(GdalUtils.getFormatShortNameFromFilename(out))
arguments.extend(self.getParameterValue(merge.INPUT).split(';'))
arguments.extend(self.getParameterValue(self.INPUT).split(';'))

commands = []
if isWindows():

0 comments on commit f9680dd

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