/[thuban]/branches/WIP-pyshapelib-bramz/test/test_save.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/test/test_save.py

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

revision 947 by jonathan, Tue May 20 15:27:19 2003 UTC revision 1168 by jonathan, Thu Jun 12 12:42:50 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, RasterLayer  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    
84            writer.encode("hello world")
85            writer.encode(unicode("hello world"))
86            self.assertEquals(writer.encode('&"\'<>'),
87                              "&amp;&quot;&apos;&lt;&gt;")
88            self.assertEquals(writer.encode(unicode('&"\'<>')),
89                              "&amp;&quot;&apos;&lt;&gt;")
90    
91  class SaveSessionTest(unittest.TestCase, support.FileTestMixin):  class SaveSessionTest(unittest.TestCase, support.FileTestMixin):
92    
93      def compare_xml(self, xml1, xml2):      def compare_xml(self, xml1, xml2):
# Line 229  class SaveSessionTest(unittest.TestCase, Line 247  class SaveSessionTest(unittest.TestCase,
247          #print expected_contents          #print expected_contents
248          self.compare_xml(written_contents, expected_contents)          self.compare_xml(written_contents, expected_contents)
249    
250        def testClassifiedLayer(self):
251            """Save a session with a single map with a single layer
252               with a classificaton.
253            """
254            # deliberately put an apersand in the title :)
255            session = Session("single map&layer")
256            proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
257            map = Map("Test Map", projection = proj)
258            session.AddMap(map)
259            # use shapefile from the example data
260            shpfile = os.path.join(os.path.dirname(__file__),
261                                   os.pardir, "Data", "iceland", "political.shp")
262            layer = Layer("My Layer", session.OpenShapefile(shpfile))
263            map.AddLayer(layer)
264    
265            clazz = layer.GetClassification()
266    
267            clazz.SetField("AREA")
268    
269            clazz.AppendGroup(ClassGroupSingleton(42,
270                                               ClassGroupProperties(),
271                                               "single"))
272            clazz.AppendGroup(ClassGroupSingleton("text",
273                                               ClassGroupProperties(),
274                                               "single-text"))
275    
276            clazz.AppendGroup(ClassGroupRange(0, 42,
277                                               ClassGroupProperties(),
278                                               "range"))
279    
280            range = ClassGroupRange(Range("[0;42]"))
281            range.SetProperties(ClassGroupProperties())
282            range.SetLabel("new-range")
283            clazz.AppendGroup(range)
284    
285            filename = self.temp_file_name("save_singlemap.thuban")
286            save_session(session, filename)
287    
288            file = open(filename)
289            written_contents = file.read()
290            file.close()
291            expected_template = '''<?xml version="1.0" encoding="UTF-8"?>
292            <!DOCTYPE session SYSTEM "thuban.dtd">
293            <session title="single map&amp;layer">
294                <map title="Test Map">
295                    <projection name="Unknown">
296                        <parameter value="zone=26"/>
297                        <parameter value="proj=utm"/>
298                        <parameter value="ellps=clrk66"/>
299                    </projection>
300                    <layer title="My Layer" filename="%s"
301                    fill="None" stroke="#000000" stroke_width="1" visible="%s">
302                        <classification field="AREA" field_type="double">
303                            <clnull label="">
304                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
305                            </clnull>
306                            <clpoint value="42" label="single">
307                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
308                            </clpoint>
309                            <clpoint value="text" label="single-text">
310                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
311                            </clpoint>
312                            <clrange range="[0;42[" label="range">
313                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
314                            </clrange>
315                            <clrange range="[0;42]" label="new-range">
316                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
317                            </clrange>
318                        </classification>
319                    </layer>
320                </map>
321            </session>'''
322            
323            expected_contents = expected_template % \
324                (os.path.join("..", "..", "Data", "iceland", "political.shp"),
325                 "true")
326    
327            #print written_contents
328            #print "********************************************"
329            #print expected_contents
330            self.compare_xml(written_contents, expected_contents)
331    
332            session.Destroy()
333    
334    
335  if __name__ == "__main__":  if __name__ == "__main__":
336      # Fake the __file__ global because it's needed by a test      # Fake the __file__ global because it's needed by a test

Legend:
Removed from v.947  
changed lines
  Added in v.1168

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26