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

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

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

revision 1599 by bh, Mon Aug 18 12:45:28 2003 UTC revision 2644 by bh, Tue Jul 5 16:30:51 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 33  from Thuban.Model.proj import Projection Line 33  from Thuban.Model.proj import Projection
33  from Thuban.Model.data import DerivedShapeStore  from Thuban.Model.data import DerivedShapeStore
34  from Thuban.Model.classification import Classification, ClassGroupSingleton, \  from Thuban.Model.classification import Classification, ClassGroupSingleton, \
35       ClassGroupRange       ClassGroupRange
36    from Thuban.Model.color import Color
37    
38  import Thuban.Model.resource  import Thuban.Model.resource
39    
# Line 48  class TestLayer(unittest.TestCase, suppo Line 49  class TestLayer(unittest.TestCase, suppo
49    
50      def tearDown(self):      def tearDown(self):
51          """Call the layer's Destroy method and set session and layer to None"""          """Call the layer's Destroy method and set session and layer to None"""
52            self.session.Destroy()
53          self.session = None          self.session = None
54          if self.layer is not None:          if self.layer is not None:
55              self.layer.Destroy()              self.layer.Destroy()
# Line 78  class TestLayer(unittest.TestCase, suppo Line 80  class TestLayer(unittest.TestCase, suppo
80          self.assertEquals(layer.GetProjection(), None)          self.assertEquals(layer.GetProjection(), None)
81    
82          # set/get projection          # set/get projection
83          proj = Projection(["proj=utm", "zone=26"])          proj = Projection(["proj=utm", "zone=26", "ellps=clrk66"])
84    
85          layer.SetProjection(proj)          layer.SetProjection(proj)
86          self.failUnless(layer.GetProjection() is proj)          self.failUnless(layer.GetProjection() is proj)
# Line 164  class TestLayer(unittest.TestCase, suppo Line 166  class TestLayer(unittest.TestCase, suppo
166          self.assertEquals([s.ShapeID() for s in shapes],          self.assertEquals([s.ShapeID() for s in shapes],
167                            [0, 1, 2, 3, 4, 5, 27, 28, 29, 30, 31])                            [0, 1, 2, 3, 4, 5, 27, 28, 29, 30, 31])
168    
169      def test_point_layer_with_projection(self):      def test_arc_layer_with_projection(self):
170          """Test Layer with point shapes and a projection"""          """Test Layer with point shapes and a projection"""
171          # We use mock data here so that we have precise control over the          # We use mock data here so that we have precise control over the
172          # values          # values
173          table = MemoryTable([("FOO", FIELDTYPE_STRING)], [("bla",)])          table = MemoryTable([("FOO", FIELDTYPE_STRING)], [("bla",)])
174          store = mockgeo.SimpleShapeStore(SHAPETYPE_POINT, [[[(10,10)]]], table)          store = mockgeo.SimpleShapeStore(SHAPETYPE_ARC,
175                                  [[[(9884828.7209840547, 5607720.9774499247),
176                                     (11298336.04640449, 9287823.2044059951)]]],
177                                             table)
178          layer = self.layer = Layer("Test Layer", store)          layer = self.layer = Layer("Test Layer", store)
179    
180          # Rotation by 45 degrees counter clockwise. This detects a bug          proj = Projection(["proj=lcc", "lon_0=0", "lat_1=20n", "lat_2=60n",
181          # in the ShapesInRegion method which transforms the bounding box                             "ellps=clrk66"])
         # by only transforming two opposite corners because they have  
         # the same x or y coordinates after application of the  
         # projection or its inverse.  
         proj = mockgeo.AffineProjection((1, 1, -1, 1, 0, 0))  
182          layer.SetProjection(proj)          layer.SetProjection(proj)
183    
184          self.assertEquals(layer.BoundingBox(), (10, 10, 10, 10))          self.assertFloatSeqEqual(layer.BoundingBox(),
185          self.assertEquals(layer.LatLongBoundingBox(), (10.0, 0.0, 10.0, 0.0))                                   (9884828.7209840547, 5607720.9774499247,
186          shapes = layer.ShapesInRegion((0, 0, 20, 20))                                    11298336.04640449, 9287823.2044059951))
187            self.assertFloatSeqEqual(layer.LatLongBoundingBox(),
188                                     (90.0, -8.90043373, 120, 11.1616263))
189            shapes = layer.ShapesInRegion((100, -10, 150, +10))
190          self.assertEquals([s.ShapeID() for s in shapes], [0])          self.assertEquals([s.ShapeID() for s in shapes], [0])
191            self.assertFloatSeqEqual(layer.ShapesBoundingBox([0]),
192                                     (90.0, -8.90043373, 120, 11.1616263))
193    
194            # Test a very large bounding box in the query.  Naive inverse
195            # projection will create infs instead of proper coordinate
196            # values and a different result (an empty list instead of [0])
197            shapes = layer.ShapesInRegion((-180, -170, 200, +120))
198            self.assertEquals([s.ShapeID() for s in shapes],[0])
199    
200      def test_empty_layer(self):      def test_empty_layer(self):
201          """Test Layer with empty shape file"""          """Test Layer with empty shape file"""
# Line 196  class TestLayer(unittest.TestCase, suppo Line 208  class TestLayer(unittest.TestCase, suppo
208          dbffilename = self.temp_file_name("layer_empty.dbf")          dbffilename = self.temp_file_name("layer_empty.dbf")
209          dbf = dbflib.create(dbffilename)          dbf = dbflib.create(dbffilename)
210          dbf.add_field("NAME", dbflib.FTString, 20, 0)          dbf.add_field("NAME", dbflib.FTString, 20, 0)
211            dbf.close()
212    
213          # Now try to open it.          # Now try to open it.
214          layer = self.layer = Layer("Empty Layer",          layer = self.layer = Layer("Empty Layer",
# Line 219  class TestLayer(unittest.TestCase, suppo Line 232  class TestLayer(unittest.TestCase, suppo
232          layer = RasterLayer("Test RasterLayer", filename)          layer = RasterLayer("Test RasterLayer", filename)
233          self.failIf(layer.HasClassification())          self.failIf(layer.HasClassification())
234          self.failIf(layer.HasShapes())          self.failIf(layer.HasShapes())
235            self.assertEquals(layer.MaskType(), layer.MASK_BIT)
236          self.assertEquals(layer.GetImageFilename(), os.path.abspath(filename))          self.assertEquals(layer.GetImageFilename(), os.path.abspath(filename))
237          self.assertFloatSeqEqual(layer.BoundingBox(),          self.assertFloatSeqEqual(layer.BoundingBox(),
238                                   [-24.5500000, 63.2833330,                                   [-24.5500000, 63.2833330,
# Line 227  class TestLayer(unittest.TestCase, suppo Line 241  class TestLayer(unittest.TestCase, suppo
241                                   [-24.5500000, 63.2833330,                                   [-24.5500000, 63.2833330,
242                                    -13.4916670, 66.5666670])                                    -13.4916670, 66.5666670])
243    
244            info = layer.ImageInfo()
245            self.failIf(info is None)
246            self.failUnless(info.has_key("nBands"))
247            self.failUnless(info.has_key("Size"))
248            self.failUnless(info.has_key("Driver"))
249            self.failUnless(info.has_key("BandData"))
250    
251            self.assertEquals(info["nBands"], 1)
252            self.assertEquals(info["Size"], (5002, 394))
253            self.assertEquals(info["Driver"], "GTiff")
254            self.assertEquals(info["BandData"], [(0.0, 140.0)])
255    
256      def test_derived_store(self):      def test_derived_store(self):
257          """Test layer with derived store"""          """Test layer with derived store"""
258          layer = self.layer = Layer("Test Layer",          layer = self.layer = Layer("Test Layer",
# Line 349  class TestLayerModification(unittest.Tes Line 375  class TestLayerModification(unittest.Tes
375          """          """
376          self.clear_messages()          self.clear_messages()
377          self.session = Session("Test session for %s" % self.__class__)          self.session = Session("Test session for %s" % self.__class__)
378          filename = os.path.join("..", "Data", "iceland", "political.shp")          self.filename = os.path.join("..", "Data", "iceland", "political.shp")
379          self.layer = Layer("Test Layer",          self.layer = Layer("Test Layer",
380                             self.session.OpenShapefile(filename))                             self.session.OpenShapefile(self.filename))
381          self.layer.Subscribe(LAYER_LEGEND_CHANGED, self.subscribe_with_params,          self.layer.Subscribe(LAYER_LEGEND_CHANGED, self.subscribe_with_params,
382                               LAYER_LEGEND_CHANGED)                               LAYER_LEGEND_CHANGED)
383          self.layer.Subscribe(LAYER_VISIBILITY_CHANGED,          self.layer.Subscribe(LAYER_VISIBILITY_CHANGED,
# Line 369  class TestLayerModification(unittest.Tes Line 395  class TestLayerModification(unittest.Tes
395          self.session = None          self.session = None
396          self.clear_messages()          self.clear_messages()
397    
398        def build_path(self, filename):
399            return os.path.join("..", "Data", "iceland", filename)
400    
401      def test_sanity(self):      def test_sanity(self):
402          """TestLayerModification Sanity Checks"""          """TestLayerModification Sanity Checks"""
403          # test default settings          # test default settings
# Line 410  class TestLayerModification(unittest.Tes Line 439  class TestLayerModification(unittest.Tes
439          self.failUnless(self.layer.WasModified())          self.failUnless(self.layer.WasModified())
440    
441    
442  #      def test_tree_info(self):
443  # the tree info now contains Color objects which are difficult to test          """Test Layer.TreeInfo"""
444  #          self.assertEquals(self.layer.TreeInfo(),
445  #   def test_tree_info(self):                            ("Layer 'Test Layer'",
446  #       """Test Layer.TreeInfo"""                             ['Filename: %s' % os.path.abspath(self.filename),
447  #       self.assertEquals(self.layer.TreeInfo(),                              'Shown',
448  #                         ("Layer 'Test Layer'",                              'Shapes: 156',
449  #                          ['Shown',                     'Extent (lat-lon): (-24.5465, 63.2868, -13.4958, 66.5638)',
450  #                           'Shapes: 156',                              'Shapetype: Polygon',
451  #                           ('Extent (lat-lon):'                              self.layer.GetClassification()]))
452  #                            ' (-24.5465, 63.2868, -13.4958, 66.5638)'),  
453  #                           'Shapetype: Polygon',      def test_raster_layer(self):
454  #                           'Fill: None',          if not Thuban.Model.resource.has_gdal_support():
455  #                           'Outline: (0.000, 0.000, 0.000)']))              raise support.SkipTest("No gdal support")
456    
457    
458            filename = self.build_path("island.tif")
459            layer = RasterLayer("Test RasterLayer", filename)
460    
461            layer.Subscribe(LAYER_CHANGED, self.subscribe_with_params,
462                            LAYER_CHANGED)
463    
464            self.assertEquals(layer.MaskType(), layer.MASK_BIT)
465    
466            layer.SetMaskType(layer.MASK_NONE)
467            self.failIf(layer.MaskType() != layer.MASK_NONE)
468            self.check_messages([(layer, LAYER_CHANGED)])
469            self.clear_messages()
470    
471            layer.SetMaskType(layer.MASK_NONE)
472            self.failIf(layer.MaskType() != layer.MASK_NONE)
473            self.check_messages([])
474            self.clear_messages()
475    
476            layer.SetMaskType(layer.MASK_BIT)
477            self.failIf(layer.MaskType() != layer.MASK_BIT)
478            self.check_messages([(layer, LAYER_CHANGED)])
479            self.clear_messages()
480    
481            layer.SetMaskType(layer.MASK_BIT)
482            self.failIf(layer.MaskType() != layer.MASK_BIT)
483            self.check_messages([])
484            self.clear_messages()
485    
486            layer.SetMaskType(layer.MASK_ALPHA)
487            self.failIf(layer.MaskType() != layer.MASK_ALPHA)
488    
489            layer.SetOpacity(0)
490            self.assertEquals(layer.Opacity(), 0)
491            layer.SetOpacity(0.5)
492            self.assertEquals(layer.Opacity(), 0.5)
493    
494            self.clear_messages()
495            layer.SetOpacity(1)
496            self.assertEquals(layer.Opacity(), 1)
497            self.check_messages([(layer, LAYER_CHANGED)])
498            self.clear_messages()
499    
500            self.assertRaises(ValueError, layer.SetOpacity, -0.1)
501            self.assertRaises(ValueError, layer.SetOpacity, 1.1)
502    
503            layer.SetMaskType(layer.MASK_NONE)
504            self.clear_messages()
505            self.assertEquals(layer.Opacity(), 1)
506            self.check_messages([])
507            self.clear_messages()
508    
509            self.assertRaises(ValueError, layer.SetMaskType, -1)
510            self.assertRaises(ValueError, layer.SetMaskType, 4)
511    
512    
513  if __name__ == "__main__":  if __name__ == "__main__":

Legend:
Removed from v.1599  
changed lines
  Added in v.2644

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26