python算两个点的距离公式_Python中的Haversine公式(两个GPS点之间的方位和距离)...

Problem

I would like to know how to get the distance and bearing between 2 GPS points.

I have researched on the haversine formula.

Someone told me that I could also find the bearing using the same data.

Edit

Everything is working fine but the bearing doesn't quite work right yet. The bearing outputs negative but should be between 0 - 360 degrees.

The set data should make the horizontal bearing 96.02166666666666

and is:

Start point: 53.32055555555556 , -1.7297222222222221

Bearing: 96.02166666666666

Distance: 2 km

Destination point: 53.31861111111111, -1.6997222222222223

Final bearing: 96.04555555555555

Here is my new code:

from math import *

Aaltitude = 2000

Oppsite = 20000

lat1 = 53.32055555555556

lat2 = 53.31861111111111

lon1 = -1.7297222222222221

lon2 = -1.6997222222222223

lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])

dlon = lon2 - lon1

dlat = lat2 - lat1

a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2

c = 2 * atan2(sqrt(a), sqrt(1-a))

Base = 6371 * c

Bearing =atan2(cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon2-lon1), sin(lon2-lon1)*cos(lat2))

Bearing = degrees(Bearing)

print ""

print ""

print "--------------------"

print "Horizontal Distance:"

print Base

print "--------------------"

print "Bearing:"

print Bearing

print "--------------------"

Base2 = Base * 1000

distance = Base * 2 + Oppsite * 2 / 2

Caltitude = Oppsite - Aaltitude

a = Oppsite/Base

b = atan(a)

c = degrees(b)

distance = distance / 1000

print "The degree of vertical angle is:"

print c

print "--------------------"

print "The distance between the Balloon GPS and the Antenna GPS is:"

print distance

print "--------------------"

解决方案

Here's a Python version:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值