Skip to content

Instantly share code, notes, and snippets.

@idshklein
Created January 15, 2023 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idshklein/3f4a5aacaa64e7a0c03005a802e37058 to your computer and use it in GitHub Desktop.
Save idshklein/3f4a5aacaa64e7a0c03005a802e37058 to your computer and use it in GitHub Desktop.
pacman::p_load(tidyverse,sf,sfnetworks,nngeo,tidygraph)
options(scipen = 10000)
outer = matrix(c(0,0,1000,0,1000,1000,0,1000,0,0),ncol=2, byrow=TRUE)
make_grid <- function(street_length){st_polygon(list(outer)) %>%
st_make_grid(street_length) %>%
st_segments() }
x <- 200
map_df(c(2,4,5,8,10,20),~make_grid(1000/.x) %>%
st_segmentize(20) %>%
st_segments() %>%
as_sfnetwork(directed = F) %E>%
distinct() %>%
st_network_cost() %>%
as.vector() %>%
data.frame() %>%
rename(x = 1) %>%
mutate(street_length = 1000/.x)) %>%
ggplot(aes(x =x,fill = factor(street_length))) +
# geom_density(alpha=.5)
geom_histogram()
map_df(c(2,4,5,8,10,20),~make_grid(1000/.x) %>%
st_segmentize(20) %>%
st_segments() %>%
as_sfnetwork(directed = F) %E>%
distinct() %N>%
as_tibble() %>%
mutate(street_length = 1000/.x)) %>%
ggplot()+
geom_sf() +
facet_wrap(~street_length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment