/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/Model/resource.py
ViewVC logotype

Contents of /branches/WIP-pyshapelib-bramz/Thuban/Model/resource.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 696 - (show annotations)
Wed Apr 16 16:39:49 2003 UTC (21 years, 10 months ago) by jonathan
Original Path: trunk/thuban/Thuban/Model/resource.py
File MIME type: text/x-python
File size: 3135 byte(s)
New. Contains general utilities for read and writing projection files.

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 Thuban
10 from Thuban.Model.load import XMLProcessor, load_xmlfile
11 from Thuban.Model.save import XMLSaver
12 from Thuban.Model.proj import Projection, ProjFile
13
14 projdir = \
15 os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Projections")
16
17 usrdir = os.path.expanduser("~/.thuban")
18
19 PROJ_EXT = ".proj"
20
21 def ReadProjFile(filename):
22 """Read a single .proj file and return a ProjFile object."""
23
24 projs = []
25 try:
26 handler = ProjFileReader(filename)
27
28 load_xmlfile(filename, handler)
29 pf = handler.GetProjFile()
30 except OSError:
31 pass
32
33 return pf
34
35 def WriteProjFile(pf):
36 saver = ProjFileSaver(pf)
37 saver.write(pf.GetFileName())
38
39 def GetProjections(dir):
40 """Read all .proj files in the given directory and
41 return a list of ProjFile objects.
42 """
43
44 list = []
45 try:
46 for file in filter(lambda s: s.endswith(PROJ_EXT), os.listdir(dir)):
47 filename = os.path.join(dir, file)
48 list.extend(ReadProjFile(filename))
49 except OSError:
50 pass
51
52 return list
53
54 def GetSuppliedProjections():
55 """Return a list of ProjFile objects from files that are
56 supplied by Thuban.
57 """
58 return GetProjections(projdir)
59
60 def GetUserProjections():
61 """Return a list of ProjFile objects from files that are user-defined."""
62 return GetProjections(usrdir)
63
64 class ProjFileReader(XMLProcessor):
65
66 def __init__(self, filename):
67 XMLProcessor.__init__(self, os.path.dirname(filename))
68
69 self.pf = ProjFile(filename)
70
71 def start_projection(self, name, qname, attrs):
72 self.params = []
73 self.name = attrs.get((None, 'name'), "Unknown")
74 XMLProcessor.start_dispatcher['projection'] = "start_projection"
75
76 def end_projection(self, name, qname):
77 self.pf.Add(Projection(self.params, self.name))
78 XMLProcessor.end_dispatcher['projection'] = "end_projection"
79
80 def start_parameter(self, name, qname, attrs):
81 s = attrs.get((None, 'value'))
82 s = str(s) # we can't handle unicode in proj
83 self.params.append(s)
84 XMLProcessor.start_dispatcher['parameter'] = "start_parameter"
85
86 def GetProjFile(self):
87 return self.pf
88
89
90 class ProjFileSaver(XMLSaver):
91
92 def __init__(self, pf):
93 XMLSaver.__init__(self)
94 self.pf = pf
95
96 def write(self, file_or_filename):
97 XMLSaver.write(self, file_or_filename)
98
99 self.write_header("projfile", "thuban.dtd")
100 self.write_projfile(self.pf)
101 self.close()
102
103 def write_projfile(self, pf):
104
105 self.open_element("projectionlist")
106
107 for p in pf.GetProjections():
108 self.open_element("projection", {"name": p.GetName()})
109
110 for param in p.GetParameters():
111 self.write_element("parameter", {"value": param})
112
113 self.close_element("projection")
114
115 self.close_element("projectionlist")
116
117

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26