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)
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
|