33 |
from Thuban.Model.classification import Classification, \ |
from Thuban.Model.classification import Classification, \ |
34 |
ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap, \ |
ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap, \ |
35 |
ClassGroupProperties |
ClassGroupProperties |
36 |
|
from Thuban.Model.data import DerivedShapeStore, ShapefileStore |
37 |
|
from Thuban.Model.table import DBFTable |
38 |
|
from Thuban.Model.transientdb import TransientJoinedTable |
39 |
|
|
40 |
|
class LoadError(Exception): |
41 |
|
pass |
42 |
|
|
43 |
from Thuban.Model.xmlreader import XMLReader |
from Thuban.Model.xmlreader import XMLReader |
44 |
import resource |
import resource |
65 |
raise ValueError(_("Invalid color specification %s") % color) |
raise ValueError(_("Invalid color specification %s") % color) |
66 |
return result |
return result |
67 |
|
|
68 |
|
class AttrDesc: |
69 |
|
|
70 |
|
def __init__(self, name, required = False, default = "", |
71 |
|
conversion = None): |
72 |
|
if not isinstance(name, tuple): |
73 |
|
fullname = (None, name) |
74 |
|
else: |
75 |
|
fullname = name |
76 |
|
name = name[1] |
77 |
|
self.name = name |
78 |
|
self.fullname = fullname |
79 |
|
self.required = required |
80 |
|
self.default = default |
81 |
|
self.conversion = conversion |
82 |
|
|
83 |
|
# set by the SessionLoader's check_attrs method |
84 |
|
self.value = None |
85 |
|
|
86 |
|
|
87 |
class SessionLoader(XMLReader): |
class SessionLoader(XMLReader): |
88 |
|
|
89 |
def __init__(self): |
def __init__(self): |
94 |
self.aMap = None |
self.aMap = None |
95 |
self.aLayer = None |
self.aLayer = None |
96 |
|
|
97 |
XMLReader.AddDispatchers(self, |
# Map ids used in the thuban file to the corresponding objects |
98 |
{'session' : ("start_session", "end_session"), |
# in the session |
99 |
'map' : ("start_map", "end_map"), |
self.idmap = {} |
100 |
'projection' : ("start_projection", "end_projection"), |
|
101 |
'parameter' : ("start_parameter", None), |
dispatchers = { |
102 |
'layer' : ("start_layer", "end_layer"), |
'session' : ("start_session", "end_session"), |
103 |
'rasterlayer' : ("start_rasterlayer", "end_rasterlayer"), |
'fileshapesource': ("start_fileshapesource", None), |
104 |
'classification': ("start_classification", "end_classification"), |
'derivedshapesource': ("start_derivedshapesource", None), |
105 |
'clnull' : ("start_clnull", "end_clnull"), |
'filetable': ("start_filetable", None), |
106 |
'clpoint' : ("start_clpoint", "end_clpoint"), |
'jointable': ("start_jointable", None), |
107 |
'clrange' : ("start_clrange", "end_clrange"), |
|
108 |
'cldata' : ("start_cldata", "end_cldata"), |
'map' : ("start_map", "end_map"), |
109 |
'table' : ("start_table", "end_table"), |
'projection' : ("start_projection", "end_projection"), |
110 |
'labellayer' : ("start_labellayer", None), |
'parameter' : ("start_parameter", None), |
111 |
'label' : ("start_label", None)}) |
'layer' : ("start_layer", "end_layer"), |
112 |
|
'rasterlayer' : ("start_rasterlayer", "end_rasterlayer"), |
113 |
|
'classification': ("start_classification", "end_classification"), |
114 |
|
'clnull' : ("start_clnull", "end_clnull"), |
115 |
|
'clpoint' : ("start_clpoint", "end_clpoint"), |
116 |
|
'clrange' : ("start_clrange", "end_clrange"), |
117 |
|
'cldata' : ("start_cldata", "end_cldata"), |
118 |
|
'table' : ("start_table", "end_table"), |
119 |
|
'labellayer' : ("start_labellayer", None), |
120 |
|
'label' : ("start_label", None)} |
121 |
|
|
122 |
|
# all dispatchers should be used for the 0.8 namespace |
123 |
|
xmlns = "http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
124 |
|
for key, value in dispatchers.items(): |
125 |
|
dispatchers[(xmlns, key)] = value |
126 |
|
|
127 |
|
XMLReader.AddDispatchers(self, dispatchers) |
128 |
|
|
129 |
def start_session(self, name, qname, attrs): |
def start_session(self, name, qname, attrs): |
130 |
self.theSession = Session(self.encode(attrs.get((None, 'title'), None))) |
self.theSession = Session(self.encode(attrs.get((None, 'title'), |
131 |
|
None))) |
132 |
|
|
133 |
def end_session(self, name, qname): |
def end_session(self, name, qname): |
134 |
pass |
pass |
135 |
|
|
136 |
|
def check_attrs(self, element, attrs, descr): |
137 |
|
"""Check and convert some of the attributes of an element |
138 |
|
|
139 |
|
Parameters: |
140 |
|
element -- The element name |
141 |
|
attrs -- The attrs mapping as passed to the start_* methods |
142 |
|
descr -- Sequence of attribute descriptions (AttrDesc instances) |
143 |
|
|
144 |
|
Return a dictionary containig normalized versions of the |
145 |
|
attributes described in descr. The keys of that dictionary are |
146 |
|
the name attributes of the attribute descriptions. The attrs |
147 |
|
dictionary will not be modified. |
148 |
|
|
149 |
|
If the attribute is required, i.e. the 'required' attribute of |
150 |
|
the descrtiption is true, but it is not in attrs, raise a |
151 |
|
LoadError. |
152 |
|
|
153 |
|
If the attribute has a default value and it is not present in |
154 |
|
attrs, use that default value as the value in the returned dict. |
155 |
|
|
156 |
|
If a conversion is specified, convert the value before putting |
157 |
|
it into the returned dict. The following conversions are |
158 |
|
available: |
159 |
|
|
160 |
|
'filename' -- The attribute is a filename. |
161 |
|
|
162 |
|
If the filename is a relative name, interpret |
163 |
|
it relative to the directory containing the |
164 |
|
.thuban file and make it an absolute name |
165 |
|
|
166 |
|
'shapestore' -- The attribute is the ID of a shapestore |
167 |
|
defined earlier in the .thuban file. Look it |
168 |
|
up self.idmap |
169 |
|
|
170 |
|
'table' -- The attribute is the ID of a table or shapestore |
171 |
|
defined earlier in the .thuban file. Look it up |
172 |
|
self.idmap. If it's the ID of a shapestore the |
173 |
|
value will be the table of the shapestore. |
174 |
|
""" |
175 |
|
normalized = {} |
176 |
|
|
177 |
|
for d in descr: |
178 |
|
if d.required and not attrs.has_key(d.fullname): |
179 |
|
pass |
180 |
|
#raise LoadError("Element %s requires an attribute %r" |
181 |
|
# % (element, d.name)) |
182 |
|
value = attrs.get(d.fullname, d.default) |
183 |
|
|
184 |
|
if d.conversion == "shapesource": |
185 |
|
if value in self.idmap: |
186 |
|
value = self.idmap[value] |
187 |
|
else: |
188 |
|
raise LoadError("Element %s requires an already defined ID" |
189 |
|
" in attribute %r" |
190 |
|
% (element, d.name)) |
191 |
|
elif d.conversion == "table": |
192 |
|
if value in self.idmap: |
193 |
|
value = self.idmap[value] |
194 |
|
if isinstance(value, ShapefileStore): |
195 |
|
value = value.Table() |
196 |
|
else: |
197 |
|
raise LoadError("Element %s requires an already defined ID" |
198 |
|
" in attribute %r" |
199 |
|
% (element, d.name)) |
200 |
|
elif d.conversion == "filename": |
201 |
|
value = os.path.abspath(os.path.join(self.GetDirectory(), |
202 |
|
value)) |
203 |
|
|
204 |
|
normalized[d.name] = value |
205 |
|
return normalized |
206 |
|
|
207 |
|
def start_fileshapesource(self, name, qname, attrs): |
208 |
|
attrs = self.check_attrs(name, attrs, |
209 |
|
[AttrDesc("id", True), |
210 |
|
AttrDesc("filename", True, |
211 |
|
conversion = "filename"), |
212 |
|
AttrDesc("filetype", True)]) |
213 |
|
ID = attrs["id"] |
214 |
|
filename = attrs["filename"] |
215 |
|
filetype = attrs["filetype"] |
216 |
|
if filetype != "shapefile": |
217 |
|
raise LoadError("shapesource filetype %r not supported" % filetype) |
218 |
|
self.idmap[ID] = self.theSession.OpenShapefile(filename) |
219 |
|
|
220 |
|
def start_derivedshapesource(self, name, qname, attrs): |
221 |
|
attrs = self.check_attrs(name, attrs, |
222 |
|
[AttrDesc("id", True), |
223 |
|
AttrDesc("shapesource", True, |
224 |
|
conversion = "shapesource"), |
225 |
|
AttrDesc("table", True, conversion="table")]) |
226 |
|
store = DerivedShapeStore(attrs["shapesource"], attrs["table"]) |
227 |
|
self.theSession.AddShapeStore(store) |
228 |
|
self.idmap[attrs["id"]] = store |
229 |
|
|
230 |
|
def start_filetable(self, name, qname, attrs): |
231 |
|
attrs = self.check_attrs(name, attrs, |
232 |
|
[AttrDesc("id", True), |
233 |
|
AttrDesc("title", True), |
234 |
|
AttrDesc("filename", True, |
235 |
|
conversion = "filename"), |
236 |
|
AttrDesc("filetype")]) |
237 |
|
filetype = attrs["filetype"] |
238 |
|
if filetype != "DBF": |
239 |
|
raise LoadError("shapesource filetype %r not supported" % filetype) |
240 |
|
table = DBFTable(attrs["filename"]) |
241 |
|
table.SetTitle(attrs["title"]) |
242 |
|
self.idmap[attrs["id"]] = self.theSession.AddTable(table) |
243 |
|
|
244 |
|
def start_jointable(self, name, qname, attrs): |
245 |
|
attrs = self.check_attrs(name, attrs, |
246 |
|
[AttrDesc("id", True), |
247 |
|
AttrDesc("title", True), |
248 |
|
AttrDesc("left", True, conversion="table"), |
249 |
|
AttrDesc("leftcolumn", True), |
250 |
|
AttrDesc("right", True, conversion="table"), |
251 |
|
AttrDesc("rightcolumn")]) |
252 |
|
table = TransientJoinedTable(self.theSession.TransientDB(), |
253 |
|
attrs["left"], attrs["leftcolumn"], |
254 |
|
attrs["right"], attrs["rightcolumn"]) |
255 |
|
table.SetTitle(attrs["title"]) |
256 |
|
self.idmap[attrs["id"]] = self.theSession.AddTable(table) |
257 |
|
|
258 |
def start_map(self, name, qname, attrs): |
def start_map(self, name, qname, attrs): |
259 |
"""Start a map.""" |
"""Start a map.""" |
260 |
self.aMap = Map(attrs.get((None, 'title'), None)) |
self.aMap = Map(attrs.get((None, 'title'), None)) |
299 |
fill = parse_color(attrs.get((None, 'fill'), "None")) |
fill = parse_color(attrs.get((None, 'fill'), "None")) |
300 |
stroke = parse_color(attrs.get((None, 'stroke'), "#000000")) |
stroke = parse_color(attrs.get((None, 'stroke'), "#000000")) |
301 |
stroke_width = int(attrs.get((None, 'stroke_width'), "1")) |
stroke_width = int(attrs.get((None, 'stroke_width'), "1")) |
302 |
self.aLayer = layer_class(title, |
if attrs.has_key((None, "shapestore")): |
303 |
self.theSession.OpenShapefile(filename), |
store = self.idmap[attrs[(None, "shapestore")]] |
304 |
|
else: |
305 |
|
store = self.theSession.OpenShapefile(filename) |
306 |
|
self.aLayer = layer_class(title, store, |
307 |
fill = fill, stroke = stroke, |
fill = fill, stroke = stroke, |
308 |
lineWidth = stroke_width, |
lineWidth = stroke_width, |
309 |
visible = visible) |
visible = visible) |
410 |
def end_cldata(self, name, qname): |
def end_cldata(self, name, qname): |
411 |
pass |
pass |
412 |
|
|
|
def start_table(self, name, qname, attrs): |
|
|
#print "table title: %s" % attrs.get('title', None) |
|
|
pass |
|
|
|
|
|
def end_table(self, name, qname): |
|
|
pass |
|
|
|
|
413 |
def start_labellayer(self, name, qname, attrs): |
def start_labellayer(self, name, qname, attrs): |
414 |
self.aLayer = self.aMap.LabelLayer() |
self.aLayer = self.aMap.LabelLayer() |
415 |
|
|