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.table import FIELDTYPE_INT, FIELDTYPE_DOUBLE, \ |
26 |
|
FIELDTYPE_STRING |
27 |
|
|
28 |
from Thuban.Model.session import Session |
from Thuban.Model.session import Session |
29 |
from Thuban.Model.map import Map |
from Thuban.Model.map import Map |
30 |
from Thuban.Model.layer import Layer |
from Thuban.Model.layer import Layer |
31 |
from Thuban.Model.color import Color |
from Thuban.Model.color import Color |
32 |
from Thuban.Model.proj import Projection |
from Thuban.Model.proj import Projection |
33 |
from Thuban.Model.classification import Classification |
from Thuban.Model.classification import Classification, \ |
34 |
|
ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap, \ |
35 |
|
ClassGroupProperties |
36 |
|
|
37 |
|
|
38 |
def parse_color(color): |
def parse_color(color): |
43 |
""" |
""" |
44 |
color = string.strip(color) |
color = string.strip(color) |
45 |
if color == "None": |
if color == "None": |
46 |
result = None |
result = Color.None |
47 |
elif color[0] == '#': |
elif color[0] == '#': |
48 |
if len(color) == 7: |
if len(color) == 7: |
49 |
r = string.atoi(color[1:3], 16) / 255.0 |
r = string.atoi(color[1:3], 16) / 255.0 |
51 |
b = string.atoi(color[5:7], 16) / 255.0 |
b = string.atoi(color[5:7], 16) / 255.0 |
52 |
result = Color(r, g, b) |
result = Color(r, g, b) |
53 |
else: |
else: |
54 |
raise ValueError("Invalid hexadecimal color specification %s" |
raise ValueError(_("Invalid hexadecimal color specification %s") |
55 |
% color) |
% color) |
56 |
else: |
else: |
57 |
raise ValueError("Invalid color specification %s" % color) |
raise ValueError(_("Invalid color specification %s") % color) |
58 |
return result |
return result |
59 |
|
|
60 |
|
|
154 |
stroke = parse_color(attrs.get((None, 'stroke'), "#000000")) |
stroke = parse_color(attrs.get((None, 'stroke'), "#000000")) |
155 |
stroke_width = int(attrs.get((None, 'stroke_width'), "1")) |
stroke_width = int(attrs.get((None, 'stroke_width'), "1")) |
156 |
self.aLayer = layer_class(title, filename, fill = fill, |
self.aLayer = layer_class(title, filename, fill = fill, |
157 |
stroke = stroke, stroke_width = stroke_width) |
stroke = stroke, lineWidth = stroke_width) |
158 |
start_dispatcher['layer'] = "start_layer" |
start_dispatcher['layer'] = "start_layer" |
159 |
|
|
160 |
def end_layer(self, name, qname): |
def end_layer(self, name, qname): |
162 |
end_dispatcher['layer'] = "end_layer" |
end_dispatcher['layer'] = "end_layer" |
163 |
|
|
164 |
def start_classification(self, name, qname, attrs): |
def start_classification(self, name, qname, attrs): |
165 |
self.aLayer.classification.setField(attrs.get((None, 'field'), None)) |
field = attrs.get((None, 'field'), None) |
166 |
|
|
167 |
|
fieldType = attrs.get((None, 'field_type'), None) |
168 |
|
dbFieldType = self.aLayer.GetFieldType(field) |
169 |
|
|
170 |
|
if fieldType != dbFieldType: |
171 |
|
raise ValueError(_("xml field type differs from database!")) |
172 |
|
|
173 |
|
# setup conversion routines depending on the kind of data |
174 |
|
# we will be seeing later on |
175 |
|
if fieldType == FIELDTYPE_STRING: |
176 |
|
self.conv = str |
177 |
|
elif fieldType == FIELDTYPE_INT: |
178 |
|
self.conv = lambda p: int(float(p)) |
179 |
|
elif fieldType == FIELDTYPE_DOUBLE: |
180 |
|
self.conv = float |
181 |
|
|
182 |
|
self.aLayer.GetClassification().SetField(field) |
183 |
|
|
184 |
start_dispatcher['classification'] = "start_classification" |
start_dispatcher['classification'] = "start_classification" |
185 |
|
|
186 |
def end_classification(self, name, qname): |
def end_classification(self, name, qname): |
188 |
end_dispatcher['classification'] = "end_classification" |
end_dispatcher['classification'] = "end_classification" |
189 |
|
|
190 |
def start_clnull(self, name, qname, attrs): |
def start_clnull(self, name, qname, attrs): |
191 |
self.cl_data = {} |
self.cl_group = ClassGroupDefault() |
192 |
|
self.cl_group.SetLabel(attrs.get((None, 'label'), "")) |
193 |
|
self.cl_prop = ClassGroupProperties() |
194 |
start_dispatcher['clnull'] = "start_clnull" |
start_dispatcher['clnull'] = "start_clnull" |
195 |
|
|
196 |
def end_clnull(self, name, qname): |
def end_clnull(self, name, qname): |
197 |
self.aLayer.classification.setNull(self.cl_data) |
self.cl_group.SetProperties(self.cl_prop) |
198 |
del self.cl_data |
self.aLayer.GetClassification().SetDefaultGroup(self.cl_group) |
199 |
|
del self.cl_group, self.cl_prop |
200 |
end_dispatcher['clnull'] = "end_clnull" |
end_dispatcher['clnull'] = "end_clnull" |
201 |
|
|
202 |
def start_clpoint(self, name, qname, attrs): |
def start_clpoint(self, name, qname, attrs): |
203 |
attrib_value = attrs.get((None, 'value'), "0") |
attrib_value = attrs.get((None, 'value'), "0") |
204 |
|
|
205 |
try: |
#try: |
206 |
self.cl_value = int(attrib_value) |
#value = Str2Num(attrib_value) |
207 |
except: |
#except: |
208 |
self.cl_value = attrib_value |
#value = attrib_value |
209 |
|
|
210 |
|
value = self.conv(attrib_value) |
211 |
|
|
212 |
|
self.cl_group = ClassGroupSingleton(value) |
213 |
|
self.cl_group.SetLabel(attrs.get((None, 'label'), "")) |
214 |
|
self.cl_prop = ClassGroupProperties() |
215 |
|
|
|
self.cl_data = {} |
|
216 |
start_dispatcher['clpoint'] = "start_clpoint" |
start_dispatcher['clpoint'] = "start_clpoint" |
217 |
|
|
218 |
def end_clpoint(self, name, qname): |
def end_clpoint(self, name, qname): |
219 |
self.aLayer.classification.addPoint(self.cl_value, self.cl_data) |
self.cl_group.SetProperties(self.cl_prop) |
220 |
del self.cl_value, self.cl_data |
self.aLayer.GetClassification().AddGroup(self.cl_group) |
221 |
|
del self.cl_group, self.cl_prop |
222 |
end_dispatcher['clpoint'] = "end_clpoint" |
end_dispatcher['clpoint'] = "end_clpoint" |
223 |
|
|
224 |
def start_clrange(self, name, qname, attrs): |
def start_clrange(self, name, qname, attrs): |
225 |
|
|
226 |
try: |
try: |
227 |
self.cl_low = int(attrs.get((None, 'low'), "0")) |
min = self.conv(attrs.get((None, 'min'), "0")) |
228 |
self.cl_high = int(attrs.get((None, 'high'), "0")) |
max = self.conv(attrs.get((None, 'max'), "0")) |
229 |
|
#min = Str2Num(attrs.get((None, 'min'), "0")) |
230 |
|
#max = Str2Num(attrs.get((None, 'max'), "0")) |
231 |
except ValueError: |
except ValueError: |
232 |
raise ValueError("Classification range is not a number!") |
raise ValueError(_("Classification range is not a number!")) |
233 |
|
|
234 |
|
self.cl_group = ClassGroupRange(min, max) |
235 |
|
self.cl_group.SetLabel(attrs.get((None, 'label'), "")) |
236 |
|
self.cl_prop = ClassGroupProperties() |
237 |
|
|
|
self.cl_data = {} |
|
238 |
start_dispatcher['clrange'] = "start_clrange" |
start_dispatcher['clrange'] = "start_clrange" |
239 |
|
|
240 |
def end_clrange(self, name, qname): |
def end_clrange(self, name, qname): |
241 |
self.aLayer.classification.addRange( |
self.cl_group.SetProperties(self.cl_prop) |
242 |
self.cl_low, self.cl_high, self.cl_data) |
self.aLayer.GetClassification().AddGroup(self.cl_group) |
243 |
del self.cl_low, self.cl_high, self.cl_data |
del self.cl_group, self.cl_prop |
244 |
end_dispatcher['clrange'] = "end_clrange" |
end_dispatcher['clrange'] = "end_clrange" |
245 |
|
|
246 |
def start_cldata(self, name, qname, attrs): |
def start_cldata(self, name, qname, attrs): |
247 |
self.cl_data['stroke'] = parse_color( |
self.cl_prop.SetLineColor( |
248 |
attrs.get((None, 'stroke'), "None")) |
parse_color(attrs.get((None, 'stroke'), "None"))) |
249 |
self.cl_data['stroke_width'] = int( |
self.cl_prop.SetLineWidth( |
250 |
attrs.get((None, 'stroke_width'), "0")) |
int(attrs.get((None, 'stroke_width'), "0"))) |
251 |
self.cl_data['fill'] = parse_color( |
self.cl_prop.SetFill(parse_color(attrs.get((None, 'fill'), "None"))) |
|
attrs.get((None, 'fill'), "None")) |
|
252 |
start_dispatcher['cldata'] = "start_cldata" |
start_dispatcher['cldata'] = "start_cldata" |
253 |
|
|
254 |
def end_cldata(self, name, qname): |
def end_cldata(self, name, qname): |
256 |
end_dispatcher['cldata'] = "end_cldata" |
end_dispatcher['cldata'] = "end_cldata" |
257 |
|
|
258 |
def start_table(self, name, qname, attrs): |
def start_table(self, name, qname, attrs): |
259 |
print "table title: %s" % attrs.get('title', None) |
#print "table title: %s" % attrs.get('title', None) |
260 |
|
pass |
261 |
start_dispatcher['table'] = "start_table" |
start_dispatcher['table'] = "start_table" |
262 |
|
|
263 |
def end_table(self, name, qname): |
def end_table(self, name, qname): |