Skip to content

Commit

Permalink
[processing][gdal] Add missing parameters for buildvrt algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 18, 2018
1 parent 76b13ff commit a17b77b
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
35 changes: 35 additions & 0 deletions python/plugins/processing/algs/gdal/buildvrt.py
Expand Up @@ -32,11 +32,13 @@

from qgis.core import (QgsProcessingAlgorithm,
QgsProcessing,
QgsProcessingParameterDefinition,
QgsProperty,
QgsProcessingParameterMultipleLayers,
QgsProcessingParameterEnum,
QgsProcessingParameterBoolean,
QgsProcessingParameterRasterDestination,
QgsProcessingParameterCrs,
QgsProcessingOutputLayerDefinition,
QgsProcessingUtils)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
Expand All @@ -52,8 +54,12 @@ class buildvrt(GdalAlgorithm):
RESOLUTION = 'RESOLUTION'
SEPARATE = 'SEPARATE'
PROJ_DIFFERENCE = 'PROJ_DIFFERENCE'
ADD_ALPHA = 'ADD_ALPHA'
ASSIGN_CRS = 'ASSIGN_CRS'
RESAMPLING = 'RESAMPLING'

RESOLUTION_OPTIONS = ['average', 'highest', 'lowest']
RESAMPLING_OPTIONS = ['nearest', 'bilinear', 'cubic', 'cubicspline', 'lanczos', 'average', 'mode']

def __init__(self):
super().__init__()
Expand Down Expand Up @@ -88,6 +94,26 @@ def defaultFileExtension(self):
self.addParameter(QgsProcessingParameterBoolean(self.PROJ_DIFFERENCE,
QCoreApplication.translate("ParameterVrtDestination", 'Allow projection difference'),
defaultValue=False))

