Skip to content

Addition of Geocoding Python #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Geocoding Google API/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## HOW TO USE:
1. Simply just grab an api key from the google maps api/geocode.
2. Create an excel workbook with a name of your choosing.
3. Create a sheet name in this excel workbook with a name of your choosing.
4a. Create a column labeled address where your addresses will be listed
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
5. The program will then grab each address and convert it to fit the url format and then fetch the corresponding latitude
& longitutde to be returned
6. It will be then outputed to a new excel file called output and saved.

'''
'''
if there is an error in retrieving the lat or long, the program
will instead put a 0,0. this is because the program doesn't account for errors on the request
of which it responds with a 200 success, but results in an index out of bounds error
because the return specifies the api key is wrong yet it is right.
for the few entries this problem occurs it can be done manually or programmed further to account for

81 changes: 81 additions & 0 deletions Geocoding Google API/geo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import pandas as pd
import requests
import openpyxl
from requests.models import HTTPError
import xlsxwriter
'''
HOW TO USE:
1. Simply just grab an api key from the google maps api/geocode.
2. Create an excel workbook with a name of your choosing.
3. Create a sheet name in this excel workbook with a name of your choosing.
4a. Create a column labeled address where your addresses will be listed
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
5. The program will then grab each address and convert it to fit the url format and then fetch the corresponding latitude
& longitutde to be returned
6. It will be then outputed to a new excel file called output and saved.
'''
'''
if there is an error in retrieving the lat or long, the program
will instead put a 0,0. this is because the program doesn't account for errors on the request
of which it responds with a 200 success, but results in an index out of bounds error
because the return specifies the api key is wrong yet it is right.
for the few entries this problem occurs it can be done manually or programmed further to account for
'''
my_new_data = []

workbook = xlsxwriter.Workbook('output.xlsx')
worksheet = workbook.add_worksheet()

# editable
COLUMN_IDX_START = 10 # -> corresponds to the latitude column
COLUMN_IDX_START_2 = 11 # -> corresponds to the longitude column
WORKBOOK_NAME = 'YOURWORKBOOKNAMEHERE.xlsx'
SHEET_NAME = 'YOURSHEETNAMEHERE'
COLUMN_NAME = 'address'
MAPS_URL = 'https://maps.googleapis.com/maps/api/geocode/json?address='
GOOGLE_API_KEY = 'YOURAPI_KEY HERE'
API_KEY = '&key=' + GOOGLE_API_KEY


def getGeoCodeFromAddress():
wb = openpyxl.load_workbook(WORKBOOK_NAME)
ws = wb[SHEET_NAME]
excel_data_df = pd.read_excel(WORKBOOK_NAME, sheet_name=SHEET_NAME)
list_data = excel_data_df[COLUMN_NAME].tolist()
for address in list_data:
sanitized_add = address.replace(" ", "+")
my_new_data.append(sanitized_add)
for row_num, data in enumerate(my_new_data, start=1):
try:
lat, long = getLatAndLong(data)
except:
lat, long = 0, 0
worksheet.write(row_num, COLUMN_IDX_START, lat)
worksheet.write(row_num, COLUMN_IDX_START_2, long)
workbook.close()


def getLatAndLong(url):
url = MAPS_URL + url + API_KEY
try:
response = requests.get(url)
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}') # Python 3.6
except Exception as err:
print(f'Other error occurred: {err}') # Python 3.6
else:
print('Success!')

json_response = response.json()
data = json_response['results'][0]['geometry']['location']
latitude = data['lat']
longitutde = data['lng']
# print(str(latitude) + 'lat')
# print(str(longitutde) + 'long')
return latitude, longitutde


getGeoCodeFromAddress()
13 changes: 13 additions & 0 deletions Geocoding Google API/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
certifi==2021.5.30
charset-normalizer==2.0.4
et-xmlfile==1.1.0
idna==3.2
numpy==1.21.1
openpyxl==3.0.7
pandas==1.3.1
python-dateutil==2.8.2
pytz==2021.1
requests==2.26.0
six==1.16.0
urllib3==1.26.6
XlsxWriter==1.4.5