Created
July 10, 2013 04:24
-
-
Save tcurvelo/5963456 to your computer and use it in GitHub Desktop.
Testing some maps API's
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/usr/bin/env python | |
# -*- encoding: utf-8 -*- | |
import unittest | |
import requests | |
import json | |
class Distance(object): | |
def __init__(self): | |
pass | |
def __call__(self, begin, end): | |
request_url = self.create_request_url(begin, end) | |
result = requests.get(request_url) | |
return self.get_distance_from_result(result) | |
def create_request_url(self, begin, end): | |
pass | |
def get_distance_from_result(self, result): | |
pass | |
class DistanceGoogleMaps(Distance): | |
def create_request_url(self, begin, end): | |
return "http://maps.google.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false" % (begin[0], begin[1], end[0], end[1]) | |
def get_distance_from_result(self, result): | |
result_dict = json.loads(result.content) | |
return result_dict['routes'][0]['legs'][0]['distance']['value'] | |
class DistanceBingMaps(Distance): | |
def __init__(self, api_key): | |
self.api_key = api_key | |
def create_request_url(self, begin, end): | |
return "https://dev.virtualearth.net/REST/v1/Routes?wp.1=%f,%f&wp.2=%f,%f&optmz=distance&key=%s" \ | |
% (begin[0], begin[1], end[0], end[1], self.api_key ) | |
def get_distance_from_result(self, result): | |
result_dict = json.loads(result.content) | |
return int( | |
1000*result_dict['resourceSets'][0]['resources'][0]['travelDistance'] | |
) | |
class DistanceCloudmade(Distance): | |
def __init__(self, api_key): | |
self.api_key=api_key | |
def create_request_url(self, begin, end): | |
return "http://navigation.cloudmade.com/%s/api/latest/%f,%f,%f,%f/car.js" \ | |
% (self.api_key, begin[0], begin[1], end[0], end[1]) | |
def get_distance_from_result(self, result): | |
result_dict = json.loads(result.content) | |
return result_dict['route_summary']['total_distance'] | |
class TestMaps(unittest.TestCase): | |
def setUp(self): | |
#-7.13951890,-34.84447680,-7.13509410,-34.87438930 | |
self.begin = (-7.13951890, -34.84447680) | |
self.end = (-7.13509410, -34.87438930) | |
self.bing_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" | |
self.cloudmade_key = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" | |
def test_distance_btw_coords_google(self): | |
distance = DistanceGoogleMaps() | |
self.assertEqual(distance(self.begin, self.end), 5908) | |
def test_distance_btw_coords_bing(self): | |
distance = DistanceBingMaps(self.bing_key) | |
self.assertEqual(distance(self.begin, self.end), 5691) | |
def test_distance_btw_coords_cloudmade(self): | |
distance = DistanceCloudmade(self.cloudmade_key) | |
self.assertEqual(distance(self.begin, self.end), 5995) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment