Skip to content

Commit dc15292

Browse files
committed
[sextante] escape filenames, passed to GDAL tools (fix #5923)
1 parent 5f9eca2 commit dc15292

13 files changed

Lines changed: 181 additions & 134 deletions

File tree

python/plugins/sextante/gdal/GdalUtils.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,20 @@
2323
# This will get replaced with a git SHA1 when you do a git archive
2424
__revision__ = '$Format:%H$'
2525

26-
from PyQt4.QtCore import *
27-
from PyQt4.QtGui import *
26+
import os
2827
import subprocess
28+
29+
from PyQt4.QtCore import *
30+
2931
from sextante.core.SextanteLog import SextanteLog
30-
import os
3132

3233
try:
3334
from osgeo import gdal
3435
gdalAvailable = True
3536
except:
3637
gdalAvailable = False
3738

38-
class GdalUtils():
39+
class GdalUtils:
3940

4041
supportedRasters = None
4142

@@ -108,3 +109,14 @@ def getFormatShortNameFromFilename(filename):
108109
if ext in exts:
109110
return name
110111
return "GTiff"
112+
113+
@staticmethod
114+
def escapeAndJoin(strList):
115+
joined = QString()
116+
for s in strList:
117+
if " " in s:
118+
escaped = '"' + s.replace('\\', '\\\\').replace('"', '\\"') + '"'
119+
else:
120+
escaped = s
121+
joined += escaped + " "
122+
return joined.trimmed()

python/plugins/sextante/gdal/gdaladdo.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424
# This will get replaced with a git SHA1 when you do a git archive
2525
__revision__ = '$Format:%H$'
2626

27+
import os
28+
2729
from PyQt4 import QtGui
30+
2831
from sextante.core.GeoAlgorithm import GeoAlgorithm
2932
from sextante.parameters.ParameterRaster import ParameterRaster
3033
from sextante.outputs.OutputRaster import OutputRaster
31-
import os
34+
3235
from sextante.gdal.GdalUtils import GdalUtils
3336

3437
class gdaladdo(GeoAlgorithm):
@@ -50,10 +53,10 @@ def defineCharacteristics(self):
5053
self.addOutput(OutputRaster(gdaladdo.OUTPUT, "Output layer", True))
5154

5255
def processAlgorithm(self, progress):
53-
commands = ["gdaladdo"]
54-
input = self.getParameterValue(gdaladdo.INPUT)
55-
self.setOutputValue(gdaladdo.OUTPUT, input)
56-
commands.append(input)
57-
commands.append(self.getParameterValue(gdaladdo.LEVELS))
56+
arguments = []
57+
inFile = self.getParameterValue(gdaladdo.INPUT)
58+
arguments.append(inFile)
59+
arguments.append(self.getParameterValue(gdaladdo.LEVELS))
60+
self.setOutputValue(gdaladdo.OUTPUT, inFile)
5861

59-
GdalUtils.runGdal(commands, progress)
62+
GdalUtils.runGdal(["gdaladdo", GdalUtils.escapeAndJoin(arguments)], progress)

python/plugins/sextante/gdal/information.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@
2323
# This will get replaced with a git SHA1 when you do a git archive
2424
__revision__ = '$Format:%H$'
2525

26+
import os
27+
2628
from PyQt4 import QtGui
29+
2730
from sextante.core.GeoAlgorithm import GeoAlgorithm
31+
2832
from sextante.parameters.ParameterRaster import ParameterRaster
2933
from sextante.parameters.ParameterBoolean import ParameterBoolean
30-
import os
31-
from sextante.gdal.GdalUtils import GdalUtils
34+
3235
from sextante.outputs.OutputHTML import OutputHTML
3336

37+
from sextante.gdal.GdalUtils import GdalUtils
38+
3439
class information(GeoAlgorithm):
3540

3641
INPUT = "INPUT"
@@ -51,13 +56,13 @@ def defineCharacteristics(self):
5156
self.addOutput(OutputHTML(information.OUTPUT, "Layer information"))
5257

5358
def processAlgorithm(self, progress):
54-
commands = ["gdalinfo"]
59+
arguments = []
5560
if self.getParameterValue(information.NOGCP):
56-
commands.append("-nogcp")
61+
arguments.append("-nogcp")
5762
if self.getParameterValue(information.NOMETADATA):
58-
commands.append("-nomd")
59-
commands.append(self.getParameterValue(information.INPUT))
60-
GdalUtils.runGdal(commands, progress)
63+
arguments.append("-nomd")
64+
arguments.append(self.getParameterValue(information.INPUT))
65+
GdalUtils.runGdal(["gdalinfo", GdalUtils.escapeAndJoin(arguments)], progress)
6166
output = self.getOutputValue(information.OUTPUT)
6267
f = open(output, "w")
6368
for s in GdalUtils.getConsoleOutput()[1:]:

python/plugins/sextante/gdal/merge.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,19 @@
2323
# This will get replaced with a git SHA1 when you do a git archive
2424
__revision__ = '$Format:%H$'
2525

26+
import os
27+
2628
from PyQt4 import QtGui
29+
2730
from sextante.core.GeoAlgorithm import GeoAlgorithm
28-
from sextante.outputs.OutputRaster import OutputRaster
29-
import os
30-
from sextante.gdal.GdalUtils import GdalUtils
3131
from sextante.core.SextanteUtils import SextanteUtils
32+
33+
from sextante.outputs.OutputRaster import OutputRaster
3234
from sextante.parameters.ParameterBoolean import ParameterBoolean
3335
from sextante.parameters.ParameterMultipleInput import ParameterMultipleInput
3436

37+
from sextante.gdal.GdalUtils import GdalUtils
38+
3539
class merge(GeoAlgorithm):
3640

3741
INPUT = "INPUT"
@@ -52,20 +56,22 @@ def defineCharacteristics(self):
5256
self.addOutput(OutputRaster(merge.OUTPUT, "Output layer"))
5357

5458
def processAlgorithm(self, progress):
55-
if SextanteUtils.isWindows():
56-
commands = ["cmd.exe", "/C ", "gdal_merge.bat"]
57-
else:
58-
commands = ["gdal_merge.py"]
59+
arguments = []
5960
if self.getParameterValue(merge.SEPARATE):
60-
commands.append("-separate")
61+
arguments.append("-separate")
6162
if self.getParameterValue(merge.PCT):
62-
commands.append("-pct")
63-
commands.append("-o")
63+
arguments.append("-pct")
64+
arguments.append("-o")
6465
out = self.getOutputValue(merge.OUTPUT)
65-
commands.append(out)
66-
commands.append("-of")
67-
commands.append(GdalUtils.getFormatShortNameFromFilename(out))
68-
commands.append(self.getParameterValue(merge.INPUT).replace(";", " "))
66+
arguments.append(out)
67+
arguments.append("-of")
68+
arguments.append(GdalUtils.getFormatShortNameFromFilename(out))
69+
arguments.append(self.getParameterValue(merge.INPUT).replace(";", " "))
6970

71+
commands = []
72+
if SextanteUtils.isWindows():
73+
commands = ["cmd.exe", "/C ", "gdal_merge.bat", GdalUtils.escapeAndJoin(arguments)]
74+
else:
75+
commands = ["gdal_merge.py", GdalUtils.escapeAndJoin(arguments)]
7076

7177
GdalUtils.runGdal(commands, progress)

python/plugins/sextante/gdal/nearblack.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
# This will get replaced with a git SHA1 when you do a git archive
2424
__revision__ = '$Format:%H$'
2525

26+
import os
2627
from PyQt4 import QtGui
28+
2729
from sextante.core.GeoAlgorithm import GeoAlgorithm
2830
from sextante.parameters.ParameterRaster import ParameterRaster
2931
from sextante.parameters.ParameterNumber import ParameterNumber
3032
from sextante.parameters.ParameterBoolean import ParameterBoolean
3133
from sextante.outputs.OutputRaster import OutputRaster
32-
import os
34+
3335
from sextante.gdal.GdalUtils import GdalUtils
3436

3537
class nearblack(GeoAlgorithm):
@@ -52,12 +54,12 @@ def defineCharacteristics(self):
5254
self.addOutput(OutputRaster(nearblack.OUTPUT, "Output layer"))
5355

5456
def processAlgorithm(self, progress):
55-
commands = ["nearblack"]
56-
commands.append("-o")
57-
commands.append(self.getOutputValue(nearblack.OUTPUT))
58-
commands.append("-near")
59-
commands.append(str(self.getParameterValue(nearblack.NEAR)))
57+
arguments = []
58+
arguments.append("-o")
59+
arguments.append(self.getOutputValue(nearblack.OUTPUT))
60+
arguments.append("-near")
61+
arguments.append(str(self.getParameterValue(nearblack.NEAR)))
6062
if self.getParameterValue(nearblack.WHITE):
61-
commands.append("-white")
62-
commands.append(self.getParameterValue(nearblack.INPUT))
63-
GdalUtils.runGdal(commands, progress)
63+
arguments.append("-white")
64+
arguments.append(self.getParameterValue(nearblack.INPUT))
65+
GdalUtils.runGdal(["nearblack", GdalUtils.escapeAndJoin(arguments)], progress)

python/plugins/sextante/gdal/ogr2ogr.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,28 @@
2323
# This will get replaced with a git SHA1 when you do a git archive
2424
__revision__ = '$Format:%H$'
2525

26+
import os
27+
import re
28+
import string
29+
from string import Template
30+
import tempfile
31+
32+
from PyQt4.QtCore import *
33+
from PyQt4.QtGui import *
34+
35+
from qgis.core import *
2636

27-
from sextante.outputs.OutputVector import OutputVector
28-
from sextante.parameters.ParameterVector import ParameterVector
29-
from sextante.parameters.ParameterString import ParameterString
3037
from sextante.core.SextanteLog import SextanteLog
3138
from sextante.core.QGisLayers import QGisLayers
39+
40+
from sextante.parameters.ParameterVector import ParameterVector
41+
from sextante.parameters.ParameterString import ParameterString
42+
from sextante.outputs.OutputVector import OutputVector
43+
3244
from sextante.gdal.OgrAlgorithm import OgrAlgorithm
3345
from sextante.gdal.pyogr.ogr2ogr import *
34-
from qgis.core import *
35-
from PyQt4.QtCore import *
36-
from PyQt4.QtGui import *
37-
import string
38-
from string import Template
39-
import re
40-
import os
41-
import tempfile
46+
47+
from sextante.gdal.GdalUtils import GdalUtils
4248

4349
try:
4450
from osgeo import gdal, ogr, osr

python/plugins/sextante/gdal/pct2rgb.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@
2323
# This will get replaced with a git SHA1 when you do a git archive
2424
__revision__ = '$Format:%H$'
2525

26+
import os
2627
from PyQt4 import QtGui
28+
2729
from sextante.core.GeoAlgorithm import GeoAlgorithm
30+
from sextante.core.SextanteUtils import SextanteUtils
31+
2832
from sextante.parameters.ParameterRaster import ParameterRaster
33+
from sextante.parameters.ParameterSelection import ParameterSelection
2934
from sextante.outputs.OutputRaster import OutputRaster
30-
import os
35+
3136
from sextante.gdal.GdalUtils import GdalUtils
32-
from sextante.parameters.ParameterSelection import ParameterSelection
33-
from sextante.core.SextanteUtils import SextanteUtils
3437

3538
class pct2rgb(GeoAlgorithm):
3639

@@ -53,16 +56,19 @@ def defineCharacteristics(self):
5356
self.addOutput(OutputRaster(pct2rgb.OUTPUT, "Output layer"))
5457

5558
def processAlgorithm(self, progress):
59+
arguments = []
60+
arguments.append("-b")
61+
arguments.append(str(self.getParameterValue(pct2rgb.NBAND) + 1))
62+
arguments.append("-of")
63+
out = self.getOutputValue(pct2rgb.OUTPUT)
64+
arguments.append(GdalUtils.getFormatShortNameFromFilename(out))
65+
arguments.append(self.getParameterValue(pct2rgb.INPUT))
66+
arguments.append(out)
67+
68+
commands = []
5669
if SextanteUtils.isWindows():
57-
commands = ["cmd.exe", "/C ", "pct2rgb.bat"]
70+
commands = ["cmd.exe", "/C ", "pct2rgb.bat", GdalUtils.escapeAndJoin(arguments)]
5871
else:
59-
commands = ["pct2rgb.py"]
60-
commands.append("-b")
61-
commands.append(str(self.getParameterValue(pct2rgb.NBAND) + 1))
62-
commands.append("-of")
63-
out = self.getOutputValue(pct2rgb.OUTPUT)
64-
commands.append(GdalUtils.getFormatShortNameFromFilename(out))
65-
commands.append(self.getParameterValue(pct2rgb.INPUT))
66-
commands.append(out)
72+
commands = ["pct2rgb.py", GdalUtils.escapeAndJoin(arguments)]
6773

6874
GdalUtils.runGdal(commands, progress)

python/plugins/sextante/gdal/polygonize.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@
2323
# This will get replaced with a git SHA1 when you do a git archive
2424
__revision__ = '$Format:%H$'
2525

26+
import os
2627
from PyQt4 import QtGui, QtCore
28+
2729
from sextante.core.GeoAlgorithm import GeoAlgorithm
30+
from sextante.core.SextanteUtils import SextanteUtils
31+
2832
from sextante.parameters.ParameterRaster import ParameterRaster
29-
import os
30-
from sextante.gdal.GdalUtils import GdalUtils
3133
from sextante.parameters.ParameterString import ParameterString
3234
from sextante.outputs.OutputVector import OutputVector
33-
from sextante.core.SextanteUtils import SextanteUtils
35+
36+
from sextante.gdal.GdalUtils import GdalUtils
3437

3538
class polygonize(GeoAlgorithm):
3639

@@ -50,16 +53,19 @@ def defineCharacteristics(self):
5053
self.addOutput(OutputVector(polygonize.OUTPUT, "Output layer"))
5154

5255
def processAlgorithm(self, progress):
56+
arguments = []
57+
arguments.append(self.getParameterValue(polygonize.INPUT))
58+
arguments.append('-f')
59+
arguments.append('"ESRI Shapefile"')
60+
output = self.getOutputValue(polygonize.OUTPUT)
61+
arguments.append(output)
62+
arguments.append(QtCore.QFileInfo(output).baseName())
63+
arguments.append(self.getParameterValue(polygonize.FIELD))
64+
65+
commands = []
5366
if SextanteUtils.isWindows():
54-
commands = ["cmd.exe", "/C ", "gdal_polygonize.bat"]
67+
commands = ["cmd.exe", "/C ", "gdal_polygonize.bat", GdalUtils.escapeAndJoin(arguments)]
5568
else:
56-
commands = ["gdal_polygonize.py"]
57-
commands.append(self.getParameterValue(polygonize.INPUT))
58-
commands.append('-f')
59-
commands.append('"ESRI Shapefile"')
60-
output = self.getOutputValue(polygonize.OUTPUT)
61-
commands.append(output)
62-
commands.append(QtCore.QFileInfo(output).baseName())
63-
commands.append(self.getParameterValue(polygonize.FIELD))
69+
commands = ["gdal_polygonize.py", GdalUtils.escapeAndJoin(arguments)]
6470

6571
GdalUtils.runGdal(commands, progress)

python/plugins/sextante/gdal/rgb2pct.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@
2323
# This will get replaced with a git SHA1 when you do a git archive
2424
__revision__ = '$Format:%H$'
2525

26+
import os
2627
from PyQt4 import QtGui
28+
2729
from sextante.core.GeoAlgorithm import GeoAlgorithm
30+
from sextante.core.SextanteUtils import SextanteUtils
31+
2832
from sextante.parameters.ParameterRaster import ParameterRaster
33+
from sextante.parameters.ParameterNumber import ParameterNumber
2934
from sextante.outputs.OutputRaster import OutputRaster
30-
import os
35+
3136
from sextante.gdal.GdalUtils import GdalUtils
32-
from sextante.parameters.ParameterNumber import ParameterNumber
33-
from sextante.core.SextanteUtils import SextanteUtils
3437

3538
class rgb2pct(GeoAlgorithm):
3639

@@ -50,16 +53,18 @@ def defineCharacteristics(self):
5053
self.addOutput(OutputRaster(rgb2pct.OUTPUT, "Output layer"))
5154

5255
def processAlgorithm(self, progress):
56+
arguments = []
57+
arguments.append("-n")
58+
arguments.append(str(self.getParameterValue(rgb2pct.NCOLORS)))
59+
arguments.append("-of")
60+
out = self.getOutputValue(rgb2pct.OUTPUT)
61+
arguments.append(GdalUtils.getFormatShortNameFromFilename(out))
62+
arguments.append(self.getParameterValue(rgb2pct.INPUT))
63+
arguments.append(out)
64+
5365
if SextanteUtils.isWindows():
54-
commands = ["cmd.exe", "/C ", "rgb2pct.bat"]
66+
commands = ["cmd.exe", "/C ", "rgb2pct.bat", GdalUtils.escapeAndJoin(arguments)]
5567
else:
56-
commands = ["rgb2pct.py"]
57-
commands.append("-n")
58-
commands.append(str(self.getParameterValue(rgb2pct.NCOLORS)))
59-
commands.append("-of")
60-
out = self.getOutputValue(rgb2pct.OUTPUT)
61-
commands.append(GdalUtils.getFormatShortNameFromFilename(out))
62-
commands.append(self.getParameterValue(rgb2pct.INPUT))
63-
commands.append(out)
68+
commands = ["rgb2pct.py", GdalUtils.escapeAndJoin(arguments)]
6469

6570
GdalUtils.runGdal(commands, progress)

0 commit comments

Comments
 (0)