6 |
# Read the file COPYING coming with Thuban for details. |
# Read the file COPYING coming with Thuban for details. |
7 |
|
|
8 |
import os |
import os |
9 |
|
import os.path |
10 |
|
|
11 |
import Thuban |
import Thuban |
12 |
from Thuban import _ |
from Thuban import _ |
13 |
|
|
14 |
from Thuban.Model.load import XMLReader |
from Thuban.Lib.fileutil import get_application_dir |
15 |
from Thuban.Model.save import XMLWriter |
|
16 |
|
from Thuban.Model.xmlreader import XMLReader |
17 |
|
from Thuban.Model.xmlwriter import XMLWriter |
18 |
from Thuban.Model.proj import Projection, ProjFile |
from Thuban.Model.proj import Projection, ProjFile |
19 |
from xml.sax import SAXParseException |
from xml.sax import SAXParseException |
20 |
|
|
21 |
projdir = \ |
projdir = \ |
22 |
os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Projections") |
os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Projections") |
23 |
|
|
|
usrdir = os.path.expanduser("~/.thuban") |
|
24 |
|
|
25 |
PROJ_EXT = ".proj" |
PROJ_EXT = ".proj" |
26 |
|
|
27 |
def ReadProjFile(filename): |
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. |
"""Read a single .proj file and return a ProjFile object. |
37 |
|
|
38 |
Raises IOError if the file cannot be opened. |
Raises IOError if the file cannot be opened. |
44 |
handler.read(filename) |
handler.read(filename) |
45 |
return handler.GetProjFile() |
return handler.GetProjFile() |
46 |
|
|
47 |
def WriteProjFile(pf): |
def write_proj_file(pf): |
48 |
"""Write a single .proj file |
"""Write a single .proj file |
49 |
|
|
50 |
Raises IOError if the file cannot be written. |
Raises IOError if the file cannot be written. |
53 |
saver = ProjFileSaver(pf) |
saver = ProjFileSaver(pf) |
54 |
saver.write(pf.GetFilename()) |
saver.write(pf.GetFilename()) |
55 |
|
|
56 |
def GetProjFiles(dir): |
def get_proj_files(dir): |
57 |
"""Read all .proj files in the given directory and |
"""Read all .proj files in the given directory and |
58 |
return a list of ProjFile objects. |
return a list of ProjFile objects. |
59 |
""" |
""" |
67 |
for file in filter(lambda s: s.endswith(PROJ_EXT), dirlist): |
for file in filter(lambda s: s.endswith(PROJ_EXT), dirlist): |
68 |
try: |
try: |
69 |
filename = os.path.join(dir, file) |
filename = os.path.join(dir, file) |
70 |
list.append(ReadProjFile(filename)) |
list.append(read_proj_file(filename)) |
71 |
except (OSError, IOError, SAXParseException): |
except (OSError, IOError, SAXParseException): |
72 |
pass # just move onto the next file |
pass # just move onto the next file |
73 |
|
|
74 |
return list |
return list |
75 |
|
|
76 |
def GetSystemProjFiles(): |
def get_system_proj_files(): |
77 |
"""Return a list of ProjFile objects from files that are |
"""Return a list of ProjFile objects from files that are |
78 |
supplied by Thuban. |
supplied by Thuban. |
79 |
|
|
82 |
""" |
""" |
83 |
filename = os.path.join(projdir, "defaults.proj") |
filename = os.path.join(projdir, "defaults.proj") |
84 |
try: |
try: |
85 |
return [ReadProjFile(filename)] |
return [read_proj_file(filename)] |
86 |
except (OSError, IOError, SAXParseException): |
except (OSError, IOError, SAXParseException): |
87 |
return [ProjFile(filename)] |
return [ProjFile(filename)] |
88 |
|
|
89 |
def GetUserProjFiles(): |
def get_user_proj_files(): |
90 |
"""Return a list of ProjFile objects from files that are user-defined. |
"""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 |
If no files could not be opened return a list with one |
93 |
empty projection file set to store data in the default file. |
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") |
filename = os.path.join(usrdir, "user.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 |
|
|
130 |
def GetProjFile(self): |
def GetProjFile(self): |
131 |
return self.__pf |
return self.__pf |
132 |
|
|
|
|
|
133 |
class ProjFileSaver(XMLWriter): |
class ProjFileSaver(XMLWriter): |
134 |
|
|
135 |
def __init__(self, pf): |
def __init__(self, pf): |