[英]How to create an interactive plot of GTFS data in R using Leaflet?
我想創建一個顯示城市公共交通線路的互動地圖。 我正在嘗試使用R中的Leaflet(但我願意接受替代方案,建議嗎?)
數據:傳輸系統的數據采用GTFS格式,以文本文件(.txt)組織,我將其作為數據框讀入R.
問題:我找不到如何指示每條Poly線的id(變量shape_id),因此該圖實際上將遵循每條傳輸線的路線。 相反,它是以隨機順序連接點。
這是我嘗試過的,到目前為止沒有成功:
# Download GTFS data of the Victoria Regional Transit System
tf <- tempfile()
td <- tempdir()
ftp.path <- "http://www.gtfs-data-exchange.com/agency/bc-transit-victoria-regional-transit-system/latest.zip"
download.file(ftp.path, tf)
# Read text file to a data frame
zipfile <- unzip( tf , exdir = td )
shape <- read.csv(zipfile[9])
# Create base map
basemap <- leaflet() %>% addTiles()
# Add transit layer
basemap %>% addPolylines(lng=shape$shape_pt_lon, lat=shape$shape_pt_lat,
fill = FALSE,
layerId =shape$shape_id)
我很樂意對此發表意見。
*我知道可以將這些數據導入GIS軟件(例如QGIS)以創建shapefile,然后使用readOGR將shapefile讀入R. Robin Lovelace已經展示了如何做到這一點 。 但是,我正在尋找純R解決方案。 ;)
PS。 Kyle Walker使用Leaflet為R中的交互式地圖寫了一篇很棒的介紹 。 不幸的是,他沒有在他的教程中介紹多邊形線。
您的問題不是方法而是數據:注意您下載8 MB並且您嘗試通過閃亮加載到Leaflet的行文件是5 MB。 作為一般原則,在擴展之前,您應該首先嘗試使用微小數據集的新方法。 這就是我在下面做的,以診斷問題並解決它。
pkgs <- c("leaflet", "shiny" # packages we'll use
, "maps" # to test antiquated 'maps' data type
, "maptools" # to convert 'maps' data type to Spatial* data
)
lapply(pkgs, "library", character.only = TRUE)
class(shape)
## [1] "data.frame"
head(shape)
## shape_id shape_pt_lon shape_pt_lat shape_pt_sequence
## 1 1-39-220 -123.4194 48.49065 0
## 2 1-39-220 -123.4195 48.49083 1
## 3 1-39-220 -123.4195 48.49088 2
## 4 1-39-220 -123.4196 48.49123 3
## 5 1-39-220 -123.4197 48.49160 4
## 6 1-39-220 -123.4196 48.49209 5
object.size(shape) / 1000000 # 5 MB!!!
## 5.538232 bytes
summary(shape$shape_id)
shape$shape_id <- as.character(shape$shape_id)
ids <- unique(shape$shape_id)
shape_orig <- shape
shape <- shape[shape$shape_id == ids[1],] # subset the data
這就像地圖中的data.frame
對象嗎?
state.map <- map("state", plot = FALSE, fill = TRUE)
str(state.map)
## List of 4
## $ x : num [1:15599] -87.5 -87.5 -87.5 -87.5 -87.6 ...
## $ y : num [1:15599] 30.4 30.4 30.4 30.3 30.3 ...
## $ range: num [1:4] -124.7 -67 25.1 49.4
## $ names: chr [1:63] "alabama" "arizona" "arkansas" "california" ...
## - attr(*, "class")= chr "map"
是的 ,它是類似的,所以我們可以使用map2Spatial*
來轉換它:
shape_map <- list(x = shape$shape_pt_lon, y = shape$shape_pt_lat)
shape_lines <- map2SpatialLines(shape_map, IDs = ids[1])
plot(shape_lines) # success - this plots a single line!
for
循環可以很好地完成這項工作。 注意我們只使用前10行。 使用2:length(ids)
所有行的2:length(ids)
:
for(i in 2:10){
shape <- shape_orig[shape_orig$shape_id == ids[i],]
shape_map <- list(x = shape$shape_pt_lon, y = shape$shape_pt_lat)
shape_temp <- map2SpatialLines(shape_map, IDs = ids[i])
shape_lines <- spRbind(shape_lines, shape_temp)
}
使用SpatialLines
對象使代碼更短 - 在這種情況下,這將繪制前10行:
leaflet() %>%
addTiles() %>%
addPolylines(data = shape_lines)
在將數據轉換為用於繪圖的Spatial *數據類型之前,您需要使用數據並對其進行操作,並使用正確的ID。 maptools::map2Spatial*
, unique()
和一個聰明的for
循環可以解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.