Skip to content

Commit 8c24fa8

Browse files
authoredJan 22, 2019
Merge pull request #8927 from alexbruy/processing-vrt
[processing][needs-docs] add missed "srcnodata" parameter to the buildvrt algorithm (fix #20586)
2 parents e75a888 + 133b405 commit 8c24fa8

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed
 

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
QgsProcessingParameterBoolean,
4040
QgsProcessingParameterRasterDestination,
4141
QgsProcessingParameterCrs,
42+
QgsProcessingParameterString,
4243
QgsProcessingOutputLayerDefinition,
4344
QgsProcessingUtils)
4445
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
@@ -57,6 +58,7 @@ class buildvrt(GdalAlgorithm):
5758
ADD_ALPHA = 'ADD_ALPHA'
5859
ASSIGN_CRS = 'ASSIGN_CRS'
5960
RESAMPLING = 'RESAMPLING'
61+
SRC_NODATA = 'SRC_NODATA'
6062

6163
RESOLUTION_OPTIONS = ['average', 'highest', 'lowest']
6264
RESAMPLING_OPTIONS = ['nearest', 'bilinear', 'cubic', 'cubicspline', 'lanczos', 'average', 'mode']
@@ -82,38 +84,45 @@ def defaultFileExtension(self):
8284
return 'vrt'
8385

8486
self.addParameter(QgsProcessingParameterMultipleLayers(self.INPUT,
85-
QCoreApplication.translate("ParameterVrtDestination", 'Input layers'),
87+
self.tr('Input layers'),
8688
QgsProcessing.TypeRaster))
8789
self.addParameter(QgsProcessingParameterEnum(self.RESOLUTION,
88-
QCoreApplication.translate("ParameterVrtDestination", 'Resolution'),
90+
self.tr('Resolution'),
8991
options=self.RESOLUTION_OPTIONS,
9092
defaultValue=0))
9193
self.addParameter(QgsProcessingParameterBoolean(self.SEPARATE,
92-
QCoreApplication.translate("ParameterVrtDestination", 'Place each input file into a separate band'),
94+
self.tr('Place each input file into a separate band'),
9395
defaultValue=True))
9496
self.addParameter(QgsProcessingParameterBoolean(self.PROJ_DIFFERENCE,
95-
QCoreApplication.translate("ParameterVrtDestination", 'Allow projection difference'),
97+
self.tr('Allow projection difference'),
9698
defaultValue=False))
9799

