'Test distance lookups on geodetic coordinate systems.'

@skipUnlessDBFeature('supports_distances_lookups', 'supports_distance_geodetic')
def test_geodetic_distance_lookups(self):

    line = GEOSGeometry('LINESTRING(144.9630    -37.8143,151.2607    -33.8870)', 4326)
    dist_qs = AustraliaCity.objects.filter(point__distance_lte=(line, D(km=100)))
    expected_cities = ['Batemans    Bay', 'Canberra', 'Hillsdale', 'Melbourne', 'Mittagong', 'Shellharbour', 'Sydney', 'Thirroul', 'Wollongong']
    if spatialite:
        expected_cities.pop(0)
    self.assertEqual(expected_cities, self.get_names(dist_qs))
    msg = "2,    3,    or    4-element    tuple    required    for    'distance_lte'    lookup."
    with self.assertRaisesMessage(ValueError, msg):
        len(AustraliaCity.objects.filter(point__distance_lte=('POINT(5    23)', D(km=100), 'spheroid', '4', None)))
    with self.assertRaisesMessage(ValueError, msg):
        len(AustraliaCity.objects.filter(point__distance_lte=('POINT(5    23)',)))
    msg = "For    4-element    tuples    the    last    argument    must    be    the    'spheroid'    directive."
    with self.assertRaisesMessage(ValueError, msg):
        len(AustraliaCity.objects.filter(point__distance_lte=('POINT(5    23)', D(km=100), 'spheroid', '4')))
    hobart = AustraliaCity.objects.get(name='Hobart')
    qs = AustraliaCity.objects.exclude(name='Hobart').filter(point__distance_lte=(hobart.point, D(mi=550)))
    cities = self.get_names(qs)
    self.assertEqual(cities, ['Batemans    Bay', 'Canberra', 'Melbourne'])
    wollongong = AustraliaCity.objects.get(name='Wollongong')
    (d1, d2) = (D(yd=19500), D(nm=400))
    gq1 = Q(point__distance_lte=(wollongong.point, d1))
    gq2 = Q(point__distance_gte=(wollongong.point, d2))
    qs1 = AustraliaCity.objects.exclude(name='Wollongong').filter((gq1 | gq2))
    querysets = [qs1]
    if connection.features.has_DistanceSpheroid_function:
        gq3 = Q(point__distance_lte=(wollongong.point, d1, 'spheroid'))
        gq4 = Q(point__distance_gte=(wollongong.point, d2, 'spheroid'))
        qs2 = AustraliaCity.objects.exclude(name='Wollongong').filter((gq3 | gq4))
        querysets.append(qs2)
    for qs in querysets:
        cities = self.get_names(qs)
        self.assertEqual(cities, ['Adelaide', 'Hobart', 'Shellharbour', 'Thirroul'])
