5

I am having trouble trying to figure out if python is the best choice for interpolating a data set that I have

**lat   lon    time**
   x1    y1    3:02(t1)
            
       
   x2    y2    3:05(t2)




   x3    y3    3:10(t3)

 
   x4    y4    3:13(t4)

I left spaces to emphasis what I am seeking. I'm looking to fill the missing time in 1 second intervals. So I think what I need to do is t4-t1 in order to get 11 equal spaces, converting time to seconds to make calculations easier. Then the graph should look like this:

**lat   lon    time**

   x1    y1    3:02(t1)
   a      b    3:03
   c      d    3:04
   x2    y2    3:05(t2)
               3:06
               3:07
               3:08
               3:09
   x3    y3    3:10(t3)
               3:11
               3:12
   x4    y4    3:13(t4)

After this I am looking for the latitude and longitude at each second that I have newly created. So between x2,y2 and x1,y1 I want to find the values of a,b,c,d which will be an interpolated lat and lon.

Is there a way to do this with no consistent values between each set?

wxJunkie
  • 51
  • 2
  • 6
  • 1
    See [this (language-neutral) question and answer](http://stackoverflow.com/questions/1739019/how-to-use-linear-interpolation-estimate-current-position-between-two-geo-coordi) - depending on the speeds you're dealing with, linear interpolation may not be sufficient. – Lack Jan 17 '15 at 01:28
  • I recommend checking my answer [here](https://gis.stackexchange.com/a/267004/84004) for doing this over the earth ellipsoid using python – josue.0 Jan 05 '18 at 04:37

1 Answers1

3

The numpy library for python has a function for interpolation built-in. Note that in this example I'm using an integer for the time. You should convert the time into an array of numbers (e.g. seconds) for numpy.interp to work.

import numpy as np

times = [2, 5, 10, 13]
lat = [1.5, 2, 4, 7]
lon = [3, 2, 1, -2]
t = np.arange(2, 14)

latint = np.interp(t, times, lat)
lonint = np.interp(t, times, lon)
for pos in zip(latint, lonint, t):
    print(pos)

Should print:

(1.5, 3.0, 2)
(1.6666666666666667, 2.6666666666666665, 3)
(1.8333333333333333, 2.3333333333333335, 4)
(2.0, 2.0, 5)
(2.3999999999999999, 1.8, 6)
(2.7999999999999998, 1.6000000000000001, 7)
(3.2000000000000002, 1.3999999999999999, 8)
(3.6000000000000001, 1.2, 9)
(4.0, 1.0, 10)
(5.0, 0.0, 11)
(6.0, -1.0, 12)
(7.0, -2.0, 13)
Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • Thank you Roland! I'm starting to follow a little better. What if I wanted to import a CSV for Lat, Lon, and Time? Would I need to then extrapolate the variables according? – wxJunkie Jan 20 '15 at 20:40
  • How would I know? I don't know what is in the CSV file nor do I know what you want to do with it... – Roland Smith Jan 24 '15 at 17:34