trafficonese / leaflet.extras

Extra functionality for leaflet R package.
https://trafficonese.github.io/leaflet.extras/
GNU General Public License v3.0
216 stars 74 forks source link

wish: Leaflet.Geodesic plugin #18

Closed mdsumner closed 7 years ago

mdsumner commented 7 years ago

This plugin provides curvature based on the Geodesic between points, including wrap at the longitude boundary:

https://github.com/henrythasler/Leaflet.Geodesic

bhaskarvk commented 7 years ago

Checkout feature/geodesic branch. It's kind of wonky at best. I also had to patch the original plugin to make it work. And it looks like it won't work with custom projections.

Imgur

Do you still want it ?

mdsumner commented 7 years ago

Definitely if it doesn't cause problems, but happy to take your branch as an example just to learn too. Thanks! It's beautiful :)

bhaskarvk commented 7 years ago

I don't think it will cause any problems, I'll wait for a bit for the plugin author to henrythasler/Leaflet.Geodesic#22, but if he/she doesn't respond, I'll go ahead with my patched ver.

bhaskarvk commented 7 years ago

Also for now the restriction is to use either plain old data frame, or a single Polyline, as Leaflet doesn't support MultiPolylines. i.e. you can plot only one line (with as many stops as you want) per invocation. So for multipolylines, you'll have to break them up and call the function per line.

mdsumner commented 7 years ago

Gosh that's awesome! Thanks :)

Try this

 colshorten <- function(x) lapply(strsplit(x, ""), function(a) paste(a[c(1, 2, 4, 6)], collapse = ""))

library(trip)  ## for the walrus tracks
## coerce to multi-lines (necessarily dropping all the per-fix attributes)
## and transform to longlat
ll <- spTransform(as(walrus818, "SpatialLinesDataFrame"), "+init=epsg:4326")
addGeodesicPolylines( addPolylines(af, data = spTransform(ll, "+init=epsg:4326"), color = colshorten(viridis::viridis(nrow(ll))), group = "tripID"), c(-172.5, -161.8), c(66.8, 70.3), color = "black")

You can perhaps see that the shorter path along the great circle matters there if you are hopeful, but I'll find some better examples, it's much clearer for shearwaters flying between Antarctica and Alaska. :)

I have some leaflet q's which probably don't belong here:

mdsumner commented 7 years ago

Here's another example with a different kind of seal:

http://rpubs.com/cyclemumner/ellie_gc

Which, huh, only shows that the geodesic didn't survive the trip to Rpubs:

image

bhaskarvk commented 7 years ago

For your first code sample,

ll <- spTransform(as(walrus818, "SpatialLinesDataFrame"), "+init=epsg:4326")
addGeodesicPolylines( addPolylines(af, data = spTransform(ll, "+init=epsg:4326"), color = colshorten(viridis::viridis(nrow(ll))), group = "tripID"), c(-172.5, -161.8), c(66.8, 70.3), color = "black")

I'm a bit confused. you are calling addPolyLines inside addGeodesicPolylines. That doesn't seem right, perhaps you meant to call them right after each other like you did on the Rpubs page. Also why double spTransform to 4236. You can do it just once. Also the colshorten can be done more intuitively, but the current ver. works too. But you do need to convert your coords to 4326, that's a leaflet JS lib limitation.

I too noticed that geodesic lines didn't make it to Rpubs, and I'm not sure why, that'll take some debugging.

mdsumner commented 7 years ago

ur, sorry was just going too fast - I was working in a console when I wrote it so avoided the pipe operator, but it's just nesting function calls rather than piping them, which is the same right?

should be:

colshorten <- function(x) lapply(strsplit(x, ""), function(a) paste(a[c(1, 2, 4, 6)], collapse = ""))

library(trip)  ## for the walrus tracks
## coerce to multi-lines (necessarily dropping all the per-fix attributes)
## and transform to longlat
ll <- spTransform(as(walrus818, "SpatialLinesDataFrame"), "+init=epsg:4326")
af <- addTiles(leaflet())
addGeodesicPolylines( addPolylines(af, data =ll, color = colshorten(viridis::viridis(nrow(ll))), group = "tripID"), c(-172.5, -161.8), c(66.8, 70.3), color = "black")

Alternatively:

colshorten <- function(x) lapply(strsplit(x, ""), function(a) paste(a[c(1, 2, 4, 6)], collapse = ""))

library(trip)

af <- addTiles(leaflet())
ll <- spTransform(as(walrus818, "SpatialLinesDataFrame"), "+init=epsg:4326")
af %>% addPolylines(data = ll, color = colshorten(viridis::viridis(nrow(ll))), group = "tripID") %>%
  addGeodesicPolylines( lng = c(-172.5, -161.8), lat =  c(66.8, 70.3), color = "black")

Again, sorry for being sloppy, got excited and moved too quickly

bhaskarvk commented 7 years ago

Lastly the Rpubs problem is because the plugin uses some funky unicode characters, which are butchered by the rpubs uploader. I'll fix that my substituting the unicodes to ASCII.

bhaskarvk commented 7 years ago

I yes my bad then. Pipes have spoiled me, I can't even recognize non-pipe code any more.

bhaskarvk commented 7 years ago

It does support custom projections. I think that makes it super useful coz you see those as straight lines.

Imgur

bhaskarvk commented 7 years ago

I'll double check a few things and merge it in master soon.

bhaskarvk commented 7 years ago

This is causing the mess when uploaded to Rpubs rstudio/rmarkdown#881

mdsumner commented 7 years ago

This is great news!

bhaskarvk commented 7 years ago

Feature implemented in 6a1bd70. Demo rpubs.com/bhaskarvk/geodesic