Skip to content

Commit

Permalink
Added support and tests for reverse geocoding when you submit a lat/l…
Browse files Browse the repository at this point in the history
…ng tuple. Fixes #4.
  • Loading branch information
palewire committed Aug 17, 2011
1 parent 3995d53 commit a94b385
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
24 changes: 15 additions & 9 deletions googlegeocoder/__init__.py
Expand Up @@ -23,11 +23,14 @@ def _fetch_json(self, params):
response = urllib2.urlopen(request)
return json.loads(response.read())

def get(self, address, sensor='false', bounding_box=None, region=None):
params = {
'address': address,
'sensor': sensor,
}
def get(self, submission, sensor='false', bounding_box=None, region=None):
params = {'sensor': sensor}
if isinstance(submission, basestring):
params['address'] = submission
elif len(submission) == 2:
params['latlng'] = ",".join(map(str, submission))
else:
raise ValueError("Your submission could not be parsed.")
if bounding_box:
if len(bounding_box) != 2:
raise ValueError("You have submitted a bad bounding box.")
Expand All @@ -38,10 +41,10 @@ def get(self, address, sensor='false', bounding_box=None, region=None):
)
if region:
params['region'] = region
json = self._fetch_json(params)
if json["status"] != "OK":
data = self._fetch_json(params)
if data["status"] != "OK":
raise ValueError(json["status"])
return [GeocoderResult(i) for i in json.get("results")]
return [GeocoderResult(i) for i in data.get("results")]


class BaseAPIObject(object):
Expand Down Expand Up @@ -133,7 +136,10 @@ class Geometry(BaseAPIObject):
"""
def __init__(self, d):
self.__dict__ = d
self.bounds = Bounds(self.bounds)
if hasattr(self, "bounds"):
self.bounds = Bounds(self.bounds)
else:
self.bounds = None
self.viewport = Bounds(self.viewport)
self.location = Coordinate(self.location)
if hasattr(self, "partial_match"):
Expand Down
6 changes: 5 additions & 1 deletion tests.py
Expand Up @@ -11,10 +11,14 @@ def setUp(self):

class GoogleTest(BaseTest):

def test_search(self):
def test_address(self):
result = self.geocoder.get("Winnetka")
self.assertEqual(type(result[0]), GeocoderResult)

def test_latlng(self):
result = self.geocoder.get((34.236144,-118.500938))
self.assertEqual(len(result), 8)

def test_result_attributes(self):
result = self.geocoder.get("Winnetka")[0]
self.assertEqual(type(result.address_components), type([]))
Expand Down

0 comments on commit a94b385

Please sign in to comment.