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 |
from Thuban.Model.range import Range |
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.__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) |
|
|
|
|
|
parser = make_parser() |
|
|
parser.setContentHandler(self) |
|
|
parser.setErrorHandler(ErrorHandler()) |
|
|
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: |
|
|
parser.setFeature(xml.sax.handler.feature_validation,0) |
|
|
parser.setFeature(xml.sax.handler.feature_external_ges,0) |
|
|
parser.setFeature(xml.sax.handler.feature_external_pes,0) |
|
|
except SAXNotRecognizedException: |
|
|
pass |
|
|
|
|
|
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) |
|
|
|
|
|
def encode(self, str): |
|
|
"""Assume that str is in Unicode and encode it into Latin1. |
|
|
|
|
|
If str is None, return None |
|
|
""" |
|
|
|
|
|
if str is not None: |
|
|
return str.encode("latin1") |
|
|
else: |
|
|
return None |
|
|
|
|
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"), |
130 |
title = self.encode(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 |
visible = attrs.get((None, 'visible'), "true") |
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")) |
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 != "false") |
visible = visible) |
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.aLayer = 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) |
163 |
|
|