/[thuban]/trunk/thuban/test/test_save.py
ViewVC logotype

Diff of /trunk/thuban/test/test_save.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 775 by jonathan, Tue Apr 29 14:34:57 2003 UTC revision 1200 by jonathan, Fri Jun 13 15:04:44 2003 UTC
# Line 24  from xml.sax import make_parser, ErrorHa Line 24  from xml.sax import make_parser, ErrorHa
24  import support  import support
25  support.initthuban()  support.initthuban()
26    
27  from Thuban.Model.save import save_session  from Thuban.Model.save import XMLWriter, save_session
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, RasterLayer
31  from Thuban.Model.proj import Projection  from Thuban.Model.proj import Projection
32    
33    from Thuban.Model.classification import ClassGroupSingleton, ClassGroupRange, \
34        ClassGroupProperties
35    
36    from Thuban.Model.range import Range
37    
38    
39  class SaxEventLister(xml.sax.handler.ContentHandler):  class SaxEventLister(xml.sax.handler.ContentHandler):
40    
# Line 70  def sax_eventlist(data): Line 75  def sax_eventlist(data):
75    
76      return handler.eventlist      return handler.eventlist
77    
78    class XMLWriterTest(unittest.TestCase):
79    
80        def testEncode(self):
81            """Test XMLWriter.encode"""
82            writer = XMLWriter()
83            eq = self.assertEquals
84    
85            eq(writer.encode("hello world"), "hello world")
86            eq(writer.encode(unicode("hello world")), unicode("hello world"))
87    
88            eq(writer.encode("\x80\x90\xc2\x100"),
89                             "\xc2\x80\xc2\x90\xc3\x82\x100")
90            eq(writer.encode(u"\x80\x90\xc2\x100"),
91                             "\xc2\x80\xc2\x90\xc3\x82\x100")
92            eq(writer.encode(u"\xFF5E"), "\xc3\xbf5E")
93    
94            eq(writer.encode('&"\'<>'), "&amp;&quot;&apos;&lt;&gt;")
95            eq(writer.encode(unicode('&"\'<>')), "&amp;&quot;&apos;&lt;&gt;")
96    
97  class SaveSessionTest(unittest.TestCase, support.FileTestMixin):  class SaveSessionTest(unittest.TestCase, support.FileTestMixin):
98    
99      def compare_xml(self, xml1, xml2):      def compare_xml(self, xml1, xml2):
# Line 195  class SaveSessionTest(unittest.TestCase, Line 219  class SaveSessionTest(unittest.TestCase,
219          self.compare_xml(written_contents, expected_contents)          self.compare_xml(written_contents, expected_contents)
220    
221                    
222        def testRasterLayer(self):
223            # deliberately put an apersand in the title :)
224            session = Session("single map&layer")
225            map = Map("Test Map")
226            session.AddMap(map)
227            # use shapefile from the example data
228            imgfile = os.path.join(os.path.dirname(__file__),
229                                   os.pardir, "Data", "iceland", "island.tif")
230            layer = RasterLayer("My RasterLayer", imgfile)
231            map.AddLayer(layer)
232                                                                                    
233            filename = self.temp_file_name("save_singlemap.thuban")
234            save_session(session, filename)
235            session.Destroy()
236                                                                                    
237            file = open(filename)
238            written_contents = file.read()
239            file.close()
240            expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
241            <!DOCTYPE session SYSTEM "thuban.dtd">
242            <session title="single map&amp;layer">
243                <map title="Test Map">
244                    <rasterlayer title="My RasterLayer" filename="%s"
245                                 visible="true">
246                    </rasterlayer>
247                </map>
248            </session>''' % os.path.join(os.path.dirname(__file__),
249                                         os.pardir, "Data", "iceland",
250                                         "island.tif")
251            #print written_contents
252            #print "********************************************"
253            #print expected_contents
254            self.compare_xml(written_contents, expected_contents)
255    
256        def testClassifiedLayer(self):
257            """Save a session with a single map with a single layer
258               with a classificaton.
259            """
260            # deliberately put an apersand in the title :)
261            session = Session("single map&layer")
262            proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
263            map = Map("Test Map", projection = proj)
264            session.AddMap(map)
265            # use shapefile from the example data
266            shpfile = os.path.join(os.path.dirname(__file__),
267                                   os.pardir, "Data", "iceland", "political.shp")
268            layer = Layer("My Layer", session.OpenShapefile(shpfile))
269            map.AddLayer(layer)
270    
271            clazz = layer.GetClassification()
272    
273            clazz.SetField("AREA")
274    
275            clazz.AppendGroup(ClassGroupSingleton(42,
276                                               ClassGroupProperties(),
277                                               "single"))
278            clazz.AppendGroup(ClassGroupSingleton("text",
279                                               ClassGroupProperties(),
280                                               "single-text"))
281    
282            clazz.AppendGroup(ClassGroupRange(0, 42,
283                                               ClassGroupProperties(),
284                                               "range"))
285    
286            range = ClassGroupRange(Range("[0;42]"))
287            range.SetProperties(ClassGroupProperties())
288            range.SetLabel("new-range")
289            clazz.AppendGroup(range)
290    
291            filename = self.temp_file_name("save_singlemap.thuban")
292            save_session(session, filename)
293    
294            file = open(filename)
295            written_contents = file.read()
296            file.close()
297            expected_template = '''<?xml version="1.0" encoding="UTF-8"?>
298            <!DOCTYPE session SYSTEM "thuban.dtd">
299            <session title="single map&amp;layer">
300                <map title="Test Map">
301                    <projection name="Unknown">
302                        <parameter value="zone=26"/>
303                        <parameter value="proj=utm"/>
304                        <parameter value="ellps=clrk66"/>
305                    </projection>
306                    <layer title="My Layer" filename="%s"
307                    fill="None" stroke="#000000" stroke_width="1" visible="%s">
308                        <classification field="AREA" field_type="double">
309                            <clnull label="">
310                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
311                            </clnull>
312                            <clpoint value="42" label="single">
313                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
314                            </clpoint>
315                            <clpoint value="text" label="single-text">
316                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
317                            </clpoint>
318                            <clrange range="[0;42[" label="range">
319                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
320                            </clrange>
321                            <clrange range="[0;42]" label="new-range">
322                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
323                            </clrange>
324                        </classification>
325                    </layer>
326                </map>
327            </session>'''
328            
329            expected_contents = expected_template % \
330                (os.path.join("..", "..", "Data", "iceland", "political.shp"),
331                 "true")
332    
333            #print written_contents
334            #print "********************************************"
335            #print expected_contents
336            self.compare_xml(written_contents, expected_contents)
337    
338            session.Destroy()
339    
340    
341  if __name__ == "__main__":  if __name__ == "__main__":

Legend:
Removed from v.775  
changed lines
  Added in v.1200

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26