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("https://raw.githubusercontent.com/cahyass/GIS_Approach_on_Covid19/master/Hospital_for_treatment_covid19.csv")
# View the top rows of the dataset
df_hospital
# 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']
list2_lat.append(lat)
list2_long.append(long)
# create new columns from lists
df_hospital['Latitude'] = list2_lat
df_hospital['Longitude'] = list2_long
df_hospital
df_hospital.to_csv('Hospital.csv',index=False)
df_hospital = pd.read_csv("C:/Users/Cahya/Downloads/ibm-solutions-summit-2020-dataplatform-master/Hospital.csv")
# View the top rows of the dataset
df_hospital

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
df_timesheet
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]
add.append(value[1])

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

--

--

--

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

State of Art Tools for Rapid Compiler Development

BQN, Lettuce and Tomato

IoT Based Drip Irrigation and Weather Forecasting

Python Socket Timeouts