7

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.

Community
  • 1
  • 1
user231302
  • 111
  • 1
  • 1
  • 3
  • for speed you need the time it took to travel from A to B. Haversine works to find distance. So speed is trivial to find. Find direction here: http://stackoverflow.com/questions/3209899/determine-compass-direction-from-one-lat-lon-to-the-other – zengr Jul 26 '14 at 06:30
  • You must have missed this: http://stackoverflow.com/questions/15736995/how-can-i-quickly-estimate-the-distance-between-two-latitude-longitude-points – dappiu Jul 26 '14 at 06:32

2 Answers2

3
  1. Find distance between A and B by haversine. Haversine Formula in Python (Bearing and Distance between two GPS points)
  2. Find direction from A to B (bearing): Determine compass direction from one lat/lon to the other
  3. Assuming you know the time to travel from A to B. Speed = distance/time
Community
  • 1
  • 1
zengr
  • 38,346
  • 37
  • 130
  • 192
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
WraithWireless
  • 634
  • 9
  • 21