Find the shortest path between points on a map made by the leaflet package

Dear friends,

I would like to know how do I get the shortest path between two points on a map made by the leaflet package. I have the map done (figure below), but I would like to calculate the shortest path, as well as show this route on the map. It is possible?? The executable code is below.:

library(leaflet)
library(geosphere)

#database
df<-structure(list(Properties = c(1,2,3,4,5,6,7,8,9,10), Latitude = c(-23.2, -23.6, -23.9, -23.9, -23.6,  -23.5, -23.9, -23.9, -23.6, -23.9), 
Longitude = c(-49.6, -49.6, -49.6, -49.4, -49.3, -49.9, -49.3, -49.2, -49.6, -49.9)), class="data.frame",row.names = c(NA, -10L))

#clusters
d<-as.dist(distm(df[,2:1]))
fit.average<-hclust(d,method="average") 
clusters<-cutree(fit.average, 4) 
df$cluster<-clusters

#Map using leaflet

example=df
getColor <- function(example) {
  sapply(example$cluster, function(cluster) {
    if(cluster == 1) {
      "blue"
    } else if(cluster == 2) {
      "green"
    } else if(cluster == 3) {
      "orange"
    } else {
      "red"
    } })
}

icons <- awesomeIcons(
  icon = 'ios-close',
  iconColor = 'black',
  library = 'ion',
  markerColor = getColor(example)
)

m=leaflet(example) %>% addTiles() %>%
  addAwesomeMarkers(lat=~Latitude, lng = ~Longitude, icon=icons, label=~as.character(cluster))
m

Thank you friends!

Example that could be


Source:https://gist.github.com/ramnathv/9998388#file-code-r


Source: https://www.liedman.net/leaflet-routing-machine/tutorials/interaction/

The shortest path is a straight line.

But I would like to see the route. Does the leaflet not work with routing to show these paths on the map?

Do you mean by road? I don't think leaflet knows where the roads are, it just serves the map as a background for you to draw your own data on.

See here for example.
Or here

Thanks for reply. I thought of something like the figures I just inserted above. Shows the route between two points, however this route is the shortest path.

That's impressive. Maybe you could try getting their examples running yourself.

There are two steps here for what you need to do:

  1. Calculate the shortest path with a street network (harder than straight-line distance, which is just sf::st_distance)
  2. Visualize it interactively (you already know how to do this!)

You can't do both with the same workflow or with the same tool. For the first, I'd check out the {osrm} package and calculate the shortest path between points: https://rgeomatic.hypotheses.org/category/osrm

Once you have that spatial line object, you can then visualize it with {leaflet}. Basically, you need to do data analysis/prep (routing) and then data viz (mapping). Just like tabular data (i.e. dplyr --> ggplot2), it's a two-step process (osrm --> leaflet).

2 Likes

Thank you very much Angela, I will do it the way you suggested. And I'll tell you if it worked or not.

1 Like

Angela, please clarify a doubt: I am running some tests with the osrm package. I am using a point shapefile and calculate the distance from one point to another. The route is based on the OpenStreetmap. It worked for me. But I have another shapefile file with more roads, which in OpenStreetmap does not have, is it possible to include these new roads in some way in the code? for OpenStreetMap to include those extra roads.

Thank you so much!

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