使用gcIntermediate在R传单/闪亮中绘制跨越日期线的最短飞行路径 [英] Mapping the shortest flight path across the date line in R leaflet/Shiny, using gcIntermediate

查看:23
本文介绍了使用gcIntermediate在R传单/闪亮中绘制跨越日期线的最短飞行路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 R-Leaflet 创建澳大利亚机场及其国际目的地的地图.

I'm creating a map of Australian airports and their international destinations using R-Leaflet.

这是我的示例数据:

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)
           )

我认为使用 gcIntermediate 使用弯曲的飞行路径会很酷,所以我创建了一个 SpatialLines 对象:

I thought it would be cool to use curved flight paths using gcIntermediate, so I created a SpatialLines object:

library(rgeos)
library(geosphere)

p1<-as.matrix(df[,c(3,4)])

p2<-as.matrix(df[,c(5,6)])

df2 <-gcIntermediate(p1, p2, breakAtDateLine=F, 
                    n=100, 
                    addStartEnd=TRUE,
                    sp=T) 

然后我使用传单和 Shiny 绘制它:

And then I plotted it using leaflet and Shiny:

server <-function(input, output) {

airportmap<- leaflet() %>% addTiles() %>% 
    addCircleMarkers(df, lng = df$Australian_lon, lat = df$Australian_lat, 
    radius = 2, label = paste(df$Australian_Airport, "Airport"))%>% 
    addPolylines(data = df2, weight = 1)

output$mymap <- renderLeaflet({airportmap}) # render the base map
  }


ui<-  navbarPage("International flight path statistics - top routes",
      tabPanel("Interactive map",

      leafletOutput('mymap',  width="100%", height=900)

         )
         )

# Run the application 
shinyApp(ui = ui, server = server)

看起来像这样:

因此,如果它们越过日期线,则路径不正确.将 breakAtDateLine 更改为 FALSE 并不能修复它(该行消失但路径仍然损坏).在这个阶段,我怀疑我可能需要使用不同的地图系统或其他东西,但如果有人有一些建议,我将非常感激.

So the paths are incorrect if they cross the date line. Changing breakAtDateLine to FALSE doesn't fix it (the line disappears but the path is still broken). At this stage, I suspect I may need to use a different mapping system or something but I'd be very grateful if anyone has some advice.

提前致谢.

推荐答案

如果你对另一个地图库感兴趣,那么 googleway 使用谷歌地图,根据我的经验,它更擅长处理交叉线日期线.

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.

注意事项

  1. 要使用 Google 地图,您需要 API 密钥
  2. 目前只支持 sf 对象,不支持 sp
  3. 这也适用于闪亮;我只是给你看这里的基本地图
  4. 我创作了 googleway
  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)

这篇关于使用gcIntermediate在R传单/闪亮中绘制跨越日期线的最短飞行路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