20 |
from xml.sax import make_parser, ErrorHandler, SAXNotRecognizedException |
from xml.sax import make_parser, ErrorHandler, SAXNotRecognizedException |
21 |
|
|
22 |
from Thuban import _ |
from Thuban import _ |
|
from Thuban.common import * |
|
23 |
|
|
24 |
from Thuban.Model.table import FIELDTYPE_INT, FIELDTYPE_DOUBLE, \ |
from Thuban.Model.table import FIELDTYPE_INT, FIELDTYPE_DOUBLE, \ |
25 |
FIELDTYPE_STRING |
FIELDTYPE_STRING |
26 |
|
|
27 |
from Thuban.Model.session import Session |
from Thuban.Model.session import Session |
28 |
from Thuban.Model.map import Map |
from Thuban.Model.map import Map |
29 |
from Thuban.Model.layer import Layer |
from Thuban.Model.layer import Layer, RasterLayer |
30 |
from Thuban.Model.color import Color |
from Thuban.Model.color import Color |
31 |
from Thuban.Model.proj import Projection |
from Thuban.Model.proj import Projection |
32 |
|
from Thuban.Model.range import Range |
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 |
|
|
37 |
|
from Thuban.Model.xmlreader import XMLReader |
38 |
|
import resource |
39 |
|
|
40 |
def parse_color(color): |
def parse_color(color): |
41 |
"""Return the color object for the string color. |
"""Return the color object for the string color. |
59 |
raise ValueError(_("Invalid color specification %s") % color) |
raise ValueError(_("Invalid color specification %s") % color) |
60 |
return result |
return result |
61 |
|
|
|
|
|
|
class XMLReader(xml.sax.handler.ContentHandler): |
|
|
|
|
|
# Dictionary mapping element names (or (URI, element name) pairs for |
|
|
# documents using namespaces) to method names. The methods should |
|
|
# accept the same parameters as the startElement (or startElementNS) |
|
|
# methods. The start_dispatcher is used by the default startElement |
|
|
# and startElementNS methods to call a method for the open tag of an |
|
|
# element. |
|
|
start_dispatcher = {} |
|
|
|
|
|
# end_dispatcher works just like start_dispatcher but it's used by |
|
|
# endElement and endElementNS. The method whose names it maps to |
|
|
# should accept the same parameters as endElement and endElementNS. |
|
|
end_dispatcher = {} |
|
|
|
|
|
|
|
|
def __init__(self): |
|
|
self.chars = '' |
|
|
self.__parser = None |
|
|
self.__directory = "" |
|
|
self.__dispatchers = {} |
|
|
|
|
|
def read(self, file_or_filename): |
|
|
|
|
|
if hasattr(file_or_filename, "read"): |
|
|
# it's a file object |
|
|
self.__directory = "" |
|
|
self.__file = file_or_filename |
|
|
else: |
|
|
filename = file_or_filename |
|
|
self.__directory = os.path.dirname(filename) |
|
|
self.__file = open(filename) |
|
|
|
|
|
if self.__parser is None: |
|
|
self.__parser = make_parser() |
|
|
self.__parser.setContentHandler(self) |
|
|
self.__parser.setErrorHandler(ErrorHandler()) |
|
|
self.__parser.setFeature(xml.sax.handler.feature_namespaces, 1) |
|
|
|
|
|
# |
|
|
# Well, this isn't pretty, but it appears that if you |
|
|
# use Python 2.2 without the site-package _xmlplus then |
|
|
# the following will fail, and without them it will work. |
|
|
# However, if you do have the site-package and you don't |
|
|
# call these functions, the reader raises an exception |
|
|
# |
|
|
# The reason we set these to 0 in the first place is |
|
|
# because there is an unresolved issue with external |
|
|
# entities causing an exception in the reader |
|
|
# |
|
|
try: |
|
|
self.__parser.setFeature(xml.sax.handler.feature_validation,0) |
|
|
self.__parser.setFeature(xml.sax.handler.feature_external_ges,0) |
|
|
self.__parser.setFeature(xml.sax.handler.feature_external_pes,0) |
|
|
except SAXNotRecognizedException: |
|
|
pass |
|
|
|
|
|
self.__parser.parse(self.__file) |
|
|
|
|
|
self.close() |
|
|
|
|
|
def close(self): |
|
|
self.__file.close() |
|
|
|
|
|
def GetFilename(self): |
|
|
if hasattr(self.__file, "name"): |
|
|
return self.__file.name |
|
|
|
|
|
return "" |
|
|
|
|
|
def GetDirectory(self): |
|
|
return self.__directory |
|
|
|
|
|
|
|
|
def AddDispatchers(self, dict): |
|
|
"""Add the function names that should be used to process XML tags. |
|
|
|
|
|
dict -- a dictionary whose keys are XML tag strings and whose values |
|
|
are pairs of strings such that the first string is |
|
|
the name of the function that should be called when the |
|
|
XML tag opens and the second string is the name of the |
|
|
function that should be called when the XML tag closes. |
|
|
If a pair element is None, no function is called. |
|
|
""" |
|
|
|
|
|
self.__dispatchers.update(dict) |
|
|
|
|
|
def startElementNS(self, name, qname, attrs): |
|
|
"""Call the method given for name in self.start_dispatcher |
|
|
""" |
|
|
if name[0] is None: |
|
|
method_name = self.__dispatchers.get(name[1]) |
|
|
else: |
|
|
# Dispatch with namespace |
|
|
method_name = self.__dispatchers.get(name) |
|
|
if method_name is not None and method_name[0] is not None: |
|
|
getattr(self, method_name[0])(name, qname, attrs) |
|
|
|
|
|
def endElementNS(self, name, qname): |
|
|
"""Call the method given for name in self.end_dispatcher |
|
|
""" |
|
|
if name[0] is None: |
|
|
method_name = self.__dispatchers.get(name[1]) |
|
|
else: |
|
|
# Dispatch with namespace |
|
|
method_name = self.__dispatchers.get(name) |
|
|
if method_name is not None and method_name[1] is not None: |
|
|
getattr(self, method_name[1])(name, qname) |
|
|
|
|
62 |
class SessionLoader(XMLReader): |
class SessionLoader(XMLReader): |
63 |
|
|
64 |
def __init__(self): |
def __init__(self): |
75 |
'projection' : ("start_projection", "end_projection"), |
'projection' : ("start_projection", "end_projection"), |
76 |
'parameter' : ("start_parameter", None), |
'parameter' : ("start_parameter", None), |
77 |
'layer' : ("start_layer", "end_layer"), |
'layer' : ("start_layer", "end_layer"), |
78 |
|
'rasterlayer' : ("start_rasterlayer", "end_rasterlayer"), |
79 |
'classification': ("start_classification", "end_classification"), |
'classification': ("start_classification", "end_classification"), |
80 |
'clnull' : ("start_clnull", "end_clnull"), |
'clnull' : ("start_clnull", "end_clnull"), |
81 |
'clpoint' : ("start_clpoint", "end_clpoint"), |
'clpoint' : ("start_clpoint", "end_clpoint"), |
86 |
'label' : ("start_label", None)}) |
'label' : ("start_label", None)}) |
87 |
|
|
88 |
def start_session(self, name, qname, attrs): |
def start_session(self, name, qname, attrs): |
89 |
self.theSession = Session(attrs.get((None, 'title'), None)) |
self.theSession = Session(self.encode(attrs.get((None, 'title'), None))) |
90 |
|
|
91 |
def end_session(self, name, qname): |
def end_session(self, name, qname): |
92 |
pass |
pass |
94 |
def start_map(self, name, qname, attrs): |
def start_map(self, name, qname, attrs): |
95 |
"""Start a map.""" |
"""Start a map.""" |
96 |
self.aMap = Map(attrs.get((None, 'title'), None)) |
self.aMap = Map(attrs.get((None, 'title'), None)) |
|
self.__projReceiver = self.aMap |
|
97 |
|
|
98 |
def end_map(self, name, qname): |
def end_map(self, name, qname): |
99 |
self.theSession.AddMap(self.aMap) |
self.theSession.AddMap(self.aMap) |
100 |
self.__projReceiver = None |
self.aMap = None |
101 |
|
|
102 |
def start_projection(self, name, qname, attrs): |
def start_projection(self, name, qname, attrs): |
103 |
|
self.ProjectionName = self.encode(attrs.get((None, 'name'), None)) |
104 |
self.ProjectionParams = [ ] |
self.ProjectionParams = [ ] |
105 |
|
|
106 |
def end_projection(self, name, qname): |
def end_projection(self, name, qname): |
107 |
self.__projReceiver.SetProjection(Projection(self.ProjectionParams)) |
if self.aLayer is not None: |
108 |
|
obj = self.aLayer |
109 |
|
elif self.aMap is not None: |
110 |
|
obj = self.aMap |
111 |
|
else: |
112 |
|
assert False, "projection tag out of context" |
113 |
|
pass |
114 |
|
|
115 |
|
obj.SetProjection( |
116 |
|
Projection(self.ProjectionParams, self.ProjectionName)) |
117 |
|
|
118 |
def start_parameter(self, name, qname, attrs): |
def start_parameter(self, name, qname, attrs): |
119 |
s = attrs.get((None, 'value')) |
s = attrs.get((None, 'value')) |
127 |
attrs which may be a dictionary as well as the normal SAX attrs |
attrs which may be a dictionary as well as the normal SAX attrs |
128 |
object and bind it to self.aLayer. |
object and bind it to self.aLayer. |
129 |
""" |
""" |
130 |
title = attrs.get((None, 'title'), "") |
title = self.encode(attrs.get((None, 'title'), "")) |
131 |
filename = attrs.get((None, 'filename'), "") |
filename = attrs.get((None, 'filename'), "") |
132 |
filename = os.path.join(self.GetDirectory(), filename) |
filename = os.path.join(self.GetDirectory(), filename) |
133 |
|
filename = self.encode(filename) |
134 |
|
visible = self.encode(attrs.get((None, 'visible'), "true")) != "false" |
135 |
fill = parse_color(attrs.get((None, 'fill'), "None")) |
fill = parse_color(attrs.get((None, 'fill'), "None")) |
136 |
stroke = parse_color(attrs.get((None, 'stroke'), "#000000")) |
stroke = parse_color(attrs.get((None, 'stroke'), "#000000")) |
137 |
stroke_width = int(attrs.get((None, 'stroke_width'), "1")) |
stroke_width = int(attrs.get((None, 'stroke_width'), "1")) |
138 |
self.aLayer = layer_class(title, |
self.aLayer = layer_class(title, |
139 |
self.theSession.OpenShapefile(filename), |
self.theSession.OpenShapefile(filename), |
140 |
fill = fill, stroke = stroke, |
fill = fill, stroke = stroke, |
141 |
lineWidth = stroke_width) |
lineWidth = stroke_width, |
142 |
|
visible = visible) |
|
self.__projReceiver = self.aLayer |
|
143 |
|
|
144 |
def end_layer(self, name, qname): |
def end_layer(self, name, qname): |
145 |
self.aMap.AddLayer(self.aLayer) |
self.aMap.AddLayer(self.aLayer) |
146 |
self.__projReceiver = None |
self.aLayer = None |
147 |
|
|
148 |
|
def start_rasterlayer(self, name, qname, attrs, layer_class = RasterLayer): |
149 |
|
title = self.encode(attrs.get((None, 'title'), "")) |
150 |
|
filename = attrs.get((None, 'filename'), "") |
151 |
|
filename = os.path.join(self.GetDirectory(), filename) |
152 |
|
filename = self.encode(filename) |
153 |
|
visible = self.encode(attrs.get((None, 'visible'), "true")) != "false" |
154 |
|
|
155 |
|
self.aLayer = layer_class(title, filename, visible = visible) |
156 |
|
|
157 |
|
def end_rasterlayer(self, name, qname): |
158 |
|
self.aMap.AddLayer(self.aLayer) |
159 |
|
self.aLayer = None |
160 |
|
|
161 |
def start_classification(self, name, qname, attrs): |
def start_classification(self, name, qname, attrs): |
162 |
field = attrs.get((None, 'field'), None) |
field = attrs.get((None, 'field'), None) |
178 |
|
|
179 |
self.aLayer.GetClassification().SetField(field) |
self.aLayer.GetClassification().SetField(field) |
180 |
|
|
|
|
|
181 |
def end_classification(self, name, qname): |
def end_classification(self, name, qname): |
182 |
pass |
pass |
183 |
|
|
184 |
def start_clnull(self, name, qname, attrs): |
def start_clnull(self, name, qname, attrs): |
185 |
self.cl_group = ClassGroupDefault() |
self.cl_group = ClassGroupDefault() |
186 |
self.cl_group.SetLabel(attrs.get((None, 'label'), "")) |
self.cl_group.SetLabel(self.encode(attrs.get((None, 'label'), ""))) |
187 |
self.cl_prop = ClassGroupProperties() |
self.cl_prop = ClassGroupProperties() |
188 |
|
|
189 |
def end_clnull(self, name, qname): |
def end_clnull(self, name, qname): |
194 |
def start_clpoint(self, name, qname, attrs): |
def start_clpoint(self, name, qname, attrs): |
195 |
attrib_value = attrs.get((None, 'value'), "0") |
attrib_value = attrs.get((None, 'value'), "0") |
196 |
|
|
|
#try: |
|
|
#value = Str2Num(attrib_value) |
|
|
#except: |
|
|
#value = attrib_value |
|
|
|
|
197 |
value = self.conv(attrib_value) |
value = self.conv(attrib_value) |
198 |
|
|
199 |
self.cl_group = ClassGroupSingleton(value) |
self.cl_group = ClassGroupSingleton(value) |
200 |
self.cl_group.SetLabel(attrs.get((None, 'label'), "")) |
self.cl_group.SetLabel(self.encode(attrs.get((None, 'label'), ""))) |
201 |
self.cl_prop = ClassGroupProperties() |
self.cl_prop = ClassGroupProperties() |
202 |
|
|
203 |
|
|
208 |
|
|
209 |
def start_clrange(self, name, qname, attrs): |
def start_clrange(self, name, qname, attrs): |
210 |
|
|
211 |
|
range = attrs.get((None, 'range'), None) |
212 |
|
# for backward compatibility (min/max are not saved) |
213 |
|
min = attrs.get((None, 'min'), None) |
214 |
|
max = attrs.get((None, 'max'), None) |
215 |
|
|
216 |
try: |
try: |
217 |
min = self.conv(attrs.get((None, 'min'), "0")) |
if range is not None: |
218 |
max = self.conv(attrs.get((None, 'max'), "0")) |
self.cl_group = ClassGroupRange(Range(range)) |
219 |
#min = Str2Num(attrs.get((None, 'min'), "0")) |
elif min is not None and max is not None: |
220 |
#max = Str2Num(attrs.get((None, 'max'), "0")) |
self.cl_group = ClassGroupRange(self.conv(min), self.conv(max)) |
221 |
|
else: |
222 |
|
self.cl_group = ClassGroupRange(Range(None)) |
223 |
|
|
224 |
except ValueError: |
except ValueError: |
225 |
raise ValueError(_("Classification range is not a number!")) |
raise ValueError(_("Classification range is not a number!")) |
226 |
|
|
|
self.cl_group = ClassGroupRange(min, max) |
|
227 |
self.cl_group.SetLabel(attrs.get((None, 'label'), "")) |
self.cl_group.SetLabel(attrs.get((None, 'label'), "")) |
228 |
self.cl_prop = ClassGroupProperties() |
self.cl_prop = ClassGroupProperties() |
229 |
|
|
256 |
def start_label(self, name, qname, attrs): |
def start_label(self, name, qname, attrs): |
257 |
x = float(attrs[(None, 'x')]) |
x = float(attrs[(None, 'x')]) |
258 |
y = float(attrs[(None, 'y')]) |
y = float(attrs[(None, 'y')]) |
259 |
text = attrs[(None, 'text')] |
text = self.encode(attrs[(None, 'text')]) |
260 |
halign = attrs[(None, 'halign')] |
halign = attrs[(None, 'halign')] |
261 |
valign = attrs[(None, 'valign')] |
valign = attrs[(None, 'valign')] |
262 |
self.aLayer.AddLabel(x, y, text, halign = halign, valign = valign) |
self.aLayer.AddLabel(x, y, text, halign = halign, valign = valign) |