Skip to content
Browse files

Support multiple output file raster formats:

* A new createopt textbox has been added to the parameters dialog for algorithms which exports to raster files.
* A new metaopt textbox has also been added to the Algorithm parameters dialog.
* Raster file format is detected from output filename extension.
* GdalUtils has been improved to correctly detect raster formats supported for creation.
* QFileDialog for output rasters now display only file filters for supported output raster file formats.
  • Loading branch information
Médéric RIBREUX committed Nov 5, 2017
1 parent aa17df1 commit d10aaf4bd4ff47660592413e2c6a65b6b923b29b
@@ -61,6 +61,7 @@ class GdalUtils(object):

supportedRasters = None
supportedOutputRasters = None

def runGdal(commands, feedback=None):
@@ -135,7 +136,10 @@ def getSupportedRasters():

GdalUtils.supportedRasters = {}
GdalUtils.supportedOutputRasters = {}
GdalUtils.supportedRasters['GTiff'] = ['tif']
GdalUtils.supportedOutputRasters['GTiff'] = ['tif']

for i in range(gdal.GetDriverCount()):
driver = gdal.GetDriver(i)
if driver is None:
@@ -146,18 +150,31 @@ def getSupportedRasters():
or metadata[gdal.DCAP_RASTER] != 'YES':

# ===================================================================
# if gdal.DCAP_CREATE not in metadata \
# or metadata[gdal.DCAP_CREATE] != 'YES':
# continue
# ===================================================================
if gdal.DMD_EXTENSION in metadata:
extensions = metadata[gdal.DMD_EXTENSION].split('/')
if extensions:
GdalUtils.supportedRasters[shortName] = extensions
# Only creatable rasters can be referenced in output rasters
if ((gdal.DCAP_CREATE in metadata
and metadata[gdal.DCAP_CREATE] == 'YES')
or (gdal.DCAP_CREATECOPY in metadata
and metadata[gdal.DCAP_CREATECOPY] == 'YES')):
GdalUtils.supportedOutputRasters[shortName] = extensions

return GdalUtils.supportedRasters

def getSupportedOutputRasters():
if not gdalAvailable:
return {}

if GdalUtils.supportedOutputRasters is not None:
return GdalUtils.supportedOutputRasters

return GdalUtils.supportedOutputRasters

def getSupportedRasterExtensions():
allexts = ['tif']
@@ -167,6 +184,15 @@ def getSupportedRasterExtensions():
return allexts

def getSupportedOutputRasterExtensions():
allexts = ['tif']
for exts in list(GdalUtils.getSupportedOutputRasters().values()):
for ext in exts:
if ext not in allexts and ext != '':
return allexts

def getVectorDriverFromFileName(filename):
ext = os.path.splitext(filename)[1]
@@ -86,6 +86,8 @@ class Grass7Algorithm(QgsProcessingAlgorithm):

OUTPUT_TYPES = ['auto', 'point', 'line', 'area']
QGIS_OUTPUT_TYPES = {QgsProcessing.TypeVectorAnyGeometry: 'auto',
@@ -227,6 +229,7 @@ def defineCharacteristicsFromFile(self):

if hasRasterOutput:
# Add a cellsize parameter
param = QgsProcessingParameterNumber(
self.GRASS_REGION_CELLSIZE_PARAMETER,'GRASS GIS 7 region cellsize (leave 0 for default)'),
@@ -236,6 +239,24 @@ def defineCharacteristicsFromFile(self):
param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)

# Add a createopt parameter for format export
param = QgsProcessingParameterString(
self.GRASS_RASTER_FORMAT_OPT,'Output Rasters format options (createopt)'),
multiLine=True, optional=True
param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)

# Add a metadata parameter for format export
param = QgsProcessingParameterString(
self.GRASS_RASTER_FORMAT_META,'Output Rasters format metadata options (metaopt)'),
multiLine=True, optional=True
param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)

if hasVectorInput:
param = QgsProcessingParameterNumber(self.GRASS_SNAP_TOLERANCE_PARAMETER,' snap tolerance (-1 = no snap)'),
@@ -459,7 +480,9 @@ def processCommand(self, parameters, context, delOutputs=False):

# Raster and vector layers
@@ -622,24 +645,39 @@ def exportRasterLayerFromParameter(self, name, parameters, context, colorTable=T
fileName = os.path.normpath(
self.parameterAsOutputLayer(parameters, name, context))
grassName = '{}{}'.format(name, self.uniqueSuffix)
self.exportRasterLayer(grassName, fileName, colorTable)
outFormat = Grass7Utils.getRasterFormatFromFilename(fileName)
createOpt = self.parameterAsString(parameters, self.GRASS_RASTER_FORMAT_OPT, context)
metaOpt = self.parameterAsString(parameters, self.GRASS_RASTER_FORMAT_META, context)
self.exportRasterLayer(grassName, fileName, colorTable, outFormat, createOpt, metaOpt)

def exportRasterLayer(self, grassName, fileName, colorTable=True):
def exportRasterLayer(self, grassName, fileName,
colorTable=True, outFormat='GTiff',
Creates a dedicated command to export a raster from
temporary GRASS DB into a file via gdal.
:param grassName: name of the parameter
:param fileName: file path of raster layer
:param grassName: name of the raster to export.
:param fileName: file path of raster layer.
:param colorTable: preserve color Table.
:param outFormat: file format for export.
:param createOpt: creation options for format.
:param metatOpt: metadata options for export.
if not createOpt:
createOpt = Grass7Utils.GRASS_RASTER_FORMATS_CREATEOPTS[outFormat]

for cmd in [self.commands, self.outputCommands]:
# Adjust region to layer before exporting
cmd.append('g.region raster={}'.format(grassName))
'r.out.gdal -m{0} {3} input="{1}" output="{2}"'.format(
'r.out.gdal -c -m{0} input="{1}" output="{2}" format="{3}" {4}{5} --overwrite'.format(
' -t' if colorTable else '',
grassName, fileName,
'--overwrite -c createopt="TFW=YES,COMPRESS=LZW"'
' createopt="{}"'.format(createOpt) if createOpt else '',
' metaopt="{}"'.format(metaOpt) if metaOpt else ''

@@ -38,7 +38,6 @@
from .Grass7Algorithm import Grass7Algorithm
from import isWindows, isMac
#from .nviz7 import nviz7
from processing.algs.gdal.GdalUtils import GdalUtils

pluginPath = os.path.normpath(os.path.join(
os.path.split(os.path.dirname(__file__))[0], os.pardir))
@@ -146,11 +145,7 @@ def supportedOutputVectorLayerExtensions(self):
return QgsVectorFileWriter.supportedFormatExtensions()

def supportedOutputRasterLayerExtensions(self):
# We use the same extensions than GDAL because:
# - GRASS is also using GDAL for raster imports.
# - Chances that GRASS is compiled with another version of
# GDAL than QGIS are very limited!
return GdalUtils.getSupportedRasterExtensions()
return Grass7Utils.getSupportedOutputRasterExtensions()

def canBeActivated(self):
return not bool(Grass7Utils.checkGrass7IsInstalled())
@@ -40,6 +40,7 @@
from processing.core.ProcessingConfig import ProcessingConfig
from import userFolder, isWindows, isMac, mkdir
from processing.tests.TestData import points
from processing.algs.gdal.GdalUtils import GdalUtils

class Grass7Utils(object):
@@ -55,6 +56,13 @@ class Grass7Utils(object):

# TODO Review all default options formats
'PNG': 'ZLEVEL=9',

sessionRunning = False
sessionLayers = {}
projectionSet = False
@@ -515,3 +523,27 @@ def grassHelpPath():
# GRASS not available!
return ''

def getSupportedOutputRasterExtensions():
# We use the same extensions than GDAL because:
# - GRASS is also using GDAL for raster imports.
# - Chances that GRASS is compiled with another version of
# GDAL than QGIS are very limited!
return GdalUtils.getSupportedOutputRasterExtensions()

def getRasterFormatFromFilename(filename):
Returns Raster format name from a raster filename.
:param filename: The name with extension of the raster.
:return: The Gdal short format name for extension.
ext = os.path.splitext(filename)[1].lower()
ext = ext.lstrip('.')
supported = GdalUtils.getSupportedRasters()
for name in list(supported.keys()):
exts = supported[name]
if ext in exts:
return name
return 'GTiff'
@@ -17,7 +17,6 @@ QGIS3 Processing Port
* TODO Improve unit tests.
* TODO Use prepareAlgorithm for algorithm preparation.
* TODO Support ParameterTable.
* TODO Support multiple output raster formats.
* TODO Support multiple output vector formats.
* TODO Try to use v.external.out on simple algorithms.
* TODO Add an optional/advanced 'format option' textbox if vector output is detected.
@@ -231,6 +230,10 @@ QGIS3 Processing Port

* DONE Support multiple output file raster formats.
* DONE Add an optional/advanced 'format option' textbox if raster output is detected.
* DONE Detext file format from extension.
* DONE Improve GdalUtils to report raster formats that can be created with GDAL.
* DONE Add GRASS 7.2 new algorithms.
* DONE Remove r.aspect => r.slope.aspect.
* DONE Remove r.median.
@@ -59,9 +59,7 @@ def getFileFilter(param):
elif param.type() == 'raster':
return QgsProviderRegistry.instance().fileRasterFilters()
elif param.type() == 'rasterDestination':
exts = dataobjects.getSupportedOutputRasterLayerExtensions()
for i in range(len(exts)):
exts[i] = tr('{0} files (*.{1})', 'QgsProcessingParameterRasterDestination').format(exts[i].upper(), exts[i].lower())
exts = dataobjects.getSupportedOutputRasterFilters()
return ';;'.join(exts) + ';;' + tr('All files (*.*)')
elif param.type() == 'table':
exts = ['csv', 'dbf']
@@ -220,6 +220,7 @@ def getFileName(self, initial_value=''):
path = ''

# TODO: should use selectedFilter argument for default file format
filename, selected_filter = QFileDialog.getOpenFileName(self.widget,'Select file'),
path, getFileFilter(self.param))
if filename:
@@ -117,6 +117,24 @@ def getSupportedOutputRasterLayerExtensions():
return allexts

def getSupportedOutputRasterFilters():
Return a list of file filters for supported raster formats.
Supported formats come from Gdal.
:return: a list of strings for Qt file filters.
allFilters = []
supported = GdalUtils.getSupportedOutputRasters()
formatList = sorted(supported.keys())
# Place GTiff as the first format
if 'GTiff' in formatList:
formatList.insert(0, 'GTiff')
for f in formatList:
allFilters.append('{0} files (*.{1})'.format(f, ' *.'.join(supported[f])))
return allFilters

def load(fileName, name=None, crs=None, style=None, isRaster=False):
"""Loads a layer/table into the current project, given its file.

0 comments on commit d10aaf4

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