LoginSignup
35
32

More than 5 years have passed since last update.

郵便番号のジオコーディング

Last updated at Posted at 2016-04-20

この前の論文では郵便番号から緯度経度座標を求めるのにGoogle Maps Geocoding APIを使いましたが,それ以外にもHeartRailsというところが郵便番号や住所データの変換用API(HeartRails Geo API)を提供しています。このAPIをpythonから使う場合,requestsパッケージとjsonバッケージがあれば事足ります。

基本的な手順としては,必要なパラメターをpayloadに設定し,requests.get()でAPIのURLにアクセスするだけです。正しく接続されれば,結果がjson形式で返ってきますので,あとは必要な部分だけ抜き出します。

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']))

上の例では,コーディング方法をsearchByPostal(郵便番号検索)に,検索する郵便番号を100-0001にしています。たったこれだけのスクリプトでジオコーディングができてしまいます。

変換すべき郵便番号データがたくさんある場合には,それをリストに格納してforループなどで処理すればいいでしょう。ただし,一度に大量のデータを変換した場合,アクセス制限がかけられたりすることがあるかもしれません。使用にあたっては,APIサービスの利用規約を遵守するようご注意ください。

35
32
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
35
32