Skip to content

Instantly share code, notes, and snippets.

@bertspaan
Last active May 13, 2017 22:47
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save bertspaan/e82190e3d02e059c05da to your computer and use it in GitHub Desktop.
Save bertspaan/e82190e3d02e059c05da to your computer and use it in GitHub Desktop.
Ruby script to convert GTFS stops and shapes to GeoJSON.

Ruby script to convert stops.txt and shapes.txt to GeoJSON.

require 'csv'
require 'json'
# TODO: scripts expects shape_id and shape_pt_sequence to be in order in file
# e.g. all points of single shape together, and shape_pt_sequence increasing
features = []
coordinates = []
shape_id = nil
CSV.foreach('shapes.txt', headers: true) do |row|
if row['shape_id'] != shape_id
# new shape!
if shape_id
features << {
type: 'Feature',
properties: {},
geometry: {
type: 'LineString',
coordinates: coordinates
}
}
end
coordinates = []
shape_id = row['shape_id']
end
coordinates << [row['shape_pt_lon'].to_f, row['shape_pt_lat'].to_f]
end
File.open('shapes.json', 'w') do |f|
f.write(JSON.pretty_generate({type: 'FeatureCollection', features: features}))
end
require 'csv'
require 'json'
features = []
CSV.foreach('stops.txt', headers: true) do |row|
features << {
type: 'Feature',
properties: {
name: row['stop_name']
},
geometry: {
type: 'Point',
coordinates: [row['stop_lon'].to_f, row['stop_lat'].to_f]
}
}
end
File.open('stops.json', 'w') do |f|
f.write(JSON.pretty_generate({type: 'FeatureCollection', features: features}))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment