-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeocode.py
35 lines (29 loc) · 1.23 KB
/
geocode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 3 10:39:17 2018
@author: Chintan Maniyar
"""
import requests, csv, math
import pandas as pd
GEOCODE_URL = 'https://maps.googleapis.com/maps/api/geocode'
df = pd.read_csv('prod.csv')
api_key = ""
coordinates = {'lat':[], 'lng':[]}
def generateGeocodes():
for address in df['place']:
if address != "00":
print(address)
api_response = requests.get(GEOCODE_URL + '/json?address={0}&key={1}'.format(address, api_key))
api_response_dict = api_response.json()
if api_response_dict['status'] == 'OK':
print(api_response_dict['results'][0]['geometry']['location']['lat'])
print(api_response_dict['results'][0]['geometry']['location']['lng'])
coordinates['lat'].append(api_response_dict['results'][0]['geometry']['location']['lat'])
coordinates['lng'].append(api_response_dict['results'][0]['geometry']['location']['lng'])
try:
with open('coordinates.csv', 'w', encoding='utf-8') as file:
writer = csv.writer(file, delimiter=',')
writer.writerow(coordinates.keys())
writer.writerows(zip(*coordinates.values()))
except IOError as e:
print(e)