gtfs_df 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/.conform.yaml +25 -0
  3. data/.envrc +12 -0
  4. data/.rspec +3 -0
  5. data/.rubocop.yml +2 -0
  6. data/.solargraph.yml +26 -0
  7. data/.standard.yml +3 -0
  8. data/CHANGELOG.md +3 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +105 -0
  11. data/Rakefile +10 -0
  12. data/devenv.lock +171 -0
  13. data/devenv.nix +13 -0
  14. data/devenv.yaml +8 -0
  15. data/examples/split-by-agency/.gitignore +1 -0
  16. data/examples/split-by-agency/Gemfile +5 -0
  17. data/examples/split-by-agency/Gemfile.lock +52 -0
  18. data/examples/split-by-agency/README.md +26 -0
  19. data/examples/split-by-agency/split_by_agency.rb +52 -0
  20. data/lib/gtfs_df/base_gtfs_table.rb +45 -0
  21. data/lib/gtfs_df/feed.rb +152 -0
  22. data/lib/gtfs_df/graph.rb +131 -0
  23. data/lib/gtfs_df/reader.rb +28 -0
  24. data/lib/gtfs_df/schema/agency.rb +26 -0
  25. data/lib/gtfs_df/schema/areas.rb +18 -0
  26. data/lib/gtfs_df/schema/attributions.rb +28 -0
  27. data/lib/gtfs_df/schema/booking_rules.rb +19 -0
  28. data/lib/gtfs_df/schema/calendar.rb +32 -0
  29. data/lib/gtfs_df/schema/calendar_dates.rb +19 -0
  30. data/lib/gtfs_df/schema/enum_values.rb +147 -0
  31. data/lib/gtfs_df/schema/fare_attributes.rb +25 -0
  32. data/lib/gtfs_df/schema/fare_leg_join_rules.rb +20 -0
  33. data/lib/gtfs_df/schema/fare_leg_rules.rb +21 -0
  34. data/lib/gtfs_df/schema/fare_media.rb +18 -0
  35. data/lib/gtfs_df/schema/fare_products.rb +24 -0
  36. data/lib/gtfs_df/schema/fare_rules.rb +19 -0
  37. data/lib/gtfs_df/schema/fare_transfer_rules.rb +23 -0
  38. data/lib/gtfs_df/schema/feed_info.rb +21 -0
  39. data/lib/gtfs_df/schema/frequencies.rb +22 -0
  40. data/lib/gtfs_df/schema/levels.rb +15 -0
  41. data/lib/gtfs_df/schema/location_group_stops.rb +17 -0
  42. data/lib/gtfs_df/schema/location_groups.rb +17 -0
  43. data/lib/gtfs_df/schema/networks.rb +17 -0
  44. data/lib/gtfs_df/schema/pathways.rb +29 -0
  45. data/lib/gtfs_df/schema/rider_categories.rb +18 -0
  46. data/lib/gtfs_df/schema/route_networks.rb +17 -0
  47. data/lib/gtfs_df/schema/routes.rb +33 -0
  48. data/lib/gtfs_df/schema/shapes.rb +24 -0
  49. data/lib/gtfs_df/schema/stop_areas.rb +19 -0
  50. data/lib/gtfs_df/schema/stop_attributes.rb +17 -0
  51. data/lib/gtfs_df/schema/stop_times.rb +38 -0
  52. data/lib/gtfs_df/schema/stops.rb +34 -0
  53. data/lib/gtfs_df/schema/transfers.rb +20 -0
  54. data/lib/gtfs_df/schema/translations.rb +24 -0
  55. data/lib/gtfs_df/schema/trips.rb +30 -0
  56. data/lib/gtfs_df/schema_validator.rb +89 -0
  57. data/lib/gtfs_df/utils.rb +52 -0
  58. data/lib/gtfs_df/version.rb +5 -0
  59. data/lib/gtfs_df/writer.rb +26 -0
  60. data/lib/gtfs_df.rb +49 -0
  61. data/sig/gtfs-df.rbs +4 -0
  62. metadata +148 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d799dcd02b51045c948123e3b6bc64c41b5505279a583d98350377e6d06b4205
