How To Reverse Geocode Serverside With Python, Json And Google Maps?
Solution 1:
Processing the response
There is no need to parse the JSON - it is already parsed by json.load()
and returned as Python's data structure. Use it like simple dictionary with lists or different dictionaries in it.
Accessing the needed part of the response
To access data you should be working with you can use the following:
jsondata['results'][0]['address_components']
which is where all information on geographical names is included:
[{u'long_name': u'S\xf6dra L\xe4nken', u'types': [u'route'], u'short_name': u'S\xf6dra L\xe4nken'}, {u'long_name': u'Stockholm', u'types': [u'locality', u'political'], u'short_name': u'Stockholm'}, {u'long_name': u'Stockholm', u'types': [u'administrative_area_level_1', u'political'], u'short_name': u'Stockholm'}, {u'long_name': u'Sweden', u'types': [u'country', u'political'], u'short_name': u'SE'}, {u'long_name': u'12146', u'types': [u'postal_code'], u'short_name': u'12146'}, {u'long_name': u'Johanneshov', u'types': [u'postal_town'], u'short_name': u'Johanneshov'}]
Filtering data you need
As you can see, there is plenty of data you do not need, but you want only locality
and administrative_area_level_1
information. You can filter the data using filter()
Python function like that:
>>>mydata = jsondata['results'][0]['address_components']>>>types = ['locality', 'administrative_area_level_1']>>>geonames = filter(lambda x: len(set(x['types']).intersection(types)), mydata)
Basically you are getting only elements that have 'locality' or 'administrative_area_level_1' in their "types" lists. After the above, geonames
will be list containing dictionaries you need:
[{u'long_name': u'Stockholm', u'types': [u'locality', u'political'], u'short_name': u'Stockholm'}, {u'long_name': u'Stockholm', u'types': [u'administrative_area_level_1', u'political'], u'short_name': u'Stockholm'}]
Displaying the data
To display their names you can eg. iterate through them, displaying long_name
s and respective types
values:
>>> for geoname in geonames:
common_types = set(geoname['types']).intersection(set(types))
print '{} ({})'.format(geoname['long_name'], str(', '.join(common_types)))
Stockholm (locality)
Stockholm (administrative_area_level_1)
Is this what you expected?
Whole code
The code could look like this:
import json
import urllib2
defget_geonames(lat, lng, types):
url = 'http://maps.googleapis.com/maps/api/geocode/json' + \
'?latlng={},{}&sensor=false'.format(lat, lng)
jsondata = json.load(urllib2.urlopen(url))
address_comps = jsondata['results'][0]['address_components']
filter_method = lambda x: len(set(x['types']).intersection(types))
returnfilter(filter_method, address_comps)
lat, lng = 59.3, 18.1
types = ['locality', 'administrative_area_level_1']
# Display all geographical names along with their typesfor geoname in get_geonames(lat, lng, types):
common_types = set(geoname['types']).intersection(set(types))
print'{} ({})'.format(geoname['long_name'], ', '.join(common_types))
Post a Comment for "How To Reverse Geocode Serverside With Python, Json And Google Maps?"