I have a data.frame
:
library(tidyverse)
library(sf)
library(geosphere)
library(osmdata)
library(rgeos)
library(readxl)
library(writexl)
d1 <- data_frame(
title = c("base1", "base2", "base3", "base4"),
lat = c(34.7453751328344, 34.721682790624, 34.7605669404291, 34.7542187),
long = c(33.3424447992104, 33.3282082548538, 33.4176870768428, 33.4173241))
d1 = data.frame(d1)
colnames(d1)= c("title","lat","long")
# convert to sf object
d1_sf <- d1 %>% st_as_sf(coords = c('long','lat')) %>% st_set_crs(4326)
osm_box <- getbb (place_name = "Cyprus") %>% opq () %>% add_osm_feature("natural", "coastline") %>% osmdata_sf()
dist <- geosphere::dist2Line(p = st_coordinates(d1_sf), line = st_coordinates(osm_box$osm_lines)[,1:2])
df <- cbind( d1 %>% rename(y=lat,x=long),dist) %>% mutate(miles=distance/1609)
I attempted to use the method found in this answer, but I am getting incorrect results.
Can someone assist?