如何使用闪亮的传单将一个位置的折线分别添加到其他位置? [英] How to add polylines from one location to others separately using leaflet in shiny?

查看:85
本文介绍了如何使用闪亮的传单将一个位置的折线分别添加到其他位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用传单中的addPolylines将某个特定位置的折线添加到 shiny R中的许多其他位置.但是,除了只能从一个位置链接到另一个位置之外,我只能将它们按顺序链接在一起.板球车轮图显示了我想要达到的最佳示例: .

I'm trying to add polylines from one specific location to many others in shiny R using addPolylines from leaflet. But instead of linking from one location to the others, I am only able to link them all together in a sequence. The best example of what I'm trying to achieve is seen here in the cricket wagon wheel diagram: .

observe({
  long.path <- c(-73.993438700, (locations$Long[1:9]))
  lat.path <- c(40.750545000, (locations$Lat[1:9]))
  proxy <- leafletProxy("map", data = locations)
  if (input$paths) {
     proxy %>% addPolylines(lng = long.path, lat = lat.path, weight = 3, fillOpacity = 0.5, 
                       layerId = ~locations, color = "red")
  }
})

它是一个反应式表达式,因为我希望通过复选框将其激活.

It is in a reactive expression as I want them to be activated by a checkbox.

真的很感谢您的帮助!

推荐答案

注意

我知道OP要求提供传单答复.但是这个问题激起了我寻找替代解决方案的兴趣,所以这里有两个

Note

I'm aware the OP asked for a leaflet answer. But this question piqued my interest to seek an alternative solution, so here are two

Mapdeck(我的软件包)在Mapbox地图上使用Deck.gl,因此您需要Mapbox API密钥才能使用它.但这确实可以让您绘制2.5d弧线

Mapdeck (my package) uses Deck.gl on a Mapbox map, so you need a Mapbox API key to use it. But it does let you plot 2.5d arcs

它适用于data.framesdata.tables(以及spsf)对象.

It works on data.frames and data.tables (as well as sp and sf) objects.

center <- c(144.983546, -37.820077)

df_hits$center_lon <- center[1]
df_hits$center_lat <- center[2]

df_hits$score <- sample(c(1:4,6), size = nrow(df_hits), replace = T)

library(mapdeck)

set_token("MAPBOX")

mapdeck(
  style = mapdeck_style("satellite")
) %>%
  add_arc(
    data = df_hits
    , origin = c("center_lon", "center_lat")
    , destination = c("lon", "lat")
    , stroke_from = "score"
    , stroke_to = "score"
    , stroke_width = "score"
    , palette = "magma"
  )

此示例使用googleway(也是我的软件包,该接口与Google Maps API相连),并且还适用于data.framesdata.tables(以及spsf)

This example uses googleway (also my package, which interfaces Google Maps API), and also works on data.frames and data.tables (as well as sp and sf)

诀窍在encodeCoordinates函数中,该函数将坐标(线)编码为 Google Polyline

The trick is in the encodeCoordinates function, which encodes coordinates (lines) into a Google Polyline

library(data.table)
library(googleway)
library(googlePolylines) ## gets installed when you install googleway

center <- c(144.983546, -37.820077)

setDT(df_hits)  ## data given at the end of the post

## generate a 'hit' id
df_hits[, hit := .I]

## generate a random score for each hit
df_hits[, score := sample(c(1:4,6), size = .N, replace = T)]

df_hits[
    , polyline := encodeCoordinates(c(lon, center[1]), c(lat, center[2]))
    , by = hit
]

set_key("GOOGLE_MAP_KEY") ## you need an API key to load the map

google_map() %>%
    add_polylines(
        data = df_hits
        , polyline = "polyline"
        , stroke_colour = "score"
        , stroke_weight = "score"
        , palette = viridisLite::plasma
    )

dplyr等效的是

df_hits %>%
    mutate(hit = row_number(), score = sample(c(1:4,6), size = n(), replace = T)) %>%
    group_by(hit, score) %>%
    mutate(
        polyline = encodeCoordinates(c(lon, center[1]), c(lat, center[2]))
    )


数据

df_hits <- structure(list(lon = c(144.982933659011, 144.983487725258, 
144.982804912978, 144.982869285995, 144.982686895782, 144.983239430839, 
144.983293075019, 144.983529109412, 144.98375441497, 144.984103102141, 
144.984376687461, 144.984183568412, 144.984344500953, 144.984097737723, 
144.984065551215, 144.984339136535, 144.984001178199, 144.984124559814, 
144.984280127936, 144.983990449363, 144.984253305846, 144.983030218536, 
144.982896108085, 144.984022635871, 144.983786601478, 144.983668584281, 
144.983673948699, 144.983577389175, 144.983416456634, 144.983577389175, 
144.983282346183, 144.983244795257, 144.98315360015, 144.982896108085, 
144.982686895782, 144.982617158347, 144.982761997634, 144.982740539962, 
144.982837099486, 144.984033364707, 144.984494704658, 144.984146017486, 
144.984205026084), lat = c(-37.8202049841516, -37.8201201023877, 
-37.8199253045246, -37.8197812267274, -37.8197727515541, -37.8195269711051, 
-37.8197600387923, -37.8193828925304, -37.8196964749506, -37.8196583366193, 
-37.8195820598976, -37.8198956414717, -37.8200651444706, -37.8203575362288, 
-37.820196509027, -37.8201032825917, -37.8200948074554, -37.8199253045246, 
-37.8197897018997, -37.8196668118057, -37.8200566693299, -37.8203829615443, 
-37.8204295746001, -37.8205355132537, -37.8194761198756, -37.8194040805737, 
-37.819569347103, -37.8197007125418, -37.8196752869912, -37.8195015454947, 
-37.8194930702893, -37.8196286734591, -37.8197558012046, -37.8198066522414, 
-37.8198151274109, -37.8199549675656, -37.8199253045246, -37.8196964749506, 
-37.8195862974953, -37.8205143255351, -37.8200270063298, -37.8197430884399, 
-37.8195354463066)), row.names = c(NA, -43L), class = "data.frame")

这篇关于如何使用闪亮的传单将一个位置的折线分别添加到其他位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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