2016-11-09 44 views
0

我最近發現我的興趣在R.映射巴士路線與宣傳單中的R

與葉包密謀當我試圖這樣做紐約公交線路shape file of NYC bus routes (zip file)的這種形狀文件,部分航線不顯示在地圖上。我可以告訴他們錯過了,因爲我覆蓋了公交車站的數據,有些沒有與路線對齊。

當我在形狀文件中讀取時,我注意到創建的空間線數據框具有嵌套列表,我認爲傳單未映射。

我需要做什麼,讓傳單讀取這些缺失路線的座標?下面是我用缺少的路線產生地圖代碼:

bus <- readOGR(dsn = path.expand("bus_route_shapefile"), layer = "bus_route_shapefile") 
bus.pj <- spTransform(bus, CRS("+proj=longlat +datum=WGS84")) 
bus.st <- readOGR(dsn = path.expand("bus_stop_shapefile"), layer = "bus_stop_shapefile") 
bus.st.pj <- spTransform(bus.st, CRS("+proj=longlat +datum=WGS84")) 

bus_map <- leaflet() %>% 
    setView(lng = -73.932667, lat = 40.717266, zoom = 11) %>% 
    addPolylines(data = bus.pj, color = "black", opacity = 1) %>% 
    addCircles([email protected],~stop_lon, ~stop_lat, color = "red") %>% 
    addTiles() 
bus_map 

回答

1

這將是更容易幫助你,如果你提供的不僅bus_routes也bus_stop (zip file)。您可以通過將bus.pj轉換爲新的SpatialLinesxxx obj來解決此問題,其中每個類Lines只有一個類Line。下面的代碼使得SLDF因爲未知而沒有[email protected]$trip_heads

library(dplyr); library(sp); library(leaflet) 

    ## resolve [email protected] into list(Line.objs) (Don't worry about warnings) 
Line_list <- lapply([email protected], getLinesLinesSlot) %>% unlist() 

     ## If you want just Lines infromation, finish with this line. 
    SL <- sapply(1:length(Line_list), function(x) Lines(Line_list[[x]], ID = x)) %>% 
     SpatialLines() 

    ## make new ids (originalID_nth) 
ori_id <- getSLLinesIDSlots(bus.pj)       # get original ids 
LinLS <- sapply([email protected], function(x) length([email protected])) # how many Line.obj does each Lines.obj has 
new_id <- sapply(1:length(LinLS), function(x) paste0(x, "_", seq.int(LinLS[[x]]))) %>% 
    unlist() 

    ## make a new data.frame (only route_id) 
df <- data.frame(route_id = rep([email protected]$route_id, times = LinLS)) 
rownames(df) <- new_id 

    ## integrate Line.objs, ids and a data.frame into SpatialLinesDataFrame.obj 
SLDF <- mapply(function(x, y) Lines(x, ID = y), x = Line_list, y = new_id) %>% 
    SpatialLines() %>% SpatialLinesDataFrame(data = df) 


leaflet() %>% 
    setView(lng = -73.932667, lat = 40.717266, zoom = 11) %>% 
    addPolylines(data = SLDF, color = "black", opacity = 1, weight = 1) %>% 
    addCircles([email protected],~stop_lon, ~stop_lat, color = "red", weight = 0.3) 

enter image description here