Skip to content

Instantly share code, notes, and snippets.

@idshklein
Created January 30, 2022 21:00
Show Gist options
  • Save idshklein/3d32e4f26424a59076395faff3c0110a to your computer and use it in GitHub Desktop.
Save idshklein/3d32e4f26424a59076395faff3c0110a to your computer and use it in GitHub Desktop.
library(tidyverse)
library(sf)
library(sfnetworks)
library(igraph)
library(tidygraph)
library(ggspatial)
eds <- st_read("D:/Downloads/israel-ISR_gpkg/tel_aviv-4309.gpkg",layer = "edges") %>% st_transform(2039) %>% select(geom)
net <- as_sfnetwork(eds) %>%
convert(to_spatial_smooth)
cmps <- net %>%
components() %>%
`$`(membership)
fnet <- net %>%
mutate(cmps =cmps) %>%
filter(cmps == 1) %>%
convert(to_spatial_smooth)
matr <- fnet %>%
st_network_cost()
gc()
n <- nrow(matr)
hunds <- numeric(n)
maxs <- numeric(n)
for(i in 1:n){
sorted = sort(matr[i,])
hunds[i] <- sorted[100]
maxs[i] <- sorted[n]
print(i)
if(i %% 1000==0) {gc()}
}
# sorted <- t(apply(matr,1,sort))
min100 <- min(hunds)
max100 <- max(hunds)
minmax <- min(maxs)
maxmax <- max(maxs)
mat100 <- (hunds - min100)/(max100 - min100)
matmax <- (maxs - minmax)/(maxmax-minmax)
hist(matmax)
med_max <- matmax %>% median()
hist(mat100)
med_hunds <- mat100 %>% median()
classes <- data.frame(x = mat100,y = matmax) %>%
mutate(close = matmax < med_max,
urban = mat100 < med_hunds,
clas = ifelse(close & urban, "core",
ifelse(close & !urban, "suburb",
ifelse(!close & urban,"exburb","far"))))
classes %>% ggplot(aes(x,y)) + geom_point()
p1 <- fnet %>%
mutate(clas = classes$clas) %>%
st_as_sf() %>%
ggplot(aes( color = clas)) +
annotation_map_tile(zoom = 10) +
geom_sf()
p1
fnet %>%
mutate(clas = classes$clas) %>%
st_as_sf() %>%
st_write("tlv_met_class.gpkg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment