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 |
jonathan |
718 |
"""Read a single .proj file and return a ProjFile object. |
51 |
|
|
|
52 |
|
|
Raises IOError if the file cannot be opened. |
53 |
|
|
Raises OSError if the file cannot be read. |
54 |
|
|
Raises SAXParseException if the file is not valid XML. |
55 |
|
|
""" |
56 |
jonathan |
696 |
|
57 |
jonathan |
718 |
handler = ProjFileReader() |
58 |
|
|
handler.read(filename) |
59 |
|
|
return handler.GetProjFile() |
60 |
jonathan |
696 |
|
61 |
jonathan |
1161 |
def write_proj_file(pf): |
62 |
jonathan |
727 |
"""Write a single .proj file |
63 |
|
|
|
64 |
|
|
Raises IOError if the file cannot be written. |
65 |
|
|
""" |
66 |
|
|
|
67 |
jonathan |
696 |
saver = ProjFileSaver(pf) |
68 |
jonathan |
739 |
saver.write(pf.GetFilename()) |
69 |
jonathan |
696 |
|
70 |
jonathan |
1161 |
def get_proj_files(dir): |
71 |
jonathan |
696 |
"""Read all .proj files in the given directory and |
72 |
|
|
return a list of ProjFile objects. |
73 |
|
|
""" |
74 |
|
|
|
75 |
|
|
list = [] |
76 |
|
|
try: |
77 |
jonathan |
718 |
dirlist = os.listdir(dir) |
78 |
jonathan |
696 |
except OSError: |
79 |
jonathan |
718 |
pass # if we can't get a directory listing just return [] |
80 |
|
|
else: |
81 |
|
|
for file in filter(lambda s: s.endswith(PROJ_EXT), dirlist): |
82 |
|
|
try: |
83 |
|
|
filename = os.path.join(dir, file) |
84 |
jonathan |
1179 |
list.append(read_proj_file(filename)) |
85 |
jonathan |
718 |
except (OSError, IOError, SAXParseException): |
86 |
|
|
pass # just move onto the next file |
87 |
jonathan |
696 |
|
88 |
|
|
return list |
89 |
|
|
|
90 |
jonathan |
1161 |
def get_system_proj_files(): |
91 |
jonathan |
696 |
"""Return a list of ProjFile objects from files that are |
92 |
|
|
supplied by Thuban. |
93 |
jonathan |
727 |
|
94 |
|
|
If no files could not be opened return a list with one |
95 |
|
|
empty projection file set to store data in the default file. |
96 |
jonathan |
696 |
""" |
97 |
jonathan |
727 |
filename = os.path.join(projdir, "defaults.proj") |
98 |
|
|
try: |
99 |
jonathan |
1179 |
return [read_proj_file(filename)] |
100 |
jonathan |
727 |
except (OSError, IOError, SAXParseException): |
101 |
|
|
return [ProjFile(filename)] |
102 |
jonathan |
696 |
|
103 |
jonathan |
1161 |
def get_user_proj_files(): |
104 |
jonathan |
727 |
"""Return a list of ProjFile objects from files that are user-defined. |
105 |
jonathan |
696 |
|
106 |
jonathan |
727 |
If no files could not be opened return a list with one |
107 |
|
|
empty projection file set to store data in the default file. |
108 |
|
|
""" |
109 |
|
|
|
110 |
jonathan |
1189 |
usrdir = get_application_dir() |
111 |
jonathan |
727 |
filename = os.path.join(usrdir, "user.proj") |
112 |
|
|
try: |
113 |
jonathan |
1179 |
return [read_proj_file(filename)] |
114 |
jonathan |
727 |
except (OSError, IOError, SAXParseException): |
115 |
|
|
return [ProjFile(filename)] |
116 |
|
|
|
117 |
jonathan |
709 |
class ProjFileReader(XMLReader): |
118 |
jonathan |
696 |
|
119 |
jonathan |
709 |
def __init__(self): |
120 |
|
|
XMLReader.__init__(self) |
121 |
|
|
self.__pf = ProjFile("") |
122 |
|
|
|
123 |
|
|
XMLReader.AddDispatchers(self, |
124 |
|
|
{'projection': ("start_projection", "end_projection"), |
125 |
|
|
'parameter': ("start_parameter", None)}) |
126 |
|
|
|
127 |
|
|
def read(self, file_or_filename): |
128 |
|
|
XMLReader.read(self, file_or_filename) |
129 |
|
|
|
130 |
jonathan |
739 |
self.__pf.SetFilename(XMLReader.GetFilename(self)) |
131 |
jonathan |
696 |
|
132 |
|
|
def start_projection(self, name, qname, attrs): |
133 |
|
|
self.params = [] |
134 |
jonathan |
875 |
self.name = self.encode(attrs.get((None, 'name'), _("Unknown"))) |
135 |
jonathan |
696 |
|
136 |
|
|
def end_projection(self, name, qname): |
137 |
jonathan |
709 |
self.__pf.Add(Projection(self.params, self.name)) |
138 |
jonathan |
696 |
|
139 |
|
|
def start_parameter(self, name, qname, attrs): |
140 |
|
|
s = attrs.get((None, 'value')) |
141 |
|
|
s = str(s) # we can't handle unicode in proj |
142 |
|
|
self.params.append(s) |
143 |
|
|
|
144 |
|
|
def GetProjFile(self): |
145 |
jonathan |
709 |
return self.__pf |
146 |
jonathan |
696 |
|
147 |
jonathan |
709 |
class ProjFileSaver(XMLWriter): |
148 |
jonathan |
696 |
|
149 |
|
|
def __init__(self, pf): |
150 |
jonathan |
709 |
XMLWriter.__init__(self) |
151 |
|
|
self.__pf = pf |
152 |
jonathan |
696 |
|
153 |
|
|
def write(self, file_or_filename): |
154 |
jonathan |
709 |
XMLWriter.write(self, file_or_filename) |
155 |
jonathan |
696 |
|
156 |
jonathan |
875 |
self.write_header("projfile", "projfile.dtd") |
157 |
jonathan |
709 |
self.write_projfile(self.__pf) |
158 |
jonathan |
696 |
self.close() |
159 |
|
|
|
160 |
|
|
def write_projfile(self, pf): |
161 |
|
|
|
162 |
|
|
self.open_element("projectionlist") |
163 |
|
|
|
164 |
|
|
for p in pf.GetProjections(): |
165 |
|
|
self.open_element("projection", {"name": p.GetName()}) |
166 |
|
|
|
167 |
jonathan |
709 |
for param in p.GetAllParameters(): |
168 |
jonathan |
696 |
self.write_element("parameter", {"value": param}) |
169 |
|
|
|
170 |
|
|
self.close_element("projection") |
171 |
|
|
|
172 |
|
|
self.close_element("projectionlist") |
173 |
|
|
|
174 |
|
|
|