4
+ data.tar.gz: 57ff89fd953842d68a5d4c7424a91b54484415a0b093bbe0d0b0411b025c6ef4
5
+ SHA512:
6
+ metadata.gz: 13b027c13cfec0a3493662cb048dd20f1b44aac778eb0e2d124ad873ae3649b1622758550399d9e457065401bfa04dbbb71e5e36da467599f8aac9675b2431ad
7
+ data.tar.gz: 9d248e0fe06e69af6a5d86114e77e10583dcbf9956858411cc6ed2cc2d03ebf4ce2047d3e8bbc1eb5eb5530a2e544bf8cc21c4d0184fe331558008fce64a055a
data/.conform.yaml ADDED
@@ -0,0 +1,25 @@
1
+ policies:
2
+ - type: commit
3
+ spec:
4
+ header:
5
+ length: 89
6
+ imperative: true
7
+ case: lower
8
+ invalidLastCharacters: .
9
+ body:
10
+ required: true
11
+ dco: false
12
+ spellcheck:
13
+ locale: US
14
+ maximumOfOneCommit: false
15
+ conventional:
16
+ scopes:
17
+ - master
18
+ - deps
19
+ types:
20
+ - feat
21
+ - fix
22
+ - chore
23
+ - deps
24
+ - docs
25
+ - ci
data/.envrc ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env bash
2
+
3
+ export DIRENV_WARN_TIMEOUT=20s
4
+
5
+ eval "$(devenv direnvrc)"
6
+
7
+ # `use devenv` supports the same options as the `devenv shell` command.
8
+ #
9
+ # To silence all output, use `--quiet`.
10
+ #
11
+ # Example usage: use devenv --quiet --impure --option services.postgres.enable:bool true
12
+ use devenv
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ inherit_gem:
2
+ standard: config/base.yml
data/.solargraph.yml ADDED
@@ -0,0 +1,26 @@
1
+ ---
2
+ include:
3
+ - Rakefile
4
+ - Gemfile
5
+ - "*.gemspec"
6
+ - "**/*.rb"
7
+ exclude:
8
+ - spec/**/*
9
+ - test/**/*
10
+ - vendor/**/*
11
+ - ".bundle/**/*"
12
+ require: []
13
+ domains: []
14
+ reporters:
15
+ - rubocop
16
+ - require_not_found
17
+ formatter:
18
+ rubocop:
19
+ cops: safe
20
+ except: []
21
+ only: []
22
+ extra_args: []
23
+ lint: true
24
+ require_paths: []
25
+ plugins: []
26
+ max_files: 5000
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [0.1.0] - 2025-11-10
2
+
3
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 David Mejorado
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,105 @@
1
+ # ruby-gtfs-df
2
+
3
+ A ruby gem to manipulate [GTFS] feeds using DataFrames using [Polars] ([ruby-polars])
4
+
5
+ This project was created to bring the power of [partridge] to ruby.
6
+
7
+ > **⚠️ Warning:** This gem is not ready for production use. It is currently in active development and the API may change without notice.
8
+
9
+ ## Installation
10
+
11
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
12
+
13
+ Install the gem and add to the application's Gemfile by executing:
14
+
15
+ ```bash
16
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
17
+ ```
18
+
19
+ If bundler is not being used to manage dependencies, install the gem by executing:
20
+
21
+ ```bash
22
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### Loading a GTFS feed
28
+
29
+ ```ruby
30
+ require 'gtfs_df'
31
+
32
+ # Load from a zip file
33
+ feed = GtfsDf::Reader.load_from_zip('path/to/gtfs.zip')
34
+
35
+ # Access dataframes for each GTFS file
36
+ puts feed.agency.head
37
+ puts feed.routes.head
38
+ puts feed.trips.head
39
+ puts feed.stop_times.head
40
+ puts feed.stops.head
41
+ ```
42
+
43
+ ### Filtering feeds
44
+
45
+ The library supports filtering feeds by any field in any table. The filter automatically cascades through the dependency graph to ensure referential integrity.
46
+
47
+ ```ruby
48
+ # Filter by agency
49
+ filtered_feed = feed.filter('agency' => { 'agency_id' => 'MTA' })
50
+
51
+ # Filter by route
52
+ filtered_feed = feed.filter('routes' => { 'route_id' => ['1', '2', '3'] })
53
+
54
+ # Filter by a service
55
+ filtered_feed = feed.filter('calendar' => { 'service_id' => 'WEEKDAY' })
56
+
57
+ # Multiple filters
58
+ filtered_feed = feed.filter(
59
+ 'agency' => { 'agency_id' => 'MTA' },
60
+ 'routes' => { 'route_type' => 1 } # Filter to subway routes
61
+ )
62
+ ```
63
+
64
+ When you filter by a field, the library automatically:
65
+ 1. Filters the specified table
66
+ 2. Cascades related tables following foreign key relationships
67
+ 3. Keeps only the data that maintains referential integrity
68
+
69
+ For example, filtering by `agency_id` will automatically filter routes, trips, stop_times, and stops to only include data for that agency.
70
+
71
+ ### Writing filtered feeds
72
+
73
+ ```ruby
74
+ # Write to a new zip file
75
+ GtfsDf::Writer.write_to_zip(filtered_feed, 'output/filtered_gtfs.zip')
76
+ ```
77
+
78
+ ### Example: Split feed by agency
79
+
80
+ See [examples/split-by-agency](examples/split-by-agency) for a complete example that splits a multi-agency GTFS feed into separate files per agency.
81
+
82
+ ## Development
83
+
84
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
85
+
86
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
87
+
88
+ ## TODO
89
+
90
+ - [ ] Time parsing
91
+ Just like partridge, we should parse Time as seconds since midnight. There's a draft in `lib/gtfs_df/utils.rb` but it's not used anywhere.
92
+ I haven't figured out how to properly implement with Polars.
93
+
94
+ ## Contributing
95
+
96
+ Bug reports and pull requests are welcome on GitHub at https://github.com/davidmh/ruby-gtfs_df.
97
+
98
+ ## License
99
+
100
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
101
+
102
+ [GTFS]: https://gtfs.org/
103
+ [Polars]: https://pola.rs/
104
+ [ruby-polars]: https://github.com/ankane/ruby-polars
105
+ [partridge]: https://github.com/remix/partridge
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
data/devenv.lock ADDED
@@ -0,0 +1,171 @@
1
+ {
2
+ "nodes": {
3
+ "devenv": {
4
+ "locked": {
5
+ "dir": "src/modules",
6
+ "lastModified": 1761427990,
7
+ "owner": "cachix",
8
+ "repo": "devenv",
9
+ "rev": "7419c04fc798d5d5918413d4cb6c8629f9d4e8a3",
10
+ "type": "github"
11
+ },
12
+ "original": {
13
+ "dir": "src/modules",
14
+ "owner": "cachix",
15
+ "repo": "devenv",
16
+ "type": "github"
17
+ }
18
+ },
19
+ "flake-compat": {
20
+ "flake": false,
21
+ "locked": {
22
+ "lastModified": 1747046372,
23
+ "owner": "edolstra",
24
+ "repo": "flake-compat",
25
+ "rev": "9100a0f413b0c601e0533d1d94ffd501ce2e7885",
26
+ "type": "github"
27
+ },
28
+ "original": {
29
+ "owner": "edolstra",
30
+ "repo": "flake-compat",
31
+ "type": "github"
32
+ }
33
+ },
34
+ "flake-compat_2": {
35
+ "flake": false,
36
+ "locked": {
37
+ "lastModified": 1747046372,
38
+ "owner": "edolstra",
39
+ "repo": "flake-compat",
40
+ "rev": "9100a0f413b0c601e0533d1d94ffd501ce2e7885",
41
+ "type": "github"
42
+ },
43
+ "original": {
44
+ "owner": "edolstra",
45
+ "repo": "flake-compat",
46
+ "type": "github"
47
+ }
48
+ },
49
+ "flake-utils": {
50
+ "inputs": {
51
+ "systems": "systems"
52
+ },
53
+ "locked": {
54
+ "lastModified": 1731533236,
55
+ "owner": "numtide",
56
+ "repo": "flake-utils",
57
+ "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
58
+ "type": "github"
59
+ },
60
+ "original": {
61
+ "owner": "numtide",
62
+ "repo": "flake-utils",
63
+ "type": "github"
64
+ }
65
+ },
66
+ "git-hooks": {
67
+ "inputs": {
68
+ "flake-compat": "flake-compat",
69
+ "gitignore": "gitignore",
70
+ "nixpkgs": [
71
+ "nixpkgs"
72
+ ]
73
+ },
74
+ "locked": {
75
+ "lastModified": 1760663237,
76
+ "owner": "cachix",
77
+ "repo": "git-hooks.nix",
78
+ "rev": "ca5b894d3e3e151ffc1db040b6ce4dcc75d31c37",
79
+ "type": "github"
80
+ },
81
+ "original": {
82
+ "owner": "cachix",
83
+ "repo": "git-hooks.nix",
84
+ "type": "github"
85
+ }
86
+ },
87
+ "gitignore": {
88
+ "inputs": {
89
+ "nixpkgs": [
90
+ "git-hooks",
91
+ "nixpkgs"
92
+ ]
93
+ },
94
+ "locked": {
95
+ "lastModified": 1709087332,
96
+ "owner": "hercules-ci",
97
+ "repo": "gitignore.nix",
98
+ "rev": "637db329424fd7e46cf4185293b9cc8c88c95394",
99
+ "type": "github"
100
+ },
101
+ "original": {
102
+ "owner": "hercules-ci",
103
+ "repo": "gitignore.nix",
104
+ "type": "github"
105
+ }
106
+ },
107
+ "nixpkgs": {
108
+ "locked": {
109
+ "lastModified": 1758532697,
110
+ "owner": "cachix",
111
+ "repo": "devenv-nixpkgs",
112
+ "rev": "207a4cb0e1253c7658c6736becc6eb9cace1f25f",
113
+ "type": "github"
114
+ },
115
+ "original": {
116
+ "owner": "cachix",
117
+ "ref": "rolling",
118
+ "repo": "devenv-nixpkgs",
119
+ "type": "github"
120
+ }
121
+ },
122
+ "nixpkgs-ruby": {
123
+ "inputs": {
124
+ "flake-compat": "flake-compat_2",
125
+ "flake-utils": "flake-utils",
126
+ "nixpkgs": [
127
+ "nixpkgs"
128
+ ]
129
+ },
130
+ "locked": {
131
+ "lastModified": 1759902829,
132
+ "owner": "bobvanderlinden",
133
+ "repo": "nixpkgs-ruby",
134
+ "rev": "5fba6c022a63f1e76dee4da71edddad8959f088a",
135
+ "type": "github"
136
+ },
137
+ "original": {
138
+ "owner": "bobvanderlinden",
139
+ "repo": "nixpkgs-ruby",
140
+ "type": "github"
141
+ }
142
+ },
143
+ "root": {
144
+ "inputs": {
145
+ "devenv": "devenv",
146
+ "git-hooks": "git-hooks",
147
+ "nixpkgs": "nixpkgs",
148
+ "nixpkgs-ruby": "nixpkgs-ruby",
149
+ "pre-commit-hooks": [
150
+ "git-hooks"
151
+ ]
152
+ }
153
+ },
154
+ "systems": {
155
+ "locked": {
156
+ "lastModified": 1681028828,
157
+ "owner": "nix-systems",
158
+ "repo": "default",
159
+ "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
160
+ "type": "github"
161
+ },
162
+ "original": {
163
+ "owner": "nix-systems",
164
+ "repo": "default",
165
+ "type": "github"
166
+ }
167
+ }
168
+ },
169
+ "root": "root",
170
+ "version": 7
171
+ }
data/devenv.nix ADDED
@@ -0,0 +1,13 @@
1
+ { pkgs, lib, config, inputs, ... }:
2
+
3
+ {
4
+ languages.ruby = {
5
+ enable = true;
6
+ version = "3.4.7";
7
+ bundler.enable = false;
8
+ };
9
+
10
+ pre-commit.hooks = {
11
+ conform.enable = true;
12
+ };
13
+ }
data/devenv.yaml ADDED
@@ -0,0 +1,8 @@
1
+ inputs:
2
+ nixpkgs:
3
+ url: github:cachix/devenv-nixpkgs/rolling
4
+ nixpkgs-ruby:
5
+ url: github:bobvanderlinden/nixpkgs-ruby
6
+ inputs:
7
+ nixpkgs:
8
+ follows: nixpkgs
@@ -0,0 +1 @@
1
+ output/
@@ -0,0 +1,5 @@
1
+ source "https://gem.coop"
2
+
3
+ gem "gtfs_df", path: "../.."
4
+ gem "optparse"
5
+ gem "whirly"
@@ -0,0 +1,52 @@
1
+ PATH
2
+ remote: ../..
3
+ specs:
4
+ gtfs_df (0.1.0)
5
+ networkx (~> 0.4)
6
+ polars-df (~> 0.22)
7
+ rubyzip (~> 2.3)
8
+
9
+ GEM
10
+ remote: https://gem.coop/
11
+ specs:
12
+ bigdecimal (3.3.1)
13
+ matrix (0.4.3)
14
+ networkx (0.4.0)
15
+ matrix (~> 0.4)
16
+ rb_heap (~> 1.0)
17
+ optparse (0.8.0)
18
+ polars-df (0.23.0-aarch64-linux)
19
+ bigdecimal
20
+ polars-df (0.23.0-aarch64-linux-musl)
21
+ bigdecimal
22
+ polars-df (0.23.0-arm64-darwin)
23
+ bigdecimal
24
+ polars-df (0.23.0-x86_64-darwin)
25
+ bigdecimal
26
+ polars-df (0.23.0-x86_64-linux)
27
+ bigdecimal
28
+ polars-df (0.23.0-x86_64-linux-musl)
29
+ bigdecimal
30
+ rb_heap (1.1.0)
31
+ rubyzip (2.4.1)
32
+ unicode-display_width (3.2.0)
33
+ unicode-emoji (~> 4.1)
34
+ unicode-emoji (4.1.0)
35
+ whirly (0.3.0)
36
+ unicode-display_width (>= 1.1)
37
+
38
+ PLATFORMS
39
+ aarch64-linux
40
+ aarch64-linux-musl
41
+ arm64-darwin
42
+ x86_64-darwin
43
+ x86_64-linux
44
+ x86_64-linux-musl
45
+
46
+ DEPENDENCIES
47
+ gtfs_df!
48
+ optparse
49
+ whirly
50
+
51
+ BUNDLED WITH
52
+ 2.6.9
@@ -0,0 +1,26 @@
1
+ # Split GTFS by Agency Example
2
+
3
+ This example demonstrates how to split a GTFS zip file into multiple files, one for each specified `agency_id`, using the `gtfs_df` Ruby gem.
4
+
5
+ ## Usage
6
+
7
+ ```
8
+ bundle install
9
+ ruby split_by_agency.rb -i <input-gtfs.zip> --ids agency1,agency2
10
+ ```
11
+
12
+ - The output files will be written to the `output/` directory, named `<agency_id>.zip`.
13
+
14
+ ## Options
15
+ - `-i`, `--input PATH` — Path to the input GTFS zip file
16
+ - `--ids IDS` — Comma-separated list of agency IDs to extract
17
+
18
+ ## Example
19
+
20
+ ```
21
+ ruby split_by_agency.rb -i ../../spec/fixtures/sample_gtfs.zip --ids DTA,OTA
22
+ ```
23
+
24
+ ---
25
+
26
+ This is a port of the [original Python script](https://gist.github.com/davidmh/f51e5d93a9213e0e606a43167ff87403) using Partridge.
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "optparse"
5
+ require "fileutils"
6
+ require "gtfs_df"
7
+ require "whirly"
8
+
9
+ options = {}
10
+ OptionParser.new do |opts|
11
+ opts.banner = "Usage: split_by_agency.rb -i <input-gtfs.zip> --ids NUMBERS"
12
+
13
+ opts.on("-i", "--input PATH", "Path to the input GTFS file") do |v|
14
+ options[:input] = v
15
+ end
16
+ opts.on("--ids IDS", "Comma-separated list of agency_ids") do |v|
17
+ options[:ids] = v
18
+ end
19
+ end.parse!
20
+
21
+ unless options[:input] && options[:ids]
22
+ warn "Both --input and --ids are required."
23
+ exit 1
24
+ end
25
+
26
+ input_path = File.expand_path(options[:input])
27
+ agency_ids = options[:ids].split(",")
28
+ output_dir = File.expand_path("./output", __dir__)
29
+ FileUtils.mkdir_p(output_dir)
30
+
31
+ feed = nil
32
+
33
+ Whirly.configure spinner: "dots", stop: "✓"
34
+
35
+ Whirly.start do
36
+ Whirly.status = "Loading"
37
+ feed = GtfsDf::Reader.load_from_zip(input_path)
38
+ Whirly.status = "Loaded"
39
+ end
40
+
41
+ agency_ids.each do |agency_id|
42
+ Whirly.start do
43
+ Whirly.status = "-> #{agency_id} filtering..."
44
+ output_path = File.join(output_dir, "#{agency_id}.zip")
45
+ filtered_feed = feed.filter("agency" => {"agency_id" => agency_id})
46
+ Whirly.status = "-> #{agency_id} writing..."
47
+ GtfsDf::Writer.write_to_zip(filtered_feed, output_path)
48
+ Whirly.status = "-> #{agency_id}"
49
+ end
50
+ end
51
+
52
+ puts "✓ Done, all files are stored in the output/ directory"
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GtfsDf
4
+ class BaseGtfsTable
5
+ attr_reader :df, :validator
6
+
7
+ # @param input [Polars::DataFrame, String, Array] A dataframe, a csv path or an array-based table
8
+ def initialize(input)
9
+ @df =
10
+ if input.is_a?(Polars::DataFrame)
11
+ input
12
+ elsif input.is_a?(String)
13
+ Polars.read_csv(input, dtypes: self.class::SCHEMA)
14
+ elsif input.is_a?(Array)
15
+ head, *body = input
16
+ df_input = body.each_with_object({}) do |row, acc|
17
+ head.each_with_index do |column, index|
18
+ acc[column] ||= []
19
+ acc[column] << row[index]
20
+ end
21
+ end
22
+ Polars::DataFrame.new(df_input, schema_overrides: self.class::SCHEMA, strict: false)
23
+ else
24
+ throw GtfsDf::Error, "Unrecognized input"
25
+ end
26
+ @validator = SchemaValidator.new(@df, self.class)
27
+ end
28
+
29
+ def fields
30
+ self.class::SCHEMA.keys
31
+ end
32
+
33
+ def valid?
34
+ @validator.valid?
35
+ end
36
+
37
+ def errors
38
+ @validator.errors
39
+ end
40
+
41
+ def dataframe
42
+ @df
43
+ end
44
+ end
45
+ end