98100
add_alpha_param = QgsProcessingParameterBoolean(self.ADD_ALPHA,
99-
QCoreApplication.translate("ParameterVrtDestination", 'Add alpha mask band to VRT when source raster has none'),
101+
self.tr('Add alpha mask band to VRT when source raster has none'),
100102
defaultValue=False)
101103
add_alpha_param.setFlags(add_alpha_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
102104
self.addParameter(add_alpha_param)
103105

104106
assign_crs = QgsProcessingParameterCrs(self.ASSIGN_CRS,
105-
QCoreApplication.translate("ParameterVrtDestination", 'Override projection for the output file'),
107+
self.tr('Override projection for the output file'),
106108
defaultValue=None, optional=True)
107109
assign_crs.setFlags(assign_crs.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
108110
self.addParameter(assign_crs)
109111

110112
resampling = QgsProcessingParameterEnum(self.RESAMPLING,
111-
QCoreApplication.translate("ParameterVrtDestination", 'Resampling algorithm'),
113+
self.tr('Resampling algorithm'),
112114
options=self.RESAMPLING_OPTIONS,
113115
defaultValue=0)
114116
resampling.setFlags(resampling.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
115117
self.addParameter(resampling)
116118

119+
src_nodata_param = QgsProcessingParameterString(self.SRC_NODATA,
120+
self.tr('Nodata value(s) for input bands (space separated)'),
121+
defaultValue=None,
122+
optional=True)
123+
src_nodata_param.setFlags(src_nodata_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
124+
self.addParameter(src_nodata_param)
125+
117126
self.addParameter(ParameterVrtDestination(self.OUTPUT, QCoreApplication.translate("ParameterVrtDestination", 'Virtual')))
118127

119128
def name(self):
@@ -151,6 +160,10 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
151160
arguments.append('-r')
152161
arguments.append(self.RESAMPLING_OPTIONS[self.parameterAsEnum(parameters, self.RESAMPLING, context)])
153162

163+
if self.SRC_NODATA in parameters and parameters[self.SRC_NODATA] not in (None, ''):
164+
nodata = self.parameterAsString(parameters, self.SRC_NODATA, context)
165+
arguments.append('-srcnodata "{}"'.format(nodata))
166+
154167
# Always write input files to a text file in case there are many of them and the
155168
# length of the command will be longer then allowed in command prompt
156169
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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,51 @@ def testBuildVrt(self):
11041104
self.assertIn('-input_file_list', commands[1])
11051105
self.assertIn(outdir + '/test.vrt', commands[1])
11061106

1107+
commands = alg.getConsoleCommands({'LAYERS': [source],
1108+
'SRC_NODATA': '-9999',
1109+
'OUTPUT': outdir + '/test.vrt'}, context, feedback)
1110+
self.assertEqual(len(commands), 2)
1111+
self.assertEqual(commands[0], 'gdalbuildvrt')
1112+
self.assertIn('-resolution average', commands[1])
1113+
self.assertIn('-separate', commands[1])
1114+
self.assertNotIn('-allow_projection_difference', commands[1])
1115+
self.assertNotIn('-add_alpha', commands[1])
1116+
self.assertNotIn('-a_srs', commands[1])
1117+
self.assertIn('-r nearest', commands[1])
1118+
self.assertIn('-srcnodata "-9999"', commands[1])
1119+
self.assertIn('-input_file_list', commands[1])
1120+
self.assertIn(outdir + '/test.vrt', commands[1])
1121+
1122+
commands = alg.getConsoleCommands({'LAYERS': [source],
1123+
'SRC_NODATA': '-9999 9999',
1124+
'OUTPUT': outdir + '/test.vrt'}, context, feedback)
1125+
self.assertEqual(len(commands), 2)
1126+
self.assertEqual(commands[0], 'gdalbuildvrt')
1127+
self.assertIn('-resolution average', commands[1])
1128+
self.assertIn('-separate', commands[1])
1129+
self.assertNotIn('-allow_projection_difference', commands[1])
1130+
self.assertNotIn('-add_alpha', commands[1])
1131+
self.assertNotIn('-a_srs', commands[1])
1132+
self.assertIn('-r nearest', commands[1])
1133+
self.assertIn('-srcnodata "-9999 9999"', commands[1])
1134+
self.assertIn('-input_file_list', commands[1])
1135+
self.assertIn(outdir + '/test.vrt', commands[1])
1136+
1137+
commands = alg.getConsoleCommands({'LAYERS': [source],
1138+
'SRC_NODATA': '',
1139+
'OUTPUT': outdir + '/test.vrt'}, context, feedback)
1140+
self.assertEqual(len(commands), 2)
1141+
self.assertEqual(commands[0], 'gdalbuildvrt')
1142+
self.assertIn('-resolution average', commands[1])
1143+
self.assertIn('-separate', commands[1])
1144+
self.assertNotIn('-allow_projection_difference', commands[1])
1145+
self.assertNotIn('-add_alpha', commands[1])
1146+
self.assertNotIn('-a_srs', commands[1])
1147+
self.assertIn('-r nearest', commands[1])
1148+
self.assertNotIn('-srcnodata', commands[1])
1149+
self.assertIn('-input_file_list', commands[1])
1150+
self.assertIn(outdir + '/test.vrt', commands[1])
1151+
11071152
def testGdalInfo(self):
11081153
context = QgsProcessingContext()
11091154
feedback = QgsProcessingFeedback()

0 commit comments

Comments
 (0)
Please sign in to comment.