Skip to content

Instantly share code, notes, and snippets.

@idshklein
Created January 31, 2023 09: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/a71baaf2118da8dae44c76d7cd2c87b1 to your computer and use it in GitHub Desktop.
Save idshklein/a71baaf2118da8dae44c76d7cd2c87b1 to your computer and use it in GitHub Desktop.
pacman::p_load(jsonlite,tidyverse,sf,lubridate,ggspatial)
df <- read_json("https://open-bus-stride-api.hasadna.org.il/siri_rides/list?gtfs_route__route_mkt=59015&gtfs_route__route_direction=2&limit=200000")
# df <- read_json("https://open-bus-stride-api.hasadna.org.il/siri_rides/list?gtfs_route__route_mkt=13490&gtfs_route__route_direction=3&limit=200000")
df1 <- df %>% map_df(~.x)
s_vec <- seq(1,nrow(df1),500)
e_vec <- c(seq(500,nrow(df1),500),nrow(df1))
starts <- map2_dfr(s_vec,e_vec,~read_json(paste0("https://open-bus-stride-api.hasadna.org.il/siri_vehicle_locations/list?get_count=false&limit=100000&siri_vehicle_location_ids=",df1$first_vehicle_location_id[.x:.y] %>% paste0(collapse = ","))))%>% map_df(~.x)
ends <- map2_dfr(s_vec,e_vec,~read_json(paste0("https://open-bus-stride-api.hasadna.org.il/siri_vehicle_locations/list?get_count=false&limit=100000&siri_vehicle_location_ids=",df1$last_vehicle_location_id[.x:.y] %>% paste0(collapse = ","))))%>% map_df(~.x)
start15 <- c(35.213151,31.746651) %>% st_point() %>% st_sfc(crs = 4326) %>% st_transform(2039)
end15 <- c(35.200707,31.783688) %>% st_point() %>% st_sfc(crs = 4326) %>% st_transform(2039)
# start276 <- c(34.808725,31.860352) %>% st_point() %>% st_sfc(crs = 4326) %>% st_transform(2039)
# end276 <- c(34.796688,32.08283) %>% st_point() %>% st_sfc(crs = 4326) %>% st_transform(2039)
# start490 <- c(35.214011,31.74531) %>% st_point() %>% st_sfc(crs = 4326) %>% st_transform(2039)
# end490 <- c(34.796688,32.08283) %>% st_point() %>% st_sfc(crs = 4326) %>% st_transform(2039)
startok <- starts %>% st_as_sf(coords = c("lon","lat"),crs = 4326) %>% st_transform(2039) %>% st_distance(start15) %>% as.numeric() %>% `<`(300)
endok <- ends %>% st_as_sf(coords = c("lon","lat"),crs = 4326) %>% st_transform(2039) %>% st_distance(end15)%>% as.numeric() %>% `<`(500)
df1[startok&endok,] %>%
mutate(gtfs_ride__start_time = as_datetime(gtfs_ride__start_time,tz = "Israel")) %>%
filter(hour(gtfs_ride__start_time) %in% 5:9,
wday(gtfs_ride__start_time) %in% 1:5,
duration_minutes < 100) %>%
ggplot(aes(x = gtfs_ride__start_time,y=duration_minutes)) +
geom_point(alpha = 0.1) +
geom_smooth()+
scale_x_datetime(date_minor_breaks = "1 month") +
facet_grid(~paste0(hour(gtfs_ride__start_time),":",ifelse(minute(gtfs_ride__start_time)>30,"30","00"))) +
theme( text = element_text(size = 20),
axis.text.x = element_text(angle = 90))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment