1
$\begingroup$

Let's say I have two geolocations at given times. How can I interpolate these two geolocation and find any location for a given time time3 in python?

time1 = '29-07-2022 22:15:00'
lat1 = 37.55
lon1 = -122.23

time2 = '29-07-2022 22:30:00'
lat2 = 40.66
lon2 = -128.99

time3 = '29-07-2022 22:20:00'
lat3 = ?
lon3 = ?
$\endgroup$

2 Answers 2

3
$\begingroup$

The Earth is a globe and we can't just use linear interpolation by latitude and longitude.

Interpolation algorithm:

  • Get distance between start and finish
  • Get direction from start to finish
  • Calculate interpolated distance
  • Move interpolated distance from start in the same direction

Here is a code for your example. I use geographiclib package.

from geographiclib.geodesic import Geodesic
import datetime

time1 = '29-07-2022 22:15:00'
lat1 = 37.55
lon1 = -122.23

time2 = '29-07-2022 22:30:00'
lat2 = 40.66
lon2 = -128.99

time3 = '29-07-2022 22:20:00'
# lat3 = ?
# lon3 = ?

def parse_datetime(t):
    return datetime.datetime.strptime(t, '%d-%m-%Y %H:%M:%S')

def interpolate(lat1, lon1, lat2, lon2, time1, time2, time3):
    # Parse datetime to seconds since epoch
    ts1 = parse_datetime(time1).timestamp()
    ts2 = parse_datetime(time2).timestamp()
    ts3 = parse_datetime(time3).timestamp()
    
    # Get distance and direction between initial points
    inverse_result = Geodesic.WGS84.Inverse(lat1, lon1, lat2, lon2)
    distance = inverse_result['s12']
    angle = inverse_result['azi1']
    
    # Calculate new distance from the first point
    speed = distance / (ts2 - ts1)
    distance3 = speed * (ts3 - ts1)
    
    # Get new location
    direct_result = Geodesic.WGS84.Direct(lat1, lon1, angle, distance3)
    lat3 = direct_result['lat2']
    lon3 = direct_result['lon2']

    return lat3, lon3

lat3, lon3 = interpolate(lat1, lon1, lat2, lon2, time1, time2, time3)

print('lat3 =', lat3) # lat3 = 38.629939149306345
print('lon3 =', lon3) # lon3 = -124.41750500988798
$\endgroup$
1
$\begingroup$

If you have the space and time to find another space you need the velocity first, suposing your velocity is constant you can make something like this:

delta_t = t2 - t1
vx = (lat2 -  lat1) / delta_t
vy = (lon2 -  lon1) / delta_t
delta_t2 = t3 - t2
lat3 = lat1 + vx*delta_t2 
lon3 = lon1 + vy*delta_t2
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.