2016-04-26 24 views
1

我一直在繪製遠足小道的GPX數據。我可以下載並提取所有路線數據,但是當我將它們繪製爲多段線時,它只繪製一小段。我已經通過運行一個關於GPX文件的函數來確認該文件是完整的,創建經緯度數據幀並將它們繪製爲標記或圈標。這對於我正在使用的文件來說非常慢。R中的小冊子包不繪製GPX文件中的所有座標

的代碼如下:

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 

此代碼的工作,你會得到一張地圖,陰影和適當的標記是在正確的地方。

但是,只繪製了一條小線段。 GPX文件中有12行,我可以通過查看pct對象看到,但它似乎只是繪製一個。無論我下載哪個GPX文件,都是同樣的問題。

+0

是否有可能你有十二條線彼此跟隨?運行的情節(pct),你會看到一行接一行形成一條大行 – MLavoie

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

+0

謝謝@TimSalbim。我懷疑這是一個可能依賴於操作系統的單頁錯誤(我正在使用Windows)。基本上,我從這個[博客](http://spatialrecology.org/r/leafletmapping/)複製代碼。它顯示了成功的情節,我正在複製代碼和數據。 –

+0

我不確定這是否是操作系統問題。在Ubuntu 10.04上我有同樣的問題。我已經更新了我的答案,以便現在看起來應該與您提到的博客文章的版本類似。 – TimSalabim

+0

我在安裝時遇到一個錯誤,尋找一個名爲'stringi'的包,所以當我運行上面的代碼時,它會遍歷所有的數據,但無法進行繪圖。 –