Module: GTFS::GeoJSON

Defined in:
lib/gtfs-geojson.rb,
lib/gtfs-geojson/version.rb

Constant Summary collapse

VERSION =
'0.0.3'

Class Method Summary collapse

Class Method Details

.generate(path) ⇒ Object

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gtfs-geojson.rb', line 10

def self.generate(path)
  raise ArgumentError if !File.exist?(path)

  # Confirm the GTFS feed is valid
  source = GTFS::Source.build(path,{strict:true})
  
  routes   = {}
  features = []
 
  # Group the routes by the route id 
  source.shapes.each do |shape|
    if routes[shape.id]
      routes[shape.id] << shape
    else
      routes[shape.id] = [shape]
    end
  end
  
  # Process the data a route at a time
  routes.each do |route,points|
    coordinates = []

    # Sort the points by their point sequence ID, as it may be
    # out of order in the GTFS feed, but must be in order to appear
    # correct for GeoJSON
    points.sort! { |a,b| a.pt_sequence.to_i <=> b.pt_sequence.to_i }
  
    # Add each sorted point to a list of coordinates for this route
    points.each do |point|
      coordinates << [ point.pt_lon.to_f, point.pt_lat.to_f ]
    end

    # Construct a feature and add it to the feature list for this route
    features << { type: 'Feature',
                  properties: { name: route
                              },
                  geometry: { type: 'LineString',
                              coordinates: coordinates
                            }
                }
  
  end
  
  # Create the GeoJSON file by generating a JSON string wrapped 
  # in a FeatureCollection
  return JSON.generate({type: 'FeatureCollection', features: features})
end