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 |
|
projfile_cache = weakref.WeakValueDictionary() |
51 |
|
|
52 |
|
def clear_proj_file_cache(): |
53 |
|
"""Clear the cache of ProjFile objects maintained by read_proj_file. |
54 |
|
|
55 |
|
This function is probably only useful for the test suite. |
56 |
|
""" |
57 |
|
projfile_cache.clear() |
58 |
|
|
59 |
def read_proj_file(filename): |
def read_proj_file(filename): |
60 |
"""Read a .proj file and return a ProjFile object and warnings |
"""Read a .proj file and return a ProjFile object and warnings |
61 |
|
|
62 |
The return value is a tuple with the ProjFile object and a list of |
The return value is a tuple with the ProjFile object and a list of |
63 |
strings with warnings messages that might have been generated by the |
strings with warnings messages that might have been generated by the |
64 |
proj ifle parser. |
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 |
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. |
cannot be read and SAXParseException if the file is not valid XML. |
74 |
""" |
""" |
75 |
handler = ProjFileReader() |
filename = os.path.abspath(filename) |
76 |
handler.read(filename) |
if filename in projfile_cache: |
77 |
return handler.GetProjFile(), handler.GetWarnings() |
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 |
154 |
if name is None: |
if name is None: |
155 |
name = _("Unknown") |
name = _("Unknown") |
156 |
self.name = name |
self.name = name |
157 |
|
self.epsg = self.encode(attrs.get((None, 'epsg'))) |
158 |
|
|
159 |
def end_projection(self, name, qname): |
def end_projection(self, name, qname): |
160 |
try: |
try: |
161 |
proj = Projection(self.params, self.name) |
proj = Projection(self.params, self.name, epsg = self.epsg) |
162 |
except IOError, val: |
except IOError, val: |
163 |
self.warnings.append(_('Error in projection "%s": %s') |
self.warnings.append(_('Error in projection "%s": %s') |
164 |
% (self.name, str(val))) |
% (self.name, str(val))) |
196 |
self.open_element("projectionlist") |
self.open_element("projectionlist") |
197 |
|
|
198 |
for p in pf.GetProjections(): |
for p in pf.GetProjections(): |
199 |
self.open_element("projection", {"name": p.GetName()}) |
attrs = {"name": p.GetName()} |
200 |
|
if p.EPSGCode(): |
201 |
|
attrs["epsg"] = p.EPSGCode() |
202 |
|
self.open_element("projection", attrs) |
203 |
|
|
204 |
for param in p.GetAllParameters(): |
for param in p.GetAllParameters(): |
205 |
self.write_element("parameter", {"value": param}) |
self.write_element("parameter", {"value": param}) |