def testUnitsStr(self):
        "Testing conversion to strings"
        d1 = D(m=100)
        d2 = D(km=3.5)

        self.assertEqual(str(d1), '100.0 m')
        self.assertEqual(str(d2), '3.5 km')
        self.assertEqual(repr(d1), 'Distance(m=100.0)')
        self.assertEqual(repr(d2), 'Distance(km=3.5)')
    def testComparisons(self):
        "Testing comparisons"
        d1 = D(m=100)
        d2 = D(km=1)
        d3 = D(km=0)

        self.assertTrue(d2 > d1)
        self.assertTrue(d1 == d1)
        self.assertTrue(d1 < d2)
        self.failIf(d3)
 def test02_distance_lookup(self):
     "Testing GeoQuerySet distance lookup support on non-point geography fields."
     z = Zipcode.objects.get(code='77002')
     cities1 = list(
         City.objects.filter(point__distance_lte=(z.poly, D(
             mi=500))).order_by('name').values_list('name', flat=True))
     cities2 = list(
         City.objects.filter(point__dwithin=(z.poly, D(
             mi=500))).order_by('name').values_list('name', flat=True))
     for cities in [cities1, cities2]:
         self.assertEqual(['Dallas', 'Houston', 'Oklahoma City'], cities)
    def testUnitConversions(self):
        "Testing default units during maths"
        d1 = D(m=100)
        d2 = D(km=1)

        d3 = d1 + d2
        self.assertEqual(d3._default_unit, 'm')
        d4 = d2 + d1
        self.assertEqual(d4._default_unit, 'km')
        d5 = d1 * 2
        self.assertEqual(d5._default_unit, 'm')
        d6 = d1 / 2
        self.assertEqual(d6._default_unit, 'm')
Beispiel #5
0
    def test02_dwithin(self):
        "Testing the `dwithin` lookup type."
        # Distances -- all should be equal (except for the
        # degree/meter pair in au_cities, that's somewhat
        # approximate).
        tx_dists = [(7000, 22965.83), D(km=7), D(mi=4.349)]
        au_dists = [(0.5, 32000), D(km=32), D(mi=19.884)]

        # Expected cities for Australia and Texas.
        tx_cities = ['Downtown Houston', 'Southside Place']
        au_cities = ['Mittagong', 'Shellharbour', 'Thirroul', 'Wollongong']

        # Performing distance queries on two projected coordinate systems one
        # with units in meters and the other in units of U.S. survey feet.
        for dist in tx_dists:
            if isinstance(dist, tuple): dist1, dist2 = dist
            else: dist1 = dist2 = dist
            qs1 = SouthTexasCity.objects.filter(point__dwithin=(self.stx_pnt,
                                                                dist1))
            qs2 = SouthTexasCityFt.objects.filter(point__dwithin=(self.stx_pnt,
                                                                  dist2))
            for qs in qs1, qs2:
                self.assertEqual(tx_cities, self.get_names(qs))

        # Now performing the `dwithin` queries on a geodetic coordinate system.
        for dist in au_dists:
            if isinstance(dist, D) and not oracle: type_error = True
            else: type_error = False

            if isinstance(dist, tuple):
                if oracle: dist = dist[1]
                else: dist = dist[0]

            # Creating the query set.
            qs = AustraliaCity.objects.order_by('name')
            if type_error:
                # A ValueError should be raised on PostGIS when trying to pass
                # Distance objects into a DWithin query using a geodetic field.
                self.assertRaises(
                    ValueError,
                    AustraliaCity.objects.filter(point__dwithin=(self.au_pnt,
                                                                 dist)).count)
            else:
                self.assertEqual(
                    au_cities,
                    self.get_names(
                        qs.filter(point__dwithin=(self.au_pnt, dist))))
 def testUnitAttName(self):
     "Testing the `unit_attname` class method"
     unit_tuple = [('Yard', 'yd'), ('Nautical Mile', 'nm'),
                   ('German legal metre', 'german_m'),
                   ('Indian yard', 'indian_yd'),
                   ('Chain (Sears)', 'chain_sears'), ('Chain', 'chain')]
     for nm, att in unit_tuple:
         self.assertEqual(att, D.unit_attname(nm))
    def testAddition(self):
        "Test addition & subtraction"
        d1 = D(m=100)
        d2 = D(m=200)

        d3 = d1 + d2
        self.assertEqual(d3.m, 300)
        d3 += d1
        self.assertEqual(d3.m, 400)

        d4 = d1 - d2
        self.assertEqual(d4.m, -100)
        d4 -= d1
        self.assertEqual(d4.m, -200)

        try:
            d5 = d1 + 1
        except TypeError, e:
            pass
Beispiel #8
0
    def test04_distance_lookups(self):
        "Testing the `distance_lt`, `distance_gt`, `distance_lte`, and `distance_gte` lookup types."
        # Retrieving the cities within a 20km 'donut' w/a 7km radius 'hole'
        # (thus, Houston and Southside place will be excluded as tested in
        # the `test02_dwithin` above).
        qs1 = SouthTexasCity.objects.filter(
            point__distance_gte=(self.stx_pnt, D(km=7))).filter(
                point__distance_lte=(self.stx_pnt, D(km=20)))

        # Can't determine the units on SpatiaLite from PROJ.4 string, and
        # Oracle 11 incorrectly thinks it is not projected.
        if spatialite or oracle:
            dist_qs = (qs1, )
        else:
            qs2 = SouthTexasCityFt.objects.filter(
                point__distance_gte=(self.stx_pnt, D(km=7))).filter(
                    point__distance_lte=(self.stx_pnt, D(km=20)))
            dist_qs = (qs1, qs2)

        for qs in dist_qs:
            cities = self.get_names(qs)
            self.assertEqual(cities,
                             ['Bellaire', 'Pearland', 'West University Place'])

        # Doing a distance query using Polygons instead of a Point.
        z = SouthTexasZipcode.objects.get(name='77005')
        qs = SouthTexasZipcode.objects.exclude(name='77005').filter(
            poly__distance_lte=(z.poly, D(m=275)))
        self.assertEqual(['77025', '77401'], self.get_names(qs))
        # If we add a little more distance 77002 should be included.
        qs = SouthTexasZipcode.objects.exclude(name='77005').filter(
            poly__distance_lte=(z.poly, D(m=300)))
        self.assertEqual(['77002', '77025', '77401'], self.get_names(qs))
    def testInit(self):
        "Testing initialisation from valid units"
        d = Distance(m=100)
        self.assertEqual(d.m, 100)

        d1, d2, d3 = D(m=100), D(meter=100), D(metre=100)
        for d in (d1, d2, d3):
            self.assertEqual(d.m, 100)

        d = D(nm=100)
        self.assertEqual(d.m, 185200)

        y1, y2, y3 = D(yd=100), D(yard=100), D(Yard=100)
        for d in (y1, y2, y3):
            self.assertEqual(d.yd, 100)

        mm1, mm2 = D(millimeter=1000), D(MiLLiMeTeR=1000)
        for d in (mm1, mm2):
            self.assertEqual(d.m, 1.0)
            self.assertEqual(d.mm, 1000.0)
    def testMultiplication(self):
        "Test multiplication & division"
        d1 = D(m=100)

        d3 = d1 * 2
        self.assertEqual(d3.m, 200)
        d3 = 2 * d1
        self.assertEqual(d3.m, 200)
        d3 *= 5
        self.assertEqual(d3.m, 1000)

        d4 = d1 / 2
        self.assertEqual(d4.m, 50)
        d4 /= 5
        self.assertEqual(d4.m, 10)

        a5 = d1 * D(m=10)
        self.assertTrue(isinstance(a5, Area))
        self.assertEqual(a5.sq_m, 100 * 10)

        try:
            d1 *= D(m=1)
        except TypeError, e:
            pass
 def testAccessInvalid(self):
     "Testing access in invalid units"
     d = D(m=100)
     self.failIf(hasattr(d, 'banana'))
 def testAccess(self):
     "Testing access in different units"
     d = D(m=100)
     self.assertEqual(d.km, 0.1)
     self.assertAlmostEqual(d.ft, 328.084, 3)
        d4 /= 5
        self.assertEqual(d4.m, 10)

        a5 = d1 * D(m=10)
        self.assertTrue(isinstance(a5, Area))
        self.assertEqual(a5.sq_m, 100 * 10)

        try:
            d1 *= D(m=1)
        except TypeError, e:
            pass
        else:
            self.fail('Distance *= Distance should raise TypeError')

        try:
            d5 = d1 / D(m=1)
        except TypeError, e:
            pass
        else:
            self.fail('Distance / Distance should raise TypeError')

        try:
            d1 /= D(m=1)
        except TypeError, e:
            pass
        else:
            self.fail('Distance /= Distance should raise TypeError')

    def testUnitConversions(self):
        "Testing default units during maths"
        d1 = D(m=100)
Beispiel #14
0
    def test05_geodetic_distance_lookups(self):
        "Testing distance lookups on geodetic coordinate systems."
        # Line is from Canberra to Sydney.  Query is for all other cities within
        # a 100km of that line (which should exclude only Hobart & Adelaide).
        line = GEOSGeometry('LINESTRING(144.9630 -37.8143,151.2607 -33.8870)',
                            4326)
        dist_qs = AustraliaCity.objects.filter(point__distance_lte=(line,
                                                                    D(km=100)))

        if oracle or connection.ops.geography:
            # Oracle and PostGIS 1.5 can do distance lookups on arbitrary geometries.
            self.assertEqual(9, dist_qs.count())
            self.assertEqual([
                'Batemans Bay', 'Canberra', 'Hillsdale', 'Melbourne',
                'Mittagong', 'Shellharbour', 'Sydney', 'Thirroul', 'Wollongong'
            ], self.get_names(dist_qs))
        else:
            # PostGIS 1.4 and below only allows geodetic distance queries (utilizing
            # ST_Distance_Sphere/ST_Distance_Spheroid) from Points to PointFields
            # on geometry columns.
            self.assertRaises(ValueError, dist_qs.count)

            # Ensured that a ValueError was raised, none of the rest of the test is
            # support on this backend, so bail now.
            if spatialite: return

        # Too many params (4 in this case) should raise a ValueError.
        self.assertRaises(
            ValueError, len,
            AustraliaCity.objects.filter(point__distance_lte=('POINT(5 23)',
                                                              D(km=100),
                                                              'spheroid',
                                                              '4')))

        # Not enough params should raise a ValueError.
        self.assertRaises(
            ValueError, len,
            AustraliaCity.objects.filter(
                point__distance_lte=('POINT(5 23)', )))

        # Getting all cities w/in 550 miles of Hobart.
        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'])

        # Cities that are either really close or really far from Wollongong --
        # and using different units of distance.
        wollongong = AustraliaCity.objects.get(name='Wollongong')
        d1, d2 = D(yd=19500), D(nm=400)  # Yards (~17km) & Nautical miles.

        # Normal geodetic distance lookup (uses `distance_sphere` on PostGIS.
        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)

        # Geodetic distance lookup but telling GeoDjango to use `distance_spheroid`
        # instead (we should get the same results b/c accuracy variance won't matter
        # in this test case).
        if postgis:
            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 = [qs1, qs2]
        else:
            querysets = [qs1]

        for qs in querysets:
            cities = self.get_names(qs)
            self.assertEqual(
                cities, ['Adelaide', 'Hobart', 'Shellharbour', 'Thirroul'])