Objective
- Geocode the address & transform to {sf} data structure
- Plot withleaflet
Loading bibrary
pacman::p_load(
rio,
here,
ggplot2,
sf,
dplyr,
tidygeocoder,
mapboxapi,
hereR,
osrm,
leaflet
)
2 location points
# two semi-random Glasgow address
adresy <- c("34 Hillhead Street, Hillhead, Glasgow, G12 8PZ",
"5 Lymburn Street, Yorkhill, Glasgow, G3 8PD")
# geocode the two address & transform to {sf} data structure
data <- tidygeocoder::geo(adresy, method = "osm") %>%
st_as_sf(coords = c("long", "lat"), crs = 4326) # point
## Passing 2 addresses to the Nominatim single address geocoder
## Query completed in: 2 seconds
# Using OSRM engine
osroute <- osrm::osrmRoute(loc = data, returnclass = "sf") # line
## Warning: "returnclass" is deprecated.
# Plot
leaflet(data = data) %>%
addProviderTiles("CartoDB.Positron") %>%
addMarkers(label = ~address) %>%
addPolylines(data = osroute,
label = 'OSRM engine',
color = 'red')
A network
# Export data
data1 <- read.csv("data/OD_Table.csv")
data1 <- data1[1000:1200, ]
# transform to {sf} data structure
data <- st_as_sf(data1,
coords = c("longitude", "latitude"),
crs = 4326)
class(data)
## [1] "sf" "data.frame"
osroute <- osrm::osrmRoute(loc = data)
# route mapping
leaflet(data = data) %>%
addProviderTiles("CartoDB.Position") %>%
addPolylines(data = osroute,
label = "OSRM engine",
color = "red")
# point mapping
leaflet() %>% addTiles() %>% addMarkers(data = data1)
## Assuming "longitude" and "latitude" are longitude and latitude, respectively