Skip to content
This repository has been archived by the owner on Mar 8, 2021. It is now read-only.

Commit

Permalink
Merge pull request #18 from praekelt/feature/distance-bug-fix
Browse files Browse the repository at this point in the history
fix distance issue, use geo py
  • Loading branch information
moh-moola committed Nov 7, 2018
2 parents 42a9f46 + 0813ce3 commit 9aac97c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
11 changes: 8 additions & 3 deletions service_directory/api/serializers.py
@@ -1,4 +1,5 @@
import logging
from geopy.distance import distance as geo_distance
from collections import OrderedDict
from django.contrib.gis.measure import D
from django.contrib.gis.geos import Point
Expand All @@ -10,7 +11,7 @@
class PointField(serializers.CharField):

def to_representation(self, obj):
return '{} {}'.format(obj.get_x(), obj.get_y())
return '{},{}'.format(obj.get_x(), obj.get_y())

def to_internal_value(self, data):
lat, lng = data.split(',')
Expand Down Expand Up @@ -169,8 +170,12 @@ def get_distance(self, instance):
try:
lat = float(str(location[0]))
lng = float(str(location[1]))
return str('{0:.2f}km'.format(
instance.location.distance(Point(lng, lat, srid=4326)))

return '{0:.2f}km'.format(
geo_distance(
(lng, lat),
(instance.location.get_x(), instance.location.get_y())
).km
)
except (ValueError, TypeError):
pass
Expand Down
63 changes: 63 additions & 0 deletions service_directory/tests/test_api.py
Expand Up @@ -598,6 +598,7 @@ def test_keyword_filter(self):


class OrganisationDetailTestCase(TestCase):
maxDiff = None
client_class = APIClient

@classmethod
Expand Down Expand Up @@ -748,6 +749,68 @@ def test_get_with_distance(self):

self.assertJSONEqual(response.content, expected_response_content)

def test_get_with_distance_greater_than_0(self):
"""
The distance is between the two gps coordinates
-33.891937,18.505496 and -33.891937,17.505496
is 111.19 KM or 69.09 Miles or 60.04 Nautical miles or 111194.26 meters
https://gps-coordinates.org/distance-between-coordinates.php
"""
url = '/api/organisation/{0}/'.format(self.org.id)
location = '?location=-33.891937,17.505496'
response = self.client.get(
url + location, format='json')

expected_response_content = '''
{
"id":%s,
"distance":"%s",
"name":"%s",
"about":"",
"address":"",
"telephone":"",
"emergency_telephone":"",
"email":"",
"web":"",
"verified_as":"",
"age_range_min":null,
"age_range_max":null,
"opening_hours":"",
"location":"%s",
"country":
{
"id":%s,
"name":"%s",
"iso_code":"%s"
},
"categories":[
{
"id":%s,
"name":"%s",
"show_on_home_page":%s
}
],
"keywords":[
{
"id":%s,
"name":"%s",
"show_on_home_page":%s,
"categories":[
%s
]
}
],
"facility_code":""
}
''' % (self.org.id, '110.68km', self.org.name, self.org.location,
self.country.id, self.country.name, self.country.iso_code,
self.category.id, self.category.name,
str(self.category.show_on_home_page).lower(),
self.keyword.id, self.keyword.name,
str(self.keyword.show_on_home_page).lower(), self.category.id)

self.assertJSONEqual(response.content, expected_response_content)


class OrganisationReportIncorrectInformationTestCase(TestCase):
client_class = APIClient
Expand Down

0 comments on commit 9aac97c

Please sign in to comment.