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 ()
0 commit comments