Skip to content

Commit 30df5ce

Browse files
committed
initial commit
1 parent 6fd020f commit 30df5ce

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

Geocoding Google API/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## HOW TO USE:
2+
1. Simply just grab an api key from the google maps api/geocode.
3+
2. Create an excel workbook with a name of your choosing.
4+
3. Create a sheet name in this excel workbook with a name of your choosing.
5+
4a. Create a column labeled address where your addresses will be listed
6+
4b. If you want to label your column something else, you can do so, but make sure to change it in the geo.py file
7+
5. The program will then grab each address and convert it to fit the url format and then fetch the corresponding latitude
8+
& longitutde to be returned
9+
6. It will be then outputed to a new excel file called output and saved.
10+
11+
'''
12+
'''
13+
if there is an error in retrieving the lat or long, the program
14+
will instead put a 0,0. this is because the program doesn't account for errors on the request
15+
of which it responds with a 200 success, but results in an index out of bounds error
16+
because the return specifies the api key is wrong yet it is right.
17+
for the few entries this problem occurs it can be done manually or programmed further to account for
18+

Geocoding Google API/geo.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import pandas as pd
2+
import requests
3+
import openpyxl
4+
from requests.models import HTTPError
5+
import xlsxwriter
6+
'''
7+
HOW TO USE:
8+
1. Simply just grab an api key from the google maps api/geocode.
9+
2. Create an excel workbook with a name of your choosing.
10+
3. Create a sheet name in this excel workbook with a name of your choosing.
11+
4a. Create a column labeled address where your addresses will be listed
12+
4b. If you want to label your column something else, you can do so, but make sure to change it in the geo.py file
13+
5. The program will then grab each address and convert it to fit the url format and then fetch the corresponding latitude
14+
& longitutde to be returned
15+
6. It will be then outputed to a new excel file called output and saved.
16+
17+
'''
18+
'''
19+
if there is an error in retrieving the lat or long, the program
20+
will instead put a 0,0. this is because the program doesn't account for errors on the request
21+
of which it responds with a 200 success, but results in an index out of bounds error
22+
because the return specifies the api key is wrong yet it is right.
23+
for the few entries this problem occurs it can be done manually or programmed further to account for
24+
25+
26+
'''
27+
my_new_data = []
28+
29+
workbook = xlsxwriter.Workbook('output.xlsx')
30+
worksheet = workbook.add_worksheet()
31+
32+
# editable
33+
COLUMN_IDX_START = 10 # -> corresponds to the latitude column
34+
COLUMN_IDX_START_2 = 11 # -> corresponds to the longitude column
35+
WORKBOOK_NAME = 'YOURWORKBOOKNAMEHERE.xlsx'
36+
SHEET_NAME = 'YOURSHEETNAMEHERE'
37+
COLUMN_NAME = 'address'
38+
MAPS_URL = 'https://maps.googleapis.com/maps/api/geocode/json?address='
39+
GOOGLE_API_KEY = 'YOURAPI_KEY HERE'
40+
API_KEY = '&key=' + GOOGLE_API_KEY
41+
42+
43+
def getGeoCodeFromAddress():
44+
wb = openpyxl.load_workbook(WORKBOOK_NAME)
45+
ws = wb[SHEET_NAME]
46+
excel_data_df = pd.read_excel(WORKBOOK_NAME, sheet_name=SHEET_NAME)
47+
list_data = excel_data_df[COLUMN_NAME].tolist()
48+
for address in list_data:
49+
sanitized_add = address.replace(" ", "+")
50+
my_new_data.append(sanitized_add)
51+
for row_num, data in enumerate(my_new_data, start=1):
52+
try:
53+
lat, long = getLatAndLong(data)
54+
except:
55+
lat, long = 0, 0
56+
worksheet.write(row_num, COLUMN_IDX_START, lat)
57+
worksheet.write(row_num, COLUMN_IDX_START_2, long)
58+
workbook.close()
59+
60+
61+
def getLatAndLong(url):
62+
url = MAPS_URL + url + API_KEY
63+
try:
64+
response = requests.get(url)
65+
except HTTPError as http_err:
66+
print(f'HTTP error occurred: {http_err}') # Python 3.6
67+
except Exception as err:
68+
print(f'Other error occurred: {err}') # Python 3.6
69+
else:
70+
print('Success!')
71+
72+
json_response = response.json()
73+
data = json_response['results'][0]['geometry']['location']
74+
latitude = data['lat']
75+
longitutde = data['lng']
76+
# print(str(latitude) + 'lat')
77+
# print(str(longitutde) + 'long')
78+
return latitude, longitutde
79+
80+
81+
getGeoCodeFromAddress()

Geocoding Google API/requirements.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
certifi==2021.5.30
2+
charset-normalizer==2.0.4
3+
et-xmlfile==1.1.0
4+
idna==3.2
5+
numpy==1.21.1
6+
openpyxl==3.0.7
7+
pandas==1.3.1
8+
python-dateutil==2.8.2
9+
pytz==2021.1
10+
requests==2.26.0
11+
six==1.16.0
12+
urllib3==1.26.6
13+
XlsxWriter==1.4.5

0 commit comments

Comments
 (0)