[R-sig-Geo] Adding great circle routes as polylines in Leaflet
Ben Tupper
btupper @ending from bigelow@org
Mon Sep 3 16:57:23 CEST 2018
Hi,
Your example is so close to being reproducible, but not quite close enough. After some noodling around I figured out that you are using geosphere package in addition to leaflet, and that your tibble is assigned to the `ByRoute` variable. The following gets you closer - you'll want to resolve the warning messages ...
Warning messages:
1: In .pointsToMatrix(p2) :longitude > 180
2: In .pointsToMatrix(p2) :longitude > 180
3: In .pointsToMatrix(p2) : longitude > 180
... which you can fix in your tibble.
When adding layers to a map, you really want to create a map object, store it in a variable, say 'm', and ad each layer in an iteration enclosing the pipes (rather than within the pipes). I'm sure that there will be some wrapping issues if you copy-and-paste from this email. (Also, please put a bit more white space in your code. They are calorie-free and make life so much easier on us old fellows.)
Cheers,
Ben
library(leaflet)
library(geosphere)
ByRoute = structure(list(CommencingRegion = c("RedSea", "EastAfrica", "GulfofMexico",
"FarEast", "RedSea", "GulfofMexico"), LoadRegion = c("RedSea",
"RedSea", "GulfofMexico", "FarEast", "RedSea", "GulfofMexico"
), DischargeRegion = c("NorthWestAfrica", "WestMedditerranean",
"WestCoastLatinAmerica", "AustraliaNewZealand", "WestMedditerranean",
"WestCoastCentralAmerica"), Count = c(1L, 1L, 2L, 1L, 2L, 5L),
AvgTCE = c(38879.53, 31783.55, 28520.79, 26068.8, 26054.28,
25883.81), CLon = c(37.8274879335485, 47.0791103099334, -90.9633701509553,
146.2727573458, 37.8274879335485, -90.9633701509553), CLat =
c(21.4460561443517,
-12.9570828789565, 25.2035802054683, 47.6530892619773, 21.4460561443517,
25.2035802054683), LLon = c(37.8274879335485, 37.8274879335485,
-90.9633701509553, 146.2727573458, 37.8274879335485, -90.9633701509553
), LLat = c(21.4460561443517, 21.4460561443517, 25.2035802054683,
47.6530892619773, 21.4460561443517, 25.2035802054683), DLon =
c(-17.1597117430475,
7.03639948506481, -73.4238157230412, 151.051220297802, 7.03639948506481,
255.83509305644), DLat = c(24.2308740597312, 38.8907379374372,
-25.8934046406896, -25.1880219406131, 38.8907379374372, 21.8130318388702
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))
# create the map object
m <- leaflet() %>%
addTiles()
# iteratively add each polyline commence-to-load and load-to-destination
for(i in 1:6){
m <- m %>%
addPolylines(data=gcIntermediate(c(ByRoute$CLon[i], ByRoute$CLat[i]), c(ByRoute$LLon[i], ByRoute$CLat[i]),
n=100, addStartEnd = TRUE, sp = TRUE)) %>%
addPolylines(data=gcIntermediate(c(ByRoute$LLon[i], ByRoute$LLat[i]), c(ByRoute$DLon[i], ByRoute$DLat[i]),
n=100, addStartEnd = TRUE, sp = TRUE))
}
m
> On Sep 2, 2018, at 10:34 PM, Dhiraj Khanna <dhirajkhanna using gmail.com> wrote:
>
> I am trying to add great circle routes between various regions in R. Here’s
> the sample data:
>
> structure(list(CommencingRegion = c("RedSea", "EastAfrica", "GulfofMexico",
> "FarEast", "RedSea", "GulfofMexico"), LoadRegion = c("RedSea",
> "RedSea", "GulfofMexico", "FarEast", "RedSea", "GulfofMexico"
> ), DischargeRegion = c("NorthWestAfrica", "WestMedditerranean",
> "WestCoastLatinAmerica", "AustraliaNewZealand", "WestMedditerranean",
> "WestCoastCentralAmerica"), Count = c(1L, 1L, 2L, 1L, 2L, 5L),
> AvgTCE = c(38879.53, 31783.55, 28520.79, 26068.8, 26054.28,
> 25883.81), CLon = c(37.8274879335485, 47.0791103099334, -90.9633701509553,
> 146.2727573458, 37.8274879335485, -90.9633701509553), CLat =
> c(21.4460561443517,
> -12.9570828789565, 25.2035802054683, 47.6530892619773, 21.4460561443517,
> 25.2035802054683), LLon = c(37.8274879335485, 37.8274879335485,
> -90.9633701509553, 146.2727573458, 37.8274879335485, -90.9633701509553
> ), LLat = c(21.4460561443517, 21.4460561443517, 25.2035802054683,
> 47.6530892619773, 21.4460561443517, 25.2035802054683), DLon =
> c(-17.1597117430475,
> 7.03639948506481, -73.4238157230412, 151.051220297802, 7.03639948506481,
> 255.83509305644), DLat = c(24.2308740597312, 38.8907379374372,
> -25.8934046406896, -25.1880219406131, 38.8907379374372, 21.8130318388702
> )), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
> ))
>
> I would like to plot great circle routes between CommencingRegion to
> LoadingRegion and then from LoadingRegion to DischargeRegion for every row.
> Additionally, every row needs to be in a different color and the thickness
> needs to be proportional to AvgTCE. The last 6 variables in the data above
> are the coordinates in Lat Long for the commencing, loading and discharging
> regions respectively. I am quite clueless on how to go about achieving
> this. This is what I have tried and failed:
>
> leaflet() %>%
> addTiles() %>%
> for(i in 1:6){
> addPolylines(data=gcIntermediate(c(ByRoute$CLon[i],ByRoute$CLat[i]),c(ByRoute$LLon[i],ByRoute$CLat[i]),n=100,addStartEnd
> = T,sp=T))
> }
>
> Regards
> Dhiraj Khanna
> Mob:09873263331
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org
Ecological Forecasting: https://eco.bigelow.org/
More information about the R-sig-Geo
mailing list