/[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 2552 by jonathan, Fri Jan 28 15:54:00 2005 UTC
# Line 1  Line 1 
1  # Copyright (c) 2002, 2003 by Intevation GmbH  # Copyright (c) 2002, 2003, 2004 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            self.assertFloatSeqEqual(layer.ClipBoundingBox((-180, -6, 100, +120)),
195                                     (90.0, -6, 100, 11.1616263))
196            shapes = layer.ShapesInRegion((-180, -170, 200, +120))
197            self.assertEquals([s.ShapeID() for s in shapes],[0])
198    
199      def test_empty_layer(self):      def test_empty_layer(self):
200          """Test Layer with empty shape file"""          """Test Layer with empty shape file"""
# Line 196  class TestLayer(unittest.TestCase, suppo Line 207  class TestLayer(unittest.TestCase, suppo
207          dbffilename = self.temp_file_name("layer_empty.dbf")          dbffilename = self.temp_file_name("layer_empty.dbf")
208          dbf = dbflib.create(dbffilename)          dbf = dbflib.create(dbffilename)
209          dbf.add_field("NAME", dbflib.FTString, 20, 0)          dbf.add_field("NAME", dbflib.FTString, 20, 0)
210            dbf.close()
211    
212          # Now try to open it.          # Now try to open it.
213          layer = self.layer = Layer("Empty Layer",          layer = self.layer = Layer("Empty Layer",
# Line 219  class TestLayer(unittest.TestCase, suppo Line 231  class TestLayer(unittest.TestCase, suppo
231          layer = RasterLayer("Test RasterLayer", filename)          layer = RasterLayer("Test RasterLayer", filename)
232          self.failIf(layer.HasClassification())          self.failIf(layer.HasClassification())
233          self.failIf(layer.HasShapes())          self.failIf(layer.HasShapes())
234            self.failUnless(layer.UseMask())
235          self.assertEquals(layer.GetImageFilename(), os.path.abspath(filename))          self.assertEquals(layer.GetImageFilename(), os.path.abspath(filename))
236          self.assertFloatSeqEqual(layer.BoundingBox(),          self.assertFloatSeqEqual(layer.BoundingBox(),
237                                   [-24.5500000, 63.2833330,                                   [-24.5500000, 63.2833330,
# Line 227  class TestLayer(unittest.TestCase, suppo Line 240  class TestLayer(unittest.TestCase, suppo
240                                   [-24.5500000, 63.2833330,                                   [-24.5500000, 63.2833330,
241                                    -13.4916670, 66.5666670])                                    -13.4916670, 66.5666670])
242    
243            info = layer.ImageInfo()
244            self.failIf(info is None)
245            self.failUnless(info.has_key("nBands"))
246            self.failUnless(info.has_key("Size"))
247            self.failUnless(info.has_key("Driver"))
248            self.failUnless(info.has_key("BandData"))
249    
250            self.assertEquals(info["nBands"], 1)
251            self.assertEquals(info["Size"], (5002, 394))
252            self.assertEquals(info["Driver"], "GTiff")
253            self.assertEquals(info["BandData"], [(0.0, 140.0)])
254    
255      def test_derived_store(self):      def test_derived_store(self):
256          """Test layer with derived store"""          """Test layer with derived store"""
257          layer = self.layer = Layer("Test Layer",          layer = self.layer = Layer("Test Layer",
# Line 349  class TestLayerModification(unittest.Tes Line 374  class TestLayerModification(unittest.Tes
374          """          """
375          self.clear_messages()          self.clear_messages()
376          self.session = Session("Test session for %s" % self.__class__)          self.session = Session("Test session for %s" % self.__class__)
377          filename = os.path.join("..", "Data", "iceland", "political.shp")          self.filename = os.path.join("..", "Data", "iceland", "political.shp")
378          self.layer = Layer("Test Layer",          self.layer = Layer("Test Layer",
379                             self.session.OpenShapefile(filename))                             self.session.OpenShapefile(self.filename))
380          self.layer.Subscribe(LAYER_LEGEND_CHANGED, self.subscribe_with_params,          self.layer.Subscribe(LAYER_LEGEND_CHANGED, self.subscribe_with_params,
381                               LAYER_LEGEND_CHANGED)                               LAYER_LEGEND_CHANGED)
382          self.layer.Subscribe(LAYER_VISIBILITY_CHANGED,          self.layer.Subscribe(LAYER_VISIBILITY_CHANGED,
# Line 369  class TestLayerModification(unittest.Tes Line 394  class TestLayerModification(unittest.Tes
394          self.session = None          self.session = None
395          self.clear_messages()          self.clear_messages()
396    
397        def build_path(self, filename):
398            return os.path.join("..", "Data", "iceland", filename)
399    
400      def test_sanity(self):      def test_sanity(self):
401          """TestLayerModification Sanity Checks"""          """TestLayerModification Sanity Checks"""
402          # test default settings          # test default settings
# Line 410  class TestLayerModification(unittest.Tes Line 438  class TestLayerModification(unittest.Tes
438          self.failUnless(self.layer.WasModified())          self.failUnless(self.layer.WasModified())
439    
440    
441  #      def test_tree_info(self):
442  # the tree info now contains Color objects which are difficult to test          """Test Layer.TreeInfo"""
443  #          self.assertEquals(self.layer.TreeInfo(),
444  #   def test_tree_info(self):                            ("Layer 'Test Layer'",
445  #       """Test Layer.TreeInfo"""                             ['Filename: %s' % os.path.abspath(self.filename),
446  #       self.assertEquals(self.layer.TreeInfo(),                              'Shown',
447  #                         ("Layer 'Test Layer'",                              'Shapes: 156',
448  #                          ['Shown',                     'Extent (lat-lon): (-24.5465, 63.2868, -13.4958, 66.5638)',
449  #                           'Shapes: 156',                              'Shapetype: Polygon',
450  #                           ('Extent (lat-lon):'                              self.layer.GetClassification()]))
451  #                            ' (-24.5465, 63.2868, -13.4958, 66.5638)'),  
452  #                           'Shapetype: Polygon',      def test_raster_layer(self):
453  #                           'Fill: None',          if not Thuban.Model.resource.has_gdal_support():
454  #                           'Outline: (0.000, 0.000, 0.000)']))              raise support.SkipTest("No gdal support")
455    
456    
457            filename = self.build_path("island.tif")
458            layer = RasterLayer("Test RasterLayer", filename)
459    
460            layer.Subscribe(LAYER_CHANGED, self.subscribe_with_params,
461                            LAYER_CHANGED)
462    
463            self.failUnless(layer.UseMask())
464    
465            layer.SetUseMask(False)
466            self.failIf(layer.UseMask())
467            self.check_messages([(layer, LAYER_CHANGED)])
468            self.clear_messages()
469    
470            layer.SetUseMask(False)
471            self.failIf(layer.UseMask())
472            self.check_messages([])
473            self.clear_messages()
474    
475            layer.SetUseMask(True)
476            self.failUnless(layer.UseMask())
477            self.check_messages([(layer, LAYER_CHANGED)])
478            self.clear_messages()
479    
480            layer.SetUseMask(True)
481            self.failUnless(layer.UseMask())
482            self.check_messages([])
483            self.clear_messages()
484    
485    
486  if __name__ == "__main__":  if __name__ == "__main__":

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26