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