Skip to content

Commit

Permalink
Use google api v3
Browse files Browse the repository at this point in the history
  • Loading branch information
Fantomas42 committed Mar 16, 2015
1 parent 98551ca commit 655eced
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
22 changes: 9 additions & 13 deletions veliberator/geofinder.py
Expand Up @@ -138,22 +138,18 @@ def __init__(self, address):
self.address = address
informations = self.geocompute(address)

self.lat = informations['Point']['coordinates'][1]
self.lng = informations['Point']['coordinates'][0]
self.precision = informations['AddressDetails']['Accuracy']
self.clean_address = informations['address']

if self.precision <= 6:
raise GeoFinderError('Your address is not available.')
self.lat = informations['geometry']['location']['lat']
self.lng = informations['geometry']['location']['lng']
self.precision = informations['types'][0]
self.clean_address = informations['formatted_address']

def geocompute(self, address):
"""Geocompute an address with GMap"""
address = quote_plus(address)
request = 'http://maps.google.com/maps/geo' \
'?sensor=false&q=%s&output=%s&oe=utf8&gl=fr' % \
(address, 'json')
request = 'https://maps.googleapis.com/maps/api/geocode/json' \
'?address=%s&region=fr' % address
data = json.loads(urlopen(request).read())

if data['Status']['code'] != 200:
raise GeoFinderError('Service unavailable')
return data['Placemark'][0]
if data['status'] != 'OK':
raise GeoFinderError('Geocoding service error')
return data['results'][0]
11 changes: 4 additions & 7 deletions veliberator/tests/test_geofinder.py
Expand Up @@ -113,17 +113,14 @@ class AddressGeoFinderTestCase(unittest.TestCase):
def test_Init(self):
address = '1 place de la Bastille, 75012 Paris'
geofinder = AddressGeoFinder(address)
self.assertEquals(geofinder.precision, 8)
self.assertEquals(geofinder.lat, 48.853142800000001)
self.assertEquals(geofinder.lng, 2.3686823000000001)

address = 'Bethune, 62400'
self.assertRaises(GeoFinderError, AddressGeoFinder, address)
self.assertEquals(geofinder.precision, 'street_address')
self.assertEquals(geofinder.lat, 48.8530318)
self.assertEquals(geofinder.lng, 2.3687204)

def test_GeoCompute(self):
address = '1 place de la Bastille, 75012 Paris'
geofinder = AddressGeoFinder(address)
self.assertEquals(len(geofinder.geocompute(address)), 5)
self.assertEquals(len(geofinder.geocompute(address)), 6)


suite = unittest.TestSuite([
Expand Down

0 comments on commit 655eced

Please sign in to comment.