'Retrieve the distance parameters for the given geometry field,
distance lookup value, and the distance lookup type.
This is the most complex implementation of the spatial backends due to
what is supported on geodetic geometry columns vs. what\'s available on
projected geometry columns.  In addition, it has to take into account
the newly introduced geography column type introudced in PostGIS 1.5.'

def get_distance(self, f, dist_val, lookup_type):

    if (len(dist_val) == 1):
        (value, option) = (dist_val[0], None)
    else:
        (value, option) = dist_val
    geodetic = f.geodetic(self.connection)
    geography = (f.geography and self.geography)
    if isinstance(value, Distance):
        if geography:
            dist_param = value.m
        elif geodetic:
            if (lookup_type == 'dwithin'):
                raise ValueError('Only    numeric    values    of    degree    units    are    allowed    on    geographic    DWithin    queries.')
            dist_param = value.m
        else:
            dist_param = getattr(value, Distance.unit_attname(f.units_name(self.connection)))
    else:
        dist_param = value
    if ((not geography) and geodetic and (lookup_type != 'dwithin') and (option == 'spheroid')):
        return [f._spheroid, dist_param]
    else:
        return [dist_param]
