Skip to content

Instantly share code, notes, and snippets.

@idshklein
Created November 10, 2020 21:15
Show Gist options
  • Save idshklein/a556a3ac014cb2da469bf4cc5e610625 to your computer and use it in GitHub Desktop.
Save idshklein/a556a3ac014cb2da469bf4cc5e610625 to your computer and use it in GitHub Desktop.
library(osmdata)
library(tidyverse)
library(tidygraph)
library(igraph)
library(sf)
library(lwgeom)
library(sfnetworks)
library(stplanr)
df <- getbb("Jerusalem",format_out = "polygon")
df[[3]][2] %>% st_polygon() %>% plot()
x <- opq(bbox = df[[3]][2]) %>%
add_osm_feature(key = 'highway') %>%
osmdata_sf()
types_of_highways <- x$osm_lines[,c("highway","highway_1")] %>%
st_drop_geometry() %>%
unlist() %>%
unique()
x$osm_polygons[,c("highway","highway_1")] %>%
st_drop_geometry() %>%
unlist() %>%
unique()
lines <- x$osm_lines
polygons_to_lines <- x$osm_polygons %>% st_cast("LINESTRING")
united <- bind_rows(lines,polygons_to_lines)
isin <- united %>% st_intersects(st_polygon(df[[3]][2]),sparse = F)
main_jlm <- united[isin,] %>%
filter(! highway %in% c("footway","steps","unclassified","path","construction","track","cycleway","proposed","pedestrian"))
nodes_to_split_by <- main_jlm %>%
st_cast("POINT") %>%
group_by(osm_id) %>%
filter(row_number() == 1| row_number() == max(row_number())) %>%
st_geometry() %>%
st_sf() %>%
distinct() %>%
st_transform(2039) %>%
st_snap(main_jlm %>% st_transform(2039),tolerance = 1e-9)
main_jlm_break <- st_split(main_jlm$geometry %>% st_transform(2039), nodes_to_split_by$geometry) %>%
st_collection_extract("LINESTRING") %>%
st_sf()
main_jlm_fin <- as_sfnetwork(main_jlm_break)
cl <- clusters(main_jlm_fin)
giant_jlm <- induced.subgraph(main_jlm_fin, which(cl$membership == which.max(cl$csize))) %>%
as_tbl_graph() %>%
as_sfnetwork() %>%
activate(edges) %>%
as_tibble() %>%
st_as_sf()
groups <- rnet_group(giant_jlm,igraph::cluster_fast_greedy)
giant_jlm$group <- groups
mapview::mapviewOptions(vector.palette = randomcoloR::randomColor(max(groups)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment