示例#1
0
    def test_bounding_circle(self):
        def circle_num_points(num_seg):
            # num_seg is the number of segments per quarter circle.
            return (4 * num_seg) + 1

        expected_areas = (169, 136) if postgis else (171, 126)
        qs = Country.objects.annotate(
            circle=functions.BoundingCircle('mpoly')).order_by('name')
        self.assertAlmostEqual(qs[0].circle.area, expected_areas[0], 0)
        self.assertAlmostEqual(qs[1].circle.area, expected_areas[1], 0)
        if postgis:
            # By default num_seg=48.
            self.assertEqual(qs[0].circle.num_points, circle_num_points(48))
            self.assertEqual(qs[1].circle.num_points, circle_num_points(48))

        qs = Country.objects.annotate(circle=functions.BoundingCircle(
            'mpoly', num_seg=12)).order_by('name')
        if postgis:
            self.assertGreater(qs[0].circle.area, 168.4, 0)
            self.assertLess(qs[0].circle.area, 169.5, 0)
            self.assertAlmostEqual(qs[1].circle.area, 136, 0)
            self.assertEqual(qs[0].circle.num_points, circle_num_points(12))
            self.assertEqual(qs[1].circle.num_points, circle_num_points(12))
        else:
            self.assertAlmostEqual(qs[0].circle.area, expected_areas[0], 0)
            self.assertAlmostEqual(qs[1].circle.area, expected_areas[1], 0)
示例#2
0
    def test_bounding_circle(self):
        def circle_num_points(num_seg):
            # num_seg is the number of segments per quarter circle.
            return (4 * num_seg) + 1

        expected_areas = (169, 136) if connection.ops.postgis else (171, 126)
        qs = Country.objects.annotate(
            circle=functions.BoundingCircle("mpoly")).order_by("name")
        self.assertAlmostEqual(qs[0].circle.area, expected_areas[0], 0)
        self.assertAlmostEqual(qs[1].circle.area, expected_areas[1], 0)
        if connection.ops.postgis:
            # By default num_seg=48.
            self.assertEqual(qs[0].circle.num_points, circle_num_points(48))
            self.assertEqual(qs[1].circle.num_points, circle_num_points(48))

        tests = [12, Value(12, output_field=IntegerField())]
        for num_seq in tests:
            with self.subTest(num_seq=num_seq):
                qs = Country.objects.annotate(circle=functions.BoundingCircle(
                    "mpoly", num_seg=num_seq), ).order_by("name")
                if connection.ops.postgis:
                    self.assertGreater(qs[0].circle.area, 168.4, 0)
                    self.assertLess(qs[0].circle.area, 169.5, 0)
                    self.assertAlmostEqual(qs[1].circle.area, 136, 0)
                    self.assertEqual(qs[0].circle.num_points,
                                     circle_num_points(12))
                    self.assertEqual(qs[1].circle.num_points,
                                     circle_num_points(12))
                else:
                    self.assertAlmostEqual(qs[0].circle.area,
                                           expected_areas[0], 0)
                    self.assertAlmostEqual(qs[1].circle.area,
                                           expected_areas[1], 0)
示例#3
0
    def test_bounding_circle(self):
        qs = Country.objects.annotate(circle=functions.BoundingCircle('mpoly')).order_by('name')
        self.assertAlmostEqual(qs[0].circle.area, 168.89, 2)
        self.assertAlmostEqual(qs[1].circle.area, 135.95, 2)

        qs = Country.objects.annotate(circle=functions.BoundingCircle('mpoly', num_seg=12)).order_by('name')
        self.assertAlmostEqual(qs[0].circle.area, 168.44, 2)
        self.assertAlmostEqual(qs[1].circle.area, 135.59, 2)
示例#4
0
    def test_bounding_circle(self):
        # The weak precision in the assertions is because the BoundingCircle
        # calculation changed on PostGIS 2.3.
        qs = Country.objects.annotate(
            circle=functions.BoundingCircle('mpoly')).order_by('name')
        self.assertAlmostEqual(qs[0].circle.area, 169, 0)
        self.assertAlmostEqual(qs[1].circle.area, 136, 0)

        qs = Country.objects.annotate(circle=functions.BoundingCircle(
            'mpoly', num_seg=12)).order_by('name')
        self.assertGreater(qs[0].circle.area, 168.4, 0)
        self.assertLess(qs[0].circle.area, 169.5, 0)
        self.assertAlmostEqual(qs[1].circle.area, 136, 0)