add_alpha_param = QgsProcessingParameterBoolean(self.ADD_ALPHA,
QCoreApplication.translate("ParameterVrtDestination", 'Add alpha mask band to VRT when source raster has none'),
defaultValue=False)
add_alpha_param.setFlags(add_alpha_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(add_alpha_param)

assign_crs = QgsProcessingParameterCrs(self.ASSIGN_CRS,
QCoreApplication.translate("ParameterVrtDestination", 'Override projection for the output file'),
defaultValue=None, optional=True)
assign_crs.setFlags(assign_crs.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(assign_crs)

resampling = QgsProcessingParameterEnum(self.RESAMPLING,
QCoreApplication.translate("ParameterVrtDestination", 'Resampling algorithm'),
options=self.RESAMPLING_OPTIONS,
defaultValue=0)
resampling.setFlags(resampling.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(resampling)

self.addParameter(ParameterVrtDestination(self.OUTPUT, QCoreApplication.translate("ParameterVrtDestination", 'Virtual')))

def name(self):
Expand Down Expand Up @@ -116,6 +142,15 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
arguments.append('-separate')
if self.parameterAsBool(parameters, buildvrt.PROJ_DIFFERENCE, context):
arguments.append('-allow_projection_difference')
if self.parameterAsBool(parameters, buildvrt.ADD_ALPHA, context):
arguments.append('-addalpha')
crs = self.parameterAsCrs(parameters, self.ASSIGN_CRS, context)
if crs.isValid():
arguments.append('-a_srs')
arguments.append(GdalUtils.gdal_crs_string(crs))
arguments.append('-r')
arguments.append(self.RESAMPLING_OPTIONS[self.parameterAsEnum(parameters, self.RESAMPLING, context)])

# Always write input files to a text file in case there are many of them and the
# length of the command will be longer then allowed in command prompt
list_file = GdalUtils.writeLayerParameterToTextFile(filename='buildvrtInputFiles.txt', alg=self, parameters=parameters, parameter_name=self.INPUT, context=context, executing=executing, quote=False)
Expand Down
69 changes: 69 additions & 0 deletions python/plugins/processing/tests/GdalAlgorithmsTest.py
Expand Up @@ -653,6 +653,9 @@ def testBuildVrt(self):
self.assertIn('-resolution average', commands[1])
self.assertIn('-separate', commands[1])
self.assertNotIn('-allow_projection_difference', commands[1])
self.assertNotIn('-add_alpha', commands[1])
self.assertNotIn('-a_srs', commands[1])
self.assertIn('-r nearest', commands[1])
self.assertIn('-input_file_list', commands[1])
self.assertIn('d:/temp/test.vrt', commands[1])

Expand All @@ -664,6 +667,9 @@ def testBuildVrt(self):
self.assertIn('-resolution lowest', commands[1])
self.assertIn('-separate', commands[1])
self.assertNotIn('-allow_projection_difference', commands[1])
self.assertNotIn('-add_alpha', commands[1])
self.assertNotIn('-a_srs', commands[1])
self.assertIn('-r nearest', commands[1])
self.assertIn('-input_file_list', commands[1])
self.assertIn('d:/temp/test.vrt', commands[1])

Expand All @@ -675,6 +681,9 @@ def testBuildVrt(self):
self.assertIn('-resolution average', commands[1])
self.assertNotIn('-allow_projection_difference', commands[1])
self.assertNotIn('-separate', commands[1])
self.assertNotIn('-add_alpha', commands[1])
self.assertNotIn('-a_srs', commands[1])
self.assertIn('-r nearest', commands[1])
self.assertIn('-input_file_list', commands[1])
self.assertIn('d:/temp/test.vrt', commands[1])

Expand All @@ -686,6 +695,66 @@ def testBuildVrt(self):
self.assertIn('-resolution average', commands[1])
self.assertIn('-allow_projection_difference', commands[1])
self.assertIn('-separate', commands[1])
self.assertNotIn('-add_alpha', commands[1])
self.assertNotIn('-a_srs', commands[1])
self.assertIn('-r nearest', commands[1])
self.assertIn('-input_file_list', commands[1])
self.assertIn('d:/temp/test.vrt', commands[1])

commands = alg.getConsoleCommands({'LAYERS': [source],
'ADD_ALPHA': True,
'OUTPUT': 'd:/temp/test.vrt'}, context, feedback)
self.assertEqual(len(commands), 2)
self.assertEqual(commands[0], 'gdalbuildvrt')
self.assertIn('-resolution average', commands[1])
self.assertIn('-separate', commands[1])
self.assertNotIn('-allow_projection_difference', commands[1])
self.assertNotIn('-add_alpha', commands[1])
self.assertNotIn('-a_srs', commands[1])
self.assertIn('-r nearest', commands[1])
self.assertIn('-input_file_list', commands[1])
self.assertIn('d:/temp/test.vrt', commands[1])

commands = alg.getConsoleCommands({'LAYERS': [source],
'ASSIGN_CRS': 'EPSG:3111',
'OUTPUT': 'd:/temp/test.vrt'}, context, feedback)
self.assertEqual(len(commands), 2)
self.assertEqual(commands[0], 'gdalbuildvrt')
self.assertIn('-resolution average', commands[1])
self.assertIn('-separate', commands[1])
self.assertNotIn('-allow_projection_difference', commands[1])
self.assertNotIn('-add_alpha', commands[1])
self.assertIn('-a_srs EPSG:3111', commands[1])
self.assertIn('-r nearest', commands[1])
self.assertIn('-input_file_list', commands[1])
self.assertIn('d:/temp/test.vrt', commands[1])

custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs'
commands = alg.getConsoleCommands({'LAYERS': [source],
'ASSIGN_CRS': custom_crs,
'OUTPUT': 'd:/temp/test.vrt'}, context, feedback)
self.assertEqual(len(commands), 2)
self.assertEqual(commands[0], 'gdalbuildvrt')
self.assertIn('-resolution average', commands[1])
self.assertIn('-separate', commands[1])
self.assertNotIn('-allow_projection_difference', commands[1])
self.assertNotIn('-add_alpha', commands[1])
self.assertIn('-a_srs EPSG:20936', commands[1])
self.assertIn('-r nearest', commands[1])
self.assertIn('-input_file_list', commands[1])
self.assertIn('d:/temp/test.vrt', commands[1])

commands = alg.getConsoleCommands({'LAYERS': [source],
'RESAMPLING': 4,
'OUTPUT': 'd:/temp/test.vrt'}, context, feedback)
self.assertEqual(len(commands), 2)
self.assertEqual(commands[0], 'gdalbuildvrt')
self.assertIn('-resolution average', commands[1])
self.assertIn('-separate', commands[1])
self.assertNotIn('-allow_projection_difference', commands[1])
self.assertNotIn('-add_alpha', commands[1])
self.assertNotIn('-a_srs', commands[1])
self.assertIn('-r lanczos', commands[1])
self.assertIn('-input_file_list', commands[1])
self.assertIn('d:/temp/test.vrt', commands[1])

Expand Down

0 comments on commit a17b77b

Please sign in to comment.