1 |
bh |
723 |
# Copyright (C) 2001, 2002, 2003 by Intevation GmbH |
2 |
bh |
6 |
# Authors: |
3 |
|
|
# Jan-Oliver Wagner <[email protected]> |
4 |
bh |
267 |
# Bernhard Herzog <[email protected]> |
5 |
jonathan |
413 |
# Jonathan Coles <[email protected]> |
6 |
bh |
6 |
# |
7 |
|
|
# This program is free software under the GPL (>=v2) |
8 |
|
|
# Read the file COPYING coming with GRASS for details. |
9 |
|
|
|
10 |
|
|
""" |
11 |
|
|
Parser for thuban session files. |
12 |
|
|
""" |
13 |
|
|
|
14 |
|
|
__version__ = "$Revision$" |
15 |
|
|
|
16 |
bh |
723 |
import string, os |
17 |
bh |
267 |
|
18 |
|
|
import xml.sax |
19 |
|
|
import xml.sax.handler |
20 |
jonathan |
526 |
from xml.sax import make_parser, ErrorHandler, SAXNotRecognizedException |
21 |
bh |
267 |
|
22 |
jan |
374 |
from Thuban import _ |
23 |
jonathan |
413 |
|
24 |
jonathan |
473 |
from Thuban.Model.table import FIELDTYPE_INT, FIELDTYPE_DOUBLE, \ |
25 |
|
|
FIELDTYPE_STRING |
26 |
|
|
|
27 |
bh |
6 |
from Thuban.Model.session import Session |
28 |
|
|
from Thuban.Model.map import Map |
29 |
jonathan |
930 |
from Thuban.Model.layer import Layer, RasterLayer |
30 |
bh |
6 |
from Thuban.Model.color import Color |
31 |
|
|
from Thuban.Model.proj import Projection |
32 |
jonathan |
874 |
from Thuban.Model.range import Range |
33 |
jonathan |
413 |
from Thuban.Model.classification import Classification, \ |
34 |
jonathan |
439 |
ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap, \ |
35 |
|
|
ClassGroupProperties |
36 |
bh |
6 |
|
37 |
jonathan |
1159 |
from Thuban.Model.xmlreader import XMLReader |
38 |
|
|
import resource |
39 |
bh |
6 |
|
40 |
bh |
267 |
def parse_color(color): |
41 |
|
|
"""Return the color object for the string color. |
42 |
bh |
6 |
|
43 |
bh |
267 |
Color may be either 'None' or of the form '#RRGGBB' in the usual |
44 |
|
|
HTML color notation |
45 |
bh |
6 |
""" |
46 |
|
|
color = string.strip(color) |
47 |
|
|
if color == "None": |
48 |
jonathan |
610 |
result = Color.Transparent |
49 |
bh |
6 |
elif color[0] == '#': |
50 |
|
|
if len(color) == 7: |
51 |
|
|
r = string.atoi(color[1:3], 16) / 255.0 |
52 |
|
|
g = string.atoi(color[3:5], 16) / 255.0 |
53 |
|
|
b = string.atoi(color[5:7], 16) / 255.0 |
54 |
|
|
result = Color(r, g, b) |
55 |
|
|
else: |
56 |
jan |
374 |
raise ValueError(_("Invalid hexadecimal color specification %s") |
57 |
bh |
6 |
% color) |
58 |
|
|
else: |
59 |
jan |
374 |
raise ValueError(_("Invalid color specification %s") % color) |
60 |
bh |
6 |
return result |
61 |
|
|
|
62 |
jonathan |
706 |
class SessionLoader(XMLReader): |
63 |
jonathan |
694 |
|
64 |
jonathan |
706 |
def __init__(self): |
65 |
jonathan |
694 |
"""Inititialize the Sax handler.""" |
66 |
jonathan |
706 |
XMLReader.__init__(self) |
67 |
jonathan |
694 |
|
68 |
|
|
self.theSession = None |
69 |
|
|
self.aMap = None |
70 |
|
|
self.aLayer = None |
71 |
|
|
|
72 |
jonathan |
706 |
XMLReader.AddDispatchers(self, |
73 |
|
|
{'session' : ("start_session", "end_session"), |
74 |
|
|
'map' : ("start_map", "end_map"), |
75 |
|
|
'projection' : ("start_projection", "end_projection"), |
76 |
|
|
'parameter' : ("start_parameter", None), |
77 |
|
|
'layer' : ("start_layer", "end_layer"), |
78 |
jonathan |
930 |
'rasterlayer' : ("start_rasterlayer", "end_rasterlayer"), |
79 |
jonathan |
706 |
'classification': ("start_classification", "end_classification"), |
80 |
|
|
'clnull' : ("start_clnull", "end_clnull"), |
81 |
|
|
'clpoint' : ("start_clpoint", "end_clpoint"), |
82 |
|
|
'clrange' : ("start_clrange", "end_clrange"), |
83 |
|
|
'cldata' : ("start_cldata", "end_cldata"), |
84 |
|
|
'table' : ("start_table", "end_table"), |
85 |
|
|
'labellayer' : ("start_labellayer", None), |
86 |
|
|
'label' : ("start_label", None)}) |
87 |
|
|
|
88 |
bh |
267 |
def start_session(self, name, qname, attrs): |
89 |
jonathan |
874 |
self.theSession = Session(self.encode(attrs.get((None, 'title'), None))) |
90 |
bh |
6 |
|
91 |
bh |
267 |
def end_session(self, name, qname): |
92 |
|
|
pass |
93 |
bh |
6 |
|
94 |
bh |
267 |
def start_map(self, name, qname, attrs): |
95 |
|
|
"""Start a map.""" |
96 |
|
|
self.aMap = Map(attrs.get((None, 'title'), None)) |
97 |
|
|
|
98 |
|
|
def end_map(self, name, qname): |
99 |
|
|
self.theSession.AddMap(self.aMap) |
100 |
jonathan |
874 |
self.aMap = None |
101 |
bh |
267 |
|
102 |
|
|
def start_projection(self, name, qname, attrs): |
103 |
jonathan |
874 |
self.ProjectionName = self.encode(attrs.get((None, 'name'), None)) |
104 |
bh |
267 |
self.ProjectionParams = [ ] |
105 |
|
|
|
106 |
|
|
def end_projection(self, name, qname): |
107 |
jonathan |
874 |
if self.aLayer is not None: |
108 |
|
|
obj = self.aLayer |
109 |
|
|
elif self.aMap is not None: |
110 |
|
|
obj = self.aMap |
111 |
|
|
else: |
112 |
|
|
assert False, "projection tag out of context" |
113 |
|
|
pass |
114 |
|
|
|
115 |
|
|
obj.SetProjection( |
116 |
jonathan |
744 |
Projection(self.ProjectionParams, self.ProjectionName)) |
117 |
bh |
267 |
|
118 |
|
|
def start_parameter(self, name, qname, attrs): |
119 |
|
|
s = attrs.get((None, 'value')) |
120 |
|
|
s = str(s) # we can't handle unicode in proj |
121 |
|
|
self.ProjectionParams.append(s) |
122 |
|
|
|
123 |
|
|
def start_layer(self, name, qname, attrs, layer_class = Layer): |
124 |
|
|
"""Start a layer |
125 |
|
|
|
126 |
|
|
Instantiate a layer of class layer_class from the attributes in |
127 |
|
|
attrs which may be a dictionary as well as the normal SAX attrs |
128 |
|
|
object and bind it to self.aLayer. |
129 |
|
|
""" |
130 |
jonathan |
874 |
title = self.encode(attrs.get((None, 'title'), "")) |
131 |
bh |
267 |
filename = attrs.get((None, 'filename'), "") |
132 |
jonathan |
694 |
filename = os.path.join(self.GetDirectory(), filename) |
133 |
jonathan |
930 |
filename = self.encode(filename) |
134 |
|
|
visible = self.encode(attrs.get((None, 'visible'), "true")) != "false" |
135 |
bh |
267 |
fill = parse_color(attrs.get((None, 'fill'), "None")) |
136 |
|
|
stroke = parse_color(attrs.get((None, 'stroke'), "#000000")) |
137 |
|
|
stroke_width = int(attrs.get((None, 'stroke_width'), "1")) |
138 |
bh |
723 |
self.aLayer = layer_class(title, |
139 |
|
|
self.theSession.OpenShapefile(filename), |
140 |
|
|
fill = fill, stroke = stroke, |
141 |
jonathan |
772 |
lineWidth = stroke_width, |
142 |
jonathan |
930 |
visible = visible) |
143 |
bh |
267 |
|
144 |
|
|
def end_layer(self, name, qname): |
145 |
|
|
self.aMap.AddLayer(self.aLayer) |
146 |
jonathan |
874 |
self.aLayer = None |
147 |
bh |
267 |
|
148 |
jonathan |
930 |
def start_rasterlayer(self, name, qname, attrs, layer_class = RasterLayer): |
149 |
|
|
title = self.encode(attrs.get((None, 'title'), "")) |
150 |
|
|
filename = attrs.get((None, 'filename'), "") |
151 |
|
|
filename = os.path.join(self.GetDirectory(), filename) |
152 |
|
|
filename = self.encode(filename) |
153 |
|
|
visible = self.encode(attrs.get((None, 'visible'), "true")) != "false" |
154 |
|
|
|
155 |
|
|
self.aLayer = layer_class(title, filename, visible = visible) |
156 |
|
|
|
157 |
|
|
def end_rasterlayer(self, name, qname): |
158 |
|
|
self.aMap.AddLayer(self.aLayer) |
159 |
|
|
self.aLayer = None |
160 |
|
|
|
161 |
jonathan |
365 |
def start_classification(self, name, qname, attrs): |
162 |
jonathan |
465 |
field = attrs.get((None, 'field'), None) |
163 |
|
|
|
164 |
|
|
fieldType = attrs.get((None, 'field_type'), None) |
165 |
|
|
dbFieldType = self.aLayer.GetFieldType(field) |
166 |
|
|
|
167 |
|
|
if fieldType != dbFieldType: |
168 |
|
|
raise ValueError(_("xml field type differs from database!")) |
169 |
|
|
|
170 |
|
|
# setup conversion routines depending on the kind of data |
171 |
|
|
# we will be seeing later on |
172 |
|
|
if fieldType == FIELDTYPE_STRING: |
173 |
|
|
self.conv = str |
174 |
|
|
elif fieldType == FIELDTYPE_INT: |
175 |
|
|
self.conv = lambda p: int(float(p)) |
176 |
|
|
elif fieldType == FIELDTYPE_DOUBLE: |
177 |
|
|
self.conv = float |
178 |
|
|
|
179 |
|
|
self.aLayer.GetClassification().SetField(field) |
180 |
|
|
|
181 |
jonathan |
365 |
def end_classification(self, name, qname): |
182 |
|
|
pass |
183 |
|
|
|
184 |
|
|
def start_clnull(self, name, qname, attrs): |
185 |
jonathan |
439 |
self.cl_group = ClassGroupDefault() |
186 |
jonathan |
874 |
self.cl_group.SetLabel(self.encode(attrs.get((None, 'label'), ""))) |
187 |
jonathan |
439 |
self.cl_prop = ClassGroupProperties() |
188 |
jonathan |
365 |
|
189 |
|
|
def end_clnull(self, name, qname): |
190 |
jonathan |
439 |
self.cl_group.SetProperties(self.cl_prop) |
191 |
|
|
self.aLayer.GetClassification().SetDefaultGroup(self.cl_group) |
192 |
|
|
del self.cl_group, self.cl_prop |
193 |
jonathan |
365 |
|
194 |
|
|
def start_clpoint(self, name, qname, attrs): |
195 |
|
|
attrib_value = attrs.get((None, 'value'), "0") |
196 |
|
|
|
197 |
jonathan |
465 |
value = self.conv(attrib_value) |
198 |
|
|
|
199 |
jonathan |
439 |
self.cl_group = ClassGroupSingleton(value) |
200 |
jonathan |
874 |
self.cl_group.SetLabel(self.encode(attrs.get((None, 'label'), ""))) |
201 |
jonathan |
439 |
self.cl_prop = ClassGroupProperties() |
202 |
jonathan |
413 |
|
203 |
jonathan |
365 |
|
204 |
|
|
def end_clpoint(self, name, qname): |
205 |
jonathan |
439 |
self.cl_group.SetProperties(self.cl_prop) |
206 |
jonathan |
614 |
self.aLayer.GetClassification().AppendGroup(self.cl_group) |
207 |
jonathan |
439 |
del self.cl_group, self.cl_prop |
208 |
jonathan |
365 |
|
209 |
|
|
def start_clrange(self, name, qname, attrs): |
210 |
|
|
|
211 |
jonathan |
874 |
range = attrs.get((None, 'range'), None) |
212 |
|
|
# for backward compatibility (min/max are not saved) |
213 |
|
|
min = attrs.get((None, 'min'), None) |
214 |
|
|
max = attrs.get((None, 'max'), None) |
215 |
|
|
|
216 |
jonathan |
365 |
try: |
217 |
jonathan |
874 |
if range is not None: |
218 |
|
|
self.cl_group = ClassGroupRange(Range(range)) |
219 |
|
|
elif min is not None and max is not None: |
220 |
|
|
self.cl_group = ClassGroupRange(self.conv(min), self.conv(max)) |
221 |
|
|
else: |
222 |
|
|
self.cl_group = ClassGroupRange(Range(None)) |
223 |
|
|
|
224 |
jonathan |
365 |
except ValueError: |
225 |
jan |
374 |
raise ValueError(_("Classification range is not a number!")) |
226 |
jonathan |
365 |
|
227 |
jonathan |
439 |
self.cl_group.SetLabel(attrs.get((None, 'label'), "")) |
228 |
|
|
self.cl_prop = ClassGroupProperties() |
229 |
jonathan |
413 |
|
230 |
jonathan |
365 |
|
231 |
|
|
def end_clrange(self, name, qname): |
232 |
jonathan |
439 |
self.cl_group.SetProperties(self.cl_prop) |
233 |
jonathan |
614 |
self.aLayer.GetClassification().AppendGroup(self.cl_group) |
234 |
jonathan |
439 |
del self.cl_group, self.cl_prop |
235 |
jonathan |
365 |
|
236 |
|
|
def start_cldata(self, name, qname, attrs): |
237 |
jonathan |
465 |
self.cl_prop.SetLineColor( |
238 |
|
|
parse_color(attrs.get((None, 'stroke'), "None"))) |
239 |
|
|
self.cl_prop.SetLineWidth( |
240 |
jonathan |
390 |
int(attrs.get((None, 'stroke_width'), "0"))) |
241 |
jonathan |
439 |
self.cl_prop.SetFill(parse_color(attrs.get((None, 'fill'), "None"))) |
242 |
jonathan |
365 |
|
243 |
|
|
def end_cldata(self, name, qname): |
244 |
|
|
pass |
245 |
|
|
|
246 |
bh |
267 |
def start_table(self, name, qname, attrs): |
247 |
jonathan |
493 |
#print "table title: %s" % attrs.get('title', None) |
248 |
|
|
pass |
249 |
bh |
267 |
|
250 |
|
|
def end_table(self, name, qname): |
251 |
|
|
pass |
252 |
|
|
|
253 |
|
|
def start_labellayer(self, name, qname, attrs): |
254 |
|
|
self.aLayer = self.aMap.LabelLayer() |
255 |
|
|
|
256 |
|
|
def start_label(self, name, qname, attrs): |
257 |
|
|
x = float(attrs[(None, 'x')]) |
258 |
|
|
y = float(attrs[(None, 'y')]) |
259 |
jonathan |
874 |
text = self.encode(attrs[(None, 'text')]) |
260 |
bh |
267 |
halign = attrs[(None, 'halign')] |
261 |
|
|
valign = attrs[(None, 'valign')] |
262 |
|
|
self.aLayer.AddLabel(x, y, text, halign = halign, valign = valign) |
263 |
|
|
|
264 |
|
|
def characters(self, chars): |
265 |
|
|
pass |
266 |
|
|
|
267 |
|
|
|
268 |
jonathan |
694 |
def load_session(filename): |
269 |
|
|
"""Load a Thuban session from the file object file""" |
270 |
|
|
|
271 |
jonathan |
706 |
handler = SessionLoader() |
272 |
|
|
handler.read(filename) |
273 |
jonathan |
694 |
|
274 |
bh |
6 |
session = handler.theSession |
275 |
|
|
# Newly loaded session aren't modified |
276 |
|
|
session.UnsetModified() |
277 |
|
|
|
278 |
|
|
return session |
279 |
|
|
|