<s> from ctypes import c_double , byref <EOL> from django . contrib . gis . gdal . base import GDALBase <EOL> from django . contrib . gis . gdal . envelope import Envelope , OGREnvelope <EOL> from django . contrib . gis . gdal . error import OGRException , OGRIndexError , SRSException <EOL> from django . contrib . gis . gdal . feature import Feature <EOL> from django . contrib . gis . gdal . field import OGRFieldTypes <EOL> from django . contrib . gis . gdal . geomtype import OGRGeomType <EOL> from django . contrib . gis . gdal . geometries import OGRGeometry <EOL> from django . contrib . gis . gdal . srs import SpatialReference <EOL> from django . contrib . gis . gdal . prototypes import ds as capi , geom as geom_api , srs as srs_api <EOL> class Layer ( GDALBase ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , layer_ptr , ds ) : <EOL> """<STR_LIT>""" <EOL> if not layer_ptr : <EOL> raise OGRException ( '<STR_LIT>' ) <EOL> self . ptr = layer_ptr <EOL> self . _ds = ds <EOL> self . _ldefn = capi . get_layer_defn ( self . _ptr ) <EOL> self . _random_read = self . test_capability ( '<STR_LIT>' ) <EOL> def __getitem__ ( self , index ) : <EOL> "<STR_LIT>" <EOL> if isinstance ( index , ( int , long ) ) : <EOL> if index < <NUM_LIT:0> : raise OGRIndexError ( '<STR_LIT>' ) <EOL> return self . _make_feature ( index ) <EOL> elif isinstance ( index , slice ) : <EOL> start , stop , stride = index . indices ( self . num_feat ) <EOL> return [ self . _make_feature ( fid ) for fid in xrange ( start , stop , stride ) ] <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __iter__ ( self ) : <EOL> "<STR_LIT>" <EOL> capi . reset_reading ( self . _ptr ) <EOL> for i in xrange ( self . num_feat ) : <EOL> yield Feature ( capi . get_next_feature ( self . _ptr ) , self . _ldefn ) <EOL> def __len__ ( self ) : <EOL> "<STR_LIT>" <EOL> return self . num_feat <EOL> def __str__ ( self ) : <EOL> "<STR_LIT>" <EOL> return self . name <EOL> def _make_feature ( self , feat_id ) : <EOL> """<STR_LIT>""" <EOL> if self . _random_read : <EOL> try : <EOL> return Feature ( capi . get_feature ( self . ptr , feat_id ) , self . _ldefn ) <EOL> except OGRException : <EOL> pass <EOL> else : <EOL> for feat in self : <EOL> if feat . fid == feat_id : return feat <EOL> raise OGRIndexError ( '<STR_LIT>' % feat_id ) <EOL> @ property <EOL> def extent ( self ) : <EOL> "<STR_LIT>" <EOL> env = OGREnvelope ( ) <EOL> capi . get_extent ( self . ptr , byref ( env ) , <NUM_LIT:1> ) <EOL> return Envelope ( env ) <EOL> @ property <EOL> def name ( self ) : <EOL> "<STR_LIT>" <EOL> return capi . get_fd_name ( self . _ldefn ) <EOL> @ property <EOL> def num_feat ( self , force = <NUM_LIT:1> ) : <EOL> "<STR_LIT>" <EOL> return capi . get_feature_count ( self . ptr , force ) <EOL> @ property <EOL> def num_fields ( self ) : <EOL> "<STR_LIT>" <EOL> return capi . get_field_count ( self . _ldefn ) <EOL> @ property <EOL> def geom_type ( self ) : <EOL> "<STR_LIT>" <EOL> return OGRGeomType ( capi . get_fd_geom_type ( self . _ldefn ) ) <EOL> @ property <EOL> def srs ( self ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> ptr = capi . get_layer_srs ( self . ptr ) <EOL> return SpatialReference ( srs_api . clone_srs ( ptr ) ) <EOL> except SRSException : <EOL> return None <EOL> @ property <EOL> def fields ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ capi . get_field_name ( capi . get_field_defn ( self . _ldefn , i ) ) <EOL> for i in xrange ( self . num_fields ) ] <EOL> @ property <EOL> def field_types ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ OGRFieldTypes [ capi . get_field_type ( capi . get_field_defn ( self . _ldefn , i ) ) ] <EOL> for i in xrange ( self . num_fields ) ] <EOL> @ property <EOL> def field_widths ( self ) : <EOL> "<STR_LIT>" <EOL> return [ capi . get_field_width ( capi . get_field_defn ( self . _ldefn , i ) ) <EOL> for i in xrange ( self . num_fields ) ] <EOL> @ property <EOL> def field_precisions ( self ) : <EOL> "<STR_LIT>" <EOL> return [ capi . get_field_precision ( capi . get_field_defn ( self . _ldefn , i ) ) <EOL> for i in xrange ( self . num_fields ) ] <EOL> def _get_spatial_filter ( self ) : <EOL> try : <EOL> return OGRGeometry ( geom_api . clone_geom ( capi . get_spatial_filter ( self . ptr ) ) ) <EOL> except OGRException : <EOL> return None <EOL> def _set_spatial_filter ( self , filter ) : <EOL> if isinstance ( filter , OGRGeometry ) : <EOL> capi . set_spatial_filter ( self . ptr , filter . ptr ) <EOL> elif isinstance ( filter , ( tuple , list ) ) : <EOL> if not len ( filter ) == <NUM_LIT:4> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> xmin , ymin , xmax , ymax = map ( c_double , filter ) <EOL> capi . set_spatial_filter_rect ( self . ptr , xmin , ymin , xmax , ymax ) <EOL> elif filter is None : <EOL> capi . set_spatial_filter ( self . ptr , None ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> spatial_filter = property ( _get_spatial_filter , _set_spatial_filter ) <EOL> def get_fields ( self , field_name ) : <EOL> """<STR_LIT>""" <EOL> if not field_name in self . fields : <EOL> raise OGRException ( '<STR_LIT>' % field_name ) <EOL> return [ feat . get ( field_name ) for feat in self ] <EOL> def get_geoms ( self , geos = False ) : <EOL> """<STR_LIT>""" <EOL> if geos : <EOL> from django . contrib . gis . geos import GEOSGeometry <EOL> return [ GEOSGeometry ( feat . geom . wkb ) for feat in self ] <EOL> else : <EOL> return [ feat . geom for feat in self ] <EOL> def test_capability ( self , capability ) : <EOL> """<STR_LIT>""" <EOL> return bool ( capi . test_capability ( self . ptr , capability ) ) </s>
