<s> from django . contrib . gis import forms <EOL> from django . contrib . gis . db . backend import SpatialBackend , gqn <EOL> from django . contrib . gis . db . models . proxy import GeometryProxy <EOL> from django . contrib . gis . measure import Distance <EOL> _srid_cache = { } <EOL> def get_srid_info ( srid ) : <EOL> """<STR_LIT>""" <EOL> global _srid_cache <EOL> if SpatialBackend . mysql : <EOL> return None , None , None <EOL> if not srid in _srid_cache : <EOL> from django . contrib . gis . models import SpatialRefSys <EOL> sr = SpatialRefSys . objects . get ( srid = srid ) <EOL> units , units_name = sr . units <EOL> spheroid = SpatialRefSys . get_spheroid ( sr . wkt ) <EOL> _srid_cache [ srid ] = ( units , units_name , spheroid ) <EOL> return _srid_cache [ srid ] <EOL> class GeometryField ( SpatialBackend . Field ) : <EOL> "<STR_LIT>" <EOL> geom_type = '<STR_LIT>' <EOL> geodetic_units = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , verbose_name = None , srid = <NUM_LIT> , spatial_index = True , dim = <NUM_LIT:2> , ** 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> super ( GeometryField , self ) . __init__ ( ** kwargs ) <EOL> def _get_srid_info ( self ) : <EOL> self . _units , self . _units_name , self . _spheroid = get_srid_info ( self . srid ) <EOL> @ property <EOL> def spheroid ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _get_srid_info ( ) <EOL> return self . _spheroid <EOL> @ property <EOL> def units ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _get_srid_info ( ) <EOL> return self . _units <EOL> @ property <EOL> def units_name ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _get_srid_info ( ) <EOL> return self . _units_name <EOL> def _deprecated_warning ( self , old_name , new_name ) : <EOL> from warnings import warn <EOL> warn ( '<STR_LIT>' % <EOL> ( old_name , new_name ) ) <EOL> @ property <EOL> def _geom ( self ) : <EOL> self . _deprecated_warning ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return self . geom_type <EOL> @ property <EOL> def _index ( self ) : <EOL> self . _deprecated_warning ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return self . spatial_index <EOL> @ property <EOL> def _srid ( self ) : <EOL> self . _deprecated_warning ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return self . srid <EOL> @ property <EOL> def geodetic ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . units_name in self . geodetic_units <EOL> def get_distance ( self , dist_val , lookup_type ) : <EOL> """<STR_LIT>""" <EOL> if len ( dist_val ) == <NUM_LIT:1> : dist , option = dist_val [ <NUM_LIT:0> ] , None <EOL> else : dist , option = dist_val <EOL> if isinstance ( dist , Distance ) : <EOL> if self . geodetic : <EOL> if SpatialBackend . postgis and lookup_type == '<STR_LIT>' : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> dist_param = dist . m <EOL> else : <EOL> dist_param = getattr ( dist , Distance . unit_attname ( self . units_name ) ) <EOL> else : <EOL> dist_param = dist <EOL> if SpatialBackend . postgis and self . geodetic and lookup_type != '<STR_LIT>' and option == '<STR_LIT>' : <EOL> return [ gqn ( self . _spheroid ) , dist_param ] <EOL> else : <EOL> return [ dist_param ] <EOL> def get_geometry ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , ( tuple , list ) ) : <EOL> geom = value [ <NUM_LIT:0> ] <EOL> else : <EOL> geom = value <EOL> if isinstance ( geom , SpatialBackend . Geometry ) : <EOL> pass <EOL> elif isinstance ( geom , basestring ) : <EOL> try : <EOL> geom = SpatialBackend . Geometry ( geom ) <EOL> except SpatialBackend . GeometryException : <EOL> raise ValueError ( '<STR_LIT>' % str ( value ) ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % type ( value ) ) <EOL> geom . srid = self . get_srid ( geom ) <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 ( SpatialBackend . Geometry , 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 ) : <EOL> """<STR_LIT>""" <EOL> if lookup_type in SpatialBackend . gis_terms : <EOL> if lookup_type == '<STR_LIT>' : return [ ] , [ ] <EOL> geom = self . get_geometry ( value ) <EOL> where = [ self . get_placeholder ( geom ) ] <EOL> params = [ SpatialBackend . Adaptor ( geom ) ] <EOL> if isinstance ( value , ( tuple , list ) ) : <EOL> if lookup_type in SpatialBackend . distance_functions : <EOL> where += self . get_distance ( value [ <NUM_LIT:1> : ] , lookup_type ) <EOL> elif lookup_type in SpatialBackend . limited_where : <EOL> pass <EOL> else : <EOL> where += map ( gqn , value [ <NUM_LIT:1> : ] ) <EOL> return where , params <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % lookup_type ) <EOL> def get_db_prep_save ( self , value ) : <EOL> "<STR_LIT>" <EOL> if value is None : <EOL> return None <EOL> else : <EOL> return SpatialBackend . Adaptor ( self . get_geometry ( value ) ) <EOL> class PointField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' <EOL> class LineStringField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' <EOL> class PolygonField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' <EOL> class MultiPointField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' <EOL> class MultiLineStringField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' <EOL> class MultiPolygonField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' <EOL> class GeometryCollectionField ( GeometryField ) : <EOL> geom_type = '<STR_LIT>' </s>
