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. |
20 |
from xml.sax import make_parser, ErrorHandler |
from xml.sax import make_parser, ErrorHandler |
21 |
|
|
22 |
from Thuban import _ |
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, ClassData |
from Thuban.Model.classification import Classification, \ |
31 |
|
ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap, \ |
32 |
|
ClassGroupProperties |
33 |
|
|
34 |
|
|
35 |
def parse_color(color): |
def parse_color(color): |
40 |
""" |
""" |
41 |
color = string.strip(color) |
color = string.strip(color) |
42 |
if color == "None": |
if color == "None": |
43 |
result = None |
result = Color.None |
44 |
elif color[0] == '#': |
elif color[0] == '#': |
45 |
if len(color) == 7: |
if len(color) == 7: |
46 |
r = string.atoi(color[1:3], 16) / 255.0 |
r = string.atoi(color[1:3], 16) / 255.0 |
151 |
stroke = parse_color(attrs.get((None, 'stroke'), "#000000")) |
stroke = parse_color(attrs.get((None, 'stroke'), "#000000")) |
152 |
stroke_width = int(attrs.get((None, 'stroke_width'), "1")) |
stroke_width = int(attrs.get((None, 'stroke_width'), "1")) |
153 |
self.aLayer = layer_class(title, filename, fill = fill, |
self.aLayer = layer_class(title, filename, fill = fill, |
154 |
stroke = stroke, stroke_width = stroke_width) |
stroke = stroke, lineWidth = stroke_width) |
155 |
start_dispatcher['layer'] = "start_layer" |
start_dispatcher['layer'] = "start_layer" |
156 |
|
|
157 |
def end_layer(self, name, qname): |
def end_layer(self, name, qname): |
159 |
end_dispatcher['layer'] = "end_layer" |
end_dispatcher['layer'] = "end_layer" |
160 |
|
|
161 |
def start_classification(self, name, qname, attrs): |
def start_classification(self, name, qname, attrs): |
162 |
self.aLayer.classification.SetField(attrs.get((None, 'field'), None)) |
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 |
start_dispatcher['classification'] = "start_classification" |
start_dispatcher['classification'] = "start_classification" |
182 |
|
|
183 |
def end_classification(self, name, qname): |
def end_classification(self, name, qname): |
185 |
end_dispatcher['classification'] = "end_classification" |
end_dispatcher['classification'] = "end_classification" |
186 |
|
|
187 |
def start_clnull(self, name, qname, attrs): |
def start_clnull(self, name, qname, attrs): |
188 |
self.cl_data = ClassData() |
self.cl_group = ClassGroupDefault() |
189 |
|
self.cl_group.SetLabel(attrs.get((None, 'label'), "")) |
190 |
|
self.cl_prop = ClassGroupProperties() |
191 |
start_dispatcher['clnull'] = "start_clnull" |
start_dispatcher['clnull'] = "start_clnull" |
192 |
|
|
193 |
def end_clnull(self, name, qname): |
def end_clnull(self, name, qname): |
194 |
self.aLayer.classification.SetDefaultData(self.cl_data) |
self.cl_group.SetProperties(self.cl_prop) |
195 |
del self.cl_data |
self.aLayer.GetClassification().SetDefaultGroup(self.cl_group) |
196 |
|
del self.cl_group, self.cl_prop |
197 |
end_dispatcher['clnull'] = "end_clnull" |
end_dispatcher['clnull'] = "end_clnull" |
198 |
|
|
199 |
def start_clpoint(self, name, qname, attrs): |
def start_clpoint(self, name, qname, attrs): |
200 |
attrib_value = attrs.get((None, 'value'), "0") |
attrib_value = attrs.get((None, 'value'), "0") |
201 |
|
|
202 |
try: |
#try: |
203 |
self.cl_value = int(attrib_value) |
#value = Str2Num(attrib_value) |
204 |
except: |
#except: |
205 |
self.cl_value = attrib_value |
#value = attrib_value |
206 |
|
|
207 |
|
value = self.conv(attrib_value) |
208 |
|
|
209 |
|
self.cl_group = ClassGroupSingleton(value) |
210 |
|
self.cl_group.SetLabel(attrs.get((None, 'label'), "")) |
211 |
|
self.cl_prop = ClassGroupProperties() |
212 |
|
|
|
self.cl_data = ClassData() |
|
213 |
start_dispatcher['clpoint'] = "start_clpoint" |
start_dispatcher['clpoint'] = "start_clpoint" |
214 |
|
|
215 |
def end_clpoint(self, name, qname): |
def end_clpoint(self, name, qname): |
216 |
self.aLayer.classification.AddPoint(self.cl_value, self.cl_data) |
self.cl_group.SetProperties(self.cl_prop) |
217 |
del self.cl_value, self.cl_data |
self.aLayer.GetClassification().AddGroup(self.cl_group) |
218 |
|
del self.cl_group, self.cl_prop |
219 |
end_dispatcher['clpoint'] = "end_clpoint" |
end_dispatcher['clpoint'] = "end_clpoint" |
220 |
|
|
221 |
def start_clrange(self, name, qname, attrs): |
def start_clrange(self, name, qname, attrs): |
222 |
|
|
223 |
try: |
try: |
224 |
self.cl_low = int(attrs.get((None, 'low'), "0")) |
min = self.conv(attrs.get((None, 'min'), "0")) |
225 |
self.cl_high = int(attrs.get((None, 'high'), "0")) |
max = self.conv(attrs.get((None, 'max'), "0")) |
226 |
|
#min = Str2Num(attrs.get((None, 'min'), "0")) |
227 |
|
#max = Str2Num(attrs.get((None, 'max'), "0")) |
228 |
except ValueError: |
except ValueError: |
229 |
raise ValueError(_("Classification range is not a number!")) |
raise ValueError(_("Classification range is not a number!")) |
230 |
|
|
231 |
self.cl_data = ClassData() |
self.cl_group = ClassGroupRange(min, max) |
232 |
|
self.cl_group.SetLabel(attrs.get((None, 'label'), "")) |
233 |
|
self.cl_prop = ClassGroupProperties() |
234 |
|
|
235 |
start_dispatcher['clrange'] = "start_clrange" |
start_dispatcher['clrange'] = "start_clrange" |
236 |
|
|
237 |
def end_clrange(self, name, qname): |
def end_clrange(self, name, qname): |
238 |
self.aLayer.classification.AddRange( |
self.cl_group.SetProperties(self.cl_prop) |
239 |
self.cl_low, self.cl_high, self.cl_data) |
self.aLayer.GetClassification().AddGroup(self.cl_group) |
240 |
del self.cl_low, self.cl_high, self.cl_data |
del self.cl_group, self.cl_prop |
241 |
end_dispatcher['clrange'] = "end_clrange" |
end_dispatcher['clrange'] = "end_clrange" |
242 |
|
|
243 |
def start_cldata(self, name, qname, attrs): |
def start_cldata(self, name, qname, attrs): |
244 |
self.cl_data.SetStroke(parse_color(attrs.get((None, 'stroke'), "None"))) |
self.cl_prop.SetLineColor( |
245 |
self.cl_data.SetStrokeWidth( |
parse_color(attrs.get((None, 'stroke'), "None"))) |
246 |
|
self.cl_prop.SetLineWidth( |
247 |
int(attrs.get((None, 'stroke_width'), "0"))) |
int(attrs.get((None, 'stroke_width'), "0"))) |
248 |
self.cl_data.SetFill(parse_color(attrs.get((None, 'fill'), "None"))) |
self.cl_prop.SetFill(parse_color(attrs.get((None, 'fill'), "None"))) |
249 |
start_dispatcher['cldata'] = "start_cldata" |
start_dispatcher['cldata'] = "start_cldata" |
250 |
|
|
251 |
def end_cldata(self, name, qname): |
def end_cldata(self, name, qname): |