import math
def get_distance_m(lat1, lon1, lat2, lon2):
"""
Distance between two points(m)
Simple distance calculation using spherical trigonometry
Google Map API geometory.computeDistanceBetween logic
https://www.suzu6.net/posts/167-php-spherical-trigonometry/
"""
R = 6378137.0 #Equatorial radius
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__":
#Calculate the distance when moving 1 arcsecond in the latitude and longitude directions
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)
Execution result
39.972442531744505
Recommended Posts