1 |
# 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 |
"""Handle resources loaded from files such as projections""" |
9 |
|
10 |
__version__ = "$Revision$" |
11 |
# $Source$ |
12 |
# $Id$ |
13 |
|
14 |
|
15 |
import os |
16 |
import os.path |
17 |
|
18 |
import Thuban |
19 |
from Thuban import _ |
20 |
|
21 |
from Thuban.Lib.fileutil import get_application_dir |
22 |
|
23 |
from Thuban.Model.xmlreader import XMLReader |
24 |
from Thuban.Model.xmlwriter import XMLWriter |
25 |
from Thuban.Model.proj import Projection, ProjFile |
26 |
from xml.sax import SAXParseException |
27 |
|
28 |
projdir = \ |
29 |
os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Projections") |
30 |
|
31 |
|
32 |
PROJ_EXT = ".proj" |
33 |
|
34 |
has_gdal_support = lambda: False |
35 |
try: |
36 |
# 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 |
import gdal |
43 |
|
44 |
"""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 |
"""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 |
|
57 |
handler = ProjFileReader() |
58 |
handler.read(filename) |
59 |
return handler.GetProjFile() |
60 |
|
61 |
def write_proj_file(pf): |
62 |
"""Write a single .proj file |
63 |
|
64 |
Raises IOError if the file cannot be written. |
65 |
""" |
66 |
|
67 |
saver = ProjFileSaver(pf) |
68 |
saver.write(pf.GetFilename()) |
69 |
|
70 |
def get_proj_files(dir): |
71 |
"""Read all .proj files in the given directory and |
72 |
return a list of ProjFile objects. |
73 |
""" |
74 |
|
75 |
list = [] |
76 |
try: |
77 |
dirlist = os.listdir(dir) |
78 |
except OSError: |
79 |
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 |
list.append(read_proj_file(filename)) |
85 |
except (OSError, IOError, SAXParseException): |
86 |
pass # just move onto the next file |
87 |
|
88 |
return list |
89 |
|
90 |
def get_system_proj_files(): |
91 |
"""Return a list of ProjFile objects from files that are |
92 |
supplied by Thuban. |
93 |
|
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 |
""" |
97 |
filename = os.path.join(projdir, "defaults.proj") |
98 |
try: |
99 |
return [read_proj_file(filename)] |
100 |
except (OSError, IOError, SAXParseException): |
101 |
return [ProjFile(filename)] |
102 |
|
103 |
def get_user_proj_files(): |
104 |
"""Return a list of ProjFile objects from files that are user-defined. |
105 |
|
106 |
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 |
usrdir = get_application_dir() |
111 |
filename = os.path.join(usrdir, "user.proj") |
112 |
try: |
113 |
return [read_proj_file(filename)] |
114 |
except (OSError, IOError, SAXParseException): |
115 |
return [ProjFile(filename)] |
116 |
|
117 |
class ProjFileReader(XMLReader): |
118 |
|
119 |
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 |
self.__pf.SetFilename(XMLReader.GetFilename(self)) |
131 |
|
132 |
def start_projection(self, name, qname, attrs): |
133 |
self.params = [] |
134 |
self.name = self.encode(attrs.get((None, 'name'), _("Unknown"))) |
135 |
|
136 |
def end_projection(self, name, qname): |
137 |
self.__pf.Add(Projection(self.params, self.name)) |
138 |
|
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 |
return self.__pf |
146 |
|
147 |
class ProjFileSaver(XMLWriter): |
148 |
|
149 |
def __init__(self, pf): |
150 |
XMLWriter.__init__(self) |
151 |
self.__pf = pf |
152 |
|
153 |
def write(self, file_or_filename): |
154 |
XMLWriter.write(self, file_or_filename) |
155 |
|
156 |
self.write_header("projectionlist", "projfile.dtd") |
157 |
self.write_projfile(self.__pf) |
158 |
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 |
for param in p.GetAllParameters(): |
168 |
self.write_element("parameter", {"value": param}) |
169 |
|
170 |
self.close_element("projection") |
171 |
|
172 |
self.close_element("projectionlist") |
173 |
|
174 |
|