2 |
# Authors: |
# Authors: |
3 |
# Jan-Oliver Wagner <[email protected]> |
# Jan-Oliver Wagner <[email protected]> |
4 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
5 |
|
# Jonathan Coles <[email protected]> |
6 |
# |
# |
7 |
# This program is free software under the GPL (>=v2) |
# This program is free software under the GPL (>=v2) |
8 |
# Read the file COPYING coming with GRASS for details. |
# Read the file COPYING coming with GRASS for details. |
19 |
import xml.sax.handler |
import xml.sax.handler |
20 |
from xml.sax import make_parser, ErrorHandler |
from xml.sax import make_parser, ErrorHandler |
21 |
|
|
22 |
|
from Thuban import _ |
23 |
|
from Thuban.common import * |
24 |
|
|
25 |
from Thuban.Model.session import Session |
from Thuban.Model.session import Session |
26 |
from Thuban.Model.map import Map |
from Thuban.Model.map import Map |
27 |
from Thuban.Model.layer import Layer |
from Thuban.Model.layer import Layer |
28 |
from Thuban.Model.color import Color |
from Thuban.Model.color import Color |
29 |
from Thuban.Model.proj import Projection |
from Thuban.Model.proj import Projection |
30 |
from Thuban.Model.classification import Classification |
from Thuban.Model.classification import Classification, \ |
31 |
|
ClassDataDefault, ClassDataPoint, ClassDataRange, ClassDataMap |
32 |
|
|
33 |
|
|
34 |
def parse_color(color): |
def parse_color(color): |
39 |
""" |
""" |
40 |
color = string.strip(color) |
color = string.strip(color) |
41 |
if color == "None": |
if color == "None": |
42 |
result = None |
result = Color.None |
43 |
elif color[0] == '#': |
elif color[0] == '#': |
44 |
if len(color) == 7: |
if len(color) == 7: |
45 |
r = string.atoi(color[1:3], 16) / 255.0 |
r = string.atoi(color[1:3], 16) / 255.0 |
47 |
b = string.atoi(color[5:7], 16) / 255.0 |
b = string.atoi(color[5:7], 16) / 255.0 |
48 |
result = Color(r, g, b) |
result = Color(r, g, b) |
49 |
else: |
else: |
50 |
raise ValueError("Invalid hexadecimal color specification %s" |
raise ValueError(_("Invalid hexadecimal color specification %s") |
51 |
% color) |
% color) |
52 |
else: |
else: |
53 |
raise ValueError("Invalid color specification %s" % color) |
raise ValueError(_("Invalid color specification %s") % color) |
54 |
return result |
return result |
55 |
|
|
56 |
|
|
158 |
end_dispatcher['layer'] = "end_layer" |
end_dispatcher['layer'] = "end_layer" |
159 |
|
|
160 |
def start_classification(self, name, qname, attrs): |
def start_classification(self, name, qname, attrs): |
161 |
self.aLayer.classification.setField(attrs.get((None, 'field'), None)) |
self.aLayer.GetClassification().SetField( |
162 |
|
attrs.get((None, 'field'), None)) |
163 |
start_dispatcher['classification'] = "start_classification" |
start_dispatcher['classification'] = "start_classification" |
164 |
|
|
165 |
def end_classification(self, name, qname): |
def end_classification(self, name, qname): |
167 |
end_dispatcher['classification'] = "end_classification" |
end_dispatcher['classification'] = "end_classification" |
168 |
|
|
169 |
def start_clnull(self, name, qname, attrs): |
def start_clnull(self, name, qname, attrs): |
170 |
self.cl_data = {} |
self.cl_data = ClassDataDefault() |
171 |
|
self.cl_data.SetLabel(attrs.get((None, 'label'), "")) |
172 |
start_dispatcher['clnull'] = "start_clnull" |
start_dispatcher['clnull'] = "start_clnull" |
173 |
|
|
174 |
def end_clnull(self, name, qname): |
def end_clnull(self, name, qname): |
175 |
self.aLayer.classification.setNull(self.cl_data) |
self.aLayer.GetClassification().SetDefaultData(self.cl_data) |
176 |
del self.cl_data |
del self.cl_data |
177 |
end_dispatcher['clnull'] = "end_clnull" |
end_dispatcher['clnull'] = "end_clnull" |
178 |
|
|
180 |
attrib_value = attrs.get((None, 'value'), "0") |
attrib_value = attrs.get((None, 'value'), "0") |
181 |
|
|
182 |
try: |
try: |
183 |
self.cl_value = int(attrib_value) |
value = Str2Num(attrib_value) |
184 |
except: |
except: |
185 |
self.cl_value = attrib_value |
value = attrib_value |
186 |
|
|
187 |
|
self.cl_data = ClassDataPoint(value) |
188 |
|
self.cl_data.SetLabel(attrs.get((None, 'label'), "")) |
189 |
|
|
|
self.cl_data = {} |
|
190 |
start_dispatcher['clpoint'] = "start_clpoint" |
start_dispatcher['clpoint'] = "start_clpoint" |
191 |
|
|
192 |
def end_clpoint(self, name, qname): |
def end_clpoint(self, name, qname): |
193 |
self.aLayer.classification.addPoint(self.cl_value, self.cl_data) |
self.aLayer.GetClassification().AddClassData(self.cl_data) |
194 |
del self.cl_value, self.cl_data |
del self.cl_data |
195 |
end_dispatcher['clpoint'] = "end_clpoint" |
end_dispatcher['clpoint'] = "end_clpoint" |
196 |
|
|
197 |
def start_clrange(self, name, qname, attrs): |
def start_clrange(self, name, qname, attrs): |
198 |
|
|
199 |
try: |
try: |
200 |
self.cl_low = int(attrs.get((None, 'low'), "0")) |
min = Str2Num(attrs.get((None, 'min'), "0")) |
201 |
self.cl_high = int(attrs.get((None, 'high'), "0")) |
max = Str2Num(attrs.get((None, 'max'), "0")) |
202 |
except ValueError: |
except ValueError: |
203 |
raise ValueError("Classification range is not a number!") |
raise ValueError(_("Classification range is not a number!")) |
204 |
|
|
205 |
|
self.cl_data = ClassDataRange(min, max) |
206 |
|
self.cl_data.SetLabel(attrs.get((None, 'label'), "")) |
207 |
|
|
|
self.cl_data = {} |
|
208 |
start_dispatcher['clrange'] = "start_clrange" |
start_dispatcher['clrange'] = "start_clrange" |
209 |
|
|
210 |
def end_clrange(self, name, qname): |
def end_clrange(self, name, qname): |
211 |
self.aLayer.classification.addRange( |
self.aLayer.GetClassification().AddClassData(self.cl_data) |
212 |
self.cl_low, self.cl_high, self.cl_data) |
del self.cl_data |
|
del self.cl_low, self.cl_high, self.cl_data |
|
213 |
end_dispatcher['clrange'] = "end_clrange" |
end_dispatcher['clrange'] = "end_clrange" |
214 |
|
|
215 |
def start_cldata(self, name, qname, attrs): |
def start_cldata(self, name, qname, attrs): |
216 |
self.cl_data['stroke'] = parse_color( |
self.cl_data.SetStroke(parse_color(attrs.get((None, 'stroke'), "None"))) |
217 |
attrs.get((None, 'stroke'), "None")) |
self.cl_data.SetStrokeWidth( |
218 |
self.cl_data['stroke_width'] = int( |
int(attrs.get((None, 'stroke_width'), "0"))) |
219 |
attrs.get((None, 'stroke_width'), "0")) |
self.cl_data.SetFill(parse_color(attrs.get((None, 'fill'), "None"))) |
|
self.cl_data['fill'] = parse_color( |
|
|
attrs.get((None, 'fill'), "None")) |
|
220 |
start_dispatcher['cldata'] = "start_cldata" |
start_dispatcher['cldata'] = "start_cldata" |
221 |
|
|
222 |
def end_cldata(self, name, qname): |
def end_cldata(self, name, qname): |