/[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 1677 by bh, Thu Aug 28 13:34:28 2003 UTC revision 2642 by bh, Fri Jul 1 20:49:04 2005 UTC
# Line 1  Line 1 
1  # Copyright (c) 2002, 2003 by Intevation GmbH  # Copyright (c) 2002, 2003, 2004, 2005 by Intevation GmbH
2  # Authors:  # Authors:
3  # Bernhard Herzog <[email protected]>  # Bernhard Herzog <[email protected]>
4  #  #
# Line 25  support.initthuban() Line 25  support.initthuban()
25    
26  import dbflib  import dbflib
27    
28    from Thuban import internal_from_unicode
29  from Thuban.Lib.fileutil import relative_filename  from Thuban.Lib.fileutil import relative_filename
30  from Thuban.Model.save import XMLWriter, save_session, sort_data_stores  from Thuban.Model.save import XMLWriter, save_session, sort_data_stores
31  from Thuban.Model.session import Session  from Thuban.Model.session import Session
# Line 53  class XMLWriterTest(unittest.TestCase): Line 54  class XMLWriterTest(unittest.TestCase):
54          eq(writer.encode("hello world"), "hello world")          eq(writer.encode("hello world"), "hello world")
55          eq(writer.encode(unicode("hello world")), unicode("hello world"))          eq(writer.encode(unicode("hello world")), unicode("hello world"))
56    
57          eq(writer.encode("\x80\x90\xc2\x100"),          eq(writer.encode(internal_from_unicode(u"\x80\x90\xc2\x100")),
58                           "\xc2\x80\xc2\x90\xc3\x82\x100")                           "\xc2\x80\xc2\x90\xc3\x82\x100")
59          eq(writer.encode(u"\x80\x90\xc2\x100"),          eq(writer.encode(u"\x80\x90\xc2\x100"),
60                           "\xc2\x80\xc2\x90\xc3\x82\x100")                           "\xc2\x80\xc2\x90\xc3\x82\x100")
61          eq(writer.encode(u"\xFF5E"), "\xc3\xbf5E")          eq(writer.encode(u"\xFF5E"), "\xc3\xbf5E")
62    
# Line 65  class XMLWriterTest(unittest.TestCase): Line 66  class XMLWriterTest(unittest.TestCase):
66  class SaveSessionTest(unittest.TestCase, support.FileTestMixin,  class SaveSessionTest(unittest.TestCase, support.FileTestMixin,
67                        xmlsupport.ValidationTest):                        xmlsupport.ValidationTest):
68    
69      dtd = "http://thuban.intevation.org/dtds/thuban-0.9.dtd"      dtd = "http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"
70      thubanids = [((dtd, n), (None, "id")) for n in      thubanids = [((dtd, n), (None, "id")) for n in
71                   ["fileshapesource", "filetable", "jointable",                   ["fileshapesource", "filetable", "jointable",
72                    "derivedshapesource", "dbshapesource", "dbconnection"]]                    "derivedshapesource", "dbshapesource", "dbconnection"]]
# Line 111  class SaveSessionTest(unittest.TestCase, Line 112  class SaveSessionTest(unittest.TestCase,
112          file.close()          file.close()
113          self.compare_xml(written_contents,          self.compare_xml(written_contents,
114                           '<?xml version="1.0" encoding="UTF-8"?>\n'                           '<?xml version="1.0" encoding="UTF-8"?>\n'
115                           '<!DOCTYPE session SYSTEM "thuban-0.9.dtd">\n'                           '<!DOCTYPE session SYSTEM "thuban-1.1.dtd">\n'
116                           '<session title="empty session" '                           '<session title="empty session" '
117           'xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">'           'xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">'
118                           '\n</session>\n')                           '\n</session>\n')
119    
120          self.validate_data(written_contents)          self.validate_data(written_contents)
# Line 122  class SaveSessionTest(unittest.TestCase, Line 123  class SaveSessionTest(unittest.TestCase,
123          """Save a session with a single map with a single layer"""          """Save a session with a single map with a single layer"""
124          # deliberately put an apersand in the title :)          # deliberately put an apersand in the title :)
125          session = Session("single map&layer")          session = Session("single map&layer")
126          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])          proj = Projection(["proj=utm", "zone=27", "ellps=WGS84",
127                               "datum=WGS84", "units=m"],
128                              name = "WGS 84 / UTM zone 27N",
129                              epsg = "32627")
130          map = Map("Test Map", projection = proj)          map = Map("Test Map", projection = proj)
131          session.AddMap(map)          session.AddMap(map)
132          # use shapefile from the example data          # use shapefile from the example data
# Line 138  class SaveSessionTest(unittest.TestCase, Line 142  class SaveSessionTest(unittest.TestCase,
142          written_contents = file.read()          written_contents = file.read()
143          file.close()          file.close()
144          expected_template = '''<?xml version="1.0" encoding="UTF-8"?>          expected_template = '''<?xml version="1.0" encoding="UTF-8"?>
145          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">          <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
146          <session title="single map&amp;layer"          <session title="single map&amp;layer"
147             xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">
148              <fileshapesource id="D1" filename="%s" filetype="shapefile"/>              <fileshapesource id="D1"
149                    filename="../../Data/iceland/political.shp"
150                    filetype="shapefile"/>
151              <map title="Test Map">              <map title="Test Map">
152                  <projection name="Unknown">                  <projection epsg="32627" name="WGS 84 / UTM zone 27N">
                     <parameter value="zone=26"/>  
153                      <parameter value="proj=utm"/>                      <parameter value="proj=utm"/>
154                      <parameter value="ellps=clrk66"/>                      <parameter value="zone=27"/>
155                        <parameter value="ellps=WGS84"/>
156                        <parameter value="datum=WGS84"/>
157                        <parameter value="units=m"/>
158                  </projection>                  </projection>
159                  <layer title="My Layer" shapestore="D1"                  <layer title="My Layer" shapestore="D1"
160                  fill="None" stroke="#000000" stroke_width="1" visible="%s"/>                  fill="None" stroke="#000000" stroke_width="1" visible="%s"/>
161              </map>              </map>
162          </session>'''          </session>'''
163    
164          expected_contents = expected_template % \          expected_contents = expected_template % "true"
             (os.path.join("..", "..", "Data", "iceland", "political.shp"),  
              "true")  
165    
166          self.compare_xml(written_contents, expected_contents)          self.compare_xml(written_contents, expected_contents)
167    
168          self.validate_data(written_contents)          self.validate_data(written_contents)
169    
170            # Repeat with an invisible layer
171          layer.SetVisible(False)          layer.SetVisible(False)
172          save_session(session, filename)          save_session(session, filename)
173    
174          file = open(filename)          file = open(filename)
175          written_contents = file.read()          written_contents = file.read()
176          file.close()          file.close()
177          expected_contents = expected_template % \          expected_contents = expected_template % "false"
             (os.path.join("..", "..", "Data", "iceland", "political.shp"),  
              "false")  
178          self.compare_xml(written_contents, expected_contents)          self.compare_xml(written_contents, expected_contents)
179          self.validate_data(written_contents)          self.validate_data(written_contents)
180    
# Line 178  class SaveSessionTest(unittest.TestCase, Line 183  class SaveSessionTest(unittest.TestCase,
183      def testLayerProjection(self):      def testLayerProjection(self):
184          """Test saving layers with projections"""          """Test saving layers with projections"""
185          # deliberately put an apersand in the title :)          # deliberately put an apersand in the title :)
186          session = Session("single map&layer")          session = self.session = Session("single map&layer")
187          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])          proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
188          map = Map("Test Map", projection = proj)          map = Map("Test Map", projection = proj)
189          session.AddMap(map)          session.AddMap(map)
# Line 186  class SaveSessionTest(unittest.TestCase, Line 191  class SaveSessionTest(unittest.TestCase,
191          shpfile = os.path.join(os.path.dirname(__file__),          shpfile = os.path.join(os.path.dirname(__file__),
192                                 os.pardir, "Data", "iceland", "political.shp")                                 os.pardir, "Data", "iceland", "political.shp")
193          layer = Layer("My Layer", session.OpenShapefile(shpfile))          layer = Layer("My Layer", session.OpenShapefile(shpfile))
194          proj = Projection(["proj=lcc", "ellps=clrk66"], "Layer Projection")          proj = Projection(["proj=lcc", "ellps=clrk66",
195                               "lat_1=0", "lat_2=20"],
196                              "Layer Projection")
197          layer.SetProjection(proj)          layer.SetProjection(proj)
198          map.AddLayer(layer)          map.AddLayer(layer)
199    
200          filename = self.temp_file_name("save_layerproj.thuban")          filename = self.temp_file_name("save_layerproj.thuban")
201          save_session(session, filename)          save_session(session, filename)
         session.Destroy()  
202    
203          file = open(filename)          file = open(filename)
204          written_contents = file.read()          written_contents = file.read()
205          file.close()          file.close()
206          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
207          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">          <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
208          <session title="single map&amp;layer"          <session title="single map&amp;layer"
209             xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">
210              <fileshapesource id="D1" filename="%s" filetype="shapefile"/>              <fileshapesource id="D1"
211                    filename="../../Data/iceland/political.shp"
212                    filetype="shapefile"/>
213              <map title="Test Map">              <map title="Test Map">
214                  <projection name="Unknown">                  <projection name="Unknown">
215                      <parameter value="zone=26"/>                      <parameter value="zone=26"/>
# Line 213  class SaveSessionTest(unittest.TestCase, Line 221  class SaveSessionTest(unittest.TestCase,
221                      <projection name="Layer Projection">                      <projection name="Layer Projection">
222                          <parameter value="proj=lcc"/>                          <parameter value="proj=lcc"/>
223                          <parameter value="ellps=clrk66"/>                          <parameter value="ellps=clrk66"/>
224                            <parameter value="lat_1=0"/>
225                            <parameter value="lat_2=20"/>
226                      </projection>                      </projection>
227                  </layer>                  </layer>
228              </map>              </map>
229          </session>''' % os.path.join("..", "..", "Data", "iceland",          </session>'''
                                      "political.shp")  
230          #print written_contents          #print written_contents
231          #print "********************************************"          #print "********************************************"
232          #print expected_contents          #print expected_contents
# Line 226  class SaveSessionTest(unittest.TestCase, Line 235  class SaveSessionTest(unittest.TestCase,
235          self.validate_data(written_contents)          self.validate_data(written_contents)
236    
237      def testRasterLayer(self):      def testRasterLayer(self):
         # deliberately put an apersand in the title :)  
         session = Session("single map&layer")  
         map = Map("Test Map")  
         session.AddMap(map)  
         # use shapefile from the example data  
         imgfile = os.path.join(os.path.dirname(__file__),  
                                os.pardir, "Data", "iceland", "island.tif")  
         layer = RasterLayer("My RasterLayer", imgfile)  
         map.AddLayer(layer)  
