14 |
|
|
15 |
import os |
import os |
16 |
import os.path |
import os.path |
17 |
|
import weakref |
18 |
|
|
19 |
import Thuban |
import Thuban |
20 |
from Thuban import _ |
from Thuban import _ |
47 |
except ImportError: |
except ImportError: |
48 |
pass |
pass |
49 |
|
|
50 |
def read_proj_file(filename): |
projfile_cache = weakref.WeakValueDictionary() |
51 |
"""Read a single .proj file and return a ProjFile object. |
|
52 |
|
def clear_proj_file_cache(): |
53 |
Raises IOError if the file cannot be opened. |
"""Clear the cache of ProjFile objects maintained by read_proj_file. |
54 |
Raises OSError if the file cannot be read. |
|
55 |
Raises SAXParseException if the file is not valid XML. |
This function is probably only useful for the test suite. |
56 |
""" |
""" |
57 |
|
projfile_cache.clear() |
58 |
|
|
59 |
|
def read_proj_file(filename): |
60 |
|
"""Read a .proj file and return a ProjFile object and warnings |
61 |
|
|
62 |
handler = ProjFileReader() |
The return value is a tuple with the ProjFile object and a list of |
63 |
handler.read(filename) |
strings with warnings messages that might have been generated by the |
64 |
return handler.GetProjFile() |
proj file parser. |
65 |
|
|
66 |
|
The objects returned cached so that reading the same file |
67 |
|
(identified by its absolute name) several times yields the same |
68 |
|
ProjFile object. The cache uses weak references so the objects will |
69 |
|
be removed from the cache once the last reference an object in the |
70 |
|
cache is removed. |
71 |
|
|
72 |
|
Raises IOError if the file cannot be opened, OSError if the file |
73 |
|
cannot be read and SAXParseException if the file is not valid XML. |
74 |
|
""" |
75 |
|
filename = os.path.abspath(filename) |
76 |
|
if filename in projfile_cache: |
77 |
|
return projfile_cache[filename], [] |
78 |
|
else: |
79 |
|
handler = ProjFileReader() |
80 |
|
handler.read(filename) |
81 |
|
proj_file = handler.GetProjFile() |
82 |
|
projfile_cache[filename] = proj_file |
83 |
|
return proj_file, handler.GetWarnings() |
84 |
|
|
85 |
def write_proj_file(pf): |
def write_proj_file(pf): |
86 |
"""Write a single .proj file |
"""Write a single .proj file |
91 |
saver = ProjFileSaver(pf) |
saver = ProjFileSaver(pf) |
92 |
saver.write(pf.GetFilename()) |
saver.write(pf.GetFilename()) |
93 |
|
|
|
def get_proj_files(dir): |
|
|
"""Read all .proj files in the given directory and |
|
|
return a list of ProjFile objects. |
|
|
""" |
|
94 |
|
|
95 |
list = [] |
# Constants for the get_system_proj_file function |
96 |
try: |
DEFAULT_PROJ_FILE = "defaults.proj" |
97 |
dirlist = os.listdir(dir) |
EPSG_PROJ_FILE = "epsg.proj" |
98 |
except OSError: |
|
99 |
pass # if we can't get a directory listing just return [] |
def get_system_proj_file(filename): |
100 |
else: |
"""Return the projections from the indicated file and a list with warnings |
101 |
for file in filter(lambda s: s.endswith(PROJ_EXT), dirlist): |
|
102 |
try: |
The filename argument should be the name of a file in the directory |
103 |
filename = os.path.join(dir, file) |
with Thuban's default projection files (Resources/Projections/). If |
104 |
list.append(read_proj_file(filename)) |
possible callers should not use hardwired string literal for the |
105 |
except (OSError, IOError, SAXParseException): |
name to avoid unnecessary duplication. Instead they should use one |
106 |
pass # just move onto the next file |
of the predefined constants, currently DEFAULT_PROJ_FILE or |
107 |
|
EPSG_PROJ_FILE. |
108 |
return list |
|
109 |
|
The return value is a tuple with the projections in a ProjFile |
110 |
def get_system_proj_files(): |
object and a list of strings with warning messages. The warnings |
111 |
"""Return a list of ProjFile objects from files that are |
list is usually empty but may contain messages about ignored errors. |
|
supplied by Thuban. |
|
112 |
|
|
113 |
If no files could not be opened return a list with one |
If the file could could not be opened return an empty projection |
114 |
empty projection file set to store data in the default file. |
file object set to store data in the indicated default file. |
115 |
""" |
""" |
116 |
filename = os.path.join(projdir, "defaults.proj") |
fullname = os.path.join(projdir, filename) |
117 |
try: |
try: |
118 |
return [read_proj_file(filename)] |
return read_proj_file(fullname) |
119 |
except (OSError, IOError, SAXParseException): |
except (OSError, IOError, SAXParseException), val: |
120 |
return [ProjFile(filename)] |
msg = _('Could not read "%s": %s') % (fullname, str(val)) |
121 |
|
return ProjFile(fullname), [msg] |
122 |
def get_user_proj_files(): |
|
123 |
"""Return a list of ProjFile objects from files that are user-defined. |
def get_user_proj_file(): |
124 |
|
"""Return the user's projections and a list with warnings |
125 |
|
|
126 |
|
The projections read from the user's thuban projection file (usually |
127 |
|
in ~/.thuban/user.proj). The return value is a tuple with the |
128 |
|
projections in a ProjFile object and a list of strings with warning |
129 |
|
messages. The warnings list is usually empty but may contain |
130 |
|
messages about ignored errors. |
131 |
|
|
132 |
If no files could not be opened return a list with one |
If the file could could not be opened return an empty projection |
133 |
empty projection file set to store data in the default file. |
file object set to store data in the default file. |
134 |
""" |
""" |
|
|
|
135 |
usrdir = get_application_dir() |
usrdir = get_application_dir() |
136 |
filename = os.path.join(usrdir, "user.proj") |
filename = os.path.join(usrdir, "user.proj") |
137 |
try: |
try: |
138 |
return [read_proj_file(filename)] |
return read_proj_file(filename) |
139 |
except (OSError, IOError, SAXParseException): |
except (OSError, IOError, SAXParseException), val: |
140 |
return [ProjFile(filename)] |
msg = _('Could not read "%s": %s') % (filename, str(val)) |
141 |
|
return ProjFile(filename), [msg] |
142 |
|
|
143 |
|
|
144 |
class ProjFileReader(XMLReader): |
class ProjFileReader(XMLReader): |
145 |
|
|
146 |
def __init__(self): |
def __init__(self): |
147 |
XMLReader.__init__(self) |
XMLReader.__init__(self) |
148 |
self.__pf = ProjFile("") |
self.projfile = ProjFile("") |
149 |
|
self.warnings = [] |
150 |
|
|
151 |
XMLReader.AddDispatchers(self, |
XMLReader.AddDispatchers(self, |
152 |
{'projection': ("start_projection", "end_projection"), |
{'projection': ("start_projection", "end_projection"), |
153 |
'parameter': ("start_parameter", None)}) |
'parameter': ("start_parameter", None)}) |
154 |
|
|
155 |
def read(self, file_or_filename): |
def read(self, file_or_filename): |
156 |
XMLReader.read(self, file_or_filename) |
XMLReader.read(self, file_or_filename) |
157 |
|
|
158 |
self.__pf.SetFilename(XMLReader.GetFilename(self)) |
self.projfile.SetFilename(XMLReader.GetFilename(self)) |
159 |
|
|
160 |
def start_projection(self, name, qname, attrs): |
def start_projection(self, name, qname, attrs): |
161 |
self.params = [] |
self.params = [] |
162 |
self.name = self.encode(attrs.get((None, 'name'), _("Unknown"))) |
name = self.encode(attrs.get((None, 'name'))) |
163 |
|
if name is None: |
164 |
|
name = _("Unknown") |
165 |
|
self.name = name |
166 |
|
self.epsg = self.encode(attrs.get((None, 'epsg'))) |
167 |
|
|
168 |
def end_projection(self, name, qname): |
def end_projection(self, name, qname): |
169 |
self.__pf.Add(Projection(self.params, self.name)) |
try: |
170 |
|
proj = Projection(self.params, self.name, epsg = self.epsg) |
171 |
|
except IOError, val: |
172 |
|
self.warnings.append(_('Error in projection "%s": %s') |
173 |
|
% (self.name, str(val))) |
174 |
|
else: |
175 |
|
self.projfile.Add(proj) |
176 |
|
|
177 |
def start_parameter(self, name, qname, attrs): |
def start_parameter(self, name, qname, attrs): |
178 |
s = attrs.get((None, 'value')) |
s = attrs.get((None, 'value')) |
180 |
self.params.append(s) |
self.params.append(s) |
181 |
|
|
182 |
def GetProjFile(self): |
def GetProjFile(self): |
183 |
return self.__pf |
return self.projfile |
184 |
|
|
185 |
|
def GetWarnings(self): |
186 |
|
"""Return the list of warning messages that may have been produced""" |
187 |
|
return self.warnings |
188 |
|
|
189 |
|
|
190 |
class ProjFileSaver(XMLWriter): |
class ProjFileSaver(XMLWriter): |
191 |
|
|
205 |
self.open_element("projectionlist") |
self.open_element("projectionlist") |
206 |
|
|
207 |
for p in pf.GetProjections(): |
for p in pf.GetProjections(): |
208 |
self.open_element("projection", {"name": p.GetName()}) |
attrs = {"name": p.GetName()} |
209 |
|
if p.EPSGCode(): |
210 |
|
attrs["epsg"] = p.EPSGCode() |
211 |
|
self.open_element("projection", attrs) |
212 |
|
|
213 |
for param in p.GetAllParameters(): |
for param in p.GetAllParameters(): |
214 |
self.write_element("parameter", {"value": param}) |
self.write_element("parameter", {"value": param}) |