Skip to content

Commit

Permalink
Fixed #27573 -- Made Distance on geodetic coordinates return a raw va…
Browse files Browse the repository at this point in the history
…lue on MySQL.
  • Loading branch information
sir-sigurd authored and timgraham committed Apr 2, 2017
1 parent 35c0025 commit 898e623
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
10 changes: 4 additions & 6 deletions django/contrib/gis/db/models/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,13 @@ def source_is_geography(self):
def convert_value(self, value, expression, connection, context):
if value is None:
return None
dist_att = None
geo_field = self.geo_field
if geo_field.geodetic(connection):
dist_att = 'm'
if connection.features.supports_distance_geodetic:
dist_att = 'm'
else:
units = geo_field.units_name(connection)
if units:
dist_att = DistanceMeasure.unit_attname(units)
else:
dist_att = None
dist_att = geo_field.units_name(connection)
if dist_att:
return DistanceMeasure(**{dist_att: value})
return value
Expand Down
10 changes: 9 additions & 1 deletion tests/gis_tests/distapp/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.contrib.gis.measure import D # alias for Distance
from django.db import connection
from django.db.models import F, Q
from django.test import TestCase, skipUnlessDBFeature
from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature

from ..utils import no_oracle, oracle, postgis, spatialite
from .models import (
Expand Down Expand Up @@ -360,6 +360,14 @@ def test_distance_geodetic_spheroid(self):
for i, c in enumerate(qs):
self.assertAlmostEqual(sphere_distances[i], c.distance.m, tol)

@skipIfDBFeature("supports_distance_geodetic")
@skipUnlessDBFeature("has_Distance_function")
def test_distance_function_raw_result(self):
distance = Interstate.objects.annotate(
d=Distance(Point(0, 0, srid=4326), Point(0, 1, srid=4326)),
).first().d
self.assertEqual(distance, 1)

@no_oracle # Oracle already handles geographic distance calculation.
@skipUnlessDBFeature("has_Distance_function", 'has_Transform_function')
def test_distance_transform(self):
Expand Down

0 comments on commit 898e623

Please sign in to comment.