Forward vs Reverse Geocoding

Library & API Key

!conda install -c conda-forge geocoder --yes
print ("install geocoder")
!conda install -c conda-forge/label/gcc7 geocoder --yes
print ("install geocoder2")
!conda install -c conda-forge/label/cf201901 geocoder --yes
print ("install geocoder3")
!conda install -c conda-forge/label/cf202003 geocoder --yes
print ("install geocoder4")
!pip install opencage
print ("install opencage")
import pandas as pd # library for data analsysis
from opencage.geocoder import OpenCageGeocode
key = '<Your API key>'
geocoder = OpenCageGeocode(key)
query = 'Jakarta, Indonesia'
results = geocoder.geocode(query)

Forward Geocoding

# Read in the data Hospital for treatment covid-19
df_hospital = pd.read_csv("")
# View the top rows of the dataset
# Isolate only the Latitude & Longitude of Jakarta from the Json file
lat = results[0]['geometry']['lat']
lng = results[0]['geometry']['lng']
print ('The Latitude and Longitude of Jakarta is {} and {} repectively.'.format(lat, lng))
The Latitude and Longitude of Jakarta is -6.1753942 and 106.827183 repectively.
# Get the latitude and longitude of all of the specialist hospitals
list2_lat = [] # create empty lists for latitude
list2_long = [] # create empty lists for longitude
for index, row in df_hospital.iterrows(): # iterate over rows in dataframe
hosp = row['Hospital']
distr = row['District']
query = str(hosp) + ', ' + str(distr) + ', Jakarta'
results = geocoder.geocode(query)
lat = results[0]['geometry']['lat']
long = results[0]['geometry']['lng']
# create new columns from lists
df_hospital['Latitude'] = list2_lat
df_hospital['Longitude'] = list2_long
df_hospital = pd.read_csv("C:/Users/Cahya/Downloads/ibm-solutions-summit-2020-dataplatform-master/Hospital.csv")
# View the top rows of the dataset

Reverse Geocoding

df_timesheet = pd.read_csv("C:/Users/Cahya/Downloads/ibm-solutions-summit-2020-dataplatform-master/timesheet.csv")
# View the top rows of the dataset
add = []
for index, row in df_timesheet.iterrows(): # iterate over rows in dataframe
lat = float(df_timesheet['Latitude'][index])
lng = float(df_timesheet['Longitude'][index])
reverse = geocoder.reverse_geocode(lat,lng)
df_data = pd.json_normalize(reverse)
value = df_data.loc[0]

df_timesheet['Address'] = add
df_timesheet = pd.read_csv("C:/Users/Cahya/Downloads/ibm-solutions-summit-2020-dataplatform-master/reverse.csv")
# View the top rows of the dataset




Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
cahyati sangaji (cahya)

cahyati sangaji (cahya)

More from Medium

Rename images based on the text on a image

AI Music Generation and Interpolation

How can deep learning mitigate marine pollution?

1. Point Cloud for Newbies — After all, what is point cloud or LiDAR data?