<s> import os <EOL> import unittest <EOL> from unittest import skipUnless <EOL> from django . contrib . gis . gdal import HAS_GDAL <EOL> from . . test_data import TEST_DATA , TestDS , get_ds_file <EOL> if HAS_GDAL : <EOL> from django . contrib . gis . gdal import DataSource , Envelope , OGRGeometry , GDALException , OGRIndexError , GDAL_VERSION <EOL> from django . contrib . gis . gdal . field import OFTReal , OFTInteger , OFTString <EOL> ds_list = ( <EOL> TestDS ( <EOL> '<STR_LIT>' , nfeat = <NUM_LIT:5> , nfld = <NUM_LIT:3> , geom = '<STR_LIT>' , gtype = <NUM_LIT:1> , driver = '<STR_LIT>' , <EOL> fields = { '<STR_LIT>' : OFTReal , '<STR_LIT:int>' : OFTInteger , '<STR_LIT:str>' : OFTString } , <EOL> extent = ( - <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> ) , <EOL> srs_wkt = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) , <EOL> field_values = { <EOL> '<STR_LIT>' : [ float ( i ) for i in range ( <NUM_LIT:1> , <NUM_LIT:6> ) ] , <EOL> '<STR_LIT:int>' : list ( range ( <NUM_LIT:1> , <NUM_LIT:6> ) ) , <EOL> '<STR_LIT:str>' : [ str ( i ) for i in range ( <NUM_LIT:1> , <NUM_LIT:6> ) ] , <EOL> } , <EOL> fids = range ( <NUM_LIT:5> ) <EOL> ) , <EOL> TestDS ( <EOL> '<STR_LIT>' , ext = '<STR_LIT>' , nfeat = <NUM_LIT:3> , nfld = <NUM_LIT:3> , geom = '<STR_LIT>' , gtype = '<STR_LIT>' , <EOL> driver = '<STR_LIT>' if GDAL_VERSION >= ( <NUM_LIT:2> , <NUM_LIT:0> ) else '<STR_LIT>' , <EOL> fields = { <EOL> '<STR_LIT>' : OFTString , <EOL> '<STR_LIT>' : OFTString , <EOL> '<STR_LIT>' : OFTString , <EOL> } , <EOL> extent = ( <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> field_values = { <EOL> '<STR_LIT>' : [ '<STR_LIT:1.0>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:5>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> } , <EOL> fids = range ( <NUM_LIT:1> , <NUM_LIT:4> ) <EOL> ) , <EOL> TestDS ( <EOL> '<STR_LIT>' , nfeat = <NUM_LIT:3> , nfld = <NUM_LIT:3> , geom = '<STR_LIT>' , gtype = <NUM_LIT:3> , <EOL> driver = '<STR_LIT>' , <EOL> fields = { '<STR_LIT:float>' : OFTReal , '<STR_LIT:int>' : OFTInteger , '<STR_LIT:str>' : OFTString } , <EOL> extent = ( - <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> srs_wkt = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) , <EOL> ) <EOL> ) <EOL> bad_ds = ( TestDS ( '<STR_LIT:foo>' ) , ) <EOL> @ skipUnless ( HAS_GDAL , "<STR_LIT>" ) <EOL> class DataSourceTest ( unittest . TestCase ) : <EOL> def test01_valid_shp ( self ) : <EOL> "<STR_LIT>" <EOL> for source in ds_list : <EOL> ds = DataSource ( source . ds ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( ds ) ) <EOL> self . assertEqual ( source . ds , ds . name ) <EOL> self . assertEqual ( source . driver , str ( ds . driver ) ) <EOL> try : <EOL> ds [ len ( ds ) ] <EOL> except OGRIndexError : <EOL> pass <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def test02_invalid_shp ( self ) : <EOL> "<STR_LIT>" <EOL> for source in bad_ds : <EOL> with self . assertRaises ( GDALException ) : <EOL> DataSource ( source . ds ) <EOL> def test03a_layers ( self ) : <EOL> "<STR_LIT>" <EOL> for source in ds_list : <EOL> ds = DataSource ( source . ds ) <EOL> for layer in ds : <EOL> self . assertEqual ( len ( layer ) , source . nfeat ) <EOL> self . assertEqual ( source . nfld , layer . num_fields ) <EOL> self . assertEqual ( source . nfld , len ( layer . fields ) ) <EOL> if source . driver == '<STR_LIT>' and ( GDAL_VERSION >= ( <NUM_LIT:1> , <NUM_LIT:7> , <NUM_LIT:0> ) and GDAL_VERSION < ( <NUM_LIT:1> , <NUM_LIT:7> , <NUM_LIT:3> ) ) : <EOL> pass <EOL> else : <EOL> self . assertIsInstance ( layer . extent , Envelope ) <EOL> self . assertAlmostEqual ( source . extent [ <NUM_LIT:0> ] , layer . extent . min_x , <NUM_LIT:5> ) <EOL> self . assertAlmostEqual ( source . extent [ <NUM_LIT:1> ] , layer . extent . min_y , <NUM_LIT:5> ) <EOL> self . assertAlmostEqual ( source . extent [ <NUM_LIT:2> ] , layer . extent . max_x , <NUM_LIT:5> ) <EOL> self . assertAlmostEqual ( source . extent [ <NUM_LIT:3> ] , layer . extent . max_y , <NUM_LIT:5> ) <EOL> flds = layer . fields <EOL> for f in flds : <EOL> self . assertIn ( f , source . fields ) <EOL> with self . assertRaises ( OGRIndexError ) : <EOL> layer . __getitem__ ( - <NUM_LIT:1> ) <EOL> with self . assertRaises ( OGRIndexError ) : <EOL> layer . __getitem__ ( <NUM_LIT> ) <EOL> if hasattr ( source , '<STR_LIT>' ) : <EOL> fld_names = source . field_values . keys ( ) <EOL> for fld_name in fld_names : <EOL> self . assertEqual ( source . field_values [ fld_name ] , layer . get_fields ( fld_name ) ) <EOL> for i , fid in enumerate ( source . fids ) : <EOL> feat = layer [ fid ] <EOL> self . assertEqual ( fid , feat . fid ) <EOL> for fld_name in fld_names : <EOL> self . assertEqual ( source . field_values [ fld_name ] [ i ] , feat . get ( fld_name ) ) <EOL> def test03b_layer_slice ( self ) : <EOL> "<STR_LIT>" <EOL> source = ds_list [ <NUM_LIT:0> ] <EOL> ds = DataSource ( source . ds ) <EOL> sl = slice ( <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> feats = ds [ <NUM_LIT:0> ] [ sl ] <EOL> for fld_name in ds [ <NUM_LIT:0> ] . fields : <EOL> test_vals = [ feat . get ( fld_name ) for feat in feats ] <EOL> control_vals = source . field_values [ fld_name ] [ sl ] <EOL> self . assertEqual ( control_vals , test_vals ) <EOL> def test03c_layer_references ( self ) : <EOL> """<STR_LIT>""" <EOL> source = ds_list [ <NUM_LIT:0> ] <EOL> def get_layer ( ) : <EOL> ds = DataSource ( source . ds ) <EOL> return ds [ <NUM_LIT:0> ] <EOL> lyr = get_layer ( ) <EOL> self . assertEqual ( source . nfeat , len ( lyr ) ) <EOL> self . assertEqual ( source . gtype , lyr . geom_type . num ) <EOL> self . assertEqual ( str ( lyr [ <NUM_LIT:0> ] [ '<STR_LIT:str>' ] ) , "<STR_LIT:1>" ) <EOL> def test04_features ( self ) : <EOL> "<STR_LIT>" <EOL> for source in ds_list : <EOL> ds = DataSource ( source . ds ) <EOL> for layer in ds : <EOL> for feat in layer : <EOL> self . assertEqual ( source . nfld , len ( list ( feat ) ) ) <EOL> self . assertEqual ( source . gtype , feat . geom_type ) <EOL> for k , v in source . fields . items ( ) : <EOL> self . assertIsInstance ( feat [ k ] , v ) <EOL> for fld in feat : <EOL> self . assertIn ( fld . name , source . fields . keys ( ) ) <EOL> def test05_geometries ( self ) : <EOL> "<STR_LIT>" <EOL> for source in ds_list : <EOL> ds = DataSource ( source . ds ) <EOL> for layer in ds : <EOL> for feat in layer : <EOL> g = feat . geom <EOL> self . assertEqual ( source . geom , g . geom_name ) <EOL> self . assertEqual ( source . gtype , g . geom_type ) <EOL> if hasattr ( source , '<STR_LIT>' ) : <EOL> self . assertEqual ( <EOL> source . srs_wkt , <EOL> g . srs . wkt . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ) <EOL> def test06_spatial_filter ( self ) : <EOL> "<STR_LIT>" <EOL> ds = DataSource ( get_ds_file ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> lyr = ds [ <NUM_LIT:0> ] <EOL> self . assertIsNone ( lyr . spatial_filter ) <EOL> with self . assertRaises ( TypeError ) : <EOL> lyr . _set_spatial_filter ( '<STR_LIT:foo>' ) <EOL> with self . assertRaises ( ValueError ) : <EOL> lyr . _set_spatial_filter ( list ( range ( <NUM_LIT:5> ) ) ) <EOL> filter_extent = ( - <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> ) <EOL> lyr . spatial_filter = ( - <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> ) <EOL> self . assertEqual ( OGRGeometry . from_bbox ( filter_extent ) , lyr . spatial_filter ) <EOL> feats = [ feat for feat in lyr ] <EOL> self . assertEqual ( <NUM_LIT:1> , len ( feats ) ) <EOL> self . assertEqual ( '<STR_LIT>' , feats [ <NUM_LIT:0> ] . get ( '<STR_LIT:Name>' ) ) <EOL> filter_geom = OGRGeometry ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> lyr . spatial_filter = filter_geom <EOL> self . assertEqual ( filter_geom , lyr . spatial_filter ) <EOL> feats = [ feat for feat in lyr ] <EOL> self . assertEqual ( <NUM_LIT:1> , len ( feats ) ) <EOL> self . assertEqual ( '<STR_LIT>' , feats [ <NUM_LIT:0> ] . get ( '<STR_LIT:Name>' ) ) <EOL> lyr . spatial_filter = None <EOL> self . assertEqual ( <NUM_LIT:3> , len ( lyr ) ) <EOL> def test07_integer_overflow ( self ) : <EOL> "<STR_LIT>" <EOL> ds = DataSource ( os . path . join ( TEST_DATA , '<STR_LIT>' ) ) <EOL> feat = ds [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> self . assertEqual ( <NUM_LIT> , feat . get ( '<STR_LIT>' ) ) </s>
