/[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 755 by jonathan, Fri Apr 25 14:48:35 2003 UTC revision 1173 by jonathan, Thu Jun 12 13:37:18 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    
84            writer.encode("hello world")
85            writer.encode(unicode("hello world"))
86    
87            writer.encode("\x80\x90\xc2\x100")
88            writer.encode(u"\x80\x90\xc2\x100")
89            writer.encode(u"\xFF5E")
90    
91            self.assertEquals(writer.encode('&"\'<>'),
92                              "&amp;&quot;&apos;&lt;&gt;")
93            self.assertEquals(writer.encode(unicode('&"\'<>')),
94                              "&amp;&quot;&apos;&lt;&gt;")
95    
96  class SaveSessionTest(unittest.TestCase, support.FileTestMixin):  class SaveSessionTest(unittest.TestCase, support.FileTestMixin):
97    
98      def compare_xml(self, xml1, xml2):      def compare_xml(self, xml1, xml2):
# Line 105  class SaveSessionTest(unittest.TestCase, Line 128  class SaveSessionTest(unittest.TestCase,
128    
129          filename = self.temp_file_name("save_singlemap.thuban")          filename = self.temp_file_name("save_singlemap.thuban")
130          save_session(session, filename)          save_session(session, filename)
         session.Destroy()  
131    
132          file = open(filename)          file = open(filename)
133          written_contents = file.read()          written_contents = file.read()
134          file.close()          file.close()
135          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>          expected_template = '''<?xml version="1.0" encoding="UTF-8"?>
136          <!DOCTYPE session SYSTEM "thuban.dtd">          <!DOCTYPE session SYSTEM "thuban.dtd">
137          <session title="single map&amp;layer">          <session title="single map&amp;layer">
138              <map title="Test Map">              <map title="Test Map">
# Line 120  class SaveSessionTest(unittest.TestCase, Line 142  class SaveSessionTest(unittest.TestCase,
142                      <parameter value="ellps=clrk66"/>                      <parameter value="ellps=clrk66"/>
143                  </projection>                  </projection>
144                  <layer title="My Layer" filename="%s"                  <layer title="My Layer" filename="%s"
145                  fill="None" stroke="#000000" stroke_width="1"/>                  fill="None" stroke="#000000" stroke_width="1" visible="%s"/>
146              </map>              </map>
147          </session>''' % os.path.join("..", "..", "Data", "iceland",          </session>'''
148                                       "political.shp")          
149            expected_contents = expected_template % \
150                (os.path.join("..", "..", "Data", "iceland", "political.shp"),
151                 "true")
152    
153            #print written_contents
154            #print "********************************************"
155            #print expected_contents
156            self.compare_xml(written_contents, expected_contents)
157    
158            layer.SetVisible(False)
159            save_session(session, filename)
160    
161            file = open(filename)
162            written_contents = file.read()
163            file.close()
164            expected_contents = expected_template % \
165                (os.path.join("..", "..", "Data", "iceland", "political.shp"),
166                 "false")
167    
168          #print written_contents          #print written_contents
169          #print "********************************************"          #print "********************************************"
170          #print expected_contents          #print expected_contents
171          self.compare_xml(written_contents, expected_contents)          self.compare_xml(written_contents, expected_contents)
172    
173            session.Destroy()
174    
175      def testLayerProjection(self):      def testLayerProjection(self):
176          # deliberately put an apersand in the title :)          # deliberately put an apersand in the title :)
177          session = Session("single map&layer")          session = Session("single map&layer")
# Line 160  class SaveSessionTest(unittest.TestCase, Line 203  class SaveSessionTest(unittest.TestCase,
203                      <parameter value="ellps=clrk66"/>                      <parameter value="ellps=clrk66"/>
204                  </projection>                  </projection>
205                  <layer title="My Layer" filename="%s"                  <layer title="My Layer" filename="%s"
206                  fill="None" stroke="#000000" stroke_width="1">                  fill="None" stroke="#000000" stroke_width="1" visible="true">
207                      <projection name="Layer Projection">                      <projection name="Layer Projection">
208                          <parameter value="proj=lcc"/>                          <parameter value="proj=lcc"/>
209                          <parameter value="ellps=clrk66"/>                          <parameter value="ellps=clrk66"/>
# Line 175  class SaveSessionTest(unittest.TestCase, Line 218  class SaveSessionTest(unittest.TestCase,
218          self.compare_xml(written_contents, expected_contents)          self.compare_xml(written_contents, expected_contents)
219    
220                    
221        def testRasterLayer(self):
222            # deliberately put an apersand in the title :)
223            session = Session("single map&layer")
224            map = Map("Test Map")
225            session.AddMap(map)
226            # use shapefile from the example data
227            imgfile = os.path.join(os.path.dirname(__file__),
228                                   os.pardir, "Data", "iceland", "island.tif")
229            layer = RasterLayer("My RasterLayer", imgfile)
230            map.AddLayer(layer)
231                                                                                    
232            filename = self.temp_file_name("save_singlemap.thuban")
233            save_session(session, filename)
234            session.Destroy()
235                                                                                    
236            file = open(filename)
237            written_contents = file.read()
238            file.close()
239            expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
240            <!DOCTYPE session SYSTEM "thuban.dtd">
241            <session title="single map&amp;layer">
242                <map title="Test Map">
243                    <rasterlayer title="My RasterLayer" filename="%s"
244                                 visible="true">
245                    </rasterlayer>
246                </map>
247            </session>''' % os.path.join(os.path.dirname(__file__),
248                                         os.pardir, "Data", "iceland",
249                                         "island.tif")
250            #print written_contents
251            #print "********************************************"
252            #print expected_contents
253            self.compare_xml(written_contents, expected_contents)
254    
255        def testClassifiedLayer(self):
256            """Save a session with a single map with a single layer
257               with a classificaton.
258            """
259            # deliberately put an apersand in the title :)
260            session = Session("single map&layer")
261            proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
262            map = Map("Test Map", projection = proj)
263            session.AddMap(map)
264            # use shapefile from the example data
265            shpfile = os.path.join(os.path.dirname(__file__),
266                                   os.pardir, "Data", "iceland", "political.shp")
267            layer = Layer("My Layer", session.OpenShapefile(shpfile))
268            map.AddLayer(layer)
269    
270            clazz = layer.GetClassification()
271    
272            clazz.SetField("AREA")
273    
274            clazz.AppendGroup(ClassGroupSingleton(42,
275                                               ClassGroupProperties(),
276                                               "single"))
277            clazz.AppendGroup(ClassGroupSingleton("text",
278                                               ClassGroupProperties(),
279                                               "single-text"))
280    
281            clazz.AppendGroup(ClassGroupRange(0, 42,
282                                               ClassGroupProperties(),
283                                               "range"))
284    
285            range = ClassGroupRange(Range("[0;42]"))
286            range.SetProperties(ClassGroupProperties())
287            range.SetLabel("new-range")
288            clazz.AppendGroup(range)
289    
290            filename = self.temp_file_name("save_singlemap.thuban")
291            save_session(session, filename)
292    
293            file = open(filename)
294            written_contents = file.read()
295            file.close()
296            expected_template = '''<?xml version="1.0" encoding="UTF-8"?>
297            <!DOCTYPE session SYSTEM "thuban.dtd">
298            <session title="single map&amp;layer">
299                <map title="Test Map">
300                    <projection name="Unknown">
301                        <parameter value="zone=26"/>
302                        <parameter value="proj=utm"/>
303                        <parameter value="ellps=clrk66"/>
304                    </projection>
305                    <layer title="My Layer" filename="%s"
306                    fill="None" stroke="#000000" stroke_width="1" visible="%s">
307                        <classification field="AREA" field_type="double">
308                            <clnull label="">
309                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
310                            </clnull>
311                            <clpoint value="42" label="single">
312                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
313                            </clpoint>
314                            <clpoint value="text" label="single-text">
315                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
316                            </clpoint>
317                            <clrange range="[0;42[" label="range">
318                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
319                            </clrange>
320                            <clrange range="[0;42]" label="new-range">
321                                <cldata fill="None" stroke="#000000" stroke_width="1"/>
322                            </clrange>
323                        </classification>
324                    </layer>
325                </map>
326            </session>'''
327            
328            expected_contents = expected_template % \
329                (os.path.join("..", "..", "Data", "iceland", "political.shp"),
330                 "true")
331    
332            #print written_contents
333            #print "********************************************"
334            #print expected_contents
335            self.compare_xml(written_contents, expected_contents)
336    
337            session.Destroy()
338    
339    
340  if __name__ == "__main__":  if __name__ == "__main__":

Legend:
Removed from v.755  
changed lines
  Added in v.1173

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26