top of page
Untitled

DATA DOUBLE CONFIRM

OneMap API



If you're looking for the latitude and longitude of places in Singapore, you can make use of OneMap API. It is free for public use.


Below is an example how you can call the API:

import pandas as pd, numpy as np
import requests
import json
url = "https://www.onemap.gov.sg/api/common/elastic/search?searchVal=Jurong West Street 82 &returnGeom=Y&getAddrDetails=Y&pageNum=1"
response = requests.request("GET", url)
print(response.text)


However for some locations, they might return more than one result as the road can be very long, for example, an expressway.


url = "https://www.onemap.gov.sg/api/common/elastic/search?searchVal=PIE towards Changi&returnGeom=Y&getAddrDetails=Y&pageNum=1"


Let's say I have a dataframe that contains a list of locations that I would like to get the latitude and longitude values for. For simplicity, I will take the first pair returned within the results of the API call.


latlon_list = []
for i in range(0,2): #len(mydataset_df["location"])
	location = mydataset_df["location"][i]
	url = "https://www.onemap.gov.sg/api/common/elastic/search?searchVal={}&returnGeom=Y&getAddrDetails=Y&pageNum=1".format(mydataset_df["location"][i])
	response = requests.request("GET", url)
	k_json = json.loads(response.text)
	latitude = k_json["results"][0]['LATITUDE']
	longitude = k_json["results"][0]['LONGITUDE']
	latlon_list.append((location, latitude, longitude))

latlon_df = pd.DataFrame(np.array(latlon_list))
latlon_df.columns = ['location','latitude','longitude']

The jupyter notebook containing the code is also up on Github here: https://github.com/hxchua/datadoubleconfirm/blob/master/notebooks/singapore_latlon.ipynb. We are importing a Dataiku dataset into a dataframe at the start and hence you will notice Dataiku related packages are involved. You can still disregard the Dataiku related packages if you are not using Dataiku. You can still do this regardless as long as you have a list of locations to iterate through.






Commentaires


bottom of page