Skip to content

Instantly share code, notes, and snippets.

@idshklein
idshklein / jw_vs_geo.R
Created March 18, 2024 18:37
calculation of geographical distances vs jako-winkler distance
pacman::p_load(tidyverse,jsonlite,stringdist)
res <- read_json("https://data.gov.il/api/3/action/datastore_search?resource_id=70ba1705-3b25-416f-939c-985999f87f35&limit=10000")
df <- res$result$records %>% map_df(~.x) %>%
filter(`סוג ישוב`!="מוקד תעסוקה") %>%
mutate(cntr = row_number())
dist_geo <- df %>%
select(`אורדינטה מזרחית`,`אורדינטה צפונית`) %>%
dist() %>% as.matrix()
res1 <- map(df$`שם ישוב`,~stringdist(.x,df$`שם ישוב`, method='jw'))
matrix_from_list <- do.call(cbind, res1)
@idshklein
idshklein / night_lines.R
Created October 4, 2023 12:19
get night lines to plot
pacman::p_load(tidyverse,sf,jsonlite,geojsonsf,glue,patchwork,ggspatial)
layla <- read_json("https://data.gov.il/api/3/action/datastore_search?resource_id=9cf3237f-80cd-4646-836f-f1370088430a&filters={%22RouteParticular%22:%22%D7%9C%D7%99%D7%9C%D7%94%22}&limit=1000")
kav_layla <- layla$result$records %>% map_df(~.x)
with_geoms <- kav_layla %>%
mutate(g = map2(RouteID,RouteDirection,possibly(function(x,y){glue("https://www.markav.net/shapes?q={x}-{y}-%23") %>% geojson_sf()},NA),.progress = TRUE))
aq <- with_geoms %>%
group_by(RouteID) %>%
filter(row_number()==1) %>%
filter(!is.na(g)) %>%
select(g) %>%
pacman::p_load(tidyverse,jsonlite,lubridate)
js <-
read_json("https://data.gov.il/api/3/action/datastore_search?resource_id=e265857e-9f53-419f-ad0b-860d2bf6fbb8&filters={%22StationId%22:[%2217112%22,%2217111%22,%2217113%22,%2217110%22]}&limit=100000")
js$result$records %>%
map_df(~.x) %>%
gather(day,count,starts_with("day_")) %>%
mutate(day = str_remove(day,"day_")) %>%
mutate(date = ymd(paste(year_key,month_key,day,sep = "/")),.keep = "unused") %>%
mutate(count = coalesce(as.numeric(count),0)) %>%
filter(!is.na(date)) %>%
pacman::p_load(tidyverse,sf,osmdata,tidygraph,ggraph,igraph,RCurl,mapview,stringdist)
admin_level_8 <- osmdata_sf ('[out:xml][timeout:25];(rel["admin_level"="8"](area:3601473946););out body;>;out skel qt;')
admin_level_8$osm_polygons
city_names <- admin_level_8$osm_multipolygons %>%
st_drop_geometry() %>%
as_tibble() %>%
distinct(name,osm_id) %>%
filter(!sapply(admin_level_8$osm_multipolygons$name, function(texte) {
grepl("[\\s\u0600-\u065F\u066A-\u06EF\u06FA-\u06FF]", texte)
}), str_length(osm_id) < 8)
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()
pacman::p_load(gtfstools,lubridate,tidyverse)
gtfs <- read_gtfs("C:/Users/idshk/Downloads/israel-public-transportation (5).zip")
stop_city <- read_delim("C:/Users/idshk/Downloads/e873e6a2-66c1-494f-a677-f5e77348edb0.csv",delim = "|")
dfq <- gtfs$stop_times %>%
left_join(gtfs$stops, by = "stop_id") %>%
mutate(stop_code = as.numeric(stop_code)) %>%
left_join(stop_city, by = c("stop_code"="StationId")) %>%
distinct(trip_id,stop_code,CityName) %>%
left_join(gtfs$trips, by = "trip_id") %>%
left_join(gtfs$routes, by = "route_id") %>%
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() %>%
pacman::p_load(tidyverse,jsonlite,lubridate,rayshader)
df_2019 <- read_json("https://data.gov.il/api/3/action/datastore_search?resource_id=5ed3ac9d-0709-46fb-aa42-1156682f3b85&limit=389&filters={%22OfficeLineId%22:%2224100%22}")
df_2020 <- read_json("https://data.gov.il/api/3/action/datastore_search?resource_id=6042d958-d86f-40bc-803b-2c5620d90394&limit=392&filters={%22OfficeLineId%22:%2224100%22}")
df_2021 <- read_json("https://data.gov.il/api/3/action/datastore_search?resource_id=09de377b-6462-48d5-8470-177c17016f6e&limit=405&filters={%22OfficeLineId%22:%2224100%22}")
df_2022 <- read_json("https://data.gov.il/api/3/action/datastore_search?resource_id=bfd22231-7a8b-4fc0-91bd-342397860ab5&limit=440&filters={%22OfficeLineId%22:%2224100%22}")
dfis <- function(df){df$result$records %>% map_df(~.x)}
dfis(df_2019) %>%
bind_rows(dfis(df_2020)) %>%
bind_rows(dfis(df_2021)) %>%
mutate_all(as.character) %>%
library(sf)
library(tidyverse)
library(sfnetworks)
library(tidygraph)
library(lwgeom)
library(mapview)
library(igraph)
library(qgisprocess)
library(nngeo)
library(dbscan)
pacman::p_load(tidyverse,jsonlite,httr)
buses <- read_json("https://data.gov.il/api/3/action/datastore_search?resource_id=91d298ed-a260-4f93-9d50-d5e3c5b82ce1&limit=20000")
buses1 <- buses$result$records %>% map_df(~.x)
rishui <- read_json("https://data.gov.il/api/3/action/datastore_search?resource_id=8fd3aa6c-a64f-4c91-818e-fd35017cd19b&&filters={%22rishui_date%22:%222022-08-09T00:00:00%22}&limit=20000")
rishui1 <- rishui$result$records%>% map_df(~.x)
GET("https://open-bus-stride-api.hasadna.org.il/siri_rides/list?get_count=false&scheduled_start_time_from=2022-08-09T00%3A00%3A00%2B02%3A00&scheduled_start_time_to=2022-08-09T23%3A59%3A59%2B02%3A00&order_by=siri_route_id%20asc%2Cvehicle_ref%20desc&limit=200000",write_disk(siri <- tempfile(fileext = ".json")))
siri1 <- read_json(siri)%>% map_df(~.x)
rishui1$trips_count %>% sum()
GET("https://open-bus-stride-api.hasadna.org.il/gtfs_rides/list?get_count=false&limit=100000",write_disk(rides1 <- tempfile(fileext = ".json")))
GET("https://open-bus-stride-api.hasadna.