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

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

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

revision 1931 by bh, Tue Nov 11 13:24:45 2003 UTC revision 1989 by bh, Fri Nov 28 12:00:54 2003 UTC
# Line 38  import postgissupport Line 38  import postgissupport
38  from xmlsupport import sax_eventlist  from xmlsupport import sax_eventlist
39    
40  import dbflib  import dbflib
41    import shapelib
42    
43  from Thuban.Model.save import save_session  from Thuban.Model.save import save_session
44  from Thuban.Model.load import load_session, parse_color, LoadError, \  from Thuban.Model.load import load_session, parse_color, LoadError, \
# Line 46  from Thuban.Model.color import Transpare Line 47  from Thuban.Model.color import Transpare
47  from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\  from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\
48      ClassGroupSingleton, ClassGroupDefault      ClassGroupSingleton, ClassGroupDefault
49  from Thuban.Model.postgisdb import ConnectionError  from Thuban.Model.postgisdb import ConnectionError
50    from Thuban.Model.table import DBFTable, MemoryTable, \
51         FIELDTYPE_DOUBLE, FIELDTYPE_INT, FIELDTYPE_STRING, \
52         table_to_dbf
53    
54    
55  def filenames_equal(name1, name2):  def filenames_equal(name1, name2):
56      """Return true if the filenames name1 and name2 are equal.      """Return true if the filenames name1 and name2 are equal.
# Line 92  class LoadSessionTest(support.FileLoadTe Line 97  class LoadSessionTest(support.FileLoadTe
97                       ("jointable", "right"),                       ("jointable", "right"),
98                       ("derivedshapesource", "table"),                       ("derivedshapesource", "table"),
99                       ("derivedshapesource", "shapesource")]]                       ("derivedshapesource", "shapesource")]]
100      filenames = [((dtd, n), (None, m)) for n, m in  
101                   [("fileshapesource", "filename"),      # The filenames in the tests should be understandable on all
102                    ("rasterlayer", "filename"),      # currently supported platforms so filenames is an empty list
103                    ("filetable", "filename")]]      filenames = []
104    
105      del n, m, dtd      del n, m, dtd
106    
107      def check_format(self):      def check_format(self):
# Line 265  class TestSingleLayer(LoadSessionTest): Line 271  class TestSingleLayer(LoadSessionTest):
271          self.assertEquals(len(session.ShapeStores()), 0)          self.assertEquals(len(session.ShapeStores()), 0)
272    
273    
274    class TestNonAsciiColumnName(LoadSessionTest):
275    
276        file_contents = '''\
277    <?xml version="1.0" encoding="UTF-8"?>
278    <!DOCTYPE session SYSTEM "thuban-1.0.dtd">
279    <session xmlns="http://thuban.intevation.org/dtds/thuban-1.0-dev.dtd"
280            title="Non ASCII column name test">
281        <fileshapesource filetype="shapefile" id="D1"
282            filename="TestNonAsciiColumnName.shp"/>
283        <map title="map">
284            <projection name="Some Projection">
285                <parameter value="datum=WGS84"/>
286                <parameter value="ellps=WGS84"/>
287                <parameter value="proj=utm"/>
288                <parameter value="units=m"/>
289                <parameter value="zone=27"/>
290            </projection>
291            <layer shapestore="D1" visible="true"
292                    stroke="#000000" title="layer" stroke_width="1"
293                    fill="None">
294                <classification field="Fl\xc3\xa4che" field_type="double">
295                    <clnull label="">
296                        <cldata stroke="#000000" stroke_width="1" fill="None"/>
297                    </clnull>
298                </classification>
299            </layer>
300        </map>
301    </session>
302    '''
303    
304        def test(self):
305            """Load a session with a single map with a single layer"""
306    
307            # Create a shapefile and a dbffile with a non-ascii column name
308            dbffile = self.temp_file_name("TestNonAsciiColumnName.dbf")
309            shpfile = self.temp_file_name("TestNonAsciiColumnName.shp")
310            dbf = dbflib.create(dbffile)
311            dbf.add_field('Fl\xe4che', dbflib.FTDouble, 10, 5)
312            dbf.write_record(0, (0.0,))
313            dbf.close()
314            shp = shapelib.create(shpfile, shapelib.SHPT_POLYGON)
315            shp.write_object(-1, shapelib.SHPObject(shapelib.SHPT_POLYGON, 1,
316                                                    [[(0,0), (10, 10), (10, 0),
317                                                      (0, 0)]]))
318            shp.close()
319    
320            try:
321                session = load_session(self.filename())
322            except ValueError, v:
323                # Usually if the field name is not decoded properly the
324                # loading fails because the field type mentioned in the file
325                # is not None as returned from the layer for a non-existing
326                # column name so we check for that and report it as failure.
327                # Other exceptions are errors in the test case.
328                if str(v) == "xml field type differs from database!":
329                    self.fail("Cannot load file with non-ascii column names")
330                else:
331                    raise
332            self.session = session
333    
334            # In case Thuban could load the file anyway (i.e. no ValueError
335            # exception in load_session()), check explicitly whether the
336            # field name was decoded properly. The test will probably lead
337            # to a UnicodeError instead of a test failure so we check that
338            # too
339            layer = session.Maps()[0].Layers()[0]
340            try:
341                self.assertEquals(layer.GetClassificationColumn(), 'Fl\xe4che')
342            except UnicodeError:
343                # FIXME: Obviously this will have to change if Thuban ever
344                # supports unicode properly.
345                self.fail("Column name was not converted to a bytestring")
346    
347            # roundtrip check
348            self.check_format()
349    
350    
351  class TestLayerVisibility(LoadSessionTest):  class TestLayerVisibility(LoadSessionTest):
352    
353      file_contents = '''\      file_contents = '''\
# Line 307  class TestClassification(ClassificationT Line 390  class TestClassification(ClassificationT
390    
391      file_contents = '''\      file_contents = '''\
392  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
393  <!DOCTYPE session SYSTEM "thuban.dtd">  <!DOCTYPE session SYSTEM "thuban-1.0.dtd">
394  <session title="single map&amp;layer">  <session xmlns="http://thuban.intevation.org/dtds/thuban-1.0-dev.dtd"
395          <map title="Test Map">          title="single map&amp;layer">
396                  <projection>      <fileshapesource filetype="shapefile" id="D138389860"
397                          <parameter value="zone=26"/>          filename="../../Data/iceland/political.shp"/>
398                          <parameter value="proj=utm"/>      <fileshapesource filetype="shapefile" id="D138504492"
399                          <parameter value="ellps=clrk66"/>          filename="../../Data/iceland/political.shp"/>
400                  </projection>      <map title="Test Map">
401                  <layer title="My Layer" stroke_width="1" fill="None"          <projection name="">
402                      filename="../../Data/iceland/political.shp"              <parameter value="zone=26"/>
403                      stroke="#000000">              <parameter value="proj=utm"/>
404                <parameter value="ellps=clrk66"/>
405            </projection>
406            <layer shapestore="D138389860" visible="true" stroke="#000000"
407                    title="My Layer" stroke_width="1" fill="None">
408              <classification field="POPYREG" field_type="string">              <classification field="POPYREG" field_type="string">
409                  <clnull>                  <clnull label="">
410                      <cldata stroke="#000000" stroke_width="1" fill="None"/>                      <cldata stroke="#000000" stroke_width="1" fill="None"/>
411                  </clnull>                  </clnull>
412                  <clpoint value="1">                  <clpoint label="" value="1">
413                      <cldata stroke="#000000" stroke_width="2" fill="None"/>                      <cldata stroke="#000000" stroke_width="2" fill="None"/>
414                  </clpoint>                  </clpoint>
415                  <clpoint value="1">                  <clpoint label="" value="1">
416                      <cldata stroke="#000000" stroke_width="10" fill="None"/>                      <cldata stroke="#000000" stroke_width="10" fill="None"/>
417                  </clpoint>                  </clpoint>
418                  <clpoint value="\xc3\xa4\xc3\xb6\xc3\xbc"                  <clpoint label="\xc3\x9cml\xc3\xa4uts"
419                           label="\xc3\x9cml\xc3\xa4uts">                          value="\xc3\xa4\xc3\xb6\xc3\xbc">
420                      <cldata fill="None" stroke="#000000" stroke_width="1"/>                      <cldata stroke="#000000" stroke_width="1" fill="None"/>
421                  </clpoint>                  </clpoint>
422              </classification>              </classification>
423          </layer>          </layer>
424                  <layer title="My Layer 2" stroke_width="1" fill="None"          <layer shapestore="D138504492" visible="true" stroke="#000000"
425                      filename="../../Data/iceland/political.shp"                  title="My Layer 2" stroke_width="2" fill="None">
                     stroke="#000000">  
426              <classification field="AREA" field_type="double">              <classification field="AREA" field_type="double">
427                  <clnull>                  <clnull label="">
428                      <cldata stroke="#000000" stroke_width="2" fill="None"/>                      <cldata stroke="#000000" stroke_width="2" fill="None"/>
429                  </clnull>                  </clnull>
430                  <clrange min="0" max="1">                  <clrange label="" range="[0;1[">
431                      <cldata stroke="#111111" stroke_width="1" fill="None"/>                      <cldata stroke="#111111" stroke_width="1" fill="None"/>
432                  </clrange>                  </clrange>
433                  <clpoint value=".5">                  <clpoint label="" value="0.5">
434                      <cldata stroke="#000000" stroke_width="1" fill="#111111"/>                      <cldata stroke="#000000" stroke_width="1" fill="#111111"/>
435                  </clpoint>                  </clpoint>
436                  <clrange min="-1" max="0">                  <clrange label="" range="[-1;0[">
437                      <cldata stroke="#000000" stroke_width="1" fill="None"/>                      <cldata stroke="#000000" stroke_width="1" fill="None"/>
438                  </clrange>                  </clrange>
439                  <clpoint value="-.5">                  <clpoint label="" value="-0.5">
440                      <cldata stroke="#000000" stroke_width="1" fill="None"/>                      <cldata stroke="#000000" stroke_width="1" fill="None"/>
441                  </clpoint>                  </clpoint>
442              </classification>              </classification>
443          </layer>          </layer>
444          </map>      </map>
445  </session>  </session>
446  '''  '''
447    
# Line 389  class TestClassification(ClassificationT Line 475  class TestClassification(ClassificationT
475    
476          self.TestLayers(map.Layers(), expected)          self.TestLayers(map.Layers(), expected)
477    
478            self.check_format()
479    
480    
481  class TestLabels(ClassificationTest):  class TestLabels(ClassificationTest):
482    

Legend:
Removed from v.1931  
changed lines
  Added in v.1989

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26