In Previous paper, I used the Google Maps Geocoding API to find the latitude and longitude coordinates from the zip code, but it is also called Heart Rails. However, it provides an API for converting zip code and address data (HeartRails Geo API). If you want to use this API from python, you only need the requests
package and the json
package.
The basic procedure is to set the required parameters to payload
and access the API URL withrequests.get ()
. If the connection is correct, the result will be returned in json
format, so extract only the necessary parts.
geo_coding2.py
### sample script
import requests
import json
url = 'http://geoapi.heartrails.com/api/json'
payload = {'method':'searchByPostal'}
payload['postal']= '100-0001'
res = requests.get(url, params=payload).json()['response']['location'][0]
print('%s, %s, %s, %s, %s, %s\n' % (res['postal'], res['prefecture'], res['city'], res['town'], res['y'],res['x']))
In the above example, the coding method is searchByPostal
and the zip code to be searched is 100-0001
. Geocoding can be done with just this many scripts.
If you have a lot of zip code data to convert, you can store it in a list and process it with a for
loop. However, if you convert a large amount of data at one time, access restrictions may be applied. Please be careful to comply with the terms of use of the API service when using it.