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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1161 - (show annotations)
Thu Jun 12 12:41:01 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: 4463 byte(s)
Renamed functions to following the function_names_with_underscores style.
(has_gdal_support): New function that returns true if the gdal
        library is available. Addresses RTbug #1877.

1 # 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 import os.path
10
11 import Thuban
12 from Thuban import _
13
14 from Thuban.Lib.fileutil import get_application_dir
15
16 from Thuban.Model.xmlreader import XMLReader
17 from Thuban.Model.xmlwriter import XMLWriter
18 from Thuban.Model.proj import Projection, ProjFile
19 from xml.sax import SAXParseException
20
21 projdir = \
22 os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Projections")
23
24 usrdir = get_application_dir()
25
26 PROJ_EXT = ".proj"
27
28 has_gdal_support = lambda: False
29 try:
30 import gdal
31 """Return True if the gdal library is available."""
32 has_gdal_support = lambda: True
33 except ImportError:
34 pass
35
36 def read_proj_file(filename):
37 """Read a single .proj file and return a ProjFile object.
38
39 Raises IOError if the file cannot be opened.
40 Raises OSError if the file cannot be read.
41 Raises SAXParseException if the file is not valid XML.
42 """
43
44 handler = ProjFileReader()
45 handler.read(filename)
46 return handler.GetProjFile()
47
48 def write_proj_file(pf):
49 """Write a single .proj file
50
51 Raises IOError if the file cannot be written.
52 """
53
54 saver = ProjFileSaver(pf)
55 saver.write(pf.GetFilename())
56
57 def get_proj_files(dir):
58 """Read all .proj files in the given directory and
59 return a list of ProjFile objects.
60 """
61
62 list = []
63 try:
64 dirlist = os.listdir(dir)
65 except OSError:
66 pass # if we can't get a directory listing just return []
67 else:
68 for file in filter(lambda s: s.endswith(PROJ_EXT), dirlist):
69 try:
70 filename = os.path.join(dir, file)
71 list.append(ReadProjFile(filename))
72 except (OSError, IOError, SAXParseException):
73 pass # just move onto the next file
74
75 return list
76
77 def get_system_proj_files():
78 """Return a list of ProjFile objects from files that are
79 supplied by Thuban.
80
81 If no files could not be opened return a list with one
82 empty projection file set to store data in the default file.
83 """
84 filename = os.path.join(projdir, "defaults.proj")
85 try:
86 return [ReadProjFile(filename)]
87 except (OSError, IOError, SAXParseException):
88 return [ProjFile(filename)]
89
90 def get_user_proj_files():
91 """Return a list of ProjFile objects from files that are user-defined.
92
93 If no files could not be opened return a list with one
94 empty projection file set to store data in the default file.
95 """
96
97 filename = os.path.join(usrdir, "user.proj")
98 try:
99 return [ReadProjFile(filename)]
100 except (OSError, IOError, SAXParseException):
101 return [ProjFile(filename)]
102
103 class ProjFileReader(XMLReader):
104
105 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 self.__pf.SetFilename(XMLReader.GetFilename(self))
117
118 def start_projection(self, name, qname, attrs):
119 self.params = []
120 self.name = self.encode(attrs.get((None, 'name'), _("Unknown")))
121
122 def end_projection(self, name, qname):
123 self.__pf.Add(Projection(self.params, self.name))
124
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 return self.__pf
132
133 class ProjFileSaver(XMLWriter):
134
135 def __init__(self, pf):
136 XMLWriter.__init__(self)
137 self.__pf = pf
138
139 def write(self, file_or_filename):
140 XMLWriter.write(self, file_or_filename)
141
142 self.write_header("projfile", "projfile.dtd")
143 self.write_projfile(self.__pf)
144 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 for param in p.GetAllParameters():
154 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