Git Product home page Git Product logo

gtfs_reader's Introduction

GTFS Reader

Build Status

gem 'gtfs_reader'

GTFS Reader is a gem designed to help process the contents of a "GTFS Feed":

The General Transit Feed Specification (GTFS) defines a common format for public transportation schedules and associated geographic information. GTFS "feeds" allow public transit agencies to publish their transit data and developers to write applications that consume that data in an interoperable way.

Essentially, a GTFS feed is a ZIP file containing CSV-formatted .txt files following the specification.

Usage

Simple Example

require 'gtfs_reader'

GtfsReader.config do
  # verbose true # TODO: uncomment for verbose output
  return_hashes true

  sources do
    sample do
      url 'http://localhost/sample-feed.zip' # you can also use a filepath here
      before { |etag| puts "Processing source with tag #{etag}..." }
      handlers do
        agency { |row| puts "Read Agency: #{row[:agency_name]}" }
        routes { |row| puts "Read Route: #{row[:route_long_name]}" }
      end
    end
  end
end

GtfsReader.update(:sample) # or GtfsReader.update_all!

Assuming that http://localhost/sample-feed.zip returns the Example Feed, this script will print the following:

Processing source with tag 4d9d3040c284f0581cd5620d5c131109...
Read Agency: Demo Transit Authority
Read Route: Airport - Bullfrog
Read Route: Bullfrog - Furnace Creek Resort
Read Route: Stagecoach - Airport Shuttle
Read Route: City
Read Route: Airport - Amargosa Valley

Custom Feed Format

By default, this gem parses files in the format specified by the GTFS Feed Spec. You can see this FeedDefinition in config/defaults/gtfs_feed_definition.rb. However, in many cases these feeds are created by people who aren't technically-proficient and may not exactly conform to the spec. In the event that you want to parse a file with a different format, you can do so in the GtfsReader.config block:

GtfsReader.config do
  sources do
    sample do
      feed_definition do
        file(:drivers, required: true) do # for my_file.txt
          col(:licence_number, required: true, unique: true)

          # If the employment column contains "1", the symbol :fulltime will be
          # returned, otherwise :temporary will be returned

          col :employment, &output_map({ female: '1', male: '2' }, :unspecified)

          # This will allow you to create a custom parser. Within the given
          # block you can reference other columns in the current row by name.
          col :name do |name|
            case employment
            when :fulltime  then "Mr. #{name}"
            when :temporary then "#{name} the newbie"
            else            name
            end
          end
        end
      end

      # ...

    end
  end
end

gtfs_reader's People

Contributors

chesterbr avatar dependabot[bot] avatar merlos avatar sangster avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.