R中的Leaflet包未绘制GPX文件中的所有坐标

时间:2016-04-26 22:21:10

标签: r leaflet gpx

我一直在绘制远足径的GPX数据。我可以下载并提取所有路径数据,但是当我将它们绘制为折线时,它只绘制一个小段。我已经通过运行打破GPX文件的函数确认文件已完成,创建了lat和lon的数据框,并将它们绘制为标记或圆圈标记。这对我正在使用的文件来说非常慢。

代码如下:

library(rgdal)
library(maps)
library(htmltools)
library(devtools)
library(leaflet)
library(sp)
library(htmlwidgets)
library(plotKML)
library(maptools)
library(XML)

url <- "http://hiking.waymarkedtrails.org/en/routebrowser/1225378/gpx"
download.file(url, destfile = "pct.gpx", method = "wininet")

pct <- readOGR("pct.gpx", layer = "tracks")

# Import list with shapefiles of the three states the PCT is crossing
mapStates <- map("state", fill = TRUE,
                 plot = FALSE,
                 region = c('california', 'oregon', 'washington:main'))

your.map <- leaflet(pct) %>%

 # Add layer
  addTiles(urlTemplate = "http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png") %>%
  addPolylines(color="red", popup="PCT")  %>%
  addMarkers(-116.4697, 32.60758, popup = "Campo") %>%
  addMarkers(-120.7816, 49.06465, popup = "Manning Park, Canada") %>%
  addPolygons(data=mapStates, fillColor = heat.colors(3, alpha = NULL), stroke = FALSE) %>%

  # Add legend
  addLegend(position = 'topright', colors = "red", labels = "PCT", opacity = 0.4,
            title = 'Legend')
your.map

此代码有效,您可以获得地图,正确着色并且标记位于正确的位置。

但是,只绘制了一个小线段。我可以通过查看pct对象看到GPX文件中有12行,但它似乎只是绘制一个。无论我下载哪个GPX文件,都是同样的问题。

1 个答案:

答案 0 :(得分:1)

这可能与传单中的错误有关。 github上已经存在问题,请参阅here

mapview 中,我们修复此问题并正确绘制MultiLines。查看问题对话here

作为可重复的示例(取自 mapview 问题):

library(trajectories)
data(storms)
x = as(storms, "SpatialLinesDataFrame")
plot(x)
library(mapview)
mapview(x)

leaflet() %>% addTiles() %>% addPolylines(data = x)

或者您的示例,只需

your.map <- mapview(pct, map.types = "CartoDB.Positron")@map %>% 
  addMarkers(-116.4697, 32.60758, popup = "Campo") %>%
  addMarkers(-120.7816, 49.06465, popup = "Manning Park, Canada") %>%
  addPolygons(data=mapStates, fillColor = heat.colors(3, alpha = NULL), stroke = FALSE) %>%

  # Add legend
  addLegend(position = 'topright', colors = "red", labels = "PCT", opacity = 0.4,
            title = 'Legend')

your.map

注意:目前仅在 mapview 的开发版本中修复,该版本可与devtools::install_github("environmentalinformatics-marburg/mapview", ref = "develop")一起安装