web/lib/django/contrib/gis/gdal/tests/test_ds.py
changeset 29 cc9b7e14412b
parent 0 0d40e90630ef
--- a/web/lib/django/contrib/gis/gdal/tests/test_ds.py	Wed May 19 17:43:59 2010 +0200
+++ b/web/lib/django/contrib/gis/gdal/tests/test_ds.py	Tue May 25 02:43:45 2010 +0200
@@ -1,13 +1,11 @@
 import os, os.path, unittest
-from django.contrib.gis.gdal import DataSource, Envelope, OGRException, OGRIndexError
+from django.contrib.gis.gdal import DataSource, Envelope, OGRGeometry, OGRException, OGRIndexError
 from django.contrib.gis.gdal.field import OFTReal, OFTInteger, OFTString
 from django.contrib import gis
 
 # Path for SHP files
 data_path = os.path.join(os.path.dirname(gis.__file__), 'tests' + os.sep + 'data')
 def get_ds_file(name, ext):
-    
-
     return os.sep.join([data_path, name, name + '.%s' % ext])
 
 # Test SHP data source object
@@ -25,7 +23,7 @@
                   srs_wkt='GEOGCS["GCS_WGS_1984",DATUM["WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]',
                   field_values={'dbl' : [float(i) for i in range(1, 6)], 'int' : range(1, 6), 'str' : [str(i) for i in range(1, 6)]},
                   fids=range(5)),
-           TestDS('test_vrt', ext='vrt', nfeat=3, nfld=3, geom='POINT', gtype=1, driver='VRT',
+           TestDS('test_vrt', ext='vrt', nfeat=3, nfld=3, geom='POINT', gtype='Point25D', driver='VRT',
                   fields={'POINT_X' : OFTString, 'POINT_Y' : OFTString, 'NUM' : OFTString}, # VRT uses CSV, which all types are OFTString.
                   extent=(1.0, 2.0, 100.0, 523.5), # Min/Max from CSV
                   field_values={'POINT_X' : ['1.0', '5.0', '100.0'], 'POINT_Y' : ['2.0', '23.0', '523.5'], 'NUM' : ['5', '17', '23']},
@@ -191,7 +189,41 @@
                     if hasattr(source, 'srs_wkt'):
                         self.assertEqual(source.srs_wkt, g.srs.wkt)
 
+    def test06_spatial_filter(self):
+        "Testing the Layer.spatial_filter property."
+        ds = DataSource(get_ds_file('cities', 'shp'))
+        lyr = ds[0]
 
+        # When not set, it should be None.
+        self.assertEqual(None, lyr.spatial_filter)
+
+        # Must be set a/an OGRGeometry or 4-tuple.
+        self.assertRaises(TypeError, lyr._set_spatial_filter, 'foo')
+
+        # Setting the spatial filter with a tuple/list with the extent of
+        # a buffer centering around Pueblo.
+        self.assertRaises(ValueError, lyr._set_spatial_filter, range(5))
+        filter_extent = (-105.609252, 37.255001, -103.609252, 39.255001)
+        lyr.spatial_filter = (-105.609252, 37.255001, -103.609252, 39.255001)
+        self.assertEqual(OGRGeometry.from_bbox(filter_extent), lyr.spatial_filter)
+        feats = [feat for feat in lyr]
+        self.assertEqual(1, len(feats))
+        self.assertEqual('Pueblo', feats[0].get('Name'))
+
+        # Setting the spatial filter with an OGRGeometry for buffer centering
+        # around Houston.
+        filter_geom = OGRGeometry('POLYGON((-96.363151 28.763374,-94.363151 28.763374,-94.363151 30.763374,-96.363151 30.763374,-96.363151 28.763374))')
+        lyr.spatial_filter = filter_geom
+        self.assertEqual(filter_geom, lyr.spatial_filter)
+        feats = [feat for feat in lyr]
+        self.assertEqual(1, len(feats))
+        self.assertEqual('Houston', feats[0].get('Name'))
+
+        # Clearing the spatial filter by setting it to None.  Now
+        # should indicate that there are 3 features in the Layer.
+        lyr.spatial_filter = None
+        self.assertEqual(3, len(lyr))
+        
 def suite():
     s = unittest.TestSuite()
     s.addTest(unittest.makeSuite(DataSourceTest))