14 |
|
|
15 |
import os |
import os |
16 |
import os.path |
import os.path |
17 |
|
import weakref |
18 |
|
import traceback |
19 |
|
|
20 |
import Thuban |
import Thuban |
21 |
from Thuban import _ |
from Thuban import _ |
33 |
|
|
34 |
PROJ_EXT = ".proj" |
PROJ_EXT = ".proj" |
35 |
|
|
36 |
has_gdal_support = lambda: False |
# Determine the status of GDAL support. If GDAL is supported |
37 |
try: |
# gdal_support_status will be set to the empty string, otherwise to a |
38 |
# first try to see if our extension module is there. if it |
# string with information why it isn't supported |
39 |
# wasn't even compiled, we obviously don't have gdal support. |
# |
40 |
import gdalwarp |
# GDAL is supported if we can import both the thuban specific gdalwarp |
41 |
|
# module and the GDAL python bindings. |
42 |
# now try to import the python extension. if this doesn't |
for _module in ("gdalwarp", "gdal"): |
43 |
# exist then we can't do anything. |
try: |
44 |
import gdal |
__import__(_module) |
45 |
|
except ImportError, val: |
46 |
"""Return True if the gdal library is available.""" |
gdal_support_status = (_("No GDAL support because module '%s'" |
47 |
has_gdal_support = lambda: True |
" cannot be imported. Python exception: '%s'") |
48 |
except ImportError: |
% (_module, str(val))) |
49 |
pass |
break |
50 |
|
else: |
51 |
|
gdal_support_status = "" |
52 |
|
|
53 |
|
def has_gdal_support(): |
54 |
|
return gdal_support_status == "" |
55 |
|
|
56 |
|
|
57 |
|
projfile_cache = weakref.WeakValueDictionary() |
58 |
|
|
59 |
|
def clear_proj_file_cache(): |
60 |
|
"""Clear the cache of ProjFile objects maintained by read_proj_file. |
61 |
|
|
62 |
|
This function is probably only useful for the test suite. |
63 |
|
""" |
64 |
|
projfile_cache.clear() |
65 |
|
|
66 |
def read_proj_file(filename): |
def read_proj_file(filename): |
67 |
"""Read a .proj file and return a ProjFile object and warnings |
"""Read a .proj file and return a ProjFile object and warnings |
68 |
|
|
69 |
The return value is a tuple with the ProjFile object and a list of |
The return value is a tuple with the ProjFile object and a list of |
70 |
strings with warnings messages that might have been generated by the |
strings with warnings messages that might have been generated by the |
71 |
proj ifle parser. |
proj file parser. |
72 |
|
|
73 |
|
The objects returned cached so that reading the same file |
74 |
|
(identified by its absolute name) several times yields the same |
75 |
|
ProjFile object. The cache uses weak references so the objects will |
76 |
|
be removed from the cache once the last reference an object in the |
77 |
|
cache is removed. |
78 |
|
|
79 |
Raises IOError if the file cannot be opened, OSError if the file |
Raises IOError if the file cannot be opened, OSError if the file |
80 |
cannot be read and SAXParseException if the file is not valid XML. |
cannot be read and SAXParseException if the file is not valid XML. |
81 |
""" |
""" |
82 |
handler = ProjFileReader() |
filename = os.path.abspath(filename) |
83 |
handler.read(filename) |
if filename in projfile_cache: |
84 |
return handler.GetProjFile(), handler.GetWarnings() |
return projfile_cache[filename], [] |
85 |
|
else: |
86 |
|
handler = ProjFileReader() |
87 |
|
handler.read(filename) |
88 |
|
proj_file = handler.GetProjFile() |
89 |
|
projfile_cache[filename] = proj_file |
90 |
|
return proj_file, handler.GetWarnings() |
91 |
|
|
92 |
def write_proj_file(pf): |
def write_proj_file(pf): |
93 |
"""Write a single .proj file |
"""Write a single .proj file |
98 |
saver = ProjFileSaver(pf) |
saver = ProjFileSaver(pf) |
99 |
saver.write(pf.GetFilename()) |
saver.write(pf.GetFilename()) |
100 |
|
|
101 |
|
# |
102 |
def get_system_proj_file(): |
# Constants for the get_system_proj_file function |
103 |
"""Return the standard projections and a list with warnings |
# |
104 |
|
|
105 |
The projections read from the default thuban projection file |
# The default projection file with a few predefined projections |
106 |
(usually in Resources/Projections/defaults.proj). The return value |
DEFAULT_PROJ_FILE = "defaults.proj" |
107 |
is a tuple with the projections in a ProjFile object and a list of |
|
108 |
strings with warning messages. The warnings list is usually empty |
# The epsg projections. |
109 |
but may contain messages about ignored errors. |
EPSG_PROJ_FILE = "epsg.proj" |
110 |
|
|
111 |
|
# Deprecated EPSG projections. |
112 |
|
EPSG_DEPRECATED_PROJ_FILE = "epsg-deprecated.proj" |
113 |
|
|
114 |
|
def get_system_proj_file(filename): |
115 |
|
"""Return the projections from the indicated file and a list with warnings |
116 |
|
|
117 |
|
The filename argument should be the name of a file in the directory |
118 |
|
with Thuban's default projection files (Resources/Projections/). If |
119 |
|
possible callers should not use hardwired string literal for the |
120 |
|
name to avoid unnecessary duplication. Instead they should use one |
121 |
|
of the predefined constants, currently DEFAULT_PROJ_FILE, |
122 |
|
EPSG_PROJ_FILE or EPSG_DEPRECATED_PROJ_FILE. |
123 |
|
|
124 |
|
The return value is a tuple with the projections in a ProjFile |
125 |
|
object and a list of strings with warning messages. The warnings |
126 |
|
list is usually empty but may contain messages about ignored errors. |
127 |
|
|
128 |
If the file could could not be opened return an empty projection |
If the file could could not be opened return an empty projection |
129 |
file object set to store data in the default file. |
file object set to store data in the indicated default file. |
130 |
""" |
""" |
131 |
filename = os.path.join(projdir, "defaults.proj") |
fullname = os.path.join(projdir, filename) |
132 |
try: |
try: |
133 |
return read_proj_file(filename) |
return read_proj_file(fullname) |
134 |
except (OSError, IOError, SAXParseException), val: |
except (OSError, IOError, SAXParseException), val: |
135 |
msg = _('Could not read "%s": %s') % (filename, str(val)) |
msg = _('Could not read "%s": %s') % (fullname, str(val)) |
136 |
return ProjFile(filename), [msg] |
return ProjFile(fullname), [msg] |
137 |
|
|
138 |
def get_user_proj_file(): |
def get_user_proj_file(): |
139 |
"""Return the user's projections and a list with warnings |
"""Return the user's projections and a list with warnings |
151 |
filename = os.path.join(usrdir, "user.proj") |
filename = os.path.join(usrdir, "user.proj") |
152 |
try: |
try: |
153 |
return read_proj_file(filename) |
return read_proj_file(filename) |
154 |
except (OSError, IOError, SAXParseException): |
except (OSError, IOError, SAXParseException), val: |
155 |
msg = _('Could not read "%s": %s') % (filename, str(val)) |
msg = _('Could not read "%s": %s') % (filename, str(val)) |
156 |
return ProjFile(filename), [msg] |
return ProjFile(filename), [msg] |
157 |
|
|
178 |
if name is None: |
if name is None: |
179 |
name = _("Unknown") |
name = _("Unknown") |
180 |
self.name = name |
self.name = name |
181 |
|
self.epsg = self.encode(attrs.get((None, 'epsg'))) |
182 |
|
|
183 |
def end_projection(self, name, qname): |
def end_projection(self, name, qname): |
184 |
try: |
try: |
185 |
proj = Projection(self.params, self.name) |
proj = Projection(self.params, self.name, epsg = self.epsg) |
186 |
except IOError, val: |
except IOError, val: |
187 |
self.warnings.append(_('Error in projection "%s": %s') |
self.warnings.append(_('Error in projection "%s": %s') |
188 |
% (self.name, str(val))) |
% (self.name, str(val))) |
220 |
self.open_element("projectionlist") |
self.open_element("projectionlist") |
221 |
|
|
222 |
for p in pf.GetProjections(): |
for p in pf.GetProjections(): |
223 |
self.open_element("projection", {"name": p.GetName()}) |
attrs = {"name": p.GetName()} |
224 |
|
if p.EPSGCode(): |
225 |
|
attrs["epsg"] = p.EPSGCode() |
226 |
|
self.open_element("projection", attrs) |
227 |
|
|
228 |
for param in p.GetAllParameters(): |
for param in p.GetAllParameters(): |
229 |
self.write_element("parameter", {"value": param}) |
self.write_element("parameter", {"value": param}) |