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