Python calculate speed, distance, direction from 2 GPS coordinates
15,395
Solution 1
- Find distance between A and B by haversine. Haversine Formula in Python (Bearing and Distance between two GPS points)
- Find direction from A to B (bearing): Determine compass direction from one lat/lon to the other
- Assuming you know the time to travel from A to B. Speed = distance/time
Solution 2
try using pyproj. I had to determine distance (among other things) for a project dealing with LOBs and I found pyproj to be invaluable. (Note: my points were in MGRS format and had to be converted to lat/lon first)
_GEOD = pyproj.Geod(ellps='WGS84')
_MGRS = mgrs.MGRS()
def dist(sp,ep):
try:
# convert start point and end point
(sLat,sLon) = _MGRS.toLatLon(sp)
(eLat,eLon) = _MGRS.toLatLon(ep)
# inv returns azimuth, back azimuth and distance
a,a2,d = _GEOD.inv(sLon,sLat,eLon,eLat)
except:
raise ValueError, "Invalid MGRS point"
else:
return d,a
Author by
user231302
Updated on June 04, 2022Comments
-
user231302 almost 2 years
How do I calculate the speed, distance and direction (degrees) from 2 GPS coordinates in Python? Each point has lat, long, time.
I found the Haversine distance calculation in this post:
Calculate distance between 2 GPS coordinates.
It also has a Java version for speed and direction, but they are in metric and I need MPH, and in Python.