您的位置:首页 > 编程语言 > Python开发

python3-经纬度的相关计算

2017-07-06 17:21 1456 查看
一.计算两个点之间的距离(经纬度的计算)

#from math import radians, cos, sin, asin, sqrt
from math import *
def haversine(lon1, lat1, lon2, lat2): # 经度1,纬度1,经度2,纬度2 (十进制度数)
# 将十进制度数转化为弧度
# math.degrees(x):为弧度转换为角度
# math.radians(x):为角度转换为弧度
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine公式
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 # 地球平均半径,单位为公里
return c * r*1000

if __name__=='__main__':
a=haversine(103.85012,36.03653,103.84085,36.03842)
print(a)
#注:该结果的数值存在问题


二.计算多个经纬度的中心

#通过经纬度,找出中心点
from math import *
def center_geolocation(geolocations):
x = 0
y = 0
z = 0
lenth = len(geolocations)
for lon, lat in geolocations:
lon = radians(float(lon))
lat = radians(float(lat))

x += cos(lat) * cos(lon)
y += cos(lat) * sin(lon)
z += sin(lat)

x = float(x / lenth)
y = float(y / lenth)
z = float(z / lenth)

return (degrees(atan2(y, x)), degrees(atan2(z, sqrt(x * x + y * y))))

if __name__ == '__main__':
locations = [[116.568627, 39.994879], [116.564791, 39.990511], [116.575012, 39.984311]]
print(center_geolocation(locations))
##结果
(116.56947685224392, 39.98990040970147)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python