238    
239          filename = self.temp_file_name("%s.thuban" % self.id())          MASK_NONE = RasterLayer.MASK_NONE
240          save_session(session, filename)          MASK_BIT = RasterLayer.MASK_BIT
241          session.Destroy()          MASK_ALPHA = RasterLayer.MASK_ALPHA
242    
243            for opacity, masktype, opname, maskname in \
244                [(1,  MASK_BIT,   '', ''),
245                 (.2, MASK_BIT,   'opacity="0.2"', ''),
246                 (1,  MASK_ALPHA, '',              'masktype="alpha"'),
247                 (.5, MASK_ALPHA, 'opacity="0.5"', 'masktype="alpha"'),
248                 (1,  MASK_NONE,  '',              'masktype="none"'),
249                 (0,  MASK_NONE,  'opacity="0"',   'masktype="none"') ]:
250    
         file = open(filename)  
         written_contents = file.read()  
         file.close()  
         expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>  
         <!DOCTYPE session SYSTEM "thuban-0.9.dtd">  
         <session title="single map&amp;layer"  
            xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">  
             <map title="Test Map">  
                 <rasterlayer title="My RasterLayer" filename="%s"  
                              visible="true">  
                 </rasterlayer>  
             </map>  
         </session>''' % os.path.join(os.pardir, os.pardir, "Data", "iceland",  
                                      "island.tif")  
         #print written_contents  
         #print "********************************************"  
         #print expected_contents  
         self.compare_xml(written_contents, expected_contents)  
251    
252          self.validate_data(written_contents)              # deliberately put an apersand in the title :)
253                session = Session("single map&layer")
254                map = Map("Test Map")
255                session.AddMap(map)
256                # use shapefile from the example data
257                imgfile = os.path.join(os.path.dirname(__file__),
258                                       os.pardir, "Data", "iceland", "island.tif")
259                layer = RasterLayer("My RasterLayer", imgfile)
260    
261                layer.SetOpacity(opacity)
262                layer.SetMaskType(masktype)
263    
264                map.AddLayer(layer)
265    
266                filename = self.temp_file_name("%s.thuban" % self.id())
267                save_session(session, filename)
268                session.Destroy()
269    
270                file = open(filename)
271                written_contents = file.read()
272                file.close()
273                expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
274                <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
275                <session title="single map&amp;layer"
276                   xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">
277                    <map title="Test Map">
278                        <rasterlayer title="My RasterLayer"
279                                filename="../../Data/iceland/island.tif"
280                                visible="true" %s %s>
281                        </rasterlayer>
282                    </map>
283                </session>''' % (opname, maskname)
284                #print written_contents
285                #print "********************************************"
286                #print expected_contents
287                self.compare_xml(written_contents, expected_contents)
288    
289                self.validate_data(written_contents)
290    
291      def testClassifiedLayer(self):      def testClassifiedLayer(self):
292          """Save a session with a single map with classifications"""          """Save a session with a single map with classifications"""
# Line 299  class SaveSessionTest(unittest.TestCase, Line 326  class SaveSessionTest(unittest.TestCase,
326          layer2.SetClassificationColumn("POPYCOUN")          layer2.SetClassificationColumn("POPYCOUN")
327    
328          # Classification with Latin 1 text          # Classification with Latin 1 text
329          clazz.AppendGroup(ClassGroupSingleton('\xe4\xf6\xfc', # ae, oe, ue          clazz.AppendGroup(ClassGroupSingleton(
330                                                ClassGroupProperties(),              internal_from_unicode(u'\xe4\xf6\xfc'), # ae, oe, ue
331                                                '\xdcml\xe4uts')) # Uemlaeuts              ClassGroupProperties(),
332                internal_from_unicode(u'\xdcml\xe4uts'))) # Uemlaeuts
333    
334    
335          filename = self.temp_file_name("%s.thuban" % self.id())          filename = self.temp_file_name("%s.thuban" % self.id())
# Line 310  class SaveSessionTest(unittest.TestCase, Line 338  class SaveSessionTest(unittest.TestCase,
338          file = open(filename)          file = open(filename)
339          written_contents = file.read()          written_contents = file.read()
340          file.close()          file.close()
341          expected_template = '''<?xml version="1.0" encoding="UTF-8"?>          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
342          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">          <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
343          <session title="Map with Classifications"          <session title="Map with Classifications"
344             xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">
345              <fileshapesource id="D1" filename="%s" filetype="shapefile"/>              <fileshapesource id="D1"
346                    filename="../../Data/iceland/political.shp"
347                    filetype="shapefile"/>
348              <map title="Test Map">              <map title="Test Map">
349                  <projection name="Unknown">                  <projection name="Unknown">
350                      <parameter value="zone=26"/>                      <parameter value="zone=26"/>
# Line 322  class SaveSessionTest(unittest.TestCase, Line 352  class SaveSessionTest(unittest.TestCase,
352                      <parameter value="ellps=clrk66"/>                      <parameter value="ellps=clrk66"/>
353                  </projection>                  </projection>
354                  <layer title="My Layer" shapestore="D1"                  <layer title="My Layer" shapestore="D1"
355                  fill="None" stroke="#000000" stroke_width="1" visible="%s">                  fill="None" stroke="#000000" stroke_width="1" visible="true">
356                      <classification field="AREA" field_type="double">                      <classification field="AREA" field_type="double">
357                          <clnull label="">                          <clnull label="">
358                              <cldata fill="None" stroke="#000000" stroke_width="1"/>                              <cldata fill="None" stroke="#000000" stroke_width="1"/>
# Line 356  class SaveSessionTest(unittest.TestCase, Line 386  class SaveSessionTest(unittest.TestCase,
386              </map>              </map>
387          </session>'''          </session>'''
388    
         expected_contents = expected_template % \  
             (os.path.join("..", "..", "Data", "iceland", "political.shp"),  
              "true")  
   
389          #print written_contents          #print written_contents
390          #print "********************************************"          #print "********************************************"
391          #print expected_contents          #print expected_contents
# Line 383  class SaveSessionTest(unittest.TestCase, Line 409  class SaveSessionTest(unittest.TestCase,
409          file = open(filename)          file = open(filename)
410          written_contents = file.read()          written_contents = file.read()
411          file.close()          file.close()
412          expected_template = '''<?xml version="1.0" encoding="UTF-8"?>          expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
413          <!DOCTYPE session SYSTEM "thuban-0.9.dtd">          <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
414          <session title="a DBF Table session"          <session title="a DBF Table session"
415             xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">             xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">
416              <filetable id="D1" filename="%s" filetype="DBF" title="political"/>              <filetable id="D1" filename="../../Data/iceland/political.dbf"
417                    filetype="DBF" title="political"/>
418          </session>'''          </session>'''
419    
         expected_contents = (expected_template  
                              % os.path.join(os.pardir, os.pardir, "Data",  
                                             "iceland", "political.dbf"))  
