calculating a gps coordinate given a point, bearing and distance

会有一股神秘感。 提交于 2019-12-03 00:48:24

Why don't you use nice libraries?

from geopy import Point
from geopy.distance import distance, VincentyDistance

# given: lat1, lon1, bearing, distMiles
lat2, lon2 = VincentyDistance(miles=distMiles).destination(Point(lat1, lon1), bearing)

For lat1, lon1, distMiles, bearing = 42.189275,-76.85823, 0.5, 30 it returns 42.1955489, -76.853359.

The sin and cos functions expect their arguments in radians, not in degrees. The asin and atan2 functions produce a result in radians, not in degrees. In general, one needs to convert input angles (lat1, lon1 and bearing) from degrees to radians using math.radians() and convert output angles (lat2 and lon2) from radians to degrees using math.degrees().

Note that your code has two other problems:

(1) It doesn't allow for travel across the 180-degrees meridian of longitude; you need to constrain your answer such that -180 <= longitude_degrees <= +180.

(2) If you are going to use this function extensively, you might like to remove the redundant calculations: sin(lat1), cos(dr), cos(lat1), and sin(dr) are each calculated twice.

newer version of geopy(+ kilometers instead miles)

from geopy import Point
from geopy.distance import vincenty

distKm = 1
lat1 = 35.68096477080332 
lon1 = 139.76720809936523

print 'center', lat1, lon1
print 'north', vincenty(kilometers=distKm).destination(Point(lat1, lon1), 0).format_decimal()
print 'east', vincenty(kilometers=distKm).destination(Point(lat1, lon1), 90).format_decimal()
print 'south', vincenty(kilometers=distKm).destination(Point(lat1, lon1), 180).format_decimal()
print 'west', vincenty(kilometers=distKm).destination(Point(lat1, lon1), 270).format_decimal()

result is

center 35.6809647708 139.767208099
north 35.6899775841, 139.767208099
east 35.680964264, 139.778254714
south 35.6719519439, 139.767208099
west 35.680964264, 139.756161485
user3647420

eumiro your code
result is Too many values to unpack
how fix this

from geopy import Point
from geopy.distance import distance, VincentyDistance

# given: lat1, lon1, bearing, distMiles
lat2, lon2 = VincentyDistance(miles=9.32057).destination(Point(52.20444, 0.3605$
print lat2, lon2
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!