/
SagaUtils.py
164 lines (141 loc) · 6.51 KB
/
SagaUtils.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# -*- coding: utf-8 -*-
"""
***************************************************************************
SagaUtils.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
from sextante.tests.TestData import points
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import os
import stat
import traceback
import subprocess
from sextante.core.SextanteUtils import SextanteUtils
from sextante.core.SextanteConfig import SextanteConfig
from sextante.core.SextanteLog import SextanteLog
from qgis.core import *
from PyQt4.QtCore import *
from sextante.core.SextanteVectorWriter import SextanteVectorWriter
from sextante.core.QGisLayers import QGisLayers
class SagaUtils:
SAGA_LOG_COMMANDS = "SAGA_LOG_COMMANDS"
SAGA_LOG_CONSOLE = "SAGA_LOG_CONSOLE"
SAGA_AUTO_RESAMPLING = "SAGA_AUTO_RESAMPLING"
SAGA_RESAMPLING_REGION_XMIN = "SAGA_RESAMPLING_REGION_XMIN"
SAGA_RESAMPLING_REGION_YMIN = "SAGA_RESAMPLING_REGION_YMIN"
SAGA_RESAMPLING_REGION_XMAX = "SAGA_RESAMPLING_REGION_XMAX"
SAGA_RESAMPLING_REGION_YMAX = "SAGA_RESAMPLING_REGION_YMAX"
SAGA_RESAMPLING_REGION_CELLSIZE = "SAGA_RESAMPLING_REGION_CELLSIZE"
SAGA_FOLDER = "SAGA_FOLDER"
@staticmethod
def sagaBatchJobFilename():
if SextanteUtils.isWindows():
filename = "saga_batch_job.bat";
else:
filename = "saga_batch_job.sh";
batchfile = SextanteUtils.userFolder() + os.sep + filename
return batchfile
@staticmethod
def sagaPath():
folder = SextanteConfig.getSetting(SagaUtils.SAGA_FOLDER)
if folder == None:
folder =""
return folder
@staticmethod
def sagaDescriptionPath():
return os.path.join(os.path.dirname(__file__),"description")
@staticmethod
def createSagaBatchJobFileFromSagaCommands(commands):
fout = open(SagaUtils.sagaBatchJobFilename(), "w")
if SextanteUtils.isWindows():
fout.write("set SAGA=" + SagaUtils.sagaPath() + "\n");
fout.write("set SAGA_MLB=" + SagaUtils.sagaPath()+ os.sep + "modules" + "\n");
fout.write("PATH=PATH;%SAGA%;%SAGA_MLB%\n");
else:
pass
for command in commands:
fout.write("saga_cmd " + command.encode("utf8") + "\n")
fout.write("exit")
fout.close()
@staticmethod
def executeSaga(progress):
if SextanteUtils.isWindows():
command = ["cmd.exe", "/C ", SagaUtils.sagaBatchJobFilename()]
else:
os.chmod(SagaUtils.sagaBatchJobFilename(), stat.S_IEXEC | stat.S_IREAD | stat.S_IWRITE)
command = [SagaUtils.sagaBatchJobFilename()]
loglines = []
loglines.append("SAGA execution console output")
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.STDOUT, universal_newlines=True).stdout
for line in iter(proc.readline, ""):
if "%" in line:
s = "".join([x for x in line if x.isdigit()])
try:
progress.setPercentage(int(s))
except:
pass
else:
line = line.strip()
if line!="/" and line!="-" and line !="\\" and line!="|":
loglines.append(line)
progress.setConsoleInfo(line)
if SextanteConfig.getSetting(SagaUtils.SAGA_LOG_CONSOLE):
SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
@classmethod
def checkSagaIsInstalled(cls):
if SextanteUtils.isWindows():
path = SagaUtils.sagaPath()
if path == "":
return "SAGA folder is not configured.\nPlease configure it before running SAGA algorithms."
cmdpath = os.path.join(path, "saga_cmd.exe")
if not os.path.exists(cmdpath):
return ("The specified SAGA folder does not contain a valid SAGA executable.\n"
+ "Please, go to the SEXTANTE settings dialog, and check that the SAGA\n"
+ "folder is correctly configured")
SAGA_INSTALLED = "/SextanteQGIS/SagaInstalled"
settings = QSettings()
if settings.contains(SAGA_INSTALLED):
return
try:
#===================================================================
# qgis = QGisLayers.iface
# crs = qgis.mapCanvas().mapRenderer().destinationCrs()
# fields = []
# fields.append(QgsField("NUM_FIELD", QVariant.Int))
# filename = SextanteUtils.getTempFilename("shp")
# writer = SextanteVectorWriter(filename, None, fields, QGis.WKBPoint, crs)
# for x in range(5):
# for y in range(5):
# attrs = []
# attrs.append(QVariant(x))
# outFeat = QgsFeature()
# pt = QgsPoint(x, y)
# outFeat.setGeometry(QgsGeometry.fromPoint(pt))
# outFeat.setAttributes(attrs)
# writer.addFeature(outFeat)
# del writer.writer
# del writer
#===================================================================
from sextante.core.Sextante import runalg
result = runalg("saga:thiessenpolygons", points(), None)
if not os.path.exists(result['POLYGONS']):
return "It seems that SAGA is not correctly installed in your system.\nPlease install it before running SAGA algorithms."
except:
s = traceback.format_exc()
return "Error while checking SAGA installation. SAGA might not be correctly configured.\n" + s;
settings.setValue("/SextanteQGIS/SagaInstalled", True)