forked from geopython/pywps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grass.py
185 lines (156 loc) · 6.63 KB
/
Grass.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"""
Module is here for work with GRASS GIS environmental variables and
locations and mapsets
"""
# Author: Jachym Cepicky
# http://les-ejk.cz
# License:
#
# Web Processing Service implementation
# Copyright (C) 2006 Jachym Cepicky
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import os
import time, shutil, tempfile
import sys
from pywps import config
import logging
class Grass:
""" GRASS initialization interface """
locationDir = ""
locationName = ""
mapsetDir = ""
mapsetName = ""
gisbase = ""
def __init__(self,executeRequest):
""" Initialization of GRASS environmental variables (except GISRC). """
self.executeRequest = executeRequest
self.wps = self.executeRequest.wps
self.envs = {
"path":"PATH",
"addonPath":"GRASS_ADDON_PATH",
"version":"GRASS_VERSION",
"gui":"GRASS_GUI",
"gisbase": "GISBASE",
"ldLibraryPath": "LD_LIBRARY_PATH"
}
# put env
for key in self.envs.keys():
try:
self.setEnv(self.envs[key],config.getConfigValue("grass",key))
logging.info("GRASS environment variable %s set to %s" %\
(key, config.getConfigValue("grass",key)))
except :
logging.info("GRASS environment variable %s set to %s" %\
(key, self.envs[key]))
pass
# GIS_LOCK
self.setEnv('GIS_LOCK',str(os.getpid()))
logging.info("GRASS GIS_LOCK set to %s" % str(os.getpid()))
def mkMapset(self,location=None):
"""
Create GRASS mapset in current directory. Mapsets name is 'mapset'.
At the end, GRASS will believe, it has run correctly.
Returns name of new created mapset. location!=None, this mapset
should be deleted!
Arguments:
location - Should the new mapset be created in the some old
location, which is already on this server?
Default: only mapset within
/tmp/grasstmpSOMEHTIN/
will be created
"""
if location == None:
self.locationDir = self.executeRequest.workingDir
self.mapsetDir = tempfile.mkdtemp(prefix="pywps",dir=self.locationDir)
self.mapsetName = os.path.split(self.mapsetDir)[1]
self.locationName = os.path.split(self.locationDir)[1]
# create new WIND file
self._windFile(self.mapsetName)
# create mapset PERMANENT
os.mkdir("PERMANENT")
self._windFile("PERMANENT")
self.gisdbase = os.path.split(os.path.abspath(os.path.curdir))[0]
# location is here, we justhave to use it
else:
self.locationDir = os.path.join(config.getConfigValue("grass","gisdbase"), location)
self.mapsetDir = tempfile.mkdtemp(prefix="pywps",dir=self.locationDir)
self.mapsetName = os.path.split(self.mapsetDir)[1]
self.locationName = os.path.split(location)[-1]
self.executeRequest.dirsToBeRemoved.append(os.path.abspath(self.mapsetDir))
# copy
shutil.copy(os.path.join(
self.locationDir,"PERMANENT","DEFAULT_WIND"),
os.path.join(self.mapsetDir,"WIND"))
# export env. vars
(self.gisdbase,location) = os.path.split(self.locationDir)
# GRASS creates a temp dir for the display driver.
# Add it to dirsToBeRemoved
try:
grassTmpDir = os.path.join(tempfile.gettempdir(),
"grass"+config.getConfigValue("grass","version")[:1]+\
"-"+os.getenv("USERNAME")+\
"-"+str(os.getpid()))
self.executeRequest.dirsToBeRemoved.append(grassTmpDir)
except :
pass
self.setEnv('MAPSET', self.mapsetName)
self.setEnv('LOCATION_NAME',self.locationName)
self.setEnv('GISDBASE', self.gisdbase)
# gisrc
gisrc = open(os.path.join(self.executeRequest.workingDir,"grassrc"),"w")
gisrc.write("LOCATION_NAME: %s\n" % self.locationName)
gisrc.write("MAPSET: %s\n" % self.mapsetName)
gisrc.write("DIGITIZER: none\n")
gisrc.write("GISDBASE: %s\n" % self.gisdbase)
gisrc.write("OVERWRITE: 1\n")
gisrc.write("GRASS_GUI: text\n")
gisrc.close()
logging.info("GRASS MAPSET set to %s" % self.mapsetName)
logging.info("GRASS LOCATION_NAME set to %s" % self.locationName)
logging.info("GRASS GISDBASE set to %s" % self.gisdbase)
self.setEnv("GISRC",os.path.join(self.executeRequest.workingDir,"grassrc"))
logging.info("GRASS GISRC set to %s" % os.path.join(self.executeRequest.workingDir,"grassrc"))
return self.mapsetName
def _windFile(self,mapset):
""" Create default WIND file """
if mapset == "PERMANENT":
windname = "DEFAULT_WIND"
else:
windname = "WIND"
wind =open(
os.path.join(
os.path.abspath(self.executeRequest.workingDir),mapset,windname),"w")
wind.write("""proj: 0\n""")
wind.write("""zone: 0\n""")
wind.write("""north: 1000\n""")
wind.write("""south: 0\n""")
wind.write("""east: 1000\n""")
wind.write("""west: 0\n""")
wind.write("""cols: 1000\n""")
wind.write("""rows: 1000\n""")
wind.write("""e-w resol: 1\n""")
wind.write("""n-s resol: 1\n""")
wind.close()
return
def setEnv(self,key,value):
"""Set GRASS environmental variables """
origValue = os.getenv(key)
#REMOVED: It doesnt seem necessary
#if origValue:
# value += ":"+origValue
os.putenv(key,value)
os.environ[key] = value
return