Mapping the shortest flight path across the date line in R leaflet/Shiny, using gcIntermediate [duplicate]

妖精的绣舞 提交于 2019-11-29 07:56:08

If you are interested in another mapping library, then googleway uses Google Maps, which in my experience is better at handling lines that cross the date line.

Notes

  1. To use Google Maps you need an API key
  2. Currently only sf objects are supported, not sp
  3. This will also work in shiny; I'm just showing you the basic map here
  4. I authored googleway

library(sf)
library(googleway)

## convert the sp object to sf
sf <- sf::st_as_sf(df2)

set_key("your_api_key")

google_map() %>%
    add_polylines(data = sf)

Overview

I set the max bounds and minimum zoom level to only display the world map once. It looks okay in the RStudio viewer but fails when I display it in browser. I'm hoping this helps spark other answers.

Code

# load necessary packages
library( leaflet )
library( geosphere )

# create data
df <- 
  data.frame("Australian_Airport" = "Brisbane", 
             "International" =  c("Auckland", "Bandar Seri Begawan","Bangkok","Christchurch","Denpasar","Dunedin","Hamilton","Hong Kong","Honiara","Kuala Lumpur"),
             "Australian_lon" = c(153.117, 153.117,153.117,153.117,153.117,153.117, 153.117, 153.117, 153.117, 153.117),
             "Australian_lat" = c(-27.3842,-27.3842,-27.3842,-27.3842,-27.3842,-27.3842, -27.3842, -27.3842, -27.3842, -27.3842),
             "International_lon" = c(174.7633, 114.9398, 100.5018, 172.6362, 115.2126,-82.77177, -84.56134, 114.10950, 159.97290, 101.68685),
             "International_lat" = c(-36.848460, 4.903052, 13.756331, -43.532054,-8.670458,28.019740, 39.399501, 22.396428, -9.445638,  3.139003)
             , stringsAsFactors = FALSE
  )

# create curved lines
curved.lines <-
  gcIntermediate(
    p1 = as.matrix( x = df[ , 3:4 ] )
    , p2 = as.matrix( x = df[ , 5:6 ] )
    , breakAtDateLine = TRUE
    , n = 1000
    , addStartEnd = TRUE
    , sp = TRUE
  ) 

# create leaflet
airport <-
  leaflet( options = leafletOptions( minZoom = 1) ) %>%
  setMaxBounds( lng1 = -180
                , lat1 = -89.98155760646617
                , lng2 = 180
                , lat2 = 89.99346179538875 ) %>%
  addTiles() %>% 
  addCircleMarkers( data = df
                    , lng = ~Australian_lon
                    , lat = ~Australian_lat
                    , radius = 2
                    , color = "red"
                    , label = paste( ~Australian_Airport
                                     , "Airport" )
  ) %>% 
  addCircleMarkers( data = df
                    , lng = ~International_lon
                    , lat = ~International_lat
                    , radius = 2
                    , color = "blue"
                    , label = paste( ~International
                                     , "Airport" )
  ) %>% 
  addPolylines( data = curved.lines
                , weight = 1 
                )
# display map
airport

# end of script #
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!