2

I have the following data:

lat <- c(-1.404361, -1.403221,-1.387369)
long <- c(35.35373, 35.35047,35.35171 )
distance <- c(220,150,300)
bearing <- c(67,27,159)

df <- data.frame(lat,long,distance,bearing)

I transform and project into an sf object as follows:

df %>% st_as_sf(coords=c("long", "lat")) %>% st_set_crs(4326) -> df

My question is, how do I calculate the destination point for each long/lat origin coordinate, given the distance and bearing? I have used the destPoint in the Geosphere package, but I cannot find the equivalent in sf.

1

Answer as follows:

library(geosphere)
df_sp <- as_Spatial(df)
destPoint(df_sp, df_sp$distance, df_sp$bearing) %>%
   as.data.frame() %>%
   st_as_sf(coords = c('lon', 'lat')) -> df_sf
  • 1
    You should add library(geosphere) (lower case, not Geosphere as in the question). – Spacedman Sep 20 at 21:15
  • 1
    You can also bypass the as_Spatial conversion with st_coordinates: destPoint(st_coordinates(df), df$distance, df$bearing) - destPoint - the first thing destPoint does to an sp object is create a 2-column matrix. – Spacedman Sep 20 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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