Skip to content

Instantly share code, notes, and snippets.

@andrewharvey
Forked from bertspaan/GTFS-to-GEOJSON.md
Last active November 10, 2015 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewharvey/58e204f244bc2e58ccc8 to your computer and use it in GitHub Desktop.
Save andrewharvey/58e204f244bc2e58ccc8 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',
id: shape_id,
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',
id: row['stop_id'],
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