Skip to content

Instantly share code, notes, and snippets.

@douglasgoodwin
Forked from bertspaan/GTFS-to-GEOJSON.md
Last active August 29, 2015 14:17
Show Gist options
  • Save douglasgoodwin/9e5025192dcbea5a4341 to your computer and use it in GitHub Desktop.
Save douglasgoodwin/9e5025192dcbea5a4341 to your computer and use it in GitHub Desktop.

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