Skip to content

Commit 69bc4a0

Browse files
author
volayaf
committedApr 10, 2012
did some work on the gdal bindings
git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@68 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
1 parent f499c85 commit 69bc4a0

File tree

12 files changed

+290
-8
lines changed

12 files changed

+290
-8
lines changed
 

‎src/sextante/core/GeoAlgorithm.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
1111
import traceback
1212
from sextante.core.SextanteLog import SextanteLog
13+
from sextante.outputs.OutputVector import OutputVector
14+
from sextante.outputs.OutputRaster import OutputRaster
15+
from sextante.outputs.OutputTable import OutputTable
16+
from sextante.outputs.OutputHTML import OutputHTML
1317

1418
class GeoAlgorithm:
1519

@@ -72,6 +76,7 @@ def execute(self, progress):
7276
Raises a GeoAlgorithmExecutionException in case anything goes wrong.'''
7377
self.setOutputCRSFromInputLayers()
7478
self.resolveTemporaryOutputs()
79+
self.checkOutputFileExtensions()
7580
try:
7681
self.processAlgorithm(progress)
7782
return not self.canceled
@@ -87,6 +92,29 @@ def execute(self, progress):
8792
SextanteLog.addToLog(SextanteLog.LOG_ERROR, lines)
8893
raise GeoAlgorithmExecutionException(str(e))
8994

95+
def checkOutputFileExtensions(self):
96+
'''Checks if the values of outputs are correct and have one of the supported output extensions.
97+
If not, it adds the first one of the supported extensions, which is assumed to be the default one'''
98+
for out in self.outputs:
99+
if (not out.hidden) and out.value != None:
100+
if isinstance(out, OutputRaster):
101+
exts = self.provider.getSupportedOutputRasterLayerExtensions()
102+
elif isinstance(out, OutputVector):
103+
exts = self.provider.getSupportedOutputVectorLayerExtensions()
104+
elif isinstance(out, OutputTable):
105+
exts = self.provider.getSupportedOutputTableExtensions()
106+
elif isinstance(out, OutputHTML):
107+
exts =["html", "htm"]
108+
else:
109+
continue
110+
idx = out.value.rfind(".")
111+
if idx == -1:
112+
out.value = out.value + "." + exts[0]
113+
else:
114+
ext = out.value[idx + 1:]
115+
if ext not in exts:
116+
out.value = out.value + "." + exts[0]
117+
90118
def resolveTemporaryOutputs(self):
91119
'''sets temporary outputs (output.value = None) with a temporary file instead'''
92120
for out in self.outputs:

‎src/sextante/core/SextanteConfig.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def initialize():
1818
SextanteConfig.addSetting(Setting("General", SextanteConfig.USE_SELECTED, "Use only selected features in external application", True))
1919
SextanteConfig.addSetting(Setting("General", SextanteConfig.SHOW_RECENT_ALGORITHMS, "Show recently executed algorithms", True))
2020
SextanteConfig.addSetting(Setting("General", SextanteConfig.OUTPUT_FOLDER,
21-
"Output folder", os.path.join(SextanteUtils.userFolder(),"outputs" )))
21+
"Output folder", SextanteUtils.tempFolder()))
2222
SextanteConfig.addSetting(Setting("General", SextanteConfig.RASTER_STYLE,"Style for raster layers",""))
2323
SextanteConfig.addSetting(Setting("General", SextanteConfig.VECTOR_POINT_STYLE,"Style for point layers",""))
2424
SextanteConfig.addSetting(Setting("General", SextanteConfig.VECTOR_LINE_STYLE,"Style for line layers",""))

‎src/sextante/gdal/GdalAlgorithmProvider.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
from sextante.gdal.GdalAlgorithm import GdalAlgorithm
88
from sextante.gdal.nearblack import nearblack
99
from sextante.gdal.information import information
10+
from sextante.gdal.GdalUtils import GdalUtils
11+
from sextante.gdal.warp import warp
12+
from sextante.gdal.rgb2pct import rgb2pct
13+
from sextante.gdal.translate import translate
14+
from sextante.gdal.pct2rgb import pct2rgb
15+
from sextante.gdal.merge import merge
1016

1117
class GdalAlgorithmProvider(AlgorithmProvider):
1218

@@ -44,7 +50,7 @@ def _loadAlgorithms(self):
4450
def createAlgsList(self):
4551
#First we populate the list of algorihtms with those created extending
4652
#GeoAlgorithm directly (those that execute GDAL using the console)
47-
self.preloadedAlgs = [nearblack(), information()]
53+
self.preloadedAlgs = [nearblack(), information(), warp(), translate(), rgb2pct(), pct2rgb(), merge()]
4854
#And then we add those that are created as python scripts
4955
folder = self.scriptsFolder()
5056
for descriptionFile in os.listdir(folder):
@@ -55,3 +61,6 @@ def createAlgsList(self):
5561
self.preloadedAlgs.append(alg)
5662
except WrongScriptException,e:
5763
SextanteLog.addToLog(SextanteLog.LOG_ERROR,e.msg)
64+
65+
def getSupportedOutputRasterLayerExtensions(self):
66+
return GdalUtils.getSupportedRasterExtensions()

‎src/sextante/gdal/GdalUtils.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
from PyQt4.QtCore import *
2+
from PyQt4.QtGui import *
23
import subprocess
34
from sextante.core.SextanteLog import SextanteLog
45
import os
6+
import gdal
57

68
class GdalUtils():
79

10+
supportedRasters = None
11+
812
@staticmethod
913
def runGdal(commands, progress):
1014
settings = QSettings()
@@ -25,4 +29,44 @@ def runGdal(commands, progress):
2529
def getConsoleOutput():
2630
return GdalUtils.consoleOutput
2731

32+
@staticmethod
33+
def getSupportedRasters():
34+
'''this has been adapted from GdalTools plugin'''
35+
if GdalUtils.supportedRasters != None:
36+
return GdalUtils.supportedRasters
37+
38+
if gdal.GetDriverCount() == 0:
39+
gdal.AllRegister()
40+
41+
GdalUtils.supportedRasters = {}
42+
for i in range(gdal.GetDriverCount()):
43+
driver = gdal.GetDriver(i)
44+
if driver == None:
45+
continue
46+
47+
shortName = str(QString(driver.ShortName).remove( QRegExp( '\(.*$' ) ).trimmed())
48+
metadata = driver.GetMetadata()
49+
if metadata.has_key(gdal.DMD_EXTENSION):
50+
extensions = metadata[gdal.DMD_EXTENSION].split("/")
51+
if extensions:
52+
GdalUtils.supportedRasters[shortName] = extensions
2853

54+
return GdalUtils.supportedRasters
55+
56+
@staticmethod
57+
def getSupportedRasterExtensions():
58+
allexts = []
59+
for exts in GdalUtils.getSupportedRasters().values():
60+
for ext in exts:
61+
allexts.append(ext)
62+
return allexts
63+
64+
@staticmethod
65+
def getFormatShortNameFromFilename(filename):
66+
ext = filename[filename.rfind(".")+1:]
67+
supported = GdalUtils.getSupportedRasters()
68+
for name in supported.keys():
69+
exts = supported[name]
70+
if ext in exts:
71+
return name
72+
return "GTiff"

‎src/sextante/gdal/merge.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from PyQt4 import QtGui
2+
from sextante.core.GeoAlgorithm import GeoAlgorithm
3+
from sextante.outputs.OutputRaster import OutputRaster
4+
import os
5+
from sextante.gdal.GdalUtils import GdalUtils
6+
from sextante.core.SextanteUtils import SextanteUtils
7+
from sextante.parameters.ParameterBoolean import ParameterBoolean
8+
from sextante.parameters.ParameterMultipleInput import ParameterMultipleInput
9+
10+
class merge(GeoAlgorithm):
11+
12+
INPUT = "INPUT"
13+
OUTPUT = "OUTPUT"
14+
PCT = "PCT"
15+
SEPARATE = "SEPARATE"
16+
17+
def getIcon(self):
18+
filepath = os.path.dirname(__file__) + "/icons/merge.png"
19+
return QtGui.QIcon(filepath)
20+
21+
def defineCharacteristics(self):
22+
self.name = "rgb2pct"
23+
self.group = "Conversion"
24+
self.addParameter(ParameterMultipleInput(merge.INPUT, "Input layers", ParameterMultipleInput.TYPE_RASTER))
25+
self.addParameter(ParameterBoolean(merge.PCT, "Grab pseudocolor table from first layer", False))
26+
self.addParameter(ParameterBoolean(merge.SEPARATE, "Layer stack", False))
27+
self.addOutput(OutputRaster(merge.OUTPUT, "Output layer"))
28+
29+
def processAlgorithm(self, progress):
30+
if SextanteUtils.isWindows():
31+
commands = ["cmd.exe", "/C ", "merge.bat"]
32+
else:
33+
commands = ["merge.py"]
34+
if self.getParameterValue(merge.SEPARATE):
35+
commands.append("-separate")
36+
if self.getParameterValue(merge.PCT):
37+
commands.append("-pct")
38+
commands.append("-of")
39+
out = self.getOutputValue(merge.OUTPUT)
40+
commands.append(GdalUtils.getFormatShortNameFromFilename(out))
41+
commands.append(self.getParameterValue(merge.INPUT).replace(";", " "))
42+
commands.append(out)
43+
44+
GdalUtils.runGdal(commands, progress)

‎src/sextante/gdal/pct2rgb.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from PyQt4 import QtGui
2+
from sextante.core.GeoAlgorithm import GeoAlgorithm
3+
from sextante.parameters.ParameterRaster import ParameterRaster
4+
from sextante.outputs.OutputRaster import OutputRaster
5+
import os
6+
from sextante.gdal.GdalUtils import GdalUtils
7+
from sextante.parameters.ParameterSelection import ParameterSelection
8+
from sextante.core.SextanteUtils import SextanteUtils
9+
10+
class pct2rgb(GeoAlgorithm):
11+
12+
INPUT = "INPUT"
13+
OUTPUT = "OUTPUT"
14+
NBAND = "NBAND"
15+
16+
def getIcon(self):
17+
filepath = os.path.dirname(__file__) + "/icons/8-to-24-bits.png"
18+
return QtGui.QIcon(filepath)
19+
20+
def defineCharacteristics(self):
21+
self.name = "pct2rgb"
22+
self.group = "Conversion"
23+
self.addParameter(ParameterRaster(pct2rgb.INPUT, "Input layer", False))
24+
self.addParameter(ParameterSelection(pct2rgb.NBAND, "Band to convert", range(25)))
25+
self.addOutput(OutputRaster(pct2rgb.OUTPUT, "Output layer"))
26+
27+
def processAlgorithm(self, progress):
28+
if SextanteUtils.isWindows():
29+
commands = ["cmd.exe", "/C ", "pct2rgb.bat"]
30+
else:
31+
commands = ["pct2rgb.py"]
32+
commands.append("-b")
33+
commands.append(str(self.getParameterValue(pct2rgb.NBAND) + 1))
34+
commands.append("-of")
35+
out = self.getOutputValue(pct2rgb.OUTPUT)
36+
commands.append(GdalUtils.getFormatShortNameFromFilename(out))
37+
commands.append(self.getParameterValue(pct2rgb.INPUT))
38+
commands.append(out)
39+
40+
GdalUtils.runGdal(commands, progress)

‎src/sextante/gdal/rgb2pct.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from PyQt4 import QtGui
2+
from sextante.core.GeoAlgorithm import GeoAlgorithm
3+
from sextante.parameters.ParameterRaster import ParameterRaster
4+
from sextante.outputs.OutputRaster import OutputRaster
5+
import os
6+
from sextante.gdal.GdalUtils import GdalUtils
7+
from sextante.parameters.ParameterNumber import ParameterNumber
8+
from sextante.core.SextanteUtils import SextanteUtils
9+
10+
class rgb2pct(GeoAlgorithm):
11+
12+
INPUT = "INPUT"
13+
OUTPUT = "OUTPUT"
14+
NCOLORS = "NCOLORS"
15+
16+
def getIcon(self):
17+
filepath = os.path.dirname(__file__) + "/icons/24-to-8-bits.png"
18+
return QtGui.QIcon(filepath)
19+
20+
def defineCharacteristics(self):
21+
self.name = "rgb2pct"
22+
self.group = "Conversion"
23+
self.addParameter(ParameterRaster(rgb2pct.INPUT, "Input layer", False))
24+
self.addParameter(ParameterNumber(rgb2pct.NCOLORS, "Number of colors", 1, None, 2))
25+
self.addOutput(OutputRaster(rgb2pct.OUTPUT, "Output layer"))
26+
27+
def processAlgorithm(self, progress):
28+
if SextanteUtils.isWindows():
29+
commands = ["cmd.exe", "/C ", "rgb2pct.bat"]
30+
else:
31+
commands = ["rgb2pct.py"]
32+
commands.append("-n")
33+
commands.append(str(self.getParameterValue(rgb2pct.NCOLORS)))
34+
commands.append("-of")
35+
out = self.getOutputValue(rgb2pct.OUTPUT)
36+
commands.append(GdalUtils.getFormatShortNameFromFilename(out))
37+
commands.append(self.getParameterValue(rgb2pct.INPUT))
38+
commands.append(out)
39+
40+
GdalUtils.runGdal(commands, progress)

‎src/sextante/gdal/scripts/fillnodata.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
##src_filename=raster
55
##dst_filename=output raster
66
##Analysis=group
7+
from sextante.gdal.GdalUtils import GdalUtils
78

89
try:
910
from osgeo import gdal, ogr
@@ -23,12 +24,11 @@ def CopyBand( srcband, dstband ):
2324
options = []
2425
src_band = 1
2526

26-
format = 'GTiff'
2727
gdal.AllRegister()
2828
src_ds = gdal.Open(src_filename, gdal.GA_ReadOnly)
2929
srcband = src_ds.GetRasterBand(src_band)
3030
maskband = srcband.GetMaskBand()
31-
drv = gdal.GetDriverByName(format)
31+
drv = gdal.GetDriverByName(GdalUtils.getFormatShortNameFromFilename(dst_filename))
3232
dst_ds = drv.Create(dst_filename,src_ds.RasterXSize, src_ds.RasterYSize,1,
3333
srcband.DataType)
3434
wkt = src_ds.GetProjection()

‎src/sextante/gdal/scripts/proximity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
##nodata=number 0
1010
##distunits=selection PIXEL;GEO
1111
##fixed_buf_val=number 0
12+
from sextante.gdal.GdalUtils import GdalUtils
1213
try:
1314
from osgeo import gdal
1415
except ImportError:
1516
import gdal
1617

17-
format = 'GTiff'
1818
creation_options = []
1919
options = []
2020
src_band_n = 1
@@ -49,7 +49,7 @@
4949
# Create output file.
5050
# =============================================================================
5151
if dst_ds is None:
52-
drv = gdal.GetDriverByName(format)
52+
drv = gdal.GetDriverByName(GdalUtils.getFormatShortNameFromFilename(dst_filename))
5353
dst_ds = drv.Create( dst_filename,
5454
src_ds.RasterXSize, src_ds.RasterYSize, 1,
5555
gdal.GetDataTypeByName(creation_type) )

‎src/sextante/gdal/scripts/sieve.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
##src_filename=raster
66
##dst_filename=output raster
77
##connectedness=selection 4;8
8+
from sextante.gdal.GdalUtils import GdalUtils
89

910
try:
1011
from osgeo import gdal, ogr
@@ -15,13 +16,12 @@
1516
threshold = 2
1617
connectedness=int(connectedness)
1718
options = []
18-
format = 'GTiff'
1919

2020
src_ds = gdal.Open( src_filename, gdal.GA_ReadOnly )
2121
srcband = src_ds.GetRasterBand(1)
2222
maskband = srcband.GetMaskBand()
2323

24-
drv = gdal.GetDriverByName(format)
24+
drv = gdal.GetDriverByName(GdalUtils.getFormatShortNameFromFilename(dst_filename))
2525
dst_ds = drv.Create( dst_filename,src_ds.RasterXSize, src_ds.RasterYSize,1,
2626
srcband.DataType )
2727
wkt = src_ds.GetProjection()

‎src/sextante/gdal/translate.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from PyQt4 import QtGui
2+
from sextante.core.GeoAlgorithm import GeoAlgorithm
3+
from sextante.parameters.ParameterRaster import ParameterRaster
4+
from sextante.outputs.OutputRaster import OutputRaster
5+
import os
6+
from sextante.gdal.GdalUtils import GdalUtils
7+
8+
class translate(GeoAlgorithm):
9+
10+
INPUT = "INPUT"
11+
OUTPUT = "OUTPUT"
12+
13+
def getIcon(self):
14+
filepath = os.path.dirname(__file__) + "/icons/translate.png"
15+
return QtGui.QIcon(filepath)
16+
17+
def defineCharacteristics(self):
18+
self.name = "translate"
19+
self.group = "Conversion"
20+
self.addParameter(ParameterRaster(translate.INPUT, "Input layer", False))
21+
self.addOutput(OutputRaster(translate.OUTPUT, "Output layer"))
22+
23+
def processAlgorithm(self, progress):
24+
commands = ["gdal_translate"]
25+
commands.append("-of")
26+
out = self.getOutputValue(translate.OUTPUT)
27+
commands.append(GdalUtils.getFormatShortNameFromFilename(out))
28+
commands.append(self.getParameterValue(translate.INPUT))
29+
commands.append(out)
30+
31+
GdalUtils.runGdal(commands, progress)

‎src/sextante/gdal/warp.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from PyQt4 import QtGui
2+
from sextante.core.GeoAlgorithm import GeoAlgorithm
3+
from sextante.parameters.ParameterRaster import ParameterRaster
4+
from sextante.outputs.OutputRaster import OutputRaster
5+
import os
6+
from sextante.gdal.GdalUtils import GdalUtils
7+
from sextante.parameters.ParameterString import ParameterString
8+
from sextante.parameters.ParameterSelection import ParameterSelection
9+
10+
class warp(GeoAlgorithm):
11+
12+
INPUT = "INPUT"
13+
OUTPUT = "OUTPUT"
14+
SOURCE_SRS = "SOURCE_SRS"
15+
DEST_SRS = "DEST_SRS "
16+
METHOD = "METHOD"
17+
METHOD_OPTIONS = ["near", "bilinear", "cubic", "cubicspline", "lanczos"]
18+
19+
def getIcon(self):
20+
filepath = os.path.dirname(__file__) + "/icons/warp.png"
21+
return QtGui.QIcon(filepath)
22+
23+
def defineCharacteristics(self):
24+
self.name = "warp"
25+
self.group = "Projections"
26+
self.addParameter(ParameterRaster(warp.INPUT, "Input layer", False))
27+
self.addParameter(ParameterString(warp.SOURCE_SRS, "Source SRS (EPSG Code)", "4326"))
28+
self.addParameter(ParameterString(warp.DEST_SRS, "Destination SRS (EPSG Code)", "4326"))
29+
self.addParameter(ParameterSelection(warp.METHOD, "Resampling method", warp.METHOD_OPTIONS))
30+
self.addOutput(OutputRaster(warp.OUTPUT, "Output layer"))
31+
32+
def processAlgorithm(self, progress):
33+
commands = ["gdalwarp"]
34+
commands.append("-s_srs")
35+
commands.append("EPSG:" + self.getParameterValue(warp.SOURCE_SRS))
36+
commands.append("-t_srs")
37+
commands.append("EPSG:" + self.getParameterValue(warp.DEST_SRS))
38+
commands.append("-r")
39+
commands.append(warp.METHOD_OPTIONS[self.getParameterValue(warp.METHOD)])
40+
commands.append("-of")
41+
out = self.getOutputValue(warp.OUTPUT)
42+
commands.append(GdalUtils.getFormatShortNameFromFilename(out))
43+
commands.append(self.getParameterValue(warp.INPUT))
44+
commands.append(out)
45+
46+
GdalUtils.runGdal(commands, progress)

0 commit comments

Comments
 (0)
Please sign in to comment.