Skip to content

Instantly share code, notes, and snippets.

@stmarcin
Created October 22, 2021 19:51
Show Gist options
  • Save stmarcin/91fdfcf99e70387f25c92275cd8626f9 to your computer and use it in GitHub Desktop.
Save stmarcin/91fdfcf99e70387f25c92275cd8626f9 to your computer and use it in GitHub Desktop.
quick and dirty map to test new {r5r} function transit_network_to_sf()
# settings & libraries
library(r5r)
library(sf)
library(dplyr)
library(ggplot2)
# used but not loaded
# library(eurostat)
# library(here)
# library(rJava)
# library(osmextract)
options(java.parameters = "-Xmx2G")
# get data: OSM
osmextract::oe_download(
file_url = osmextract::oe_match("Warszawa")[[1]],
download_directory = here::here("data"))
# requires subfolders: "data" and "img"
dir.create(here::here("data"))
dir.create(here::here("img"))
# get data: GTFS
# # # # # # # # # # # # # # #
# N O T E #
# # # # # # # # # # # # # # #
# apparently this solution doesn't work. It works however, if GTFS is downloaded manually.
download.file(url = "https://transitfeeds.com/p/ztm-warszawa/720/latest/download",
destfile = here::here("data", "gtfs.zip"))
# get Warsaw's boundaries
Warszawa <- eurostat::get_eurostat_geospatial(nuts_level = 3) %>%
filter(CNTR_CODE == "PL") %>%
filter(NUTS_NAME == "Miasto Warszawa")
# setup r5
r5r_core <- setup_r5(data_path = here::here("data"), verbose = FALSE)
# get sf from gtfs
gtfs_to_map <- transit_network_to_sf(r5r_core)
# quick and dirty map
ggplot() +
geom_sf(data = Warszawa,
fill = "grey") +
geom_sf(data = gtfs_to_map[[1]],
size = 0.5) +
geom_sf(data = gtfs_to_map[[2]]) +
labs(title = "Quick & dirt map of Warsaw's stops and lines (ZTM)",
caption = "Source: GTFS by ZTM (https://transitfeeds.com/p/ztm-warszawa/720/latest)") +
theme_void() +
theme(
plot.title = element_text(hjust = 0.5)
)
# save output
ggsave(here::here("img", "map.png"))
# clean session:
stop_r5(r5r_core)
rJava::.jgc(R.gc = TRUE)
# in case of a need to rebuild network, remove also this file
unlink(here::here("data", "network.dat") )
@rafapereirabr
Copy link

Hi Marcin. This is a really nice way to integrate osmextract with r5r. If the idea to simply to make maps of GTFS networks, though, one simpler/faster alternative is to use the gtfs2gps package.

library(gtfs2gps)

# read gtfs
gtfs <- gtfs2gps::read_gtfs( here::here("data", 'gtfs.zip') )

# convert to simple features
shapes_sf <- gtfs2gps::gtfs_shapes_as_sf(gtfs)
stops_sf <- gtfs2gps::gtfs_stops_as_sf(gtfs)

@stmarcin
Copy link
Author

Thanks for the feedback :-)
I know about {gtfs2gps}, I wanted to test how r5r exports gtfs to sf. {gtfs2gps} also have a lot of functionality I am missing in {r5r} (starting with browsing gtfs feed) - and it's fine, both packages are very complementary and I like that very much :-)
And I agree - using {5r5} simply to get routes/stops from gtfs is a bit overkill. However I think it can be useful if you a map is part of the analysis, together with travel time matrix / accessibility, as gtfs is already read and stored in the memory (right?) so yes - it's great, you have created such functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment