5 |
# This program is free software under the GPL (>=v2) |
# This program is free software under the GPL (>=v2) |
6 |
# Read the file COPYING coming with Thuban for details. |
# 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 |
import os |
16 |
import os.path |
import os.path |
|
from tempfile import mktemp |
|
17 |
|
|
18 |
import Thuban |
import Thuban |
19 |
from Thuban import _ |
from Thuban import _ |
20 |
|
|
21 |
from Thuban.Model.load import XMLReader |
from Thuban.Lib.fileutil import get_application_dir |
22 |
from Thuban.Model.save import XMLWriter |
|
23 |
|
from Thuban.Model.xmlreader import XMLReader |
24 |
|
from Thuban.Model.xmlwriter import XMLWriter |
25 |
from Thuban.Model.proj import Projection, ProjFile |
from Thuban.Model.proj import Projection, ProjFile |
26 |
from xml.sax import SAXParseException |
from xml.sax import SAXParseException |
27 |
|
|
28 |
projdir = \ |
projdir = \ |
29 |
os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Projections") |
os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Projections") |
30 |
|
|
|
if os.name == 'nt': |
|
|
# This should result in something like the user directory ... |
|
|
guess = os.path.dirname(os.path.dirname(os.path.dirname(mktemp()))) |
|
|
usrdir = os.path.join(guess, ".thuban") |
|
|
if not os.path.isdir(usrdir): |
|
|
os.mkdir(usrdir) |
|
|
else: |
|
|
usrdir = os.path.expanduser("~/.thuban") |
|
31 |
|
|
32 |
PROJ_EXT = ".proj" |
PROJ_EXT = ".proj" |
33 |
|
|
34 |
def ReadProjFile(filename): |
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. |
"""Read a single .proj file and return a ProjFile object. |
51 |
|
|
52 |
Raises IOError if the file cannot be opened. |
Raises IOError if the file cannot be opened. |
58 |
handler.read(filename) |
handler.read(filename) |
59 |
return handler.GetProjFile() |
return handler.GetProjFile() |
60 |
|
|
61 |
def WriteProjFile(pf): |
def write_proj_file(pf): |
62 |
"""Write a single .proj file |
"""Write a single .proj file |
63 |
|
|
64 |
Raises IOError if the file cannot be written. |
Raises IOError if the file cannot be written. |
67 |
saver = ProjFileSaver(pf) |
saver = ProjFileSaver(pf) |
68 |
saver.write(pf.GetFilename()) |
saver.write(pf.GetFilename()) |
69 |
|
|
70 |
def GetProjFiles(dir): |
def get_proj_files(dir): |
71 |
"""Read all .proj files in the given directory and |
"""Read all .proj files in the given directory and |
72 |
return a list of ProjFile objects. |
return a list of ProjFile objects. |
73 |
""" |
""" |
81 |
for file in filter(lambda s: s.endswith(PROJ_EXT), dirlist): |
for file in filter(lambda s: s.endswith(PROJ_EXT), dirlist): |
82 |
try: |
try: |
83 |
filename = os.path.join(dir, file) |
filename = os.path.join(dir, file) |
84 |
list.append(ReadProjFile(filename)) |
list.append(read_proj_file(filename)) |
85 |
except (OSError, IOError, SAXParseException): |
except (OSError, IOError, SAXParseException): |
86 |
pass # just move onto the next file |
pass # just move onto the next file |
87 |
|
|
88 |
return list |
return list |
89 |
|
|
90 |
def GetSystemProjFiles(): |
def get_system_proj_files(): |
91 |
"""Return a list of ProjFile objects from files that are |
"""Return a list of ProjFile objects from files that are |
92 |
supplied by Thuban. |
supplied by Thuban. |
93 |
|
|
96 |
""" |
""" |
97 |
filename = os.path.join(projdir, "defaults.proj") |
filename = os.path.join(projdir, "defaults.proj") |
98 |
try: |
try: |
99 |
return [ReadProjFile(filename)] |
return [read_proj_file(filename)] |
100 |
except (OSError, IOError, SAXParseException): |
except (OSError, IOError, SAXParseException): |
101 |
return [ProjFile(filename)] |
return [ProjFile(filename)] |
102 |
|
|
103 |
def GetUserProjFiles(): |
def get_user_proj_files(): |
104 |
"""Return a list of ProjFile objects from files that are user-defined. |
"""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 |
If no files could not be opened return a list with one |
107 |
empty projection file set to store data in the default file. |
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") |
filename = os.path.join(usrdir, "user.proj") |
112 |
try: |
try: |
113 |
return [ReadProjFile(filename)] |
return [read_proj_file(filename)] |
114 |
except (OSError, IOError, SAXParseException): |
except (OSError, IOError, SAXParseException): |
115 |
return [ProjFile(filename)] |
return [ProjFile(filename)] |
116 |
|
|
144 |
def GetProjFile(self): |
def GetProjFile(self): |
145 |
return self.__pf |
return self.__pf |
146 |
|
|
|
|
|
147 |
class ProjFileSaver(XMLWriter): |
class ProjFileSaver(XMLWriter): |
148 |
|
|
149 |
def __init__(self, pf): |
def __init__(self, pf): |
153 |
def write(self, file_or_filename): |
def write(self, file_or_filename): |
154 |
XMLWriter.write(self, file_or_filename) |
XMLWriter.write(self, file_or_filename) |
155 |
|
|
156 |
self.write_header("projfile", "projfile.dtd") |
self.write_header("projectionlist", "projfile.dtd") |
157 |
self.write_projfile(self.__pf) |
self.write_projfile(self.__pf) |
158 |
self.close() |
self.close() |
159 |
|
|