Created
July 1, 2025 18:13
-
-
Save idshklein/812a5fe794a26e1d0a160f143d14fa75 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,gtfstools,jsonlite,sf,plotly,scales) | |
| options(scipen = 1000) | |
| gtfs <- read_gtfs("https://openbus-stride-public.s3.eu-west-1.amazonaws.com/gtfs_archive/2025/07/01/israel-public-transportation.zip") | |
| stops_raw <- read_json("https://data.gov.il/api/3/action/datastore_search?resource_id=e873e6a2-66c1-494f-a677-f5e77348edb0&limit=100000") | |
| settles <- read_json("https://data.gov.il/api/3/action/datastore_search?resource_id=64edd0ee-3d5d-43ce-8562-c336c24dbc1f&limit=100000") | |
| stops <- stops_raw$result$records %>% map_df(~.x) | |
| points <- read_json("https://data.gov.il/api/3/action/datastore_search?resource_id=70ba1705-3b25-416f-939c-985999f87f35&limit=10000")$result$records %>% map_df(~.x) | |
| trips <- gtfs$trips %>% | |
| group_by(route_id) %>% | |
| slice(1) %>% | |
| pull(trip_id) | |
| mini_gtfs <- gtfs %>% | |
| filter_by_trip_id(trips) | |
| trip_tbl <- mini_gtfs$stop_times %>% | |
| left_join(mini_gtfs$stops %>% select(stop_id,stop_code)) %>% | |
| left_join(stops %>% mutate(stop_code = as.character(StationId)),by = c("stop_code")) %>% | |
| group_by(trip_id) | |
| settles_over_40k <- settles %>% | |
| filter(`סהכ`>40000) %>% | |
| left_join(points %>% mutate(`סמל ישוב` = as.integer(`סמל ישוב`)),join_by(סמל_ישוב== `סמל ישוב`)) | |
| lev <- settles_over_40k %>% arrange(-`סהכ`) %>% | |
| pull(`שם_ישוב`) %>% | |
| fct_inorder() %>% levels() | |
| connections <- trip_tbl %>% | |
| left_join(settles_over_40k,join_by(CityCode == `סמל_ישוב`)) %>% | |
| filter(any(!is.na(`סהכ`))) %>% | |
| select(stop_code ,stop_sequence ,city = `שם_ישוב`) %>% | |
| mutate(next_city = lead(city)) %>% | |
| ungroup() %>% | |
| filter(city != next_city, !is.na(city),!is.na(next_city)) %>% | |
| distinct(city,next_city) %>% | |
| mutate(con = 1) | |
| pos_connections <- expand.grid(city = settles_over_40k$שם_ישוב,next_city =settles_over_40k$שם_ישוב) | |
| dists <- settles_over_40k %>% | |
| select(`שם_ישוב`,`אורדינטה מזרחית`,`אורדינטה צפונית`) %>% | |
| column_to_rownames("שם_ישוב") %>% | |
| dist() %>% | |
| as.matrix() %>% | |
| as_tibble(rownames = NA) %>% | |
| rownames_to_column("city") %>% | |
| gather(next_city,dist,-city) | |
| df <- pos_connections %>% | |
| left_join(connections) %>% | |
| left_join(dists) %>% | |
| mutate(`קו ישיר` = coalesce(ifelse(con == 1, "כן","לא"),"לא")) %>% | |
| mutate_at(vars(city,next_city),~.x %>% factor(levels = lev)) | |
| clrs <- data.frame(n = 17:0,col = c(viridis::viridis(17),"#FF0000")) | |
| x_order <- df %>% | |
| filter(con == 1) %>% | |
| count(city,.drop=F) %>% | |
| left_join(clrs) | |
| y_order <- df %>% | |
| filter(con == 1) %>% | |
| count(next_city,.drop=F) %>% | |
| left_join(clrs) | |
| df %>% ggplot(aes(x= city,y=next_city,color = `קו ישיר`,size = dist/1000)) + geom_point() + | |
| scale_size_continuous(name = 'מרחק בק"מ' ,breaks = c(5,10,25,40,80,100,150)) + | |
| scale_x_discrete(breaks = x_order$city, labels = paste(x_order$city,x_order$n,sep = "-")) + | |
| scale_y_discrete(breaks = y_order$next_city, labels = paste(y_order$next_city,y_order$n,sep = "-")) + | |
| theme(axis.text.x = element_text(angle = 90,color = x_order$col)) + | |
| theme(axis.text.y = element_text(color = y_order$col)) + | |
| xlab("עיר מוצא")+ | |
| ylab("עיר יעד") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment