Module: GTFS::GeoJSON

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

Constant Summary collapse

VERSION =
'0.0.2'

Class Method Summary collapse

Class Method Details

.generate(path) ⇒ Object



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
# File 'lib/gtfs-geojson.rb', line 10

def self.generate(path)
  source = GTFS::Source.build(path,{strict:true})
  
  routes   = {}
  features = []
  
  source.shapes.each do |shape|
    if routes[shape.id]
      routes[shape.id] << shape
    else
      routes[shape.id] = [shape]
    end
  end
  
  routes.each do |route,points|
    coordinates = []
    points.sort! { |a,b| a.pt_sequence.to_i <=> b.pt_sequence.to_i }
  
    points.each do |point|
      coordinates << [ point.pt_lon.to_f, point.pt_lat.to_f ]
    end
  
    features << { type: 'Feature',
                  properties: { name: route
                              },
                  geometry: { type: 'LineString',
                              coordinates: coordinates
                            }
                }
  
  end
  
  return JSON.generate({type: 'FeatureCollection', features: features})
end