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