Ruby script to convert stops.txt
and shapes.txt
to GeoJSON.
Last active
May 13, 2017 22:47
-
-
Save bertspaan/e82190e3d02e059c05da to your computer and use it in GitHub Desktop.
Ruby script to convert GTFS stops and shapes to GeoJSON.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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