source: TI02-CSML/trunk/services/3rdParty/pywps-1.0.0/pywps/Wps/grass.py @ 2194

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/services/3rdParty/pywps-1.0.0/pywps/Wps/grass.py@2194
Revision 2194, 5.1 KB checked in by lawrence, 13 years ago (diff)

Adding various specs and 3rd party code of interest for the CSML
services development.

Line 
1"""
2This module is here for work with GRASS GIS environment varibales and
3locations and mapsets
4"""
5# Author:       Jachym Cepicky
6#               http://les-ejk.cz
7# Lince:
8#
9# Web Processing Service implementation
10# Copyright (C) 2006 Jachym Cepicky
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
25
26import os
27import time, shutil, tempfile
28#
29import sys
30
31class GRASS:
32    def  __init__(self,grassenv):
33        """
34        Initalization of GRASS environment variables (except GISRC).
35
36        Edit this function, if your settings are different
37        """
38        self.env = grassenv
39        self.export_vars()
40        return
41
42    def export_vars(self,env=None):
43        """
44        Exports GRASS evnironment variables defined in __init__ function
45
46        Attributes:
47            env     -   eventualy dictionary with 'VAR':'VALUE' pares
48                        default: self.env
49        """
50        if not env:
51            for key in self.env.keys():
52                os.putenv(key,self.env[key])
53        else:
54            for key in env.keys():
55                os.putenv(key,env[key])
56
57        # GIS_LOCK
58        os.putenv('GIS_LOCK',str(os.getpid()))
59       
60    def mkmapset(self,location=None):
61        """
62        Create GRASS mapset in current directory. Mapsets name is 'mapset'.
63        At the end, GRASS will beleve, it was runned correctly.
64
65        Returns name of new created mapset. location!=None, this mapset
66        should be deleted!
67
68        Argumnets:
69            location     -  Should the new mapset be created in the some old
70                            location, which is allread on this server?
71                            Default: only mapset within
72                            /tmp/grasstmpSOMEHTIN/
73                            will be created
74        """
75        self.grassenv = {}
76        tempdir = os.path.abspath(os.path.curdir)# suppose, he has a
77
78        # we have to create new mapset within this directory
79        # suppose, this directory is somewhere in /tmp/grasstmpSOMETHING
80        if not location:
81            os.mkdir("mapset")
82            self.writeWIND("mapset")
83            os.mkdir("PERMANENT")
84            self.writeWIND("PERMANENT")
85                        # exporting of importand variables, writing the gisrc file
86            self.grassenv['LOCATION_NAME'] = os.path.split(
87                                    os.path.abspath(os.path.curdir))[-1]
88            self.grassenv['MAPSET'] = "mapset"
89            self.grassenv['GISDBASE'] = os.path.split(os.path.abspath(os.path.curdir))[0]
90           
91        # location is here, we justhave to use it
92        else:
93            # make mapset
94            mapset = tempfile.mkdtemp(prefix="tmpmapset",dir=location)
95            shutil.copy(os.path.join(location,"PERMANENT","DEFAULT_WIND"),os.path.join(mapset,"WIND"))
96            # export env. vars
97            (gisdbase,location_name) = os.path.split(location)
98            if not location_name:
99                (gisdbase,location_name) = os.path.split(os.path.split(location)[0])
100               
101            self.grassenv['LOCATION_NAME'] = location_name
102            self.grassenv['MAPSET'] = os.path.split(mapset)[-1]
103            self.grassenv['GISDBASE'] = gisdbase
104             
105        self.export_vars(env=self.grassenv)
106
107        # gisrc
108        gisrc = open(os.path.join(tempdir,"grass61rc"),"w")
109        gisrc.write("LOCATION_NAME: %s\n" % (self.grassenv['LOCATION_NAME']))
110        gisrc.write("MAPSET: %s\n" % (self.grassenv['MAPSET']))
111        gisrc.write("DIGITIZER: none\n")
112        gisrc.write("GISDBASE: %s\n" % (self.grassenv['GISDBASE']))
113        gisrc.write("OVERWRITE: 1\n")
114        gisrc.write("GRASS_GUI: text\n")
115        gisrc.close()
116
117        os.putenv("GISRC",os.path.join(tempdir,"grass61rc"))
118       
119        return self.grassenv['MAPSET']
120
121    def writeWIND(self,mapset):
122        """
123        Create default WIND file
124        """
125        if mapset == "PERMANENT":
126            windname = "DEFAULT_WIND"
127        else:
128            windname = "WIND"
129
130        wind =open(
131                os.path.join(
132                    os.path.abspath(os.path.curdir),mapset,windname),"w")
133        wind.write("""proj:       0\n""")
134        wind.write("""zone:       0\n""")
135        wind.write("""north:      1000\n""")
136        wind.write("""south:      0\n""")
137        wind.write("""east:       1000\n""")
138        wind.write("""west:       0\n""")
139        wind.write("""cols:       1000\n""")
140        wind.write("""rows:       1000\n""")
141        wind.write("""e-w resol:  1\n""")
142        wind.write("""n-s resol:  1\n""")
143        wind.close()
144        return
145
146
Note: See TracBrowser for help on using the repository browser.