#NEWLINE# def _distance_attribute( self , func , geom = None , tolerance = 0.05 , spheroid = False , **kwargs ) : #NEWLINE# #INDENT# '\n        DRY routine for GeoQuerySet distance attribute routines.\n        ' #NEWLINE# ( procedure_args , geo_field ) = self . _spatial_setup ( func , field_name = kwargs . get ( 'field_name' ) ) #NEWLINE# connection = connections [ self . db ] #NEWLINE# geodetic = geo_field . geodetic ( connection ) #NEWLINE# geography = geo_field . geography #NEWLINE# if geodetic : #NEWLINE# #INDENT# dist_att = 'm' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# dist_att = Distance . unit_attname ( geo_field . units_name ( connection ) ) #NEWLINE# #UNINDENT# distance = ( func == 'distance' ) #NEWLINE# length = ( func == 'length' ) #NEWLINE# perimeter = ( func == 'perimeter' ) #NEWLINE# if ( not ( distance or length or perimeter ) ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Unknown distance function: %s' % func ) ) #NEWLINE# #UNINDENT# geom_3d = ( geo_field . dim == 3 ) #NEWLINE# lookup_params = [ ( geom or 'POINT (0 0)' ) , 0 ] #NEWLINE# backend = geo_field . ops #NEWLINE# if ( spheroid or ( backend . postgis and geodetic and ( not geography ) and length ) ) : #NEWLINE# #INDENT# lookup_params . append ( 'spheroid' ) #NEWLINE# #UNINDENT# lookup_params = geo_field . get_prep_value ( lookup_params ) #NEWLINE# params = geo_field . get_db_prep_lookup ( 'distance_lte' , lookup_params , connection = connection ) #NEWLINE# geom_args = bool ( geom ) #NEWLINE# if backend . oracle : #NEWLINE# #INDENT# if distance : #NEWLINE# #INDENT# procedure_fmt = '%(geo_col)s,%(geom)s,%(tolerance)s' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( length or perimeter ) : #NEWLINE# #INDENT# procedure_fmt = '%(geo_col)s,%(tolerance)s' #NEWLINE# #UNINDENT# #UNINDENT# procedure_args [ 'tolerance' ] = tolerance #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# srid = self . query . get_context ( 'transformed_srid' ) #NEWLINE# if srid : #NEWLINE# #INDENT# ( u , unit_name , s ) = get_srid_info ( srid , connection ) #NEWLINE# geodetic = ( unit_name . lower ( ) in geo_field . geodetic_units ) #NEWLINE# #UNINDENT# if ( geodetic and ( not connection . features . supports_distance_geodetic ) ) : #NEWLINE# #INDENT# raise ValueError ( 'This database does not support linear distance calculations on geodetic coordinate systems.' ) #NEWLINE# #UNINDENT# if distance : #NEWLINE# #INDENT# if srid : #NEWLINE# #INDENT# geom_args = False #NEWLINE# procedure_fmt = ( '%s(%%(geo_col)s, %s)' % ( backend . transform , srid ) ) #NEWLINE# if ( ( geom . srid is None ) or ( geom . srid == srid ) ) : #NEWLINE# #INDENT# if backend . spatialite : #NEWLINE# #INDENT# procedure_fmt += ( ', %s(%%%%s, %s)' % ( backend . from_text , srid ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# procedure_fmt += ', %%s' #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if backend . spatialite : #NEWLINE# #INDENT# procedure_fmt += ( ', %s(%s(%%%%s, %s), %s)' % ( backend . transform , backend . from_text , geom . srid , srid ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# procedure_fmt += ( ', %s(%%%%s, %s)' % ( backend . transform , srid ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# procedure_fmt = '%(geo_col)s,%(geom)s' #NEWLINE# #UNINDENT# if ( ( not geography ) and geodetic ) : #NEWLINE# #INDENT# if ( not backend . geography ) : #NEWLINE# #INDENT# if ( not isinstance ( geo_field , PointField ) ) : #NEWLINE# #INDENT# raise ValueError ( 'Spherical distance calculation only supported on PointFields.' ) #NEWLINE# #UNINDENT# if ( not ( str ( Geometry ( six . memoryview ( params [ 0 ] . ewkb ) ) . geom_type ) == 'Point' ) ) : #NEWLINE# #INDENT# raise ValueError ( 'Spherical distance calculation only supported with Point Geometry parameters' ) #NEWLINE# #UNINDENT# #UNINDENT# if spheroid : #NEWLINE# #INDENT# procedure_fmt += ",'%(spheroid)s'" #NEWLINE# procedure_args . update ( { #NEWLINE# #INDENT# 'function' : backend . distance_spheroid , #NEWLINE# 'spheroid' : params [ 1 ] , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# procedure_args . update ( { #NEWLINE# #INDENT# 'function' : backend . distance_sphere , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( length or perimeter ) : #NEWLINE# #INDENT# procedure_fmt = '%(geo_col)s' #NEWLINE# if ( ( not geography ) and geodetic and length ) : #NEWLINE# #INDENT# procedure_fmt += ",'%(spheroid)s'" #NEWLINE# procedure_args . update ( { #NEWLINE# #INDENT# 'function' : backend . length_spheroid , #NEWLINE# 'spheroid' : params [ 1 ] , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( geom_3d and connection . features . supports_3d_functions ) : #NEWLINE# #INDENT# if perimeter : #NEWLINE# #INDENT# procedure_args . update ( { #NEWLINE# #INDENT# 'function' : backend . perimeter3d , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if length : #NEWLINE# #INDENT# procedure_args . update ( { #NEWLINE# #INDENT# 'function' : backend . length3d , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# s = { #NEWLINE# #INDENT# 'select_field' : DistanceField ( dist_att ) , #NEWLINE# 'setup' : False , #NEWLINE# 'geo_field' : geo_field , #NEWLINE# 'procedure_args' : procedure_args , #NEWLINE# 'procedure_fmt' : procedure_fmt , #NEWLINE# #UNINDENT# } #NEWLINE# if geom_args : #NEWLINE# #INDENT# s [ 'geom_args' ] = ( 'geom' , ) #NEWLINE# s [ 'procedure_args' ] [ 'geom' ] = geom #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if geom : #NEWLINE# #INDENT# s [ 'select_params' ] = [ backend . Adapter ( geom ) ] #NEWLINE# #UNINDENT# #UNINDENT# return self . _spatial_attribute ( func , s , ** kwargs )