Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce package file size #218

Merged
merged 8 commits into from
Oct 18, 2024
Merged

Reduce package file size #218

merged 8 commits into from
Oct 18, 2024

Conversation

polettif
Copy link
Contributor

@polettif polettif commented Oct 17, 2024

This PR reduces the package size below the 5 MB CRAN policy threshold. To achieve this, I removed some routes from the NYC sample feed and simplified the shapes. The routes are not explicitly used in the vignettes or examples. Given the fact that the feed is already six years old at this point I don't think there is an issue with transforming it to a pure "example feed".

Notable changes:

  • Renamed google_transit_nyc_subway.zip to nyc_subway.zip throughout the package
  • Removed the following routes from nyc_subway.zip: A, SI, H, FS, J, Z and F.
  • Minor changes in the vignette output, mainly in the servicepattern vignette but the gist of them hasn't changed.
  • Removed the leaflet map in the introduction vignette that showed the bounding boxes for feeds. It increases the package size by almost 1 MB and it's not a core application for tidytransit. The code is still there.
  • Reduced the figure size of some plots

The code to create the reduced feed is pasted below, for posterity.

library(dplyr)
gtfs_nyc = read_gtfs("google_transit_nyc_subway.zip")
gtfs = gtfs_nyc

routes_to_remove = c("A", "SI", "H", "FS", "J", "Z", "F")

# Remove constant columns ####
gtfs[!names(gtfs) %in% c(".", "agency", "transfers", "routes")] <- gtfs[!names(gtfs) %in% c(".", "agency", "transfers", "routes")] |>
  lapply(janitor::remove_constant, quiet = FALSE)

# Minimize shapes ####
shapes_sf = gtfs$shapes |> 
  shapes_as_sf() |> 
  rmapshaper::ms_simplify(keep = 0.1)
gtfs$shapes <- dplyr::as_tibble(sf_lines_to_df(shapes_sf))

# Remove select routes and trips ####
gtfs$routes |> select(route_id, route_desc) |> print(n = 30)
trips_to_keep = gtfs$trips |> 
  filter(!route_id %in% routes_to_remove) |> 
  distinct(trip_id)

# Remove unused service_ids ####
gtfs$trips <- gtfs$trips |> semi_join(trips_to_keep, "trip_id")
gtfs$stop_times <- gtfs$stop_times |> semi_join(gtfs$trips, "trip_id")

gtfs$calendar <- gtfs$calendar |> filter(service_id %in% gtfs$trips$service_id)
gtfs$calendar_dates <- gtfs$calendar_dates |> filter(service_id %in% gtfs$trips$service_id)

# Remove stops ####
stops_to_keep = gtfs$stops |> semi_join(gtfs$stop_times, "stop_id")
stops_to_keep <- c(stops_to_keep$stop_id, stops_to_keep$parent_station)

stops_to_keep <- c(stops_to_keep,
                   gtfs$transfers |> 
                     filter(from_stop_id %in% stops_to_keep | to_stop_id %in% stops_to_keep) |>
                     select(from_stop_id, to_stop_id) |> 
                     unlist(use.names = F) |> unique())

gtfs$stops <- gtfs$stops |> filter(stop_id %in% stops_to_keep)
gtfs$transfers <- gtfs$transfers |> filter(from_stop_id %in% stops_to_keep | to_stop_id %in% stops_to_keep)
gtfs$routes <- gtfs$routes |> semi_join(gtfs$trips, "route_id")

write_gtfs(gtfs, "google_transit_nyc_subway.zip")

@polettif polettif merged commit af395fb into master Oct 18, 2024
5 checks passed
@polettif polettif deleted the dev/reduce_package_size branch October 18, 2024 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant