MCPcopy
hub / github.com/django/django / test_geodetic_distance_lookups

Method test_geodetic_distance_lookups

tests/gis_tests/distapp/tests.py:158–237  ·  view source on GitHub ↗

Test distance lookups on geodetic coordinate systems.

(self)

Source from the content-addressed store, hash-verified

156
157 @skipUnlessDBFeature("supports_distances_lookups", "supports_distance_geodetic")
158 def test_geodetic_distance_lookups(self):
159 """
160 Test distance lookups on geodetic coordinate systems.
161 """
162 # Line is from Canberra to Sydney. Query is for all other cities within
163 # a 100km of that line (which should exclude only Hobart & # Adelaide).
164 line = GEOSGeometry("LINESTRING(144.9630 -37.8143,151.2607 -33.8870)", 4326)
165 dist_qs = AustraliaCity.objects.filter(point__distance_lte=(line, D(km=100)))
166 expected_cities = [
167 "Batemans Bay",
168 "Canberra",
169 "Hillsdale",
170 "Melbourne",
171 "Mittagong",
172 "Shellharbour",
173 "Sydney",
174 "Thirroul",
175 "Wollongong",
176 ]
177 if connection.ops.spatialite:
178 # SpatiaLite is less accurate and returns 102.8km for Batemans Bay.
179 expected_cities.pop(0)
180 self.assertEqual(expected_cities, self.get_names(dist_qs))
181
182 msg = "2, 3, or 4-element tuple required for 'distance_lte' lookup."
183 with self.assertRaisesMessage(ValueError, msg): # Too many params.
184 len(
185 AustraliaCity.objects.filter(
186 point__distance_lte=(
187 "POINT(5 23)",
188 D(km=100),
189 "spheroid",
190 "4",
191 None,
192 )
193 )
194 )
195
196 with self.assertRaisesMessage(ValueError, msg): # Too few params.
197 len(AustraliaCity.objects.filter(point__distance_lte=("POINT(5 23)",)))
198
199 msg = "For 4-element tuples the last argument must be the 'spheroid' directive."
200 with self.assertRaisesMessage(ValueError, msg):
201 len(
202 AustraliaCity.objects.filter(
203 point__distance_lte=("POINT(5 23)", D(km=100), "spheroid", "4")
204 )
205 )
206
207 # Getting all cities w/in 550 miles of Hobart.
208 hobart = AustraliaCity.objects.get(name="Hobart")
209 qs = AustraliaCity.objects.exclude(name="Hobart").filter(
210 point__distance_lte=(hobart.point, D(mi=550))
211 )
212 cities = self.get_names(qs)
213 self.assertEqual(cities, ["Batemans Bay", "Canberra", "Melbourne"])
214
215 # Cities that are either really close or really far from Wollongong --

Callers

nothing calls this directly

Calls 10

get_namesMethod · 0.95
GEOSGeometryClass · 0.90
QClass · 0.90
assertRaisesMessageMethod · 0.80
excludeMethod · 0.80
DClass · 0.50
filterMethod · 0.45
popMethod · 0.45
getMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected