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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6 - (show annotations)
Tue Aug 28 15:41:52 2001 UTC (23 years, 6 months ago) by bh
Original Path: trunk/thuban/Thuban/Model/load.py
File MIME type: text/x-python
File size: 5561 byte(s)
import all the source files

1 # Copyright (C) 2001 by Intevation GmbH
2 # Authors:
3 # Jan-Oliver Wagner <[email protected]>
4 #
5 # This program is free software under the GPL (>=v2)
6 # Read the file COPYING coming with GRASS for details.
7
8 """
9 Parser for thuban session files.
10 """
11
12 __version__ = "$Revision$"
13
14 import sys, string, os
15 from Thuban.Model.session import Session
16 from Thuban.Model.map import Map
17 from Thuban.Model.layer import Layer
18 from Thuban.Model.color import Color
19 from Thuban.Model.proj import Projection
20
21 oldPython=0
22
23 if not sys.__dict__.has_key("version_info"):
24 # We can assume to have python 1.5.2 or lower here now
25 oldPython=1
26
27 if oldPython:
28 try:
29 from xml.sax.saxexts import make_parser
30 from xml.sax.saxlib import HandlerBase
31 from xml.sax import saxutils
32 except ImportError:
33 sys.stdout.write(("You need to have Python-XML installed or"
34 " a modern Python!\n"
35 "Check www.python.org/sigs/xml-sig/\n\n"))
36 raise
37 else:
38 # Do the python 2.0 standard xml thing and map it on the old names
39 import xml.sax
40 import xml.sax.handler
41 HandlerBase=xml.sax.handler.ContentHandler
42 from xml.sax import make_parser
43
44 class testSAXContentHandler(HandlerBase):
45 # SAX compliant
46 def characters(self, ch, start, length):
47 pass
48
49 def test_for_broken_SAX():
50 ch=testSAXContentHandler()
51 try:
52 xml.sax.parseString("""<?xml version="1.0"?>
53 <child1 name="paul">Text goes here</child1>
54 """,ch)
55 except TypeError:
56 return 1
57 return 0
58
59
60 def parse_color(color):
61 """
62 Return the color object for the string color. Color may be either
63 'None' or of the form '#RRGGBB' in the usual HTML color notation
64 """
65 color = string.strip(color)
66 if color == "None":
67 result = None
68 elif color[0] == '#':
69 if len(color) == 7:
70 r = string.atoi(color[1:3], 16) / 255.0
71 g = string.atoi(color[3:5], 16) / 255.0
72 b = string.atoi(color[5:7], 16) / 255.0
73 result = Color(r, g, b)
74 else:
75 raise ValueError("Invalid hexadecimal color specification %s"
76 % color)
77 else:
78 raise ValueError("Invalid color specification %s" % color)
79 return result
80
81
82 class ProcessSession(HandlerBase):
83
84 def __init__(self, directory):
85 """Inititialize the Sax handler.
86
87 directory is the directory containing the session file. It's
88 needed to interpret embedded relative filenames
89 """
90 self.directory = directory
91 self.chars = ''
92 self.theSession = None
93 self.aMap = None
94 self.aLayer = None
95
96 def startElement(self, name, attrs):
97 if name == 'session':
98 self.theSession = Session(attrs.get('title', None))
99 elif name == 'map':
100 self.aMap = Map(attrs.get('title', None))
101 elif name == 'projection':
102 self.ProjectionParams = [ ]
103 elif name == 'parameter':
104 self.ProjectionParams.append(attrs.get('value', None))
105 elif name == 'layer':
106 title = attrs.get('title', "")
107 filename = attrs.get('filename', "")
108 filename = os.path.join(self.directory, filename)
109 fill = parse_color(attrs.get('fill', "None"))
110 stroke = parse_color(attrs.get('stroke', "#000000"))
111 self.aLayer = Layer(title, filename, fill = fill, stroke = stroke)
112 elif name == 'table':
113 print "table title: %s" % attrs.get('title', None)
114 elif name == 'labellayer':
115 self.aLayer = self.aMap.LabelLayer()
116 elif name == 'label':
117 x = float(attrs['x'])
118 y = float(attrs['y'])
119 text = attrs['text']
120 halign = attrs['halign']
121 valign = attrs['valign']
122 self.aLayer.AddLabel(x, y, text, halign = halign, valign = valign)
123
124
125 if not oldPython and test_for_broken_SAX():
126 # works with python 2.0, but is not SAX compliant
127 def characters(self, ch):
128 self.my_characters(ch)
129 else:
130 # SAX compliant
131 def characters(self, ch, start, length):
132 self.my_characters(ch[start:start+length])
133
134 def my_characters(self, ch):
135 self.chars = self.chars + ch
136
137 def endElement(self, name):
138 # If it's not a parameter element, ignore it
139 if name == 'session':
140 #print "end of session"
141 pass
142 if name == 'map':
143 self.theSession.AddMap(self.aMap)
144 if name == 'projection':
145 self.aMap.SetProjection(Projection(self.ProjectionParams))
146 if name == 'layer':
147 self.aMap.AddLayer(self.aLayer)
148 if name == 'table':
149 #print "end of table"
150 pass
151
152 def load_session(filename):
153 """Load a Thuban session from the file object file"""
154 dir = os.path.dirname(filename)
155 file = open(filename)
156 handler = ProcessSession(dir)
157
158 if oldPython:
159 parser = make_parser()
160 parser.setDocumentHandler(handler)
161 parser.setErrorHandler(saxutils.ErrorPrinter())
162 parser.parseFile(file)
163 parser.close()
164 else:
165 xml.sax.parse(file,handler)
166 session = handler.theSession
167 # Newly loaded session aren't modified
168 session.UnsetModified()
169
170 return session
171
172 if __name__ == "__main__":
173 # find out the command to run
174 if len(sys.argv) > 1:
175 print "usage: cat <file> | " + sys.argv[0]
176 else:
177 parseSession(sys.stdin)

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26