pip install reverse_geocoder
OR
pip install --upgrade reverse_geocoder
In [32]:
import reverse_geocoder as rg
In [33]:
import pprint
pp = pprint.PrettyPrinter(indent=4)
coordinate = (51.520328, -0.097712)
In [34]:
result = rg.get(coordinate) # default mode = 2
In [35]:
pp.pprint(result)
In [36]:
coordinates = (51.5214588,-0.1729636),(9.936033, 76.259952),(37.38605,-122.08385)
In [37]:
results = rg.search(coordinates) # default mode = 2
In [38]:
pp.pprint(results)
In [39]:
coordinates = (51.459185, -2.585109),(1.292697, 103.852033),(-27.450283, 153.016957)
In [40]:
results_slow = rg.search(coordinates,mode=1)
In [41]:
pp.pprint(results_slow)
In [42]:
import csv
input_filename = '/Users/ajay/Workspace/pydata_demo/coordinates_10000.csv'
output_filename = '/Users/ajay/Workspace/pydata_demo/coordinates_10000_geocoded.csv'
cities = [(row[0],row[1]) for row in csv.reader(open(input_filename,'rt'),delimiter='\t')]
In [43]:
pp.pprint(cities[0:5])
In [44]:
results = rg.search(cities)
In [45]:
rows = []
for idx, city in enumerate(cities):
write_row = []
lat = city[0]
lon = city[1]
gdata = results[idx]
rows.append([lat,lon,gdata['name'],gdata['admin1'],gdata['admin2'],gdata['cc']])
csvwriter = csv.writer(open(output_filename,'wt'),delimiter='\t')
csvwriter.writerows(rows)
In [45]: