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 |
|
|
24 |
usrdir = os.path.expanduser("~/.thuban") |
usrdir = get_application_dir() |
25 |
|
|
26 |
PROJ_EXT = ".proj" |
PROJ_EXT = ".proj" |
27 |
|
|
28 |
def ReadProjFile(filename): |
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 |
"""Read a single .proj file and return a ProjFile object. |
"""Read a single .proj file and return a ProjFile object. |
38 |
|
|
39 |
Raises IOError if the file cannot be opened. |
Raises IOError if the file cannot be opened. |
45 |
handler.read(filename) |
handler.read(filename) |
46 |
return handler.GetProjFile() |
return handler.GetProjFile() |
47 |
|
|
48 |
def WriteProjFile(pf): |
def write_proj_file(pf): |
49 |
"""Write a single .proj file |
"""Write a single .proj file |
50 |
|
|
51 |
Raises IOError if the file cannot be written. |
Raises IOError if the file cannot be written. |
54 |
saver = ProjFileSaver(pf) |
saver = ProjFileSaver(pf) |
55 |
saver.write(pf.GetFilename()) |
saver.write(pf.GetFilename()) |
56 |
|
|
57 |
def GetProjFiles(dir): |
def get_proj_files(dir): |
58 |
"""Read all .proj files in the given directory and |
"""Read all .proj files in the given directory and |
59 |
return a list of ProjFile objects. |
return a list of ProjFile objects. |
60 |
""" |
""" |
68 |
for file in filter(lambda s: s.endswith(PROJ_EXT), dirlist): |
for file in filter(lambda s: s.endswith(PROJ_EXT), dirlist): |
69 |
try: |
try: |
70 |
filename = os.path.join(dir, file) |
filename = os.path.join(dir, file) |
71 |
list.append(ReadProjFile(filename)) |
list.append(read_proj_file(filename)) |
72 |
except (OSError, IOError, SAXParseException): |
except (OSError, IOError, SAXParseException): |
73 |
pass # just move onto the next file |
pass # just move onto the next file |
74 |
|
|
75 |
return list |
return list |
76 |
|
|
77 |
def GetSystemProjFiles(): |
def get_system_proj_files(): |
78 |
"""Return a list of ProjFile objects from files that are |
"""Return a list of ProjFile objects from files that are |
79 |
supplied by Thuban. |
supplied by Thuban. |
80 |
|
|
83 |
""" |
""" |
84 |
filename = os.path.join(projdir, "defaults.proj") |
filename = os.path.join(projdir, "defaults.proj") |
85 |
try: |
try: |
86 |
return [ReadProjFile(filename)] |
return [read_proj_file(filename)] |
87 |
except (OSError, IOError, SAXParseException): |
except (OSError, IOError, SAXParseException): |
88 |
return [ProjFile(filename)] |
return [ProjFile(filename)] |
89 |
|
|
90 |
def GetUserProjFiles(): |
def get_user_proj_files(): |
91 |
"""Return a list of ProjFile objects from files that are user-defined. |
"""Return a list of ProjFile objects from files that are user-defined. |
92 |
|
|
93 |
If no files could not be opened return a list with one |
If no files could not be opened return a list with one |
96 |
|
|
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): |