18 |
import xml.sax.handler |
import xml.sax.handler |
19 |
from xml.sax import make_parser, ErrorHandler |
from xml.sax import make_parser, ErrorHandler |
20 |
|
|
21 |
|
from Thuban import _ |
22 |
from Thuban.Model.session import Session |
from Thuban.Model.session import Session |
23 |
from Thuban.Model.map import Map |
from Thuban.Model.map import Map |
24 |
from Thuban.Model.layer import Layer |
from Thuban.Model.layer import Layer |
25 |
from Thuban.Model.color import Color |
from Thuban.Model.color import Color |
26 |
from Thuban.Model.proj import Projection |
from Thuban.Model.proj import Projection |
27 |
|
from Thuban.Model.classification import Classification |
28 |
|
|
29 |
|
|
30 |
def parse_color(color): |
def parse_color(color): |
43 |
b = string.atoi(color[5:7], 16) / 255.0 |
b = string.atoi(color[5:7], 16) / 255.0 |
44 |
result = Color(r, g, b) |
result = Color(r, g, b) |
45 |
else: |
else: |
46 |
raise ValueError("Invalid hexadecimal color specification %s" |
raise ValueError(_("Invalid hexadecimal color specification %s") |
47 |
% color) |
% color) |
48 |
else: |
else: |
49 |
raise ValueError("Invalid color specification %s" % color) |
raise ValueError(_("Invalid color specification %s") % color) |
50 |
return result |
return result |
51 |
|
|
52 |
|
|
53 |
class ProcessSession(xml.sax.handler.ContentHandler): |
class ProcessSession(xml.sax.handler.ContentHandler): |
54 |
|
|
55 |
# Dictionary mapping element names (or (URI, element name) pairs for |
# Dictionary mapping element names (or (URI, element name) pairs for |
56 |
# documents using amespaces) to method names. The methods should |
# documents using namespaces) to method names. The methods should |
57 |
# accept the same parameters as the startElement (or startElementNS) |
# accept the same parameters as the startElement (or startElementNS) |
58 |
# methods. The start_dispatcher is used by the default startElement |
# methods. The start_dispatcher is used by the default startElement |
59 |
# and startElementNS methods to call a method for the open tag of an |
# and startElementNS methods to call a method for the open tag of an |
153 |
self.aMap.AddLayer(self.aLayer) |
self.aMap.AddLayer(self.aLayer) |
154 |
end_dispatcher['layer'] = "end_layer" |
end_dispatcher['layer'] = "end_layer" |
155 |
|
|
156 |
|
def start_classification(self, name, qname, attrs): |
157 |
|
self.aLayer.classification.setField(attrs.get((None, 'field'), None)) |
158 |
|
start_dispatcher['classification'] = "start_classification" |
159 |
|
|
160 |
|
def end_classification(self, name, qname): |
161 |
|
pass |
162 |
|
end_dispatcher['classification'] = "end_classification" |
163 |
|
|
164 |
|
def start_clnull(self, name, qname, attrs): |
165 |
|
self.cl_data = {} |
166 |
|
start_dispatcher['clnull'] = "start_clnull" |
167 |
|
|
168 |
|
def end_clnull(self, name, qname): |
169 |
|
self.aLayer.classification.setNull(self.cl_data) |
170 |
|
del self.cl_data |
171 |
|
end_dispatcher['clnull'] = "end_clnull" |
172 |
|
|
173 |
|
def start_clpoint(self, name, qname, attrs): |
174 |
|
attrib_value = attrs.get((None, 'value'), "0") |
175 |
|
|
176 |
|
try: |
177 |
|
self.cl_value = int(attrib_value) |
178 |
|
except: |
179 |
|
self.cl_value = attrib_value |
180 |
|
|
181 |
|
self.cl_data = {} |
182 |
|
start_dispatcher['clpoint'] = "start_clpoint" |
183 |
|
|
184 |
|
def end_clpoint(self, name, qname): |
185 |
|
self.aLayer.classification.addPoint(self.cl_value, self.cl_data) |
186 |
|
del self.cl_value, self.cl_data |
187 |
|
end_dispatcher['clpoint'] = "end_clpoint" |
188 |
|
|
189 |
|
def start_clrange(self, name, qname, attrs): |
190 |
|
|
191 |
|
try: |
192 |
|
self.cl_low = int(attrs.get((None, 'low'), "0")) |
193 |
|
self.cl_high = int(attrs.get((None, 'high'), "0")) |
194 |
|
except ValueError: |
195 |
|
raise ValueError(_("Classification range is not a number!")) |
196 |
|
|
197 |
|
self.cl_data = {} |
198 |
|
start_dispatcher['clrange'] = "start_clrange" |
199 |
|
|
200 |
|
def end_clrange(self, name, qname): |
201 |
|
self.aLayer.classification.addRange( |
202 |
|
self.cl_low, self.cl_high, self.cl_data) |
203 |
|
del self.cl_low, self.cl_high, self.cl_data |
204 |
|
end_dispatcher['clrange'] = "end_clrange" |
205 |
|
|
206 |
|
def start_cldata(self, name, qname, attrs): |
207 |
|
self.cl_data['stroke'] = parse_color( |
208 |
|
attrs.get((None, 'stroke'), "None")) |
209 |
|
self.cl_data['stroke_width'] = int( |
210 |
|
attrs.get((None, 'stroke_width'), "0")) |
211 |
|
self.cl_data['fill'] = parse_color( |
212 |
|
attrs.get((None, 'fill'), "None")) |
213 |
|
start_dispatcher['cldata'] = "start_cldata" |
214 |
|
|
215 |
|
def end_cldata(self, name, qname): |
216 |
|
pass |
217 |
|
end_dispatcher['cldata'] = "end_cldata" |
218 |
|
|
219 |
def start_table(self, name, qname, attrs): |
def start_table(self, name, qname, attrs): |
220 |
print "table title: %s" % attrs.get('title', None) |
print "table title: %s" % attrs.get('title', None) |
221 |
start_dispatcher['table'] = "start_table" |
start_dispatcher['table'] = "start_table" |