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 |
bh |
1542 |
"""Handle resources loaded from files such as projections""" |
9 |
|
|
|
10 |
|
|
__version__ = "$Revision$" |
11 |
|
|
# $Source$ |
12 |
|
|
# $Id$ |
13 |
|
|
|
14 |
|
|
|
15 |
jonathan |
696 |
import os |
16 |
frank |
1132 |
import os.path |
17 |
bh |
1830 |
import weakref |
18 |
frank |
1132 |
|
19 |
jonathan |
696 |
import Thuban |
20 |
jonathan |
875 |
from Thuban import _ |
21 |
|
|
|
22 |
frank |
1149 |
from Thuban.Lib.fileutil import get_application_dir |
23 |
|
|
|
24 |
jonathan |
1161 |
from Thuban.Model.xmlreader import XMLReader |
25 |
|
|
from Thuban.Model.xmlwriter import XMLWriter |
26 |
jonathan |
696 |
from Thuban.Model.proj import Projection, ProjFile |
27 |
jonathan |
727 |
from xml.sax import SAXParseException |
28 |
jonathan |
696 |
|
29 |
|
|
projdir = \ |
30 |
|
|
os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Projections") |
31 |
|
|
|
32 |
|
|
|
33 |
|
|
PROJ_EXT = ".proj" |
34 |
|
|
|
35 |
jonathan |
1161 |
has_gdal_support = lambda: False |
36 |
|
|
try: |
37 |
jonathan |
1230 |
# first try to see if our extension module is there. if it |
38 |
|
|
# wasn't even compiled, we obviously don't have gdal support. |
39 |
|
|
import gdalwarp |
40 |
|
|
|
41 |
|
|
# now try to import the python extension. if this doesn't |
42 |
|
|
# exist then we can't do anything. |
43 |
jonathan |
1161 |
import gdal |
44 |
jonathan |
1230 |
|
45 |
jonathan |
1161 |
"""Return True if the gdal library is available.""" |
46 |
|
|
has_gdal_support = lambda: True |
47 |
|
|
except ImportError: |
48 |
|
|
pass |
49 |
|
|
|
50 |
bh |
1830 |
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 |
jonathan |
1161 |
def read_proj_file(filename): |
60 |
bh |
1786 |
"""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 |
63 |
|
|
strings with warnings messages that might have been generated by the |
64 |
bh |
1830 |
proj file parser. |
65 |
bh |
1786 |
|
66 |
bh |
1830 |
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 |
bh |
1786 |
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 |
jonathan |
718 |
""" |
75 |
bh |
1830 |
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 |
jonathan |
696 |
|
85 |
jonathan |
1161 |
def write_proj_file(pf): |
86 |
jonathan |
727 |
"""Write a single .proj file |
87 |
|
|
|
88 |
|
|
Raises IOError if the file cannot be written. |
89 |
|
|
""" |
90 |
|
|
|
91 |
jonathan |
696 |
saver = ProjFileSaver(pf) |
92 |
jonathan |
739 |
saver.write(pf.GetFilename()) |
93 |
jonathan |
696 |
|
94 |
|
|
|
95 |
bh |
1786 |
def get_system_proj_file(): |
96 |
|
|
"""Return the standard projections and a list with warnings |
97 |
jonathan |
696 |
|
98 |
bh |
1786 |
The projections read from the default thuban projection file |
99 |
|
|
(usually in Resources/Projections/defaults.proj). The return value |
100 |
|
|
is a tuple with the projections in a ProjFile object and a list of |
101 |
|
|
strings with warning messages. The warnings list is usually empty |
102 |
|
|
but may contain messages about ignored errors. |
103 |
jonathan |
696 |
|
104 |
bh |
1786 |
If the file could could not be opened return an empty projection |
105 |
|
|
file object set to store data in the default file. |
106 |
jonathan |
696 |
""" |
107 |
jonathan |
727 |
filename = os.path.join(projdir, "defaults.proj") |
108 |
|
|
try: |
109 |
bh |
1786 |
return read_proj_file(filename) |
110 |
|
|
except (OSError, IOError, SAXParseException), val: |
111 |
|
|
msg = _('Could not read "%s": %s') % (filename, str(val)) |
112 |
|
|
return ProjFile(filename), [msg] |
113 |
jonathan |
696 |
|
114 |
bh |
1786 |
def get_user_proj_file(): |
115 |
|
|
"""Return the user's projections and a list with warnings |
116 |
jonathan |
696 |
|
117 |
bh |
1786 |
The projections read from the user's thuban projection file (usually |
118 |
|
|
in ~/.thuban/user.proj). The return value is a tuple with the |
119 |
|
|
projections in a ProjFile object and a list of strings with warning |
120 |
|
|
messages. The warnings list is usually empty but may contain |
121 |
|
|
messages about ignored errors. |
122 |
|
|
|
123 |
|
|
If the file could could not be opened return an empty projection |
124 |
|
|
file object set to store data in the default file. |
125 |
jonathan |
727 |
""" |
126 |
jonathan |
1189 |
usrdir = get_application_dir() |
127 |
jonathan |
727 |
filename = os.path.join(usrdir, "user.proj") |
128 |
|
|
try: |
129 |
bh |
1786 |
return read_proj_file(filename) |
130 |
jan |
1791 |
except (OSError, IOError, SAXParseException), val: |
131 |
bh |
1786 |
msg = _('Could not read "%s": %s') % (filename, str(val)) |
132 |
|
|
return ProjFile(filename), [msg] |
133 |
jonathan |
727 |
|
134 |
bh |
1786 |
|
135 |
jonathan |
709 |
class ProjFileReader(XMLReader): |
136 |
jonathan |
696 |
|
137 |
jonathan |
709 |
def __init__(self): |
138 |
|
|
XMLReader.__init__(self) |
139 |
bh |
1786 |
self.projfile = ProjFile("") |
140 |
|
|
self.warnings = [] |
141 |
jonathan |
709 |
|
142 |
|
|
XMLReader.AddDispatchers(self, |
143 |
|
|
{'projection': ("start_projection", "end_projection"), |
144 |
|
|
'parameter': ("start_parameter", None)}) |
145 |
bh |
1786 |
|
146 |
jonathan |
709 |
def read(self, file_or_filename): |
147 |
|
|
XMLReader.read(self, file_or_filename) |
148 |
|
|
|
149 |
bh |
1786 |
self.projfile.SetFilename(XMLReader.GetFilename(self)) |
150 |
|
|
|
151 |
jonathan |
696 |
def start_projection(self, name, qname, attrs): |
152 |
|
|
self.params = [] |
153 |
bh |
1786 |
name = self.encode(attrs.get((None, 'name'))) |
154 |
|
|
if name is None: |
155 |
|
|
name = _("Unknown") |
156 |
|
|
self.name = name |
157 |
bh |
1816 |
self.epsg = self.encode(attrs.get((None, 'epsg'))) |
158 |
jonathan |
696 |
|
159 |
|
|
def end_projection(self, name, qname): |
160 |
bh |
1786 |
try: |
161 |
bh |
1816 |
proj = Projection(self.params, self.name, epsg = self.epsg) |
162 |
bh |
1786 |
except IOError, val: |
163 |
|
|
self.warnings.append(_('Error in projection "%s": %s') |
164 |
|
|
% (self.name, str(val))) |
165 |
|
|
else: |
166 |
|
|
self.projfile.Add(proj) |
167 |
jonathan |
696 |
|
168 |
|
|
def start_parameter(self, name, qname, attrs): |
169 |
|
|
s = attrs.get((None, 'value')) |
170 |
|
|
s = str(s) # we can't handle unicode in proj |
171 |
|
|
self.params.append(s) |
172 |
|
|
|
173 |
|
|
def GetProjFile(self): |
174 |
bh |
1786 |
return self.projfile |
175 |
jonathan |
696 |
|
176 |
bh |
1786 |
def GetWarnings(self): |
177 |
|
|
"""Return the list of warning messages that may have been produced""" |
178 |
|
|
return self.warnings |
179 |
|
|
|
180 |
|
|
|
181 |
jonathan |
709 |
class ProjFileSaver(XMLWriter): |
182 |
jonathan |
696 |
|
183 |
|
|
def __init__(self, pf): |
184 |
jonathan |
709 |
XMLWriter.__init__(self) |
185 |
|
|
self.__pf = pf |
186 |
jonathan |
696 |
|
187 |
|
|
def write(self, file_or_filename): |
188 |
jonathan |
709 |
XMLWriter.write(self, file_or_filename) |
189 |
jonathan |
696 |
|
190 |
bh |
1755 |
self.write_header("projectionlist", "projfile.dtd") |
191 |
jonathan |
709 |
self.write_projfile(self.__pf) |
192 |
jonathan |
696 |
self.close() |
193 |
|
|
|
194 |
|
|
def write_projfile(self, pf): |
195 |
|
|
|
196 |
|
|
self.open_element("projectionlist") |
197 |
|
|
|
198 |
|
|
for p in pf.GetProjections(): |
199 |
bh |
1816 |
attrs = {"name": p.GetName()} |
200 |
|
|
if p.EPSGCode(): |
201 |
|
|
attrs["epsg"] = p.EPSGCode() |
202 |
|
|
self.open_element("projection", attrs) |
203 |
jonathan |
696 |
|
204 |
jonathan |
709 |
for param in p.GetAllParameters(): |
205 |
jonathan |
696 |
self.write_element("parameter", {"value": param}) |
206 |
|
|
|
207 |
|
|
self.close_element("projection") |
208 |
|
|
|
209 |
|
|
self.close_element("projectionlist") |
210 |
|
|
|
211 |
|
|
|