|
@@ -1,5 +1,7 @@
|
|
|
+import unittest
|
|
|
+
|
|
|
from django.contrib.gis.db.models.functions import (
|
|
|
- Area, Distance, Intersection, Length, Perimeter, Transform,
|
|
|
+ Area, Distance, Length, Perimeter, Transform, Union,
|
|
|
)
|
|
|
from django.contrib.gis.geos import GEOSGeometry, LineString, Point
|
|
|
from django.contrib.gis.measure import D # alias for Distance
|
|
@@ -7,7 +9,7 @@ from django.db import connection
|
|
|
from django.db.models import F, Q
|
|
|
from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
|
|
|
|
|
|
-from ..utils import no_oracle, oracle, postgis, spatialite
|
|
|
+from ..utils import mysql, no_oracle, oracle, postgis, spatialite
|
|
|
from .models import (
|
|
|
AustraliaCity, CensusZipcode, Interstate, SouthTexasCity, SouthTexasCityFt,
|
|
|
SouthTexasInterstate, SouthTexasZipcode,
|
|
@@ -107,8 +109,9 @@ class DistanceTest(TestCase):
|
|
|
# (thus, Houston and Southside place will be excluded as tested in
|
|
|
# the `test02_dwithin` above).
|
|
|
for model in [SouthTexasCity, SouthTexasCityFt]:
|
|
|
- qs = model.objects.filter(point__distance_gte=(self.stx_pnt, D(km=7))).filter(
|
|
|
- point__distance_lte=(self.stx_pnt, D(km=20)),
|
|
|
+ stx_pnt = self.stx_pnt.transform(model._meta.get_field('point').srid, clone=True)
|
|
|
+ qs = model.objects.filter(point__distance_gte=(stx_pnt, D(km=7))).filter(
|
|
|
+ point__distance_lte=(stx_pnt, D(km=20)),
|
|
|
)
|
|
|
cities = self.get_names(qs)
|
|
|
self.assertEqual(cities, ['Bellaire', 'Pearland', 'West University Place'])
|
|
@@ -183,8 +186,9 @@ class DistanceTest(TestCase):
|
|
|
|
|
|
@skipUnlessDBFeature("supports_distances_lookups")
|
|
|
def test_distance_lookups_with_expression_rhs(self):
|
|
|
+ stx_pnt = self.stx_pnt.transform(SouthTexasCity._meta.get_field('point').srid, clone=True)
|
|
|
qs = SouthTexasCity.objects.filter(
|
|
|
- point__distance_lte=(self.stx_pnt, F('radius')),
|
|
|
+ point__distance_lte=(stx_pnt, F('radius')),
|
|
|
).order_by('name')
|
|
|
self.assertEqual(
|
|
|
self.get_names(qs),
|
|
@@ -193,7 +197,7 @@ class DistanceTest(TestCase):
|
|
|
|
|
|
# With a combined expression
|
|
|
qs = SouthTexasCity.objects.filter(
|
|
|
- point__distance_lte=(self.stx_pnt, F('radius') * 2),
|
|
|
+ point__distance_lte=(stx_pnt, F('radius') * 2),
|
|
|
).order_by('name')
|
|
|
self.assertEqual(len(qs), 5)
|
|
|
self.assertIn('Pearland', self.get_names(qs))
|
|
@@ -207,12 +211,18 @@ class DistanceTest(TestCase):
|
|
|
self.assertEqual(self.get_names(qs), ['Canberra', 'Hobart', 'Melbourne'])
|
|
|
|
|
|
# With a complex geometry expression
|
|
|
- self.assertFalse(SouthTexasCity.objects.filter(point__distance_gt=(Intersection('point', 'point'), 0)))
|
|
|
+ self.assertFalse(SouthTexasCity.objects.filter(point__distance_gt=(Union('point', 'point'), 0)))
|
|
|
self.assertEqual(
|
|
|
- SouthTexasCity.objects.filter(point__distance_lte=(Intersection('point', 'point'), 0)).count(),
|
|
|
+ SouthTexasCity.objects.filter(point__distance_lte=(Union('point', 'point'), 0)).count(),
|
|
|
SouthTexasCity.objects.count(),
|
|
|
)
|
|
|
|
|
|
+ @unittest.skipUnless(mysql, 'This is a MySQL-specific test')
|
|
|
+ def test_mysql_geodetic_distance_error(self):
|
|
|
+ msg = 'Only numeric values of degree units are allowed on geodetic distance queries.'
|
|
|
+ with self.assertRaisesMessage(ValueError, msg):
|
|
|
+ AustraliaCity.objects.filter(point__distance_lte=(Point(0, 0), D(m=100))).exists()
|
|
|
+
|
|
|
|
|
|
'''
|
|
|
=============================
|