r - 使用传单根据海拔高度对 gpx 轨迹进行颜色着色,并在 R 中进行绘制

标签 r shiny leaflet plotly polyline

我正在尝试在 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 中集成传单插件。

enter image description here

** 读取数据 **

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

** 与 Plotly **

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

enter image description here

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

enter image description here

** 使用传单 **

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

enter image description here

addPolylines 调用中使用 color = ~ele 不起作用(绘图消失),但图 block 仍然存在。我也尝试过使用 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>)

最佳答案

这是我的尝试......

灵感来自: 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 )

enter image description here

关于r - 使用传单根据海拔高度对 gpx 轨迹进行颜色着色,并在 R 中进行绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62650706/

相关文章:

r - navebarMenu 始终突出显示

regex - 如何分隔CSV文件中特定列的变量并写入R中的CSV文件?

javascript - 使用JSON每隔1秒更新一次传单标记位置

r - 如何查找一个向量中的元素在另一向量 R 中的索引

java - 在 R : NoSuchMethodError (Java): org. apache.xmlbeans.XmlCursor.close()V 中使用 XLConnect 时出错

css - 在 R 中的 Shiny 应用程序中更改整个字体的最简单方法?

r - 如何在Shiny R App中为selectInput从server.R到ui.R获取选项向量

删除 Shiny 数据表中的标题行

javascript - 传单:按缩放级别获取缩放的边界框

javascript - 使用 turfjs 在多边形和多重多边形之间相交