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 |
import os |
9 |
import os.path |
10 |
|
11 |
import Thuban |
12 |
from Thuban import _ |
13 |
|
14 |
from Thuban.Lib.fileutil import get_application_dir |
15 |
|
16 |
from Thuban.Model.xmlreader import XMLReader |
17 |
from Thuban.Model.xmlwriter import XMLWriter |
18 |
from Thuban.Model.proj import Projection, ProjFile |
19 |
from xml.sax import SAXParseException |
20 |
|
21 |
projdir = \ |
22 |
os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Projections") |
23 |
|
24 |
|
25 |
PROJ_EXT = ".proj" |
26 |
|
27 |
has_gdal_support = lambda: False |
28 |
try: |
29 |
import gdal |
30 |
"""Return True if the gdal library is available.""" |
31 |
has_gdal_support = lambda: True |
32 |
except ImportError: |
33 |
pass |
34 |
|
35 |
def read_proj_file(filename): |
36 |
"""Read a single .proj file and return a ProjFile object. |
37 |
|
38 |
Raises IOError if the file cannot be opened. |
39 |
Raises OSError if the file cannot be read. |
40 |
Raises SAXParseException if the file is not valid XML. |
41 |
""" |
42 |
|
43 |
handler = ProjFileReader() |
44 |
handler.read(filename) |
45 |
return handler.GetProjFile() |
46 |
|
47 |
def write_proj_file(pf): |
48 |
"""Write a single .proj file |
49 |
|
50 |
Raises IOError if the file cannot be written. |
51 |
""" |
52 |
|
53 |
saver = ProjFileSaver(pf) |
54 |
saver.write(pf.GetFilename()) |
55 |
|
56 |
def get_proj_files(dir): |
57 |
"""Read all .proj files in the given directory and |
58 |
return a list of ProjFile objects. |
59 |
""" |
60 |
|
61 |
list = [] |
62 |
try: |
63 |
dirlist = os.listdir(dir) |
64 |
except OSError: |
65 |
pass # if we can't get a directory listing just return [] |
66 |
else: |
67 |
for file in filter(lambda s: s.endswith(PROJ_EXT), dirlist): |
68 |
try: |
69 |
filename = os.path.join(dir, file) |
70 |
list.append(read_proj_file(filename)) |
71 |
except (OSError, IOError, SAXParseException): |
72 |
pass # just move onto the next file |
73 |
|
74 |
return list |
75 |
|
76 |
def get_system_proj_files(): |
77 |
"""Return a list of ProjFile objects from files that are |
78 |
supplied by Thuban. |
79 |
|
80 |
If no files could not be opened return a list with one |
81 |
empty projection file set to store data in the default file. |
82 |
""" |
83 |
filename = os.path.join(projdir, "defaults.proj") |
84 |
try: |
85 |
return [read_proj_file(filename)] |
86 |
except (OSError, IOError, SAXParseException): |
87 |
return [ProjFile(filename)] |
88 |
|
89 |
def get_user_proj_files(): |
90 |
"""Return a list of ProjFile objects from files that are user-defined. |
91 |
|
92 |
If no files could not be opened return a list with one |
93 |
empty projection file set to store data in the default file. |
94 |
""" |
95 |
|
96 |
usrdir = get_application_dir() |
97 |
filename = os.path.join(usrdir, "user.proj") |
98 |
try: |
99 |
return [read_proj_file(filename)] |
100 |
except (OSError, IOError, SAXParseException): |
101 |
return [ProjFile(filename)] |
102 |
|
103 |
class ProjFileReader(XMLReader): |
104 |
|
105 |
def __init__(self): |
106 |
XMLReader.__init__(self) |
107 |
self.__pf = ProjFile("") |
108 |
|
109 |
XMLReader.AddDispatchers(self, |
110 |
{'projection': ("start_projection", "end_projection"), |
111 |
'parameter': ("start_parameter", None)}) |
112 |
|
113 |
def read(self, file_or_filename): |
114 |
XMLReader.read(self, file_or_filename) |
115 |
|
116 |
self.__pf.SetFilename(XMLReader.GetFilename(self)) |
117 |
|
118 |
def start_projection(self, name, qname, attrs): |
119 |
self.params = [] |
120 |
self.name = self.encode(attrs.get((None, 'name'), _("Unknown"))) |
121 |
|
122 |
def end_projection(self, name, qname): |
123 |
self.__pf.Add(Projection(self.params, self.name)) |
124 |
|
125 |
def start_parameter(self, name, qname, attrs): |
126 |
s = attrs.get((None, 'value')) |
127 |
s = str(s) # we can't handle unicode in proj |
128 |
self.params.append(s) |
129 |
|
130 |
def GetProjFile(self): |
131 |
return self.__pf |
132 |
|
133 |
class ProjFileSaver(XMLWriter): |
134 |
|
135 |
def __init__(self, pf): |
136 |
XMLWriter.__init__(self) |
137 |
self.__pf = pf |
138 |
|
139 |
def write(self, file_or_filename): |
140 |
XMLWriter.write(self, file_or_filename) |
141 |
|
142 |
self.write_header("projfile", "projfile.dtd") |
143 |
self.write_projfile(self.__pf) |
144 |
self.close() |
145 |
|
146 |
def write_projfile(self, pf): |
147 |
|
148 |
self.open_element("projectionlist") |
149 |
|
150 |
for p in pf.GetProjections(): |
151 |
self.open_element("projection", {"name": p.GetName()}) |
152 |
|
153 |
for param in p.GetAllParameters(): |
154 |
self.write_element("parameter", {"value": param}) |
155 |
|
156 |
self.close_element("projection") |
157 |
|
158 |
self.close_element("projectionlist") |
159 |
|
160 |
|