/
ScriptAlgorithm.py
108 lines (93 loc) · 4.23 KB
/
ScriptAlgorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.parameters.ParameterRaster import ParameterRaster
from sextante.parameters.ParameterTable import ParameterTable
from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterMultipleInput import ParameterMultipleInput
from sextante.script.WrongScriptException import WrongScriptException
from sextante.outputs.OutputTable import OutputTable
from sextante.outputs.OutputVector import OutputVector
from sextante.outputs.OutputRaster import OutputRaster
from sextante.parameters.ParameterString import ParameterString
from sextante.parameters.ParameterNumber import ParameterNumber
from sextante.parameters.ParameterBoolean import ParameterBoolean
from sextante.script.ScriptUtils import ScriptUtils
import os
from sextante.core.SextanteUtils import SextanteUtils
from sextante.parameters.ParameterDataObject import ParameterDataObject
from sextante.parameters.ParameterRange import ParameterRange
class ScriptAlgorithm(GeoAlgorithm):
def __init__(self, descriptionfile):
GeoAlgorithm.__init__(self)
self.descriptionFile = descriptionfile
self.defineCharacteristicsFromFile()
self.providerName = "script:"
def defineCharacteristicsFromFile(self):
self.script=""
self.name = self.descriptionFile[:self.descriptionFile.rfind(".")]
fullpath = os.path.join(ScriptUtils.scriptsFolder(), self.descriptionFile)
self.group = "User scripts"
lines = open(fullpath)
line = lines.readline()
while line != "":
if line.startswith("##"):
self.processParameterLine(line.strip("\n"))
self.script += line
line = lines.readline()
lines.close()
def processParameterLine(self,line):
param = None
out = None
line = line.replace("#", "");
tokens = line.split("=");
if tokens[1].lower() == "raster":
param = ParameterRaster()
param.optional = False
elif tokens[1].lower() == "vector":
param = ParameterRaster()
param.optional = False
param.shapetype = ParameterVector.VECTOR_TYPE_ANY
elif tokens[1].lower() == "table":
param = ParameterTable()
param.optional = False
elif tokens[1].lower() == "multiple raster":
param = ParameterMultipleInput();
param.datatype=ParameterMultipleInput.TYPE_RASTER
param.optional = False
elif tokens[1].lower() == "multiple vector":
param = ParameterMultipleInput();
param.datatype=ParameterMultipleInput.TYPE_VECTOR_ANY
param.optional = False
elif tokens[1].lower() == "boolean":
param = ParameterBoolean()
elif tokens[1].lower() == "number":
param = ParameterNumber()
elif tokens[1].lower() == "string":
param = ParameterString()
elif tokens[1].lower() == "output raster":
out = OutputRaster()
elif tokens[1].lower() == "output vector":
out = OutputVector()
elif tokens[1].lower() == "output table":
out = OutputTable()
if param != None:
param.name = tokens[0]
param.description = tokens[0]
self.putParameter(param)
elif out != None:
out.name = tokens[0]
out.description = tokens[0]
self.putOutput(out)
else:
raise WrongScriptException("Could not load script:" + self.descriptionFile + ". Problem with line \"" + line + "\"")
def processAlgorithm(self, progress):
#resolve temporary output files
for out in self.outputs:
if out.channel == None:
SextanteUtils.setTempOutput(out)
script = "from sextante.core.Sextante import Sextante\n"
for param in self.parameters:
script += param.name + "=" + param.getValueAsCommandLineParameter() + "\n"
for out in self.outputs:
script += out.name + "=" + out.getChannelAsCommandLineParameter() + "\n"
script+=self.script
exec(script)