0

Given the following reproducible example and referring back to these good threads:

https://stackoverflow.com/a/48758864/3585429

https://stackoverflow.com/a/34577359/3585429

library(leaflet)
library(geosphere)

mydf <- data.frame(lat_o = c(43.3, 43.1, 45.2), 
                   lon_o = c(10.1, 13.3, 11.2),
                   lat_d = c(45.4, 47.4, 46.9),
                   lon_d = c(12.5, 12.8, 11.5),
                   n=1:3,
                   route=LETTERS[1:3])


# origin matrix
Om <- as.matrix(mydf[,c("lon_o","lat_o")]) # pay attention to list lng before lat 
# destination matrix
Dm <- as.matrix(mydf[,c("lon_d","lat_d")]) # the same here

gcIntermediate(Om, Dm,  
               n=100, 
               addStartEnd=TRUE,
               sp=TRUE) %>% 
  leaflet() %>% 
  addTiles() %>% 
  addPolylines(label = mydf$n, 
               popup = mydf$route,
               weight = mydf$n*10
               )

I'm here asking for a more robust and safer method to "bring along" some attributes of the original data frame mydf to the new object wpts (a formal class SpatialLines - a list), to be directly queried inside the leaflet function addPolylines. As you can see I just workaround the problem by feeding into the arguments label, popup, and weight the attributes from the original data frame mydf. It seems to work somehow but I got a feeling it's not an entirely "bug-free" solution, especially in the case of a "real and big" data frame.

Do you have any suggestions for that?

maxbre
  • 161
  • 9
  • Any bugs show up yet? – Chris Nov 29 '22 at 18:14
  • not really up to now, my worries are just about keeping the correct order of the attributes passed by the vectors (is that approach robust enough?); secondly, I would like to pack all data attributes in a single object (maybe a SpatialLines list?) because of more easy portability for further calculations – maxbre Nov 29 '22 at 20:38
  • I was wondering about the indexing, seems your concern. Also wondering if perhaps is easier in `sf` rather than `sp`. Something that starts big, gets bigger as you increase waypoints. How big is big? – Chris Nov 29 '22 at 22:01
  • yes it's mainly about the indexing; in my case big is about 500 origin points connecting to 500 destination points; yes, I'm happy also with different solutions rather than the one I adopted so far, – maxbre Nov 30 '22 at 07:06

0 Answers0