Skip to content

Instantly share code, notes, and snippets.

@idshklein
Created April 16, 2022 20:30
Show Gist options
  • Save idshklein/29feed7d66cfa04e35f8644785802dbf to your computer and use it in GitHub Desktop.
Save idshklein/29feed7d66cfa04e35f8644785802dbf to your computer and use it in GitHub Desktop.
library(tidyverse)
library(sf)
library(sfnetworks)
library(tidygraph)
library(dbscan)
stations <- st_read("METRO_STAT.shp")
lines <- st_read("METRO_LINE.shp")
plot(stations$geometry)
clus <- st_coordinates(stations) %>%
dbscan(eps = 300,minPts = 1) %>%
`$`(cluster)
lines %>%
st_cast("LINESTRING") %>%
as_sfnetwork(directed = F) %>%
convert(to_spatial_subdivision) %>%
st_network_blend(stations) %>%
{
clus <- (.) %>%
st_coordinates() %>%
dbscan(eps = 300,minPts = 1) %>%
`$`(cluster)
(.) %>% mutate(clus = clus)
} %>%
convert(to_spatial_contracted,clus,simplify = T) %>%
mutate(bet = centrality_betweenness(),
close = centrality_closeness(),
degree = centrality_degree(),
louvain = group_louvain(edge_length()^(-1))) %>%
select(-.tidygraph_node_index) %>%
{
eds <- (.) %E>% st_as_sf()
nds <- (.) %N>% st_as_sf() #%>%
# top_n(10,close)
ggplot() +
geom_sf(data = eds) +
geom_sf(data = nds,mapping = aes(color = as.factor(louvain)))+
scale_color_manual(values = randomcoloR::distinctColorPalette(nds$louvain %>% unique() %>% length()))
# mapview::mapview(eds$geometry) + mapview::mapview(nds,zcol = "bet")
# eds %>% st_drop_geometry()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment