LoginSignup
1
0

More than 3 years have passed since last update.

pythonで緯度経度2地点間の距離計算(球面三角法利用)

Posted at
import math


def get_distance_m(lat1, lon1, lat2, lon2):
    """
    2点間の距離(m)
    球面三角法を利用した簡易的な距離計算
    GoogleMapAPIのgeometory.computeDistanceBetweenのロジック
    https://www.suzu6.net/posts/167-php-spherical-trigonometry/
    """
    R = 6378137.0  # 赤道半径
    lat1 = math.radians(lat1)
    lon1 = math.radians(lon1)
    lat2 = math.radians(lat2)
    lon2 = math.radians(lon2)
    diff_lon = lon1 - lon2
    dist = math.sin(lat1) * math.sin(lat2) + math.cos(lat1) * math.cos(lat2) * math.cos(diff_lon)
    return R * math.acos(min(max(dist, -1.0), 1.0))


if __name__ == "__main__":
    # 緯度と経度方向に1秒角移動したときの距離を計算する
    distance = get_distance_m(lon1=139.0,
                              lat1=35.0,
                              lon2=139.0 + 1.0 / 3600.0,
                              lat2=35.0 + 1.0 / 3600.0)
    print(distance)

実行結果

39.972442531744505
1
0
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
1
0