Class: GtfsDf::Writer
- Inherits:
-
Object
- Object
- GtfsDf::Writer
- Defined in:
- lib/gtfs_df/writer.rb
Class Method Summary collapse
-
.format_time_fields(file, df) ⇒ Polars::DataFrame
Formats time fields back to HH:MM:SS strings for a given GTFS file.
-
.write_to_dir(feed, dir_path) ⇒ Object
Exports a Feed to a directory as individual text files.
-
.write_to_zip(feed, zip_path) ⇒ Object
Exports a Feed to a GTFS zip file.
Class Method Details
.format_time_fields(file, df) ⇒ Polars::DataFrame
Formats time fields back to HH:MM:SS strings for a given GTFS file
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/gtfs_df/writer.rb', line 57 def self.format_time_fields(file, df) schema_class_name = file.split("_").map(&:capitalize).join schema_class = begin GtfsDf::Schema.const_get(schema_class_name) rescue nil end return df unless schema_class&.respond_to?(:time_fields) time_fields = schema_class.time_fields time_fields.each do |field| next unless df.columns.include?(field) df = df.with_columns(GtfsDf::Utils.as_time_string(field)) end df end |
.write_to_dir(feed, dir_path) ⇒ Object
Exports a Feed to a directory as individual text files
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/gtfs_df/writer.rb', line 38 def self.write_to_dir(feed, dir_path) FileUtils.mkdir_p(dir_path) GtfsDf::Feed::GTFS_FILES.each do |file| df = feed.send(file) next unless df.is_a?(Polars::DataFrame) # Convert time fields back to strings if parse_times was enabled df = format_time_fields(file, df) if feed.parse_times # Write CSV directly to file df.write_csv(File.join(dir_path, "#{file}.txt")) end end |
.write_to_zip(feed, zip_path) ⇒ Object
Exports a Feed to a GTFS zip file
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/gtfs_df/writer.rb', line 9 def self.write_to_zip(feed, zip_path) require "stringio" require "zlib" FileUtils.mkdir_p(File.dirname(zip_path)) Zip::File.open(zip_path, create: true) do |zipfile| GtfsDf::Feed::GTFS_FILES.each do |file| df = feed.send(file) next unless df.is_a?(Polars::DataFrame) # Convert time fields back to strings if parse_times was enabled if feed.parse_times df = format_time_fields(file, df) end # Write CSV to StringIO csv_io = StringIO.new df.write_csv(csv_io) csv_io.rewind # Write StringIO directly to zip zipfile.get_output_stream("#{file}.txt") { |f| f.write(csv_io.read) } end end end |