<s> from django . db . models . fields import Field <EOL> from django . db . models . sql . expressions import SQLEvaluator <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . contrib . gis import forms <EOL> from django . contrib . gis . db . models . proxy import GeometryProxy <EOL> from django . contrib . gis . geometry . backend import Geometry , GeometryException <EOL> _srid_cache = { } <EOL> def get_srid_info ( srid , connection ) : <EOL> """<STR_LIT>""" <EOL> global _srid_cache <EOL> try : <EOL> SpatialRefSys = connection . ops . spatial_ref_sys ( ) <EOL> except NotImplementedError : <EOL> return None , None , None <EOL> if not connection . alias in _srid_cache : <EOL> _srid_cache [ connection . alias ] = { } <EOL> if not srid in _srid_cache [ connection . alias ] : <EOL> sr = SpatialRefSys . objects . using ( connection . alias ) . get ( srid = srid ) <EOL> units , units_name = sr . units <EOL> spheroid = SpatialRefSys . get_spheroid ( sr . wkt ) <EOL> _srid_cache [ connection . alias ] [ srid ] = ( units , units_name , spheroid ) <EOL> return _srid_cache [ connection . alias ] [ srid ] <EOL> class GeometryField ( Field ) : <EOL> "<STR_LIT>" <EOL> geom_type = '<STR_LIT>' <EOL> geodetic_units = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> description = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , verbose_name = None , srid = <NUM_LIT> , spatial_index = True , dim = <NUM_LIT:2> , <EOL> geography = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . spatial_index = spatial_index <EOL> self . srid = srid <EOL> self . dim = dim <EOL> kwargs [ '<STR_LIT>' ] = verbose_name <EOL> self . geography = geography <EOL> self . _extent = kwargs . pop ( '<STR_LIT>' , ( - <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> self . _tolerance = kwargs . pop ( '<STR_LIT>' , <NUM_LIT> ) <EOL> super ( GeometryField , self ) . __init__ ( ** kwargs ) <EOL> def _get_srid_info ( self , connection ) : <EOL> self . _units , self . _units_name , self . _spheroid = get_srid_info ( self . srid , connection ) <EOL> def spheroid ( self , connection ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _get_srid_info ( connection ) <EOL> return self . _spheroid <EOL> def units ( self , connection ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _get_srid_info ( connection ) <EOL> return self . _units <EOL> def units_name ( self , connection ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _get_srid_info ( connection ) <EOL> return self . _units_name <EOL> def geodetic ( self , connection ) : <EOL> """<STR_LIT>""" <EOL> return self . units_name ( connection ) in self . geodetic_units <EOL> def get_distance ( self , value , lookup_type , connection ) : <EOL> """<STR_LIT>""" <EOL> return connection . ops . get_distance ( self , value , lookup_type ) <EOL> def get_prep_value ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , SQLEvaluator ) : <EOL> return value <EOL> elif isinstance ( value , ( tuple , list ) ) : <EOL> geom = value [ <NUM_LIT:0> ] <EOL> seq_value = True <EOL> else : <EOL> geom = value <EOL> seq_value = False <EOL> if isinstance ( geom , Geometry ) : <EOL> pass <EOL> elif isinstance ( geom , basestring ) or hasattr ( geom , '<STR_LIT>' ) : <EOL> try : <EOL> geom = Geometry ( geom ) <EOL> except GeometryException : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % type ( geom ) . __name__ ) <EOL> geom . srid = self . get_srid ( geom ) <EOL> if seq_value : <EOL> lookup_val = [ geom ] <EOL> lookup_val . extend ( value [ <NUM_LIT:1> : ] ) <EOL> return tuple ( lookup_val ) <EOL> else : <EOL> return geom <EOL> def get_srid ( self , geom ) : <EOL> """<STR_LIT>""" <EOL> gsrid = geom . srid <EOL> if gsrid is None or self . srid == - <NUM_LIT:1> or ( gsrid == - <NUM_LIT:1> and self . srid != - <NUM_LIT:1> ) : <EOL> return self . srid <EOL> else : <EOL> return gsrid <EOL> def contribute_to_class ( self , cls , name ) : <EOL> super ( GeometryField , self ) . contribute_to_class ( cls , name ) <EOL> setattr ( cls , self . attname , GeometryProxy ( Geometry , self ) ) <EOL> def db_type ( self , connection ) : <EOL> return connection . ops . geo_db_type ( self ) <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT>' : forms . GeometryField , <EOL> '<STR_LIT:null>' : self . null , <EOL> '<STR_LIT>' : self . geom_type , <EOL> '<STR_LIT>' : self . srid , <EOL> } <EOL> defaults . update ( kwargs ) <EOL> return super ( GeometryField , self ) . formfield ( ** defaults ) <EOL> def get_db_prep_lookup ( self , lookup_type , value , connection , prepared = False ) : <EOL> """<STR_LIT>""" <EOL> if lookup_type in connection . ops . gis_terms : <EOL> if lookup_type == '<STR_LIT>' : <EOL> return [ ] <EOL> if isinstance ( value , ( tuple , list ) ) : <EOL> params = [ connection . ops . Adapter ( value [ <NUM_LIT:0> ] ) ] <EOL> if lookup_type in connection . ops . distance_functions : <EOL> params += self . get_distance ( value [ <NUM_LIT:1> : ] , lookup_type , connection ) <EOL> elif lookup_type in connection . ops . truncate_params : <EOL> pass <EOL> else : <EOL> params += value [ <NUM_LIT:1> : ] <EOL> elif isinstance ( value , SQLEvaluator ) : <EOL> params = [ ] <EOL> else : <EOL> params = [ connection . ops . Adapter ( value ) ] <EOL> return params <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> ( lookup_type , self . __class__ . __name__ ) ) <EOL> def get_prep_lookup ( self , lookup_type , value ) : <EOL> if lookup_type == '<STR_LIT>' : <EOL> return bool ( value ) <EOL> else : <EOL> return self . get_prep_value ( value ) <EOL> def get_db_prep_save ( self , value , connection ) : <EOL> "<STR_LIT>" <EOL> if value is None : <EOL> return None <EOL> else : <EOL> return connection . ops . Adapter ( self . get_prep_value ( value ) ) <EOL> def get_placeholder ( self , value , connection ) : <EOL> """<STR_LIT>""" <EOL> return connection . ops . get_geom_placeholder ( self , value ) <EOL> class PointField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' <EOL> description = _ ( "<STR_LIT>" ) <EOL> class LineStringField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' <EOL> description = _ ( "<STR_LIT>" ) <EOL> class PolygonField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' <EOL> description = _ ( "<STR_LIT>" ) <EOL> class MultiPointField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' <EOL> description = _ ( "<STR_LIT>" ) <EOL> class MultiLineStringField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' <EOL> description = _ ( "<STR_LIT>" ) <EOL> class MultiPolygonField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' <EOL> description = _ ( "<STR_LIT>" ) <EOL> class GeometryCollectionField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' <EOL> description = _ ( "<STR_LIT>" ) </s>
