LoginSignup
11
15

More than 5 years have passed since last update.

pythonでジオコーディング

Posted at

「東京タワー」や「両国国技館」のようなランドマーク名から正確な住所や緯度・経度等の地理情報を得ることをジオコーディングという。

Googleが公開しているAPIをpythonでたたいてみた。('17年11月現在、2500req/日のリミットレート)

まず調べたいスポット名をこんな風に書いて…

queries
東京タワー
両国国技館

以下のpython geo.pyを実行

geo.py
# -*- coding: utf-8 -*-
import requests
import json
from time import sleep

wait_time = 0.3 # (sec)
base_url = 'https://maps.googleapis.com/maps/api/geocode/json?language=ja&address={}&key=****YOUR_API_KEY***'
headers = {'content-type': 'application/json'}


queries = []
with open('./queries', 'r') as f:
    queries = f.readlines()

for q in queries:
    q = q.strip()
    url = base_url.format(q)
    r = requests.get(url, headers=headers)
    data = r.json()
    if 'results' in data and len(data['results']) > 0 and 'formatted_address' in data['results'][0]:
        print '{}@{}@{}@{}'.format(
            q,
            data['results'][0]['formatted_address'].encode('utf-8'),
            data['results'][0]['geometry']['location']['lat'],
            data['results'][0]['geometry']['location']['lng']
        )
    else:
        print '{}@ @ @ '.format(q)
    sleep(wait_time)

結果

東京タワー@日本、〒105-0011 東京都港区芝公園4丁目2−8@35.6585805@139.7454329
両国国技館@日本、〒130-0015 東京都墨田区横網1丁目3番28号@35.6969758@139.7932863

スポット名@正確な住所@緯度@経度@

という形で出力。

11
15
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
11
15