420          self.compare_xml(written_contents, expected_contents)          self.compare_xml(written_contents, expected_contents)
421          self.validate_data(written_contents)          self.validate_data(written_contents)
422    
# Line 443  class SaveSessionTest(unittest.TestCase, Line 467  class SaveSessionTest(unittest.TestCase,
467              file = open(filename)              file = open(filename)
468              written_contents = file.read()              written_contents = file.read()
469              file.close()              file.close()
470              expected_template = '''<?xml version="1.0" encoding="UTF-8"?>              expected_contents = '''<?xml version="1.0" encoding="UTF-8"?>
471              <!DOCTYPE session SYSTEM "thuban-0.9.dtd">              <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
472              <session title="A Joined Table session"              <session title="A Joined Table session"
473               xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">               xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">
474                  <fileshapesource filename="%(shpfile)s"                  <fileshapesource filename="../../Data/iceland/roads-line.shp"
475                                   filetype="shapefile" id="D142197204"/>                                   filetype="shapefile" id="D142197204"/>
476                  <filetable filename="%(dbffile)s"                  <filetable filename="save_joinedtable.dbf"
477                             title="save_joinedtable"                             title="save_joinedtable"
478                             filetype="DBF" id="D141881756"/>                             filetype="DBF" id="D141881756"/>
479                  <jointable id="D142180284"                  <jointable id="D142180284"
# Line 467  class SaveSessionTest(unittest.TestCase, Line 491  class SaveSessionTest(unittest.TestCase,
491                  </map>                  </map>
492              </session>'''              </session>'''
493    
             expected_contents = expected_template % {  
                 "dbffile": relative_filename(self.temp_dir(), dbffile),  
                 "shpfile": relative_filename(self.temp_dir(), shpfile)  
                 }  
494              self.compare_xml(written_contents, expected_contents)              self.compare_xml(written_contents, expected_contents)
495              self.validate_data(written_contents)              self.validate_data(written_contents)
496          finally:          finally:
# Line 489  class SaveSessionTest(unittest.TestCase, Line 509  class SaveSessionTest(unittest.TestCase,
509          class NonConnectionStore(PostGISShapeStore):          class NonConnectionStore(PostGISShapeStore):
510              """Shapestore that doesn't try to access the server"""              """Shapestore that doesn't try to access the server"""
511              def _fetch_table_information(self):              def _fetch_table_information(self):
512                  pass                  # pretend that we've found a geometry column
513                    self.geometry_column = "the_geom"
514                def IDColumn(self):
515                    """Return an object with a name attribute with value 'gid'"""
516                    class dummycol:
517                        name = "gid"
518                    return dummycol
519    
520          session = Session("A PostGIS Session")          session = Session("A PostGIS Session")
521          try:          try:
# Line 512  class SaveSessionTest(unittest.TestCase, Line 538  class SaveSessionTest(unittest.TestCase,
538              written = file.read()              written = file.read()
539              file.close()              file.close()
540              expected = '''<?xml version="1.0" encoding="UTF-8"?>              expected = '''<?xml version="1.0" encoding="UTF-8"?>
541              <!DOCTYPE session SYSTEM "thuban-0.9.dtd">              <!DOCTYPE session SYSTEM "thuban-1.1.dtd">
542              <session title="A PostGIS Session"              <session title="A PostGIS Session"
543               xmlns="http://thuban.intevation.org/dtds/thuban-0.9.dtd">               xmlns="http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd">
544                  <dbconnection id="DB"                  <dbconnection id="DB"
545                                dbtype="postgis" dbname="plugh"                                dbtype="postgis" dbname="plugh"
546                                host="xyzzy" port="42"                                host="xyzzy" port="42"
547                                user="grue"/>                                user="grue"/>
548                  <dbshapesource id="roads" dbconn="DB" tablename="roads"/>                  <dbshapesource id="roads" dbconn="DB" tablename="roads"
549                                   id_column="gid" geometry_column="the_geom"/>
550                  <map title="Test Map">                  <map title="Test Map">
551                      <layer title="Roads to Nowhere"                      <layer title="Roads to Nowhere"
552                             shapestore="roads" visible="true"                             shapestore="roads" visible="true"

Legend:
Removed from v.1677  
changed lines
  Added in v.2642

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26