LoginSignup
3
1

More than 3 years have passed since last update.

pythonでGeocoding

Last updated at Posted at 2020-02-16

久しぶりにPythonのライブラリを試したので、備忘録として。
使ったのはgeopy と geocoder
Google Colabを使ったのでpythonのversionは3です。
https://colab.research.google.com/drive/1LlQ2131p4JV3IY9xOyZcGp_Wg3fRk2p3

インストール パッケージ

$ pip install geopy geocoder

https://github.com/geopy/geopy
https://github.com/DenisCarriere/geocoder

code geocoder

import geocoder

my_location = geocoder.ip('me')
my_location.geojson

Colabで実行したので、orgがGoogleになっています。
西の方のアドレスが出てくるのかと思ったんですが、ちゃんと自動で物理的に近いサーバに繋がるようになっているっぽいです。
ちなみに、コードを実行した場所はNYのダウンタウンです。

{'features': [{'geometry': {'coordinates': [-74.006, 40.7143],
    'type': 'Point'},
   'properties': {'address': 'New York City, New York, US',
    'city': 'New York City',
    'country': 'US',
    'hostname': '169.205.229.35.bc.googleusercontent.com',
    'ip': '35.229.205.169',
    'lat': 40.7143,
    'lng': -74.006,
    'ok': True,
    'org': 'AS15169 Google LLC',
    'postal': '10004',
    'raw': {'city': 'New York City',
     'country': 'US',
     'hostname': '169.205.229.35.bc.googleusercontent.com',
     'ip': '35.229.205.169',
     'loc': '40.7143,-74.0060',
     'org': 'AS15169 Google LLC',
     'postal': '10004',
     'readme': 'https://ipinfo.io/missingauth',
     'region': 'New York',
     'timezone': 'America/New_York'},
    'state': 'New York',
    'status': 'OK'},
   'type': 'Feature'}],
 'type': 'FeatureCollection'}
# 緯度経度
my_location.latlng

# 住所
my_location.address

住所は番地までは出てこないので、対応するデータの粒度が都市サイズとかだと、
ゴニョゴニョ簡単に出来そうです。

[40.7143, -74.006]
'New York City, New York, US'

code geopy

from geopy.geocoders import Nominatim
# locator作成
geolocator = Nominatim(user_agent="user", timeout=10)
location = geolocator.geocode('New York City, New York, US')
location.latitude, location.longitude
location = geolocator.reverse("40.7127281 -74.0060152")
location.address

比較のためにgeocoderが表示した住所を使ってみました。
geopyの方が細かく出るみたいです、Googleマップで見る限り、アドレスの精度は結構良さげです。
https://www.google.com/maps/place/40%C2%B042'45.8%22N+74%C2%B000'21.7%22W/@40.7127281,-74.0066803,18z/data=!3m1!4b1!4m5!3m4!1s0x0:0x0!8m2!3d40.7127281!4d-74.0060152

(40.7127281, -74.0060152)
'New York City Hall, 260, Broadway, Civic Center, Manhattan Community Board 1, Manhattan, New York County, New York, 10000, United States of America'

最後に東京タワーとタイムズスクウェアの距離を確認してみました。

from geopy import distance

loc_tokyo = geolocator.geocode("Tokyo tower, Tokyo, Japan")
loc_times_square = geolocator.geocode("Times Square, Manhattan, NY")
src = (loc_tokyo.latitude, loc_tokyo.longitude)
dist = (loc_times_square.latitude, loc_times_square.longitude)
distance.distance(src, dist).km
distance.distance(src, dist).miles

簡単に出せました。ただ、loc_tokyoの住所を見たら、
'東京タワー, 東京タワー通り, 麻布, 南青山6, 港区, 東京都, 105-0011, 日本 (Japan)'という知らない住所になりました。
多分、lat & lon でちゃんと東京とNYの場所を設定するば、精度は上がるのだと思います。

10870.216621346974 # km
6754.439461884453 # mile
3
1
1

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
3
1