Skip to content

Commit 342c093

Browse files
committed
[processing] Use text file for input file list to gdal tile index alg
Avoids too long command being generated with many inputs Fixes #21296
1 parent 1b228bb commit 342c093

File tree

2 files changed

+42
-47
lines changed

2 files changed

+42
-47
lines changed

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

+6-8
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,6 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
131131
self.setOutputValue(self.OUTPUT, outFile)
132132
output, outFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
133133

134-
layers = []
135-
for layer in input_layers:
136-
if layer.type() != QgsMapLayer.RasterLayer:
137-
raise QgsProcessingException(
138-
self.tr('All layers must be raster layers!'))
139-
layers.append(layer.source())
140-
141134
arguments = []
142135
arguments.append('-tileindex')
143136
arguments.append(self.parameterAsString(parameters, self.PATH_FIELD_NAME, context))
@@ -162,6 +155,11 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
162155
arguments.append('-f {}'.format(outFormat))
163156

164157
arguments.append(output)
165-
arguments.append(' '.join(layers))
158+
159+
# Always write input files to a text file in case there are many of them and the
160+
# length of the command will be longer then allowed in command prompt
161+
list_file = GdalUtils.writeLayerParameterToTextFile(filename='tile_index_files.txt', alg=self, parameters=parameters, parameter_name=self.LAYERS, context=context, quote=True, executing=executing)
162+
arguments.append('--optfile')
163+
arguments.append(list_file)
166164

167165
return [self.commandName(), GdalUtils.escapeAndJoin(arguments)]

python/plugins/processing/tests/GdalAlgorithmsTest.py

+36-39
Original file line numberDiff line numberDiff line change
@@ -1225,55 +1225,52 @@ def testGdalTindex(self):
12251225
alg.initAlgorithm()
12261226

12271227
with tempfile.TemporaryDirectory() as outdir:
1228-
self.assertEqual(
1229-
alg.getConsoleCommands({'LAYERS': [source],
1230-
'OUTPUT': outdir + '/test.shp'}, context, feedback),
1231-
['gdaltindex',
1232-
'-tileindex location -f "ESRI Shapefile" ' +
1233-
outdir + '/test.shp ' +
1234-
source])
1228+
commands = alg.getConsoleCommands({'LAYERS': [source],
1229+
'OUTPUT': outdir + '/test.shp'}, context, feedback)
1230+
self.assertEqual(len(commands), 2)
1231+
self.assertEqual(commands[0], 'gdaltindex')
1232+
self.assertIn('-tileindex location -f "ESRI Shapefile" ' + outdir + '/test.shp', commands[1])
1233+
self.assertIn('--optfile ', commands[1])
12351234

12361235
# with input srs
1237-
self.assertEqual(
1238-
alg.getConsoleCommands({'LAYERS': [source],
1239-
'TARGET_CRS': 'EPSG:3111',
1240-
'OUTPUT': outdir + '/test.shp'}, context, feedback),
1241-
['gdaltindex',
1242-
'-tileindex location -t_srs EPSG:3111 -f "ESRI Shapefile" ' +
1243-
outdir + '/test.shp ' +
1244-
source])
1236+
commands = alg.getConsoleCommands({'LAYERS': [source],
1237+
'TARGET_CRS': 'EPSG:3111',
1238+
'OUTPUT': outdir + '/test.shp'}, context, feedback)
1239+
self.assertEqual(len(commands), 2)
1240+
self.assertEqual(commands[0], 'gdaltindex')
1241+
self.assertIn('-tileindex location -t_srs EPSG:3111 -f "ESRI Shapefile" ' + outdir + '/test.shp', commands[1])
1242+
self.assertIn('--optfile ', commands[1])
12451243

12461244
# with target using proj string
12471245
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'
1248-
self.assertEqual(
1249-
alg.getConsoleCommands({'LAYERS': [source],
1250-
'TARGET_CRS': custom_crs,
1251-
'OUTPUT': outdir + '/test.shp'}, context, feedback),
1252-
['gdaltindex',
1253-
'-tileindex location -t_srs EPSG:20936 -f "ESRI Shapefile" ' +
1254-
outdir + '/test.shp ' +
1255-
source])
1246+
commands = alg.getConsoleCommands({'LAYERS': [source],
1247+
'TARGET_CRS': custom_crs,
1248+
'OUTPUT': outdir + '/test.shp'}, context, feedback)
1249+
self.assertEqual(len(commands), 2)
1250+
self.assertEqual(commands[0], 'gdaltindex')
1251+
self.assertIn('-tileindex location -t_srs EPSG:20936 -f "ESRI Shapefile" ' + outdir + '/test.shp', commands[1])
1252+
self.assertIn('--optfile ', commands[1])
12561253

12571254
# with target using custom projection
12581255
custom_crs = 'proj4: +proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs'
1259-
self.assertEqual(
1260-
alg.getConsoleCommands({'LAYERS': [source],
1261-
'TARGET_CRS': custom_crs,
1262-
'OUTPUT': outdir + '/test.shp'}, context, feedback),
1263-
['gdaltindex',
1264-
'-tileindex location -t_srs "+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs" -f "ESRI Shapefile" ' +
1265-
outdir + '/test.shp ' +
1266-
source])
1256+
commands = alg.getConsoleCommands({'LAYERS': [source],
1257+
'TARGET_CRS': custom_crs,
1258+
'OUTPUT': outdir + '/test.shp'}, context, feedback)
1259+
self.assertEqual(len(commands), 2)
1260+
self.assertEqual(commands[0], 'gdaltindex')
1261+
self.assertIn('-tileindex location -t_srs "+proj=utm +zone=36 +south +a=63785 +b=6357 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs" -f "ESRI Shapefile" ' + outdir + '/test.shp', commands[1])
1262+
self.assertIn('--optfile ', commands[1])
12671263

12681264
# with non-EPSG crs code
1269-
self.assertEqual(
1270-
alg.getConsoleCommands({'LAYERS': [source],
1271-
'TARGET_CRS': 'POSTGIS:3111',
1272-
'OUTPUT': outdir + '/test.shp'}, context, feedback),
1273-
['gdaltindex',
1274-
'-tileindex location -t_srs EPSG:3111 -f "ESRI Shapefile" ' +
1275-
outdir + '/test.shp ' +
1276-
source])
1265+
commands = alg.getConsoleCommands({'LAYERS': [source],
1266+
'TARGET_CRS': 'POSTGIS:3111',
1267+
'OUTPUT': outdir + '/test.shp'}, context, feedback)
1268+
self.assertEqual(len(commands), 2)
1269+
self.assertEqual(commands[0], 'gdaltindex')
1270+
self.assertIn(
1271+
'-tileindex location -t_srs EPSG:3111 -f "ESRI Shapefile" ' + outdir + '/test.shp',
1272+
commands[1])
1273+
self.assertIn('--optfile ', commands[1])
12771274

12781275
def testGridAverage(self):
12791276
context = QgsProcessingContext()

0 commit comments

Comments
 (0)