Skip to content

Commit

Permalink
[sextante] escape filenames, passed to GDAL tools (fix #5923)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Apr 10, 2013
1 parent 5f9eca2 commit dc15292
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 134 deletions.
20 changes: 16 additions & 4 deletions python/plugins/sextante/gdal/GdalUtils.py
Expand Up @@ -23,19 +23,20 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os
import subprocess

from PyQt4.QtCore import *

from sextante.core.SextanteLog import SextanteLog
import os

try:
from osgeo import gdal
gdalAvailable = True
except:
gdalAvailable = False

class GdalUtils():
class GdalUtils:

supportedRasters = None

Expand Down Expand Up @@ -108,3 +109,14 @@ def getFormatShortNameFromFilename(filename):
if ext in exts:
return name
return "GTiff"

@staticmethod
def escapeAndJoin(strList):
joined = QString()
for s in strList:
if " " in s:
escaped = '"' + s.replace('\\', '\\\\').replace('"', '\\"') + '"'
else:
escaped = s
joined += escaped + " "
return joined.trimmed()
17 changes: 10 additions & 7 deletions python/plugins/sextante/gdal/gdaladdo.py
Expand Up @@ -24,11 +24,14 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os

from PyQt4 import QtGui

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.parameters.ParameterRaster import ParameterRaster
from sextante.outputs.OutputRaster import OutputRaster
import os

from sextante.gdal.GdalUtils import GdalUtils

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

def processAlgorithm(self, progress):
commands = ["gdaladdo"]
input = self.getParameterValue(gdaladdo.INPUT)
self.setOutputValue(gdaladdo.OUTPUT, input)
commands.append(input)
commands.append(self.getParameterValue(gdaladdo.LEVELS))
arguments = []
inFile = self.getParameterValue(gdaladdo.INPUT)
arguments.append(inFile)
arguments.append(self.getParameterValue(gdaladdo.LEVELS))
self.setOutputValue(gdaladdo.OUTPUT, inFile)

GdalUtils.runGdal(commands, progress)
GdalUtils.runGdal(["gdaladdo", GdalUtils.escapeAndJoin(arguments)], progress)
19 changes: 12 additions & 7 deletions python/plugins/sextante/gdal/information.py
Expand Up @@ -23,14 +23,19 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os

from PyQt4 import QtGui

from sextante.core.GeoAlgorithm import GeoAlgorithm

from sextante.parameters.ParameterRaster import ParameterRaster
from sextante.parameters.ParameterBoolean import ParameterBoolean
import os
from sextante.gdal.GdalUtils import GdalUtils

from sextante.outputs.OutputHTML import OutputHTML

from sextante.gdal.GdalUtils import GdalUtils

class information(GeoAlgorithm):

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

def processAlgorithm(self, progress):
commands = ["gdalinfo"]
arguments = []
if self.getParameterValue(information.NOGCP):
commands.append("-nogcp")
arguments.append("-nogcp")
if self.getParameterValue(information.NOMETADATA):
commands.append("-nomd")
commands.append(self.getParameterValue(information.INPUT))
GdalUtils.runGdal(commands, progress)
arguments.append("-nomd")
arguments.append(self.getParameterValue(information.INPUT))
GdalUtils.runGdal(["gdalinfo", GdalUtils.escapeAndJoin(arguments)], progress)
output = self.getOutputValue(information.OUTPUT)
f = open(output, "w")
for s in GdalUtils.getConsoleOutput()[1:]:
Expand Down
34 changes: 20 additions & 14 deletions python/plugins/sextante/gdal/merge.py
Expand Up @@ -23,15 +23,19 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os

from PyQt4 import QtGui

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.outputs.OutputRaster import OutputRaster
import os
from sextante.gdal.GdalUtils import GdalUtils
from sextante.core.SextanteUtils import SextanteUtils

from sextante.outputs.OutputRaster import OutputRaster
from sextante.parameters.ParameterBoolean import ParameterBoolean
from sextante.parameters.ParameterMultipleInput import ParameterMultipleInput

from sextante.gdal.GdalUtils import GdalUtils

class merge(GeoAlgorithm):

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

def processAlgorithm(self, progress):
if SextanteUtils.isWindows():
commands = ["cmd.exe", "/C ", "gdal_merge.bat"]
else:
commands = ["gdal_merge.py"]
arguments = []
if self.getParameterValue(merge.SEPARATE):
commands.append("-separate")
arguments.append("-separate")
if self.getParameterValue(merge.PCT):
commands.append("-pct")
commands.append("-o")
arguments.append("-pct")
arguments.append("-o")
out = self.getOutputValue(merge.OUTPUT)
commands.append(out)
commands.append("-of")
commands.append(GdalUtils.getFormatShortNameFromFilename(out))
commands.append(self.getParameterValue(merge.INPUT).replace(";", " "))
arguments.append(out)
arguments.append("-of")
arguments.append(GdalUtils.getFormatShortNameFromFilename(out))
arguments.append(self.getParameterValue(merge.INPUT).replace(";", " "))

commands = []
if SextanteUtils.isWindows():
commands = ["cmd.exe", "/C ", "gdal_merge.bat", GdalUtils.escapeAndJoin(arguments)]
else:
commands = ["gdal_merge.py", GdalUtils.escapeAndJoin(arguments)]

GdalUtils.runGdal(commands, progress)
20 changes: 11 additions & 9 deletions python/plugins/sextante/gdal/nearblack.py
Expand Up @@ -23,13 +23,15 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os
from PyQt4 import QtGui

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.parameters.ParameterRaster import ParameterRaster
from sextante.parameters.ParameterNumber import ParameterNumber
from sextante.parameters.ParameterBoolean import ParameterBoolean
from sextante.outputs.OutputRaster import OutputRaster
import os

from sextante.gdal.GdalUtils import GdalUtils

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

def processAlgorithm(self, progress):
commands = ["nearblack"]
commands.append("-o")
commands.append(self.getOutputValue(nearblack.OUTPUT))
commands.append("-near")
commands.append(str(self.getParameterValue(nearblack.NEAR)))
arguments = []
arguments.append("-o")
arguments.append(self.getOutputValue(nearblack.OUTPUT))
arguments.append("-near")
arguments.append(str(self.getParameterValue(nearblack.NEAR)))
if self.getParameterValue(nearblack.WHITE):
commands.append("-white")
commands.append(self.getParameterValue(nearblack.INPUT))
GdalUtils.runGdal(commands, progress)
arguments.append("-white")
arguments.append(self.getParameterValue(nearblack.INPUT))
GdalUtils.runGdal(["nearblack", GdalUtils.escapeAndJoin(arguments)], progress)
28 changes: 17 additions & 11 deletions python/plugins/sextante/gdal/ogr2ogr.py
Expand Up @@ -23,22 +23,28 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os
import re
import string
from string import Template
import tempfile

from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *

from sextante.outputs.OutputVector import OutputVector
from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterString import ParameterString
from sextante.core.SextanteLog import SextanteLog
from sextante.core.QGisLayers import QGisLayers

from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterString import ParameterString
from sextante.outputs.OutputVector import OutputVector

from sextante.gdal.OgrAlgorithm import OgrAlgorithm
from sextante.gdal.pyogr.ogr2ogr import *
from qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import string
from string import Template
import re
import os
import tempfile

from sextante.gdal.GdalUtils import GdalUtils

try:
from osgeo import gdal, ogr, osr
Expand Down
30 changes: 18 additions & 12 deletions python/plugins/sextante/gdal/pct2rgb.py
Expand Up @@ -23,14 +23,17 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os
from PyQt4 import QtGui

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.SextanteUtils import SextanteUtils

from sextante.parameters.ParameterRaster import ParameterRaster
from sextante.parameters.ParameterSelection import ParameterSelection
from sextante.outputs.OutputRaster import OutputRaster
import os

from sextante.gdal.GdalUtils import GdalUtils
from sextante.parameters.ParameterSelection import ParameterSelection
from sextante.core.SextanteUtils import SextanteUtils

class pct2rgb(GeoAlgorithm):

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

def processAlgorithm(self, progress):
arguments = []
arguments.append("-b")
arguments.append(str(self.getParameterValue(pct2rgb.NBAND) + 1))
arguments.append("-of")
out = self.getOutputValue(pct2rgb.OUTPUT)
arguments.append(GdalUtils.getFormatShortNameFromFilename(out))
arguments.append(self.getParameterValue(pct2rgb.INPUT))
arguments.append(out)

commands = []
if SextanteUtils.isWindows():
commands = ["cmd.exe", "/C ", "pct2rgb.bat"]
commands = ["cmd.exe", "/C ", "pct2rgb.bat", GdalUtils.escapeAndJoin(arguments)]
else:
commands = ["pct2rgb.py"]
commands.append("-b")
commands.append(str(self.getParameterValue(pct2rgb.NBAND) + 1))
commands.append("-of")
out = self.getOutputValue(pct2rgb.OUTPUT)
commands.append(GdalUtils.getFormatShortNameFromFilename(out))
commands.append(self.getParameterValue(pct2rgb.INPUT))
commands.append(out)
commands = ["pct2rgb.py", GdalUtils.escapeAndJoin(arguments)]

GdalUtils.runGdal(commands, progress)
30 changes: 18 additions & 12 deletions python/plugins/sextante/gdal/polygonize.py
Expand Up @@ -23,14 +23,17 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os
from PyQt4 import QtGui, QtCore

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.SextanteUtils import SextanteUtils

from sextante.parameters.ParameterRaster import ParameterRaster
import os
from sextante.gdal.GdalUtils import GdalUtils
from sextante.parameters.ParameterString import ParameterString
from sextante.outputs.OutputVector import OutputVector
from sextante.core.SextanteUtils import SextanteUtils

from sextante.gdal.GdalUtils import GdalUtils

class polygonize(GeoAlgorithm):

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

def processAlgorithm(self, progress):
arguments = []
arguments.append(self.getParameterValue(polygonize.INPUT))
arguments.append('-f')
arguments.append('"ESRI Shapefile"')
output = self.getOutputValue(polygonize.OUTPUT)
arguments.append(output)
arguments.append(QtCore.QFileInfo(output).baseName())
arguments.append(self.getParameterValue(polygonize.FIELD))

commands = []
if SextanteUtils.isWindows():
commands = ["cmd.exe", "/C ", "gdal_polygonize.bat"]
commands = ["cmd.exe", "/C ", "gdal_polygonize.bat", GdalUtils.escapeAndJoin(arguments)]
else:
commands = ["gdal_polygonize.py"]
commands.append(self.getParameterValue(polygonize.INPUT))
commands.append('-f')
commands.append('"ESRI Shapefile"')
output = self.getOutputValue(polygonize.OUTPUT)
commands.append(output)
commands.append(QtCore.QFileInfo(output).baseName())
commands.append(self.getParameterValue(polygonize.FIELD))
commands = ["gdal_polygonize.py", GdalUtils.escapeAndJoin(arguments)]

GdalUtils.runGdal(commands, progress)
29 changes: 17 additions & 12 deletions python/plugins/sextante/gdal/rgb2pct.py
Expand Up @@ -23,14 +23,17 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os
from PyQt4 import QtGui

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.SextanteUtils import SextanteUtils

from sextante.parameters.ParameterRaster import ParameterRaster
from sextante.parameters.ParameterNumber import ParameterNumber
from sextante.outputs.OutputRaster import OutputRaster
import os

from sextante.gdal.GdalUtils import GdalUtils
from sextante.parameters.ParameterNumber import ParameterNumber
from sextante.core.SextanteUtils import SextanteUtils

class rgb2pct(GeoAlgorithm):

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

def processAlgorithm(self, progress):
arguments = []
arguments.append("-n")
arguments.append(str(self.getParameterValue(rgb2pct.NCOLORS)))
arguments.append("-of")
out = self.getOutputValue(rgb2pct.OUTPUT)
arguments.append(GdalUtils.getFormatShortNameFromFilename(out))
arguments.append(self.getParameterValue(rgb2pct.INPUT))
arguments.append(out)

if SextanteUtils.isWindows():
commands = ["cmd.exe", "/C ", "rgb2pct.bat"]
commands = ["cmd.exe", "/C ", "rgb2pct.bat", GdalUtils.escapeAndJoin(arguments)]
else:
commands = ["rgb2pct.py"]
commands.append("-n")
commands.append(str(self.getParameterValue(rgb2pct.NCOLORS)))
commands.append("-of")
out = self.getOutputValue(rgb2pct.OUTPUT)
commands.append(GdalUtils.getFormatShortNameFromFilename(out))
commands.append(self.getParameterValue(rgb2pct.INPUT))
commands.append(out)
commands = ["rgb2pct.py", GdalUtils.escapeAndJoin(arguments)]

GdalUtils.runGdal(commands, progress)

0 comments on commit dc15292

Please sign in to comment.