Interpolating between two GPS locations

I have two endpoint GPS locations and between these two points are several other points (for example 1000 points) which are unevenly spaced. My challenge is to write a code which can spread these points evenly between the two extreme GPS points which are acting as the boundary points.
I will like you all to suggest to me how to proceed.

Notes:

  1. Boundary points: [38.77813955, -1.833538909] and [38.77202007, -1.842456068]
  2. Number of arbitrary points in between the boundary point = 1000

I look forward to receiving feedback soon and thanks in advance.

Regards

hey hey,

What if you do something like:
section_width <- (boundary_max - boundary_min)/ how many divides you want?
Then do a seq(boundary_min,boundary_max,section_width)?

Hope this makes sense?

There is also a very useful function called st_make_grid that is part of the sf package, worthwhile investigating that.

Regards

thanks for the reply!
since the extreme points has both longitude and latitude, which of these two figures do I provide for the "boundary_max and boundary_min" you stated.?

Regards!

all it means is you really have to only swop the coordinates out.

so I have this:
library(sf)
library(leaflet)

#method one
points <- data.frame(lat = c(38.77813955,38.77202007),lng= c(-1.833538909,-1.842456068))

points <- st_as_sf(points,coords = c('lat','lng'))

leaflet(data = points) %>%
addProviderTiles(provider = providers$OpenStreetMap.Mapnik) %>%
addMarkers()

#method two
points <- data.frame(lat = c(38.77813955,38.77202007,38.77813955,38.77202007),lng= c(-1.833538909,-1.842456068,-1.842456068,-1.833538909))

leaflet(data = points) %>%
addProviderTiles(provider = providers$OpenStreetMap.Mapnik) %>%
addMarkers()

ShinybeaR, very grateful for your contribution.
one more question:

the #method 2 works very well however, I had to exclude the repetition of the lat and lng as shown below:

....points <- data.frame(lat = c(38.77813955,38.77202007), lng= c(-1.833538909,-1.842456068))....

The resulting figure is:
r_challenge

the main challenge now are:

  1. writing a code to divide the distance between these two points into for example 100 sections and
  2. writing another code to identify the coordinates of each division.

what do you suggest?
Thanks in advance:wink:.

ah I miss understood at first.
I see what you need.
We would need to do a little bit of math to get this done.
y = mx + c
To generate the coordinates needed.

ShinybeaR, can you elaborate a little more the need for the linear equation?

It is means to generate the coordinates of the line between the two points.
The way I would approach it is to find the m, which is the gradient (delta -y / delta - x).
You can the derive the c.

now you can start generating the code from the closet point.

But it means we'd need to know a step change (based on the number of points you want) between the x coordinates.

So now we now all the x coordinates, now we apply the for formula to each of those x's.

That will then generate the y's and in that way we have the coordinates for each point.

I propose an approach based on sf::st_line_sample(), which seems to be well suited for your use case.

It involves two steps:

  • creating a linestring object from your edge points
  • running the function sampling the line for n evenly distanced points; turning the result from multipoint to point object
library(sf)
library(dplyr)
library(leaflet)

# your data
edges <- data.frame(lat = c(38.77813955, 38.77202007),
                    lng= c(-1.833538909,-1.842456068)) 

# a line object
line <- rbind(edges[1, ],
              edges[2, ]) %>% 
  as.matrix() %>% 
  st_linestring()

# the action happens here! :)
points  <- st_line_sample(line, n = 15) %>%  # or whatever n you choose :)
  st_cast("POINT")

# everything to sf
edges <- st_as_sf(edges, coords = c('lat','lng'), crs = 4326)
points <- st_as_sf(points, crs = 4326) 

# present result
leaflet() %>% 
  addProviderTiles("OpenStreetMap.Mapnik") %>% 
  addCircleMarkers(data = edges, fillColor = "red", stroke = F, radius = 3) %>% 
  addCircleMarkers(data = points, fillColor = "green", stroke = F, radius = 3)

The only "issue" seems to be that the edge points are in a middle of nowhere, so the leaflet does not look pretty :slight_smile:

3 Likes

Thank you jlacko for the immense help!
I will take a look at the boundary coordinates

Or I have messed up the lat / lon coordinates; this happens to me all the time :slight_smile:

The code should not care - consider this updated example, sampling the distance from London to Paris.

library(sf)
library(dplyr)
library(leaflet)

# your data
edges <- data.frame(lng = c(2.3522219, -0.12775837),
                    lat= c(48.856614, 51.5073509)) 

# a line object
line <- rbind(edges[1, ],
              edges[2, ]) %>% 
  as.matrix() %>% 
  st_linestring()

# the action happens here! :)
points  <- st_line_sample(line, n = 15) %>%  # or whatever n you choose :)
  st_cast("POINT")

# everything to sf
edges <- st_as_sf(edges, coords = c('lng','lat'), crs = 4326)
points <- st_as_sf(points, crs = 4326) 

# present result
leaflet(data = edges) %>% 
  addProviderTiles("CartoDB.Positron") %>% 
  addCircleMarkers(data = edges, fillColor = "red", stroke = F, radius = 3) %>% 
  addCircleMarkers(data = points, fillColor = "green", stroke = F, radius = 3)

3 Likes

one other question!

what if I have several of these boundary coordinates (e.g. 20-pairs); do I have to repeat this code 20-times? Or there is a way of piping all the 20-pairs so that after running the code once, I can generate 20-sets of points with each corresponding to one of the boundary coordinates pairs.
Thanks in advance :slight_smile:

In principle there is no reason why a computer could not take some drudgery out of your work.

In practice a lot would depend on your iteration preferences (for cycle / apply / map) and desired input & output format of your data ...

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.