One way I can decide is to get a road shape file in Ghana and perform geospatial operations to find the nearest road. From there, you can use the Google API to get the distance to the track.
The steps in this decision are
- Download road shape file
- Find a straight line between our origin and destination
- , .
- Google Directions
, ,
library(sf)
library(googleway)
:
googleway - , API Google
set_key("my_map_key"), api = "map")
set_key("my_other_api_key")
ghanaRoads <- sf::st_read("~/Downloads/gha_roads_dcw/GHA_rds_1m_dcw.shp")
df <- data.frame(lat = 7.9254948, lon = -0.6283887)
dest <- data.frame(lat = 5.591622, lon = -0.187677)
google_map() %>%
add_markers(data = df) %>%
add_markers(data = dest) %>%
add_polylines(ghanaRoads)

, , . , , , .
sf_origin <- sf::st_sf(geometry = sf::st_sfc(sf::st_point(x = c(-0.6283887, 7.9254948))))
m <- matrix(c(-0.6283887, 7.9254948, -0.187677, 5.591622), ncol = 2, byrow = T)
sf_line <- sf::st_sf(geometry = sf::st_sfc(sf::st_linestring(x = m)))
sf::st_crs(sf_line) <- sf::st_crs(ghanaRoads)
sf::st_crs(sf_origin) <- sf::st_crs(ghanaRoads)
sf_intersections <- sf::st_intersection(ghanaRoads, sf_line)
google_map() %>%
add_markers(data = df) %>%
add_markers(data = dest) %>%
add_polylines(data = ghanaRoads) %>%
add_markers(data = sf_intersections)

.
sf - sf::st_distance, lwgeom, , ,
data.table, , . , .
library(data.table)
coords <- matrix(unlist(sf_intersections$geometry), ncol = 2, byrow = T)
dt.haversine <- function(lat_from, lon_from, lat_to, lon_to, r = 6378137){
radians <- pi/180
lat_to <- lat_to * radians
lat_from <- lat_from * radians
lon_to <- lon_to * radians
lon_from <- lon_from * radians
dLat <- (lat_to - lat_from)
dLon <- (lon_to - lon_from)
a <- (sin(dLat/2)^2) + (cos(lat_from) * cos(lat_to)) * (sin(dLon/2)^2)
return(2 * atan2(sqrt(a), sqrt(1 - a)) * r)
}
dt <- as.data.table(coords)
dt[, `:=`(origin_lon = df$lon, origin_lat = df$lat)]
dt[, distance := dt.haversine(origin_lat, origin_lon, V1, V2)]
sf_nearest <- dt[order(-distance)][1, .(lon = V1, lat = V2)]
sf_nearest <- sf::st_point(c(sf_nearest$lon, sf_nearest$lat))
sf_nearest <- sf::st_sf(geometry = sf::st_sfc(sf_nearest))
sf_nearest$colour <- "green"
google_map() %>%
add_markers(data = df) %>%
add_markers(data = dest) %>%
add_markers(data = sf_nearest, colour = "colour")

orig <- sf_nearest$geometry[[1]]
orig <- as.numeric(orig)
df_orig <- data.frame(lat = orig[2], lon = orig[1])
google_map() %>%
add_markers(df_orig)
res <- google_directions(origin = df_orig,
destination = dest)
direction_legs(res)$distance
df_route <- data.frame(polyline = direction_polyline(res))
google_map() %>%
add_markers(data = df_orig) %>%
add_markers(data = dest) %>%
add_polylines(data = df_route, polyline = "polyline")

dt[order(-distance)] ,
dt[order(-distance)][1, distance]
# [1] 1329904
. , 4 /, .
, - .
sf_buffer <- sf::st_buffer(sf_origin, dist = 0.5)
sf::st_crs(sf_buffer) <- sf::st_crs(ghanaRoads)
google_map() %>%
add_polylines(ghanaRoads) %>%
add_polygons(sf_buffer)

,
sf_intersection <- sf::st_intersection(sf_buffer, ghanaRoads)
google_map() %>%
add_markers(data = df) %>%
add_polylines(sf_intersection)

sf_intersection .