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 |
bh |
1964 |
import traceback |
19 |
frank |
1132 |
|
20 |
jonathan |
696 |
import Thuban |
21 |
jonathan |
875 |
from Thuban import _ |
22 |
|
|
|
23 |
frank |
1149 |
from Thuban.Lib.fileutil import get_application_dir |
24 |
|
|
|
25 |
jonathan |
1161 |
from Thuban.Model.xmlreader import XMLReader |
26 |
|
|
from Thuban.Model.xmlwriter import XMLWriter |
27 |
jonathan |
696 |
from Thuban.Model.proj import Projection, ProjFile |
28 |
jonathan |
727 |
from xml.sax import SAXParseException |
29 |
jonathan |
696 |
|
30 |
|
|
projdir = \ |
31 |
|
|
os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Projections") |
32 |
|
|
|
33 |
|
|
|
34 |
|
|
PROJ_EXT = ".proj" |
35 |
|
|
|
36 |
bh |
1964 |
# Determine the status of GDAL support. If GDAL is supported |
37 |
|
|
# gdal_support_status will be set to the empty string, otherwise to a |
38 |
|
|
# string with information why it isn't supported |
39 |
|
|
# |
40 |
|
|
# GDAL is supported if we can import both the thuban specific gdalwarp |
41 |
|
|
# module and the GDAL python bindings. |
42 |
|
|
for _module in ("gdalwarp", "gdal"): |
43 |
|
|
try: |
44 |
|
|
__import__(_module) |
45 |
|
|
except ImportError, val: |
46 |
|
|
gdal_support_status = (_("No GDAL support because module '%s'" |
47 |
|
|
" cannot be imported. Python exception: '%s'") |
48 |
|
|
% (_module, str(val))) |
49 |
|
|
break |
50 |
|
|
else: |
51 |
|
|
gdal_support_status = "" |
52 |
jonathan |
1230 |
|
53 |
bh |
1964 |
def has_gdal_support(): |
54 |
|
|
return gdal_support_status == "" |
55 |
jonathan |
1230 |
|
56 |
jonathan |
1161 |
|
57 |
bh |
1830 |
projfile_cache = weakref.WeakValueDictionary() |
58 |
|
|
|
59 |
|
|
def clear_proj_file_cache(): |
60 |
|
|
"""Clear the cache of ProjFile objects maintained by read_proj_file. |
61 |
|
|
|
62 |
|
|
This function is probably only useful for the test suite. |
63 |
|
|
""" |
64 |
|
|
projfile_cache.clear() |
65 |
|
|
|
66 |
jonathan |
1161 |
def read_proj_file(filename): |
67 |
bh |
1786 |
"""Read a .proj file and return a ProjFile object and warnings |
68 |
|
|
|
69 |
|
|
The return value is a tuple with the ProjFile object and a list of |
70 |
|
|
strings with warnings messages that might have been generated by the |
71 |
bh |
1830 |
proj file parser. |
72 |
bh |
1786 |
|
73 |
bh |
1830 |
The objects returned cached so that reading the same file |
74 |
|
|
(identified by its absolute name) several times yields the same |
75 |
|
|
ProjFile object. The cache uses weak references so the objects will |
76 |
|
|
be removed from the cache once the last reference an object in the |
77 |
|
|
cache is removed. |
78 |
|
|
|
79 |
bh |
1786 |
Raises IOError if the file cannot be opened, OSError if the file |
80 |
|
|
cannot be read and SAXParseException if the file is not valid XML. |
81 |
jonathan |
718 |
""" |
82 |
bh |
1830 |
filename = os.path.abspath(filename) |
83 |
|
|
if filename in projfile_cache: |
84 |
|
|
return projfile_cache[filename], [] |
85 |
|
|
else: |
86 |
|
|
handler = ProjFileReader() |
87 |
|
|
handler.read(filename) |
88 |
|
|
proj_file = handler.GetProjFile() |
89 |
|
|
projfile_cache[filename] = proj_file |
90 |
|
|
return proj_file, handler.GetWarnings() |
91 |
jonathan |
696 |
|
92 |
jonathan |
1161 |
def write_proj_file(pf): |
93 |
jonathan |
727 |
"""Write a single .proj file |
94 |
|
|
|
95 |
|
|
Raises IOError if the file cannot be written. |
96 |
|
|
""" |
97 |
|
|
|
98 |
jonathan |
696 |
saver = ProjFileSaver(pf) |
99 |
jonathan |
739 |
saver.write(pf.GetFilename()) |
100 |
jonathan |
696 |
|
101 |
bh |
1940 |
# |
102 |
|
|
# Constants for the get_system_proj_file function |
103 |
|
|
# |
104 |
jonathan |
696 |
|
105 |
bh |
1940 |
# The default projection file with a few predefined projections |
106 |
bh |
1933 |
DEFAULT_PROJ_FILE = "defaults.proj" |
107 |
bh |
1940 |
|
108 |
|
|
# The epsg projections. |
109 |
bh |
1933 |
EPSG_PROJ_FILE = "epsg.proj" |
110 |
jonathan |
696 |
|
111 |
bh |
1940 |
# Deprecated EPSG projections. |
112 |
|
|
EPSG_DEPRECATED_PROJ_FILE = "epsg-deprecated.proj" |
113 |
|
|
|
114 |
bh |
1933 |
def get_system_proj_file(filename): |
115 |
|
|
"""Return the projections from the indicated file and a list with warnings |
116 |
jonathan |
696 |
|
117 |
bh |
1933 |
The filename argument should be the name of a file in the directory |
118 |
|
|
with Thuban's default projection files (Resources/Projections/). If |
119 |
|
|
possible callers should not use hardwired string literal for the |
120 |
|
|
name to avoid unnecessary duplication. Instead they should use one |
121 |
bh |
1940 |
of the predefined constants, currently DEFAULT_PROJ_FILE, |
122 |
|
|
EPSG_PROJ_FILE or EPSG_DEPRECATED_PROJ_FILE. |
123 |
bh |
1933 |
|
124 |
|
|
The return value is a tuple with the projections in a ProjFile |
125 |
|
|
object and a list of strings with warning messages. The warnings |
126 |
|
|
list is usually empty but may contain messages about ignored errors. |
127 |
|
|
|
128 |
bh |
1786 |
If the file could could not be opened return an empty projection |
129 |
bh |
1933 |
file object set to store data in the indicated default file. |
130 |
jonathan |
696 |
""" |
131 |
bh |
1933 |
fullname = os.path.join(projdir, filename) |
132 |
jonathan |
727 |
try: |
133 |
bh |
1933 |
return read_proj_file(fullname) |
134 |
bh |
1786 |
except (OSError, IOError, SAXParseException), val: |
135 |
bh |
1933 |
msg = _('Could not read "%s": %s') % (fullname, str(val)) |
136 |
|
|
return ProjFile(fullname), [msg] |
137 |
jonathan |
696 |
|
138 |
bh |
1786 |
def get_user_proj_file(): |
139 |
|
|
"""Return the user's projections and a list with warnings |
140 |
jonathan |
696 |
|
141 |
bh |
1786 |
The projections read from the user's thuban projection file (usually |
142 |
|
|
in ~/.thuban/user.proj). The return value is a tuple with the |
143 |
|
|
projections in a ProjFile object and a list of strings with warning |
144 |
|
|
messages. The warnings list is usually empty but may contain |
145 |
|
|
messages about ignored errors. |
146 |
|
|
|
147 |
|
|
If the file could could not be opened return an empty projection |
148 |
|
|
file object set to store data in the default file. |
149 |
jonathan |
727 |
""" |
150 |
jonathan |
1189 |
usrdir = get_application_dir() |
151 |
jonathan |
727 |
filename = os.path.join(usrdir, "user.proj") |
152 |
|
|
try: |
153 |
bh |
1786 |
return read_proj_file(filename) |
154 |
jan |
1791 |
except (OSError, IOError, SAXParseException), val: |
155 |
bh |
1786 |
msg = _('Could not read "%s": %s') % (filename, str(val)) |
156 |
|
|
return ProjFile(filename), [msg] |
157 |
jonathan |
727 |
|
158 |
bh |
1786 |
|
159 |
jonathan |
709 |
class ProjFileReader(XMLReader): |
160 |
jonathan |
696 |
|
161 |
jonathan |
709 |
def __init__(self): |
162 |
|
|
XMLReader.__init__(self) |
163 |
bh |
1786 |
self.projfile = ProjFile("") |
164 |
|
|
self.warnings = [] |
165 |
jonathan |
709 |
|
166 |
|
|
XMLReader.AddDispatchers(self, |
167 |
|
|
{'projection': ("start_projection", "end_projection"), |
168 |
|
|
'parameter': ("start_parameter", None)}) |
169 |
bh |
1786 |
|
170 |
jonathan |
709 |
def read(self, file_or_filename): |
171 |
|
|
XMLReader.read(self, file_or_filename) |
172 |
|
|
|
173 |
bh |
1786 |
self.projfile.SetFilename(XMLReader.GetFilename(self)) |
174 |
|
|
|
175 |
jonathan |
696 |
def start_projection(self, name, qname, attrs): |
176 |
|
|
self.params = [] |
177 |
bh |
1786 |
name = self.encode(attrs.get((None, 'name'))) |
178 |
|
|
if name is None: |
179 |
|
|
name = _("Unknown") |
180 |
|
|
self.name = name |
181 |
bh |
1816 |
self.epsg = self.encode(attrs.get((None, 'epsg'))) |
182 |
jonathan |
696 |
|
183 |
|
|
def end_projection(self, name, qname): |
184 |
bh |
1786 |
try: |
185 |
bh |
1816 |
proj = Projection(self.params, self.name, epsg = self.epsg) |
186 |
bh |
1786 |
except IOError, val: |
187 |
|
|
self.warnings.append(_('Error in projection "%s": %s') |
188 |
|
|
% (self.name, str(val))) |
189 |
|
|
else: |
190 |
|
|
self.projfile.Add(proj) |
191 |
jonathan |
696 |
|
192 |
|
|
def start_parameter(self, name, qname, attrs): |
193 |
|
|
s = attrs.get((None, 'value')) |
194 |
|
|
s = str(s) # we can't handle unicode in proj |
195 |
|
|
self.params.append(s) |
196 |
|
|
|
197 |
|
|
def GetProjFile(self): |
198 |
bh |
1786 |
return self.projfile |
199 |
jonathan |
696 |
|
200 |
bh |
1786 |
def GetWarnings(self): |
201 |
|
|
"""Return the list of warning messages that may have been produced""" |
202 |
|
|
return self.warnings |
203 |
|
|
|
204 |
|
|
|
205 |
jonathan |
709 |
class ProjFileSaver(XMLWriter): |
206 |
jonathan |
696 |
|
207 |
|
|
def __init__(self, pf): |
208 |
jonathan |
709 |
XMLWriter.__init__(self) |
209 |
|
|
self.__pf = pf |
210 |
jonathan |
696 |
|
211 |
|
|
def write(self, file_or_filename): |
212 |
jonathan |
709 |
XMLWriter.write(self, file_or_filename) |
213 |
jonathan |
696 |
|
214 |
bh |
1755 |
self.write_header("projectionlist", "projfile.dtd") |
215 |
jonathan |
709 |
self.write_projfile(self.__pf) |
216 |
jonathan |
696 |
self.close() |
217 |
|
|
|
218 |
|
|
def write_projfile(self, pf): |
219 |
|
|
|
220 |
|
|
self.open_element("projectionlist") |
221 |
|
|
|
222 |
|
|
for p in pf.GetProjections(): |
223 |
bh |
1816 |
attrs = {"name": p.GetName()} |
224 |
|
|
if p.EPSGCode(): |
225 |
|
|
attrs["epsg"] = p.EPSGCode() |
226 |
|
|
self.open_element("projection", attrs) |
227 |
jonathan |
696 |
|
228 |
jonathan |
709 |
for param in p.GetAllParameters(): |
229 |
jonathan |
696 |
self.write_element("parameter", {"value": param}) |
230 |
|
|
|
231 |
|
|
self.close_element("projection") |
232 |
|
|
|
233 |
|
|
self.close_element("projectionlist") |
234 |
|
|
|
235 |
|
|
|