1 import os, os.path, unittest |
1 import os, os.path, unittest |
2 from django.contrib.gis.gdal import DataSource, Envelope, OGRException, OGRIndexError |
2 from django.contrib.gis.gdal import DataSource, Envelope, OGRGeometry, OGRException, OGRIndexError |
3 from django.contrib.gis.gdal.field import OFTReal, OFTInteger, OFTString |
3 from django.contrib.gis.gdal.field import OFTReal, OFTInteger, OFTString |
4 from django.contrib import gis |
4 from django.contrib import gis |
5 |
5 |
6 # Path for SHP files |
6 # Path for SHP files |
7 data_path = os.path.join(os.path.dirname(gis.__file__), 'tests' + os.sep + 'data') |
7 data_path = os.path.join(os.path.dirname(gis.__file__), 'tests' + os.sep + 'data') |
8 def get_ds_file(name, ext): |
8 def get_ds_file(name, ext): |
9 |
|
10 |
|
11 return os.sep.join([data_path, name, name + '.%s' % ext]) |
9 return os.sep.join([data_path, name, name + '.%s' % ext]) |
12 |
10 |
13 # Test SHP data source object |
11 # Test SHP data source object |
14 class TestDS: |
12 class TestDS: |
15 def __init__(self, name, **kwargs): |
13 def __init__(self, name, **kwargs): |
23 fields={'dbl' : OFTReal, 'int' : OFTInteger, 'str' : OFTString,}, |
21 fields={'dbl' : OFTReal, 'int' : OFTInteger, 'str' : OFTString,}, |
24 extent=(-1.35011,0.166623,-0.524093,0.824508), # Got extent from QGIS |
22 extent=(-1.35011,0.166623,-0.524093,0.824508), # Got extent from QGIS |
25 srs_wkt='GEOGCS["GCS_WGS_1984",DATUM["WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]', |
23 srs_wkt='GEOGCS["GCS_WGS_1984",DATUM["WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]', |
26 field_values={'dbl' : [float(i) for i in range(1, 6)], 'int' : range(1, 6), 'str' : [str(i) for i in range(1, 6)]}, |
24 field_values={'dbl' : [float(i) for i in range(1, 6)], 'int' : range(1, 6), 'str' : [str(i) for i in range(1, 6)]}, |
27 fids=range(5)), |
25 fids=range(5)), |
28 TestDS('test_vrt', ext='vrt', nfeat=3, nfld=3, geom='POINT', gtype=1, driver='VRT', |
26 TestDS('test_vrt', ext='vrt', nfeat=3, nfld=3, geom='POINT', gtype='Point25D', driver='VRT', |
29 fields={'POINT_X' : OFTString, 'POINT_Y' : OFTString, 'NUM' : OFTString}, # VRT uses CSV, which all types are OFTString. |
27 fields={'POINT_X' : OFTString, 'POINT_Y' : OFTString, 'NUM' : OFTString}, # VRT uses CSV, which all types are OFTString. |
30 extent=(1.0, 2.0, 100.0, 523.5), # Min/Max from CSV |
28 extent=(1.0, 2.0, 100.0, 523.5), # Min/Max from CSV |
31 field_values={'POINT_X' : ['1.0', '5.0', '100.0'], 'POINT_Y' : ['2.0', '23.0', '523.5'], 'NUM' : ['5', '17', '23']}, |
29 field_values={'POINT_X' : ['1.0', '5.0', '100.0'], 'POINT_Y' : ['2.0', '23.0', '523.5'], 'NUM' : ['5', '17', '23']}, |
32 fids=range(1,4)), |
30 fids=range(1,4)), |
33 TestDS('test_poly', nfeat=3, nfld=3, geom='POLYGON', gtype=3, |
31 TestDS('test_poly', nfeat=3, nfld=3, geom='POLYGON', gtype=3, |
189 |
187 |
190 # Making sure the SpatialReference is as expected. |
188 # Making sure the SpatialReference is as expected. |
191 if hasattr(source, 'srs_wkt'): |
189 if hasattr(source, 'srs_wkt'): |
192 self.assertEqual(source.srs_wkt, g.srs.wkt) |
190 self.assertEqual(source.srs_wkt, g.srs.wkt) |
193 |
191 |
194 |
192 def test06_spatial_filter(self): |
|
193 "Testing the Layer.spatial_filter property." |
|
194 ds = DataSource(get_ds_file('cities', 'shp')) |
|
195 lyr = ds[0] |
|
196 |
|
197 # When not set, it should be None. |
|
198 self.assertEqual(None, lyr.spatial_filter) |
|
199 |
|
200 # Must be set a/an OGRGeometry or 4-tuple. |
|
201 self.assertRaises(TypeError, lyr._set_spatial_filter, 'foo') |
|
202 |
|
203 # Setting the spatial filter with a tuple/list with the extent of |
|
204 # a buffer centering around Pueblo. |
|
205 self.assertRaises(ValueError, lyr._set_spatial_filter, range(5)) |
|
206 filter_extent = (-105.609252, 37.255001, -103.609252, 39.255001) |
|
207 lyr.spatial_filter = (-105.609252, 37.255001, -103.609252, 39.255001) |
|
208 self.assertEqual(OGRGeometry.from_bbox(filter_extent), lyr.spatial_filter) |
|
209 feats = [feat for feat in lyr] |
|
210 self.assertEqual(1, len(feats)) |
|
211 self.assertEqual('Pueblo', feats[0].get('Name')) |
|
212 |
|
213 # Setting the spatial filter with an OGRGeometry for buffer centering |
|
214 # around Houston. |
|
215 filter_geom = OGRGeometry('POLYGON((-96.363151 28.763374,-94.363151 28.763374,-94.363151 30.763374,-96.363151 30.763374,-96.363151 28.763374))') |
|
216 lyr.spatial_filter = filter_geom |
|
217 self.assertEqual(filter_geom, lyr.spatial_filter) |
|
218 feats = [feat for feat in lyr] |
|
219 self.assertEqual(1, len(feats)) |
|
220 self.assertEqual('Houston', feats[0].get('Name')) |
|
221 |
|
222 # Clearing the spatial filter by setting it to None. Now |
|
223 # should indicate that there are 3 features in the Layer. |
|
224 lyr.spatial_filter = None |
|
225 self.assertEqual(3, len(lyr)) |
|
226 |
195 def suite(): |
227 def suite(): |
196 s = unittest.TestSuite() |
228 s = unittest.TestSuite() |
197 s.addTest(unittest.makeSuite(DataSourceTest)) |
229 s.addTest(unittest.makeSuite(DataSourceTest)) |
198 return s |
230 return s |
199 |
231 |