/[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 2688 by frank, Fri Jun 30 12:27:20 2006 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 32  from Thuban.Model.table import FIELDTYPE Line 32  from Thuban.Model.table import FIELDTYPE
32  from Thuban.Model.proj import Projection  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, ClassGroupPattern
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 387  class TestLayerModification(unittest.Tes Line 416  class TestLayerModification(unittest.Tes
416          # the layer.          # the layer.
417          self.failIf(self.layer.WasModified())          self.failIf(self.layer.WasModified())
418    
419      def test_set_classification(self):      def test_set_classification_numerical(self):
420          """Test Layer.SetClassification"""          """Test Layer.SetClassification numerical"""
421          classification = Classification()          classification = Classification()
422          classification.AppendGroup(ClassGroupRange((0.0, 0.1)))          classification.AppendGroup(ClassGroupRange((0.0, 0.1)))
423    
# Line 409  class TestLayerModification(unittest.Tes Line 438  class TestLayerModification(unittest.Tes
438          self.check_messages([(self.layer, LAYER_CHANGED)])          self.check_messages([(self.layer, LAYER_CHANGED)])
439          self.failUnless(self.layer.WasModified())          self.failUnless(self.layer.WasModified())
440    
441        def test_set_classification_textual(self):
442            """Test Layer.SetClassification textual"""
443            classification = Classification()
444            classification.AppendGroup(ClassGroupPattern("I"))
445    
446  #          self.layer.SetClassification(classification)
447  # the tree info now contains Color objects which are difficult to test          self.layer.SetClassificationColumn("POPYCOUN")
448  #  
449  #   def test_tree_info(self):          self.check_messages([(self.layer, LAYER_CHANGED),
450  #       """Test Layer.TreeInfo"""                               (self.layer, LAYER_CHANGED)])
451  #       self.assertEquals(self.layer.TreeInfo(),          self.failUnless(self.layer.WasModified())
452  #                         ("Layer 'Test Layer'",  
453  #                          ['Shown',          self.clear_messages()
454  #                           'Shapes: 156',          self.layer.UnsetModified()
455  #                           ('Extent (lat-lon):'  
456  #                            ' (-24.5465, 63.2868, -13.4958, 66.5638)'),          # change only the classification column. This should issue a
457  #                           'Shapetype: Polygon',          # LAYER_CHANGED message as well.
458  #                           'Fill: None',          self.layer.SetClassificationColumn("POPYREG")
459  #                           'Outline: (0.000, 0.000, 0.000)']))  
460            self.check_messages([(self.layer, LAYER_CHANGED)])
461            self.failUnless(self.layer.WasModified())
462    
463    
464        def test_tree_info(self):
465            """Test Layer.TreeInfo"""
466            self.assertEquals(self.layer.TreeInfo(),
467                              ("Layer 'Test Layer'",
468                               ['Filename: %s' % os.path.abspath(self.filename),
469                                'Shown',
470                                'Shapes: 156',
471                       'Extent (lat-lon): (-24.5465, 63.2868, -13.4958, 66.5638)',
472                                'Shapetype: Polygon',
473                                self.layer.GetClassification()]))
474    
475        def test_raster_layer(self):
476            if not Thuban.Model.resource.has_gdal_support():
477                raise support.SkipTest("No gdal support")
478    
479    
480            filename = self.build_path("island.tif")
481            layer = RasterLayer("Test RasterLayer", filename)
482    
483            layer.Subscribe(LAYER_CHANGED, self.subscribe_with_params,
484                            LAYER_CHANGED)
485    
486            self.assertEquals(layer.MaskType(), layer.MASK_BIT)
487    
488            layer.SetMaskType(layer.MASK_NONE)
489            self.failIf(layer.MaskType() != layer.MASK_NONE)
490            self.check_messages([(layer, LAYER_CHANGED)])
491            self.clear_messages()
492    
493            layer.SetMaskType(layer.MASK_NONE)
494            self.failIf(layer.MaskType() != layer.MASK_NONE)
495            self.check_messages([])
496            self.clear_messages()
497    
498            layer.SetMaskType(layer.MASK_BIT)
499            self.failIf(layer.MaskType() != layer.MASK_BIT)
500            self.check_messages([(layer, LAYER_CHANGED)])
501            self.clear_messages()
502    
503            layer.SetMaskType(layer.MASK_BIT)
504            self.failIf(layer.MaskType() != layer.MASK_BIT)
505            self.check_messages([])
506            self.clear_messages()
507    
508            layer.SetMaskType(layer.MASK_ALPHA)
509            self.failIf(layer.MaskType() != layer.MASK_ALPHA)
510    
511            layer.SetOpacity(0)
512            self.assertEquals(layer.Opacity(), 0)
513            layer.SetOpacity(0.5)
514            self.assertEquals(layer.Opacity(), 0.5)
515    
516            self.clear_messages()
517            layer.SetOpacity(1)
518            self.assertEquals(layer.Opacity(), 1)
519            self.check_messages([(layer, LAYER_CHANGED)])
520            self.clear_messages()
521    
522            self.assertRaises(ValueError, layer.SetOpacity, -0.1)
523            self.assertRaises(ValueError, layer.SetOpacity, 1.1)
524    
525            layer.SetMaskType(layer.MASK_NONE)
526            self.clear_messages()
527            self.assertEquals(layer.Opacity(), 1)
528            self.check_messages([])
529            self.clear_messages()
530    
531            self.assertRaises(ValueError, layer.SetMaskType, -1)
532            self.assertRaises(ValueError, layer.SetMaskType, 4)
533    
534    
535  if __name__ == "__main__":  if __name__ == "__main__":

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26