Skip to content

Commit a17b77b

Browse files
committed
[processing][gdal] Add missing parameters for buildvrt algorithm
1 parent 76b13ff commit a17b77b

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

python/plugins/processing/algs/gdal/buildvrt.py

+35
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@
3232

3333
from qgis.core import (QgsProcessingAlgorithm,
3434
QgsProcessing,
35+
QgsProcessingParameterDefinition,
3536
QgsProperty,
3637
QgsProcessingParameterMultipleLayers,
3738
QgsProcessingParameterEnum,
3839
QgsProcessingParameterBoolean,
3940
QgsProcessingParameterRasterDestination,
41+
QgsProcessingParameterCrs,
4042
QgsProcessingOutputLayerDefinition,
4143
QgsProcessingUtils)
4244
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
@@ -52,8 +54,12 @@ class buildvrt(GdalAlgorithm):
5254
RESOLUTION = 'RESOLUTION'
5355
SEPARATE = 'SEPARATE'
5456
PROJ_DIFFERENCE = 'PROJ_DIFFERENCE'
57+
ADD_ALPHA = 'ADD_ALPHA'
58+
ASSIGN_CRS = 'ASSIGN_CRS'
59+
RESAMPLING = 'RESAMPLING'
5560

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

5864
def __init__(self):
5965
super().__init__()
@@ -88,6 +94,26 @@ def defaultFileExtension(self):
8894
self.addParameter(QgsProcessingParameterBoolean(self.PROJ_DIFFERENCE,
8995
QCoreApplication.translate("ParameterVrtDestination", 'Allow projection difference'),
9096
defaultValue=False))
97+
98+
add_alpha_param = QgsProcessingParameterBoolean(self.ADD_ALPHA,
99+
QCoreApplication.translate("ParameterVrtDestination", 'Add alpha mask band to VRT when source raster has none'),
100+
defaultValue=False)
101+
add_alpha_param.setFlags(add_alpha_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
102+
self.addParameter(add_alpha_param)
103+
104+
assign_crs = QgsProcessingParameterCrs(self.ASSIGN_CRS,
105+
QCoreApplication.translate("ParameterVrtDestination", 'Override projection for the output file'),
106+
defaultValue=None, optional=True)
107+
assign_crs.setFlags(assign_crs.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
108+
self.addParameter(assign_crs)
109+
110+
resampling = QgsProcessingParameterEnum(self.RESAMPLING,
111+
QCoreApplication.translate("ParameterVrtDestination", 'Resampling algorithm'),
112+
options=self.RESAMPLING_OPTIONS,
113+
defaultValue=0)
114+
resampling.setFlags(resampling.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
115+
self.addParameter(resampling)
116+
91117
self.addParameter(ParameterVrtDestination(self.OUTPUT, QCoreApplication.translate("ParameterVrtDestination", 'Virtual')))
92118

93119
def name(self):
@@ -116,6 +142,15 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
116142
arguments.append('-separate')
117143
if self.parameterAsBool(parameters, buildvrt.PROJ_DIFFERENCE, context):
118144
arguments.append('-allow_projection_difference')
145+
if self.parameterAsBool(parameters, buildvrt.ADD_ALPHA, context):
146+
arguments.append('-addalpha')
147+
crs = self.parameterAsCrs(parameters, self.ASSIGN_CRS, context)
148+
if crs.isValid():
149+
arguments.append('-a_srs')
150+
arguments.append(GdalUtils.gdal_crs_string(crs))
151+
arguments.append('-r')
152+
arguments.append(self.RESAMPLING_OPTIONS[self.parameterAsEnum(parameters, self.RESAMPLING, context)])
153+
119154
# Always write input files to a text file in case there are many of them and the
120155
# length of the command will be longer then allowed in command prompt
121156
list_file = GdalUtils.writeLayerParameterToTextFile(filename='buildvrtInputFiles.txt', alg=self, parameters=parameters, parameter_name=self.INPUT, context=context, executing=executing, quote=False)

python/plugins/processing/tests/GdalAlgorithmsTest.py

+69
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,9 @@ def testBuildVrt(self):
653653
self.assertIn('-resolution average', commands[1])
654654
self.assertIn('-separate', commands[1])
655655
self.assertNotIn('-allow_projection_difference', commands[1])
656+
self.assertNotIn('-add_alpha', commands[1])
657+
self.assertNotIn('-a_srs', commands[1])
658+
self.assertIn('-r nearest', commands[1])
656659
self.assertIn('-input_file_list', commands[1])
657660
self.assertIn('d:/temp/test.vrt', commands[1])
658661

@@ -664,6 +667,9 @@ def testBuildVrt(self):
664667
self.assertIn('-resolution lowest', commands[1])
665668
self.assertIn('-separate', commands[1])
666669
self.assertNotIn('-allow_projection_difference', commands[1])
670+
self.assertNotIn('-add_alpha', commands[1])
671+
self.assertNotIn('-a_srs', commands[1])
672+
self.assertIn('-r nearest', commands[1])
667673
self.assertIn('-input_file_list', commands[1])
668674
self.assertIn('d:/temp/test.vrt', commands[1])
669675

@@ -675,6 +681,9 @@ def testBuildVrt(self):
675681
self.assertIn('-resolution average', commands[1])
676682
self.assertNotIn('-allow_projection_difference', commands[1])
677683
self.assertNotIn('-separate', commands[1])
684+
self.assertNotIn('-add_alpha', commands[1])
685+
self.assertNotIn('-a_srs', commands[1])
686+
self.assertIn('-r nearest', commands[1])
678687
self.assertIn('-input_file_list', commands[1])
679688
self.assertIn('d:/temp/test.vrt', commands[1])
680689

@@ -686,6 +695,66 @@ def testBuildVrt(self):
686695
self.assertIn('-resolution average', commands[1])
687696
self.assertIn('-allow_projection_difference', commands[1])
688697
self.assertIn('-separate', commands[1])
698+
self.assertNotIn('-add_alpha', commands[1])
699+
self.assertNotIn('-a_srs', commands[1])
700+
self.assertIn('-r nearest', commands[1])
701+
self.assertIn('-input_file_list', commands[1])
702+
self.assertIn('d:/temp/test.vrt', commands[1])
703+
704+
commands = alg.getConsoleCommands({'LAYERS': [source],
705+
'ADD_ALPHA': True,
706+
'OUTPUT': 'd:/temp/test.vrt'}, context, feedback)
707+
self.assertEqual(len(commands), 2)
708+
self.assertEqual(commands[0], 'gdalbuildvrt')
709+
self.assertIn('-resolution average', commands[1])
710+
self.assertIn('-separate', commands[1])
711+
self.assertNotIn('-allow_projection_difference', commands[1])
712+
self.assertNotIn('-add_alpha', commands[1])
713+
self.assertNotIn('-a_srs', commands[1])
714+
self.assertIn('-r nearest', commands[1])
715+
self.assertIn('-input_file_list', commands[1])
716+
self.assertIn('d:/temp/test.vrt', commands[1])
717+
718+
commands = alg.getConsoleCommands({'LAYERS': [source],
719+
'ASSIGN_CRS': 'EPSG:3111',
720+
'OUTPUT': 'd:/temp/test.vrt'}, context, feedback)
721+
self.assertEqual(len(commands), 2)
722+
self.assertEqual(commands[0], 'gdalbuildvrt')
723+
self.assertIn('-resolution average', commands[1])
724+
self.assertIn('-separate', commands[1])
725+
self.assertNotIn('-allow_projection_difference', commands[1])
726+
self.assertNotIn('-add_alpha', commands[1])
727+
self.assertIn('-a_srs EPSG:3111', commands[1])
728+
self.assertIn('-r nearest', commands[1])
729+
self.assertIn('-input_file_list', commands[1])
730+
self.assertIn('d:/temp/test.vrt', commands[1])
731+
732+
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'
733+
commands = alg.getConsoleCommands({'LAYERS': [source],
734+
'ASSIGN_CRS': custom_crs,
735+
'OUTPUT': 'd:/temp/test.vrt'}, context, feedback)
736+
self.assertEqual(len(commands), 2)
737+
self.assertEqual(commands[0], 'gdalbuildvrt')
738+
self.assertIn('-resolution average', commands[1])
739+
self.assertIn('-separate', commands[1])
740+
self.assertNotIn('-allow_projection_difference', commands[1])
741+
self.assertNotIn('-add_alpha', commands[1])
742+
self.assertIn('-a_srs EPSG:20936', commands[1])
743+
self.assertIn('-r nearest', commands[1])
744+
self.assertIn('-input_file_list', commands[1])
745+
self.assertIn('d:/temp/test.vrt', commands[1])
746+
747+
commands = alg.getConsoleCommands({'LAYERS': [source],
748+
'RESAMPLING': 4,
749+
'OUTPUT': 'd:/temp/test.vrt'}, context, feedback)
750+
self.assertEqual(len(commands), 2)
751+
self.assertEqual(commands[0], 'gdalbuildvrt')
752+
self.assertIn('-resolution average', commands[1])
753+
self.assertIn('-separate', commands[1])
754+
self.assertNotIn('-allow_projection_difference', commands[1])
755+
self.assertNotIn('-add_alpha', commands[1])
756+
self.assertNotIn('-a_srs', commands[1])
757+
self.assertIn('-r lanczos', commands[1])
689758
self.assertIn('-input_file_list', commands[1])
690759
self.assertIn('d:/temp/test.vrt', commands[1])
691760

0 commit comments

Comments
 (0)