Network Anlysis: Shortest Route Anlysis in Taipei City, Taiwan

Author: YANG YU HSIANG 楊宇翔

rm(list = ls())
library(sfnetworks)
library(sf)
library(tmap)
library(igraph)
library(tidygraph)
library(tidyverse)
setwd("~/Desktop/臺大碩班/109-2 碩班/109-2 Network  Models/network model/0521 Spatial Networks_Linking Geospatial Data  ")
#讀取檔案
load("TPE_Road.Rdata")
#顯示資料格式
#資料格式為 "sf"         "data.frame"
class(road_sf)
## [1] "sf"         "data.frame"
#在viewer欄顯示地圖
tmap_mode("view")
## tmap mode set to interactive viewing
map = tm_shape(road_sf)+tm_lines()
map
#在viewer欄顯示地,與上式有類似效果
qtm(road_sf)
#依照class
tm_shape(road_sf) + tm_lines("CLASS",palette = "Reds")
# read shapefile
# MRT=st_read("D:/1092NT/VL0303V03.shp")
# qtm(MRT)

Introducing sfnetwork object: sf -> sfnetwork

road_sfnet = as_sfnetwork(road_sf,directed = FALSE) # 618 nodes and 932 edges
#顯示資料格式
#資料格式為 "sfnetwork" "tbl_graph" "igraph"  (有igraph了!可以分析了!)
class(road_sfnet)
## [1] "sfnetwork" "tbl_graph" "igraph"
plot(road_sfnet)

road_sfnet
## # A sfnetwork with 618 nodes and 932 edges
## #
## # CRS:  EPSG:3826 
## #
## # An undirected multigraph with 3 components with spatially explicit edges
## #
## # A tibble: 618 × 1
##             geometry
##          <POINT [m]>
## 1 (301553.8 2778143)
## 2   (302412 2777319)
## 3 (303246.7 2776491)
## 4 (303670.7 2776568)
## 5 (303254.7 2776263)
## 6 (309466.6 2774716)
## # ℹ 612 more rows
## #
## # A tibble: 932 × 6
##    from    to STREET_NAM INDEX_NAM CLASS                                geometry
##   <int> <int> <chr>      <chr>     <chr>                        <LINESTRING [m]>
## 1     1     2 文林北路   <NA>      7     (301553.8 2778143, 301987.8 2777730, 3…
## 2     3     4 中正路     <NA>      7        (303246.7 2776491, 303670.7 2776568)
## 3     4     5 福林路     台2甲     3        (303254.7 2776263, 303670.7 2776568)
## # ℹ 929 more rows

3. Betweenness

# calculating node's betweenness
road_sfnet = road_sfnet %>%
  activate("nodes") %>%
  mutate(bc = centrality_betweenness())
#只有路網的繪製
road_sfnet
## # A sfnetwork with 618 nodes and 932 edges
## #
## # CRS:  EPSG:3826 
## #
## # An undirected multigraph with 3 components with spatially explicit edges
## #
## # A tibble: 618 × 2
##             geometry     bc
##          <POINT [m]>  <dbl>
## 1 (301553.8 2778143) 23393.
## 2   (302412 2777319)   859.
## 3 (303246.7 2776491)  5681.
## 4 (303670.7 2776568)  6903.
## 5 (303254.7 2776263)  5133.
## 6 (309466.6 2774716)  2227.
## # ℹ 612 more rows
## #
## # A tibble: 932 × 6
##    from    to STREET_NAM INDEX_NAM CLASS                                geometry
##   <int> <int> <chr>      <chr>     <chr>                        <LINESTRING [m]>
## 1     1     2 文林北路   <NA>      7     (301553.8 2778143, 301987.8 2777730, 3…
## 2     3     4 中正路     <NA>      7        (303246.7 2776491, 303670.7 2776568)
## 3     4     5 福林路     台2甲     3        (303254.7 2776263, 303670.7 2776568)
## # ℹ 929 more rows
nodes_sf = st_as_sf(road_sfnet, "nodes")
#繪製betweenness的柱狀圖
hist(nodes_sf$bc)

#繪製到地圖上,palette表示色階
nodes_sf$bc = nodes_sf$bc / 10000
nodes_lyr = tm_shape(nodes_sf) + tm_dots("bc", palette = "GnBu",  title = "Betweenness") 

# calculating edge's betweenness
road_sfnet = road_sfnet %>%
  activate("edges") %>%
  mutate(weight = centrality_edge_betweenness())

edges_sf = st_as_sf(road_sfnet, "edges")
#繪製betweenness的柱狀圖
hist(edges_sf$weight)

#繪製到地圖上,palette表示色階
edges_sf$weight = edges_sf$weight / 10000
edges_lyr = tm_shape(edges_sf) + tm_lines("weight", palette = "OrRd") 
edges_lyr
#線+點的圖
edges_lyr + nodes_lyr
#nodes_sf$member= as.factor(comm_eb$membership)
#照分組結果繪製到地圖上
#tm_shape(nodes_sf) + tm_dots("member", palette = "Dark2",  title = "Community") 
#繪製成階層式分類圖
#comm_dend = as.dendrogram(comm_eb)
#plot(comm_dend)

5.Distance Matrix

# calculating edge's length
road_sfnet = road_sfnet %>%
  activate("edges") %>%
  mutate(length = edge_length())

# generating distance matrix  (距離矩陣)
dist_matrix =  distances(road_sfnet,
                weights = road_sfnet %>% activate(edges) %>% pull(length))
dim(dist_matrix)
## [1] 618 618
dist_matrix[1:5, 1:5]       
##          [,1]     [,2]      [,3]      [,4]      [,5]
## [1,]    0.000 1193.836 2609.7422 3040.6737 2841.0353
## [2,] 1193.836    0.000 1415.9063 1846.8378 1647.1993
## [3,] 2609.742 1415.906    0.0000  430.9315  231.2930
## [4,] 3040.674 1846.838  430.9315    0.0000  515.8335
## [5,] 2841.035 1647.199  231.2930  515.8335    0.0000

6. Generating the Path of the Shortest Distance(最短路徑分析)

road_sfnet = road_sfnet %>%
  activate("nodes") %>%
  mutate(nodeID = c(1:618))
#出發點設為id為200的節點
from_node = road_sfnet %>%
  activate(nodes) %>%
  filter(nodeID == 200) %>%
  pull(nodeID)
#終點設為id為500的節點
to_node = road_sfnet %>%
  activate(nodes) %>%
  filter(nodeID == 500) %>%
  pull(nodeID)
#最短路徑分析
mypath = shortest_paths(
  graph = road_sfnet,
  from = from_node,
  to = to_node,
  output = 'both',
  weights = road_sfnet %>% activate(edges) %>% pull(length)
)
#最短路徑經過的節點
mypath$vpath # vertex of the path
## [[1]]
## + 12/618 vertices, from 4e00bcb:
##  [1] 200 514  78 478 103 104 115 114 113 498 499 500
#最短路徑經過的edge
mypath$epath # edges of the path
## [[1]]
## + 11/932 edges from 4e00bcb:
##  [1] 200--514  78--514  78--478 103--478 103--104 104--115 114--115 113--114
##  [9] 113--498 498--499 499--500
mypath_graph = road_sfnet %>%
  subgraph.edges(eids = mypath$epath %>% unlist()) %>%
  as_tbl_graph()
#顯示資料類型: "tbl_graph" "igraph" 
class(mypath_graph)
## [1] "tbl_graph" "igraph"
epath_sf = mypath_graph %>% activate(edges) %>% as_tibble() %>% st_as_sf()
#紅線為最短路徑
mypath_lyr = tm_shape(epath_sf) + tm_lines(col="red", lwd=2)

#顯示出發點
from_node_sf = road_sfnet %>% activate(nodes) %>% filter(nodeID == 200) %>% as_tibble() %>% st_as_sf()
from_node_lyr = tm_shape(from_node_sf) + tm_dots(col="green")
#顯示終點
to_node_sf = road_sfnet %>% activate(nodes) %>% filter(nodeID == 500) %>% as_tibble() %>% st_as_sf()
to_node_lyr = tm_shape(to_node_sf) + tm_dots(col="blue")
map + mypath_lyr+ from_node_lyr + to_node_lyr