【问题标题】:Color gpx track bsaed on elevation using leaflet or plotly in R shiny使用传单或在 R 中绘制基于高程的彩色 gpx 轨迹闪亮
【发布时间】:2020-06-30 05:16:06
【问题描述】:

我正在尝试在 Shiny 应用程序中绘制 gpx 轨迹,以便 gpx 绘图颜色基于任何给定点的海拔(高度)。我的示例 gpx 文件包含 4,000 到 10,000 个点(坐标对)之间的任何位置。海拔可以显示出最小的变化(海平面附近的平坦轨道)或高达 1600m 的变化(远足径)。

** 所需的输出 **

来自https://iosphere.github.io/Leaflet.hotline/demo/ 的示例,使用 Leaflet 中的插件。更多详情:https://github.com/iosphere/Leaflet.hotline/ 没有可用的 R 代码,我不知道如何在 R 中集成传单的插件。

** 读取数据**

dat <- plotKML::readGPX(my_gpx_file) # sample file link below
track <- as.data.table(dat$tracks[[1]][[1]])
track[, ele := as.numeric(ele)]

** 与情节 **

scattermapbox 选项仅绘制可以基于列着色但输出标记不通过线连接的标记(预期)

plot_mapbox(data = track, mode = 'scattermapbox') %>%
  add_markers(x = ~lon, y = ~lat, color = ~ele, hoverinfo = 'none') %>%
  layout(
    mapbox = list(
      zoom = 10,
      center = list(lon = track[, mean(lon)], lat = track[, mean(lat)])
    )
  )

切换到add_trace(..., mode = 'lines+markers') 保留上面屏幕截图中的标记颜色,但使用统一的标准蓝色为线条着色。如果设置为add_trace(..., mode = 'lines'),绘图将消失(即不渲染):

** 使用传单 **

使用addPolyLines 进行基本通话:

leaflet(track) %>%
  fitBounds(lng1 = min(track$lon), lat1 = min(track$lat),
            lng2 = max(track$lon), lat2 = max(track$lat)) %>%
  clearShapes() %>%
  clearControls() %>%
  addProviderTiles(
    provider = providers$Thunderforest,
    options = list(variant = 'transport',
                   apikey = my_api_key)
  ) %>%
  addPolylines(lng = ~lon,
               lat = ~lat)

addPolylines 调用中使用color = ~ele 不起作用(绘图消失)但瓷砖仍然存在。我也尝试过使用colorNumericcolorRamp,结果相同。调用已修改为addPolylines(..., color = ~colorFunc(ele)),其中colorFunc 可能是:

colorFunc <- colorNumeric(
  palette = c('#000000', '#B20000') ,
  domain = track$ele
)

colorFunc <- colorRamp(
  colors = c('#FDFDFD', '#B20000'), 
  bias = 5, 
  interpolate = 'linear'
  )

colorRamp 显示不同高度值的输出变化,而colorNumeric 始终默认为高颜色 (#B20000)。 colorRampPalette 为某些人工作,但没有改变我的输出。

我在 SO 和其他论坛上看到了几个答案,但没有一个适合我。

  1. Leaflet colours for polylines
  2. How to plot polylines in multiple colors in R?
  3. Adding color to polylines in leaflet in R
  4. https://gis.stackexchange.com/questions/90193/color-code-a-leaflet-polyline-based-on-additional-values-e-g-altitude-speed

** 数据**

下面的示例数据(仅 50 点)。您可以在此处下载示例文件:https://ridewithgps.com/routes/28431977

structure(list(lat = c(45.54214, 45.54205, 45.54183, 45.54148, 
45.54103, 45.54081, 45.54041, 45.54036, 45.5403499, 45.53998, 
45.53985, 45.53954, 45.5394, 45.53918, 45.53898, 45.53893, 45.53893, 
45.53882, 45.53882, 45.53884, 45.53888, 45.5390299, 45.53926, 
45.53937, 45.53976, 45.54013, 45.54032, 45.54045, 45.54048, 45.54055, 
45.5406199, 45.54071, 45.5409099, 45.54103, 45.54131, 45.54162, 
45.54197, 45.54247, 45.5427, 45.5428, 45.5441, 45.5443799, 45.54557, 
45.54627, 45.54639, 45.54656, 45.54667, 45.54685, 45.54706, 45.54714
), lon = c(-73.55111, -73.55079, -73.55008, -73.5489, -73.54741, 
-73.54671, -73.54546, -73.54528, -73.54524, -73.54394, -73.54346, 
-73.54244, -73.54192, -73.54115, -73.54048, -73.54029, -73.54029, 
-73.54025, -73.54025, -73.54021, -73.54013, -73.53994, -73.53964, 
-73.53954, -73.53937, -73.53905, -73.5389, -73.53877, -73.53871, 
-73.53827, -73.53814, -73.53812, -73.53824, -73.53825, -73.5381, 
-73.5378, -73.53758, -73.53713, -73.53706, -73.53701, -73.53625, 
-73.536, -73.53537, -73.53502, -73.53498, -73.5349899, -73.53504, 
-73.53528, -73.53529, -73.53527), ele = c(23.7, 23.3, 22.8, 21.9, 
21.6, 21.8, 21.9, 22.1, 22.1, 21.2, 20, 17.7, 16.6, 15.3, 14.8, 
14.8, 14.8, 14.7, 14.7, 14.7, 14.7, 14.8, 14.8, 14.8, 14.3, 13.6, 
13.4, 13.2, 13.1, 12.6, 12.5, 12.4, 12.6, 12.6, 12.4, 12.2, 12.4, 
12.3, 12.2, 12.2, 12.3, 12.4, 12.7, 12.9, 12.9, 12.9, 12.9, 13.2, 
13.2, 13.2)), row.names = c(NA, -50L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x7f91fb8096e0>)

【问题讨论】:

    标签: r shiny leaflet plotly polyline


    【解决方案1】:

    这是我要做的事情......

    灵感来自: https://gist.github.com/helgasoft/799fac40f6fa2561c61cd1404521573a

    library(plotKML)  #for reading gpx
    library(dplyr)    #for setting ele to numeric
    library(leaflet)
    library(htmltools)
    library(htmlwidgets)
    
    #load gpx file, convert data to lat-lon-ele data.frame
    mydata <- plotKML::readGPX( "./temp/19_aout_2018_-_au_complet.gpx" )$tracks[[1]][[1]] %>%
      dplyr::mutate( ele = as.numeric( ele ) )
    #download the needed js-file to C:/temp (create c:/Temp first if necessairy)
    download.file("https://raw.githubusercontent.com/iosphere/Leaflet.hotline/master/dist/leaflet.hotline.js", 
                  'C:/Temp/leaflet.hotline.js', mode="wb")
    #load the plugin
    hotlinePlugin <- htmltools::htmlDependency(
      name = 'Leaflet.hotline',
      version = "0.4.0",
      src = c(file = normalizePath('C:/Temp')),
      script = "leaflet.hotline.js"
      )
    #register plugin
    registerPlugin <- function( map, plugin ) {
      map$dependencies <- c( map$dependencies, list( plugin ) )
      map
    }
    #draw leaflet
    leaflet() %>% addTiles() %>%
      fitBounds( min(mydata$lon), min(mydata$lat), max(mydata$lon), max(mydata$lat) ) %>%
      registerPlugin(hotlinePlugin) %>%
      onRender("function(el, x, data) {
        data = HTMLWidgets.dataframeToD3(data);
        data = data.map(function(val) { return [val.lat, val.lon, val.ele]; });
        L.hotline(data, {min: 15, max: 70}).addTo(this);
      }", data = mydata )
    

    【讨论】:

    • 谢谢,非常感谢!您能否就其工作原理添加一些解释 - 我还有一些参数可以作为绘图的基础(最小值、调色板等)。另外,这对于闪亮的应用程序如何工作?
    • 禁止添加的参数在您在问题中提到的 github 页面上...,您在代码中输入它们,用逗号分隔,就像我已经输入的方式 minmax: {min: 15, max: 70}.. 对于一个 hiny 应用程序,我想它的工作原理是一样的,但你必须将你的 leaflet.hotline.js 放在你的应用程序内部的数据文件夹中,并链接到那里......
    • 进一步解释......不是真的......我不是传单专家,并根据一些谷歌搜索和我自己有限的传单知识构建了这个示例。
    • 对于闪亮的部分:在我的答案的链接中,实际上是一个闪亮的应用程序......所以如果你按照这个例子,它应该可以正常工作......
    • 它适用于独立地图,但不适用于 Shiny server 代码中的 leafletProxy..
    猜你喜欢
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 2018-05-22
    • 2021-07-13
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    • 2013-04-09
    相关资源
    最近更新 更多