最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Python中如何计算两个经纬度的点之间的距离

Python crifan 5805浏览 0评论

折腾:

【已解决】如何判断某个经纬度位置在某个位置点的某个直径范围内

期间,需要去搞清楚,如何计算两个经纬度的点之间的距离,且需要在Python中实现出来。

经度 纬度 两点判断 距离

根据两点经纬度计算距离 – whh – 博客园

private const double EARTH_RADIUS = 6378.137;//地球半径
private static double rad(double d)
{
   return d * Math.PI / 180.0;
}
public static double GetDistance(double lat1, double lng1, double lat2, double lng2)
{
   double radLat1 = rad(lat1);
   double radLat2 = rad(lat2);
   double a = radLat1 – radLat2;
   double b = rad(lng1) – rad(lng2);
   double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a/2),2) +
    Math.Cos(radLat1)*Math.Cos(radLat2)*Math.Pow(Math.Sin(b/2),2)));
   s = s * EARTH_RADIUS;
   s = Math.Round(s * 10000) / 10000;
   return s;
}

【地理】两点间方向和距离的判断_调研_新浪博客

根据两点经纬度计算距离【转】_吴超_新浪博客

根据两点经纬度计算距离 – 博客频道 – CSDN.NET

/**   
 * google maps的脚本里代码   
 */      
private const double EARTH_RADIUS = 6378.137;   
private static double rad(double d)   
{   
     return d * Math.PI / 180.0;   
}    
/**   
 * 根据两点间经纬度坐标(double值),计算两点间距离,单位为米   
 */   
public static double GetDistance(double lat1, double lng1, double lat2, double lng2)   
{   
    double radLat1 = rad(lat1);   
    double radLat2 = rad(lat2);   
    double a = radLat1 – radLat2;   
    double b = rad(lng1) – rad(lng2);   
    double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a/2),2) +   
    Math.Cos(radLat1)*Math.Cos(radLat2)*Math.Pow(Math.Sin(b/2),2)));   
    s = s * EARTH_RADIUS;   
    s = Math.Round(s * 10000) / 10000;   
    return s;   
}   

mysql 下 计算 两点 经纬度 之间的距离 计算结果排序 – 静下心来写代码 – 博客频道 – CSDN.NET

python latitude longitude distance

Calculate distance between latitude longitude pairs with Python · GitHub

#!/usr/bin/env python
# Haversine formula example in Python
# Author: Wayne Dyck
import math
def distance(origin, destination):
    lat1, lon1 = origin
    lat2, lon2 = destination
    radius = 6371 # km
    dlat = math.radians(lat2-lat1)
    dlon = math.radians(lon2-lon1)
    a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
        * math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
    d = radius * c
    return d

Haversine Formula in Python (Bearing and Distance between two GPS points) – Stack Overflow

from math import radians, cos, sin, asin, sqrt
def haversine(lon1, lat1, lon2, lat2):
    “””
    Calculate the great circle distance between two points 
    on the earth (specified in decimal degrees)
    “””
    # convert decimal degrees to radians 
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    # haversine formula 
    dlon = lon2 – lon1 
    dlat = lat2 – lat1 
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a)) 
    r = 6371 # Radius of earth in kilometers. Use 3956 for miles
    return c * r

python – How can I quickly estimate the distance between two (latitude, longitude) points? – Stack Overflow

geocoding – Getting distance between two points based on latitude/longitude [python] – Stack Overflow

pip install gpxpy –user
import gpxpy.geo
# Point one
lat1 = 52.2296756
lon1 = 21.0122287
# Point two
lat2 = 52.406374
lon2 = 16.9251681
# What you were looking for
dist = gpxpy.geo.haversine_distance(lat1, lon1, lat2, lon2)
print(dist)

GitHub – tkrajina/gpxpy: gpx-py is a python GPX parser. GPX (GPS eXchange Format) is a XML based file format for GPS track logs.

geopy 1.11.0 : Python Package Index

【整理】地球半径到底是多少

【总结】

最后用代码:

from math import radians, cos, sin, asin, sqrt
### calculate distance of two point
# http://stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-points
#def haversine(lon1, lat1, lon2, lat2):
def calcDistance(lon1, lat1, lon2, lat2):
    “””
    Calculate the great circle distance between two points
    on the earth (specified in decimal degrees)
    “””
    # convert decimal degrees to radians
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    # haversine formula
    dlon = lon2 – lon1
    dlat = lat2 – lat1
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a))
    EARTH_RADIUS = 6371 # Radius of earth in kilometers. Use 3956 for miles
    return c * EARTH_RADIUS

然后可以计算出两点之间的距离。

比如:

            calculatedDistance = calcDistance(  curUser.location.longitude,
                                                curUser.location.latitude,
                                                eachUserLocation.longitude,
                                                eachUserLocation.latitude)
            gLog.debug(“curUser.location=%s, eachUserLocation=%s -> calculatedDistance=%s”,
                       curUser.location, eachUserLocation, calculatedDistance)

输出:

curUser.location=<Location:id=location-c71da291-bd83-4292-9f54-e7baa46ac32f,createdAt=2016-11-06 11:58:38,updatedAt=2016-11-06 12:02:10,longitude=31.2764,latitude=120.739,shortStr=南京大学苏州研究生院,fullStr=江苏省苏州市吴中区仁爱路42号南京大学苏州研究生院>, eachUserLocation=<Location:id=location-4a7cd6e8-ded6-4777-aa90-6756f8c5f1b0,createdAt=2016-11-04 15:59:47,updatedAt=2016-11-06 12:03:34,longitude=31.2741,latitude=120.743,shortStr=GIST国际学院,fullStr=江苏省苏州市吴中区高博软件技术学院全球技术支持中心联合教育中心GIST国际学院> -> calculatedDistance=0.463593304637

转载请注明:在路上 » 【已解决】Python中如何计算两个经纬度的点之间的距离

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.200 seconds, using 22.17MB memory