/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/Model/resource.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/Thuban/Model/resource.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1189 - (hide annotations)
Thu Jun 12 17:01:13 2003 UTC (21 years, 8 months ago) by jonathan
Original Path: trunk/thuban/Thuban/Model/resource.py
File MIME type: text/x-python
File size: 4473 byte(s)
(get_user_proj_files): Moved the
        called to get_application_dir here, so that the directory
        will only be called if this method is invoked.

1 jonathan 696 # Copyright (c) 2003 by Intevation GmbH
2     # Authors:
3     # Jonathan Coles <[email protected]>
4     #
5     # This program is free software under the GPL (>=v2)
6     # Read the file COPYING coming with Thuban for details.
7    
8     import os
9 frank 1132 import os.path
10    
11 jonathan 696 import Thuban
12 jonathan 875 from Thuban import _
13    
14 frank 1149 from Thuban.Lib.fileutil import get_application_dir
15    
16 jonathan 1161 from Thuban.Model.xmlreader import XMLReader
17     from Thuban.Model.xmlwriter import XMLWriter
18 jonathan 696 from Thuban.Model.proj import Projection, ProjFile
19 jonathan 727 from xml.sax import SAXParseException
20 jonathan 696
21     projdir = \
22     os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Projections")
23    
24    
25     PROJ_EXT = ".proj"
26    
27 jonathan 1161 has_gdal_support = lambda: False
28     try:
29     import gdal
30     """Return True if the gdal library is available."""
31     has_gdal_support = lambda: True
32     except ImportError:
33     pass
34    
35     def read_proj_file(filename):
36 jonathan 718 """Read a single .proj file and return a ProjFile object.
37    
38     Raises IOError if the file cannot be opened.
39     Raises OSError if the file cannot be read.
40     Raises SAXParseException if the file is not valid XML.
41     """
42 jonathan 696
43 jonathan 718 handler = ProjFileReader()
44     handler.read(filename)
45     return handler.GetProjFile()
46 jonathan 696
47 jonathan 1161 def write_proj_file(pf):
48 jonathan 727 """Write a single .proj file
49    
50     Raises IOError if the file cannot be written.
51     """
52    
53 jonathan 696 saver = ProjFileSaver(pf)
54 jonathan 739 saver.write(pf.GetFilename())
55 jonathan 696
56 jonathan 1161 def get_proj_files(dir):
57 jonathan 696 """Read all .proj files in the given directory and
58     return a list of ProjFile objects.
59     """
60    
61     list = []
62     try:
63 jonathan 718 dirlist = os.listdir(dir)
64 jonathan 696 except OSError:
65 jonathan 718 pass # if we can't get a directory listing just return []
66     else:
67     for file in filter(lambda s: s.endswith(PROJ_EXT), dirlist):
68     try:
69     filename = os.path.join(dir, file)
70 jonathan 1179 list.append(read_proj_file(filename))
71 jonathan 718 except (OSError, IOError, SAXParseException):
72     pass # just move onto the next file
73 jonathan 696
74     return list
75    
76 jonathan 1161 def get_system_proj_files():
77 jonathan 696 """Return a list of ProjFile objects from files that are
78     supplied by Thuban.
79 jonathan 727
80     If no files could not be opened return a list with one
81     empty projection file set to store data in the default file.
82 jonathan 696 """
83 jonathan 727 filename = os.path.join(projdir, "defaults.proj")
84     try:
85 jonathan 1179 return [read_proj_file(filename)]
86 jonathan 727 except (OSError, IOError, SAXParseException):
87     return [ProjFile(filename)]
88 jonathan 696
89 jonathan 1161 def get_user_proj_files():
90 jonathan 727 """Return a list of ProjFile objects from files that are user-defined.
91 jonathan 696
92 jonathan 727 If no files could not be opened return a list with one
93     empty projection file set to store data in the default file.
94     """
95    
96 jonathan 1189 usrdir = get_application_dir()
97 jonathan 727 filename = os.path.join(usrdir, "user.proj")
98     try:
99 jonathan 1179 return [read_proj_file(filename)]
100 jonathan 727 except (OSError, IOError, SAXParseException):
101     return [ProjFile(filename)]
102    
103 jonathan 709 class ProjFileReader(XMLReader):
104 jonathan 696
105 jonathan 709 def __init__(self):
106     XMLReader.__init__(self)
107     self.__pf = ProjFile("")
108    
109     XMLReader.AddDispatchers(self,
110     {'projection': ("start_projection", "end_projection"),
111     'parameter': ("start_parameter", None)})
112    
113     def read(self, file_or_filename):
114     XMLReader.read(self, file_or_filename)
115    
116 jonathan 739 self.__pf.SetFilename(XMLReader.GetFilename(self))
117 jonathan 696
118     def start_projection(self, name, qname, attrs):
119     self.params = []
120 jonathan 875 self.name = self.encode(attrs.get((None, 'name'), _("Unknown")))
121 jonathan 696
122     def end_projection(self, name, qname):
123 jonathan 709 self.__pf.Add(Projection(self.params, self.name))
124 jonathan 696
125     def start_parameter(self, name, qname, attrs):
126     s = attrs.get((None, 'value'))
127     s = str(s) # we can't handle unicode in proj
128     self.params.append(s)
129    
130     def GetProjFile(self):
131 jonathan 709 return self.__pf
132 jonathan 696
133 jonathan 709 class ProjFileSaver(XMLWriter):
134 jonathan 696
135     def __init__(self, pf):
136 jonathan 709 XMLWriter.__init__(self)
137     self.__pf = pf
138 jonathan 696
139     def write(self, file_or_filename):
140 jonathan 709 XMLWriter.write(self, file_or_filename)
141 jonathan 696
142 jonathan 875 self.write_header("projfile", "projfile.dtd")
143 jonathan 709 self.write_projfile(self.__pf)
144 jonathan 696 self.close()
145    
146     def write_projfile(self, pf):
147    
148     self.open_element("projectionlist")
149    
150     for p in pf.GetProjections():
151     self.open_element("projection", {"name": p.GetName()})
152    
153 jonathan 709 for param in p.GetAllParameters():
154 jonathan 696 self.write_element("parameter", {"value": param})
155    
156     self.close_element("projection")
157    
158     self.close_element("projectionlist")
159    
160    

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26