Created
April 25, 2023 20:03
-
-
Save idshklein/c988cff6743a4f91092181a829f55a39 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pacman::p_load(tidyverse,sf,osmdata,tidygraph,ggraph,igraph,RCurl,mapview,stringdist) | |
admin_level_8 <- osmdata_sf ('[out:xml][timeout:25];(rel["admin_level"="8"](area:3601473946););out body;>;out skel qt;') | |
admin_level_8$osm_polygons | |
city_names <- admin_level_8$osm_multipolygons %>% | |
st_drop_geometry() %>% | |
as_tibble() %>% | |
distinct(name,osm_id) %>% | |
filter(!sapply(admin_level_8$osm_multipolygons$name, function(texte) { | |
grepl("[\\s\u0600-\u065F\u066A-\u06EF\u06FA-\u06FF]", texte) | |
}), str_length(osm_id) < 8) | |
city_names1 <- city_names %>% | |
mutate(geom_st = map2(name,osm_id,~{ | |
print(.x) | |
osmdata_sf(paste0('[out:xml][timeout:25];(way["highway"](area:360',.y,'););out body;>;out skel qt;')) | |
})) | |
city_names2 <- city_names1 %>% | |
mutate(res = map(geom_st,~{ | |
.x$osm_lines %>% | |
st_drop_geometry() %>% | |
as_tibble() %>% | |
distinct(name) | |
})) | |
city_names3 <- city_names2 %>% | |
select(-geom_st,-osm_id) %>% | |
rename(city_name = name) %>% | |
unnest(res) %>% | |
filter(!is.na(name)) | |
# city_names4 <- map_df(city_names$name,~as.data.frame(.x) %>% | |
# mutate(q = map(.x,~{city_names3}))) | |
city_names4 <- data.frame(from = rep(city_names3$city_name,150),to = rep(city_names$name,29359),street = rep(city_names3$name,150)) | |
city_names4 %>% | |
head() %>% View() | |
city_names5 <- city_names4 %>% | |
filter(from != to) %>% | |
mutate(dist = stringdist(to,street)) | |
city_names5 %>% | |
filter(dist < 1) %>% | |
as_tbl_graph(directed = T) %>% | |
ggraph() + | |
geom_node_label(mapping = aes(label = name),position = "jitter") + | |
geom_edge_diagonal(arrow = arrow(length = unit(4, 'mm'))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment