0

I have GPS coordinates in a CSV file that I predict it using a regression model, just two columns with longitudes and latitudes that represent a race track. Now I want to plot it on Google Maps to see how it looks like.

When I do that, I noticed that the curve is not smooth which make sense since I predicted those value with my regression model and they are not taking directly from a GPS.

I made a search on how to solve this problem and I find out that usually a spline interpolation is used for this, but I have no idea how to use it. All the examples that I found in the internet assume that we have the x which are the data and y which is the function, in my case there is no function, I just give the data to the model and it predict those values that's it. so if I have longitudes and latitudes, is it possible to make some sort of interpolation so that the curve would look smooth if I plot it?

Example:

Let's say these are my data:

latitudes = array([58.846563, 58.846573, 58.846586, 58.846601, 58.846618, 58.846637,
                   58.846658, 58.846681, 58.846705, 58.846731])

longitudes = array([9.903741, 9.903733, 9.903724, 9.903713, 9.9037  , 9.903686,
                    9.90367 , 9.903652, 9.903633, 9.903612])

and when I plot this data it give me some sort of a plot where each point is connected to the other point with a straight line but what I want is to smooth it up. Is this possible to do only if I have longitudes and latitudes as variables and nothing more?

halfer
  • 19,824
  • 17
  • 99
  • 186
basilisk
  • 1,156
  • 1
  • 14
  • 34
  • https://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html#spline-interpolation – Joe Jan 07 '20 at 16:56
  • @Joe can you please explain more? I already made some research before asking. I noticed in those examples that they used x and the function to it aka y but in my case I don't have the function y. I only have x which is a 2D array of longs and lats that's it – basilisk Jan 07 '20 at 17:02
  • That's enough. The function is only for demonstration, to show what the interpolation returns and what the "real" value would be. In your case, there is (probably) no function. – Joe Jan 07 '20 at 18:24
  • Pass your data (x, y) to the interpolator, then the x_new values where you want y_new for. – Joe Jan 07 '20 at 18:25
  • @Joe tck = interpolate.splrep(x, y, s=0) this line give me an error on input data. the function expect x to be some array and y is the function that will be applied on x. in my case it is different my input x is already a 2D array composed of longitudes and latitudes – basilisk Jan 08 '20 at 08:28
  • @Joe your suggestion will not work in my case, I already tried it – basilisk Jan 08 '20 at 08:29
  • Please post your minimum working example above with the question, with imports, data, etc. – Joe Jan 08 '20 at 09:13
  • @Joe I don't have. I just have no idea how to implement this. the only thing I have is one 2D numpy array with the shape (4000, 2) the first column correspond to latitudes and the second is longitudes. for simplicity I splited it to two arrays like I describe in the question above. the first is composed of all latitude points and the second have all longitude points – basilisk Jan 08 '20 at 15:37
  • Just copy some code from here and replace x any y with your lat/lon arrays. https://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html#spline-interpolation – Joe Jan 08 '20 at 17:45

0 Answers0