Skip to content

Commit

Permalink
Merge pull request #3926 from alexbruy/processing-gdal-opts
Browse files Browse the repository at this point in the history
Processing gdal opts
  • Loading branch information
volaya authored Jan 10, 2017
2 parents 378212c + 29d108f commit 3fd27d1
Show file tree
Hide file tree
Showing 16 changed files with 393 additions and 434 deletions.
51 changes: 51 additions & 0 deletions python/gui/qgsrasterformatsaveoptionswidget.sip
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,70 @@ class QgsRasterFormatSaveOptionsWidget : QWidget
const QString& provider = "gdal" );
~QgsRasterFormatSaveOptionsWidget();

/**
* Set output raster format, it is used to determine list
* of available options
*/
void setFormat( const QString& format );

/**
* Set provider key, , it is used to determine list
* of available options
*/
void setProvider( const QString& provider );

/**
* Set output raster layer
*/
void setRasterLayer( QgsRasterLayer* rasterLayer );

/**
* Set output raster file name
*/
void setRasterFileName( const QString& file );

/**
* Returns list of selected options
* @see setOptions()
*/
QStringList options() const;

/**
* Populate widget with user-defined options. String should contain
* key=value pairs separated by spaces, e.g. "TILED=YES TFW=YES"
* @see options()
* @note added in QGIS 3.0
*/
void setOptions( const QString& options );

/**
* Set widget look and feel
*/
void setType( QgsRasterFormatSaveOptionsWidget::Type type = Default );

/**
* Set pyramids format to use
*/
void setPyramidsFormat( QgsRaster::RasterPyramidsFormat format );

public slots:

void apply();

/**
* Opens window with options desctiption for given provider
* and output format
*/
void helpOptions();

/**
* Validates options correctness
*/
QString validateOptions( bool gui = true, bool reportOk = true );

/**
* Reloads profiles list from QGIS settings
*/
void updateProfiles();

private slots:
Expand Down
2 changes: 2 additions & 0 deletions python/plugins/processing/algs/gdal/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
FILE(GLOB PY_FILES *.py)

ADD_SUBDIRECTORY(ui)

PLUGIN_INSTALL(processing ./algs/gdal ${PY_FILES})
95 changes: 22 additions & 73 deletions python/plugins/processing/algs/gdal/ClipByExtent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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')
Expand All @@ -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)
Expand Down
100 changes: 26 additions & 74 deletions python/plugins/processing/algs/gdal/ClipByMask.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

Expand All @@ -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')
Expand Down Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions python/plugins/processing/algs/gdal/GdalUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('"', '\\"') \
+ '"'
Expand Down
Loading

0 comments on commit 3fd27d1

Please sign in to comment.