Skip to content

Instantly share code, notes, and snippets.

@werebus
Created June 27, 2013 17:24
Show Gist options
  • Save werebus/5878438 to your computer and use it in GitHub Desktop.
Save werebus/5878438 to your computer and use it in GitHub Desktop.
gtfs 'stops.txt' file to geojson file
require 'json'
require 'csv'
json = { 'type' => 'FeatureCollection',
'crs' => { 'type' => 'name',
'properties' => { 'name' => 'urn:ogc:def:crs:OGC:1.3:CRS84' } },
'features' => []
}
CSV.open('stops.txt', 'r') do |csv|
fields = {}
field_list = csv.shift
field_list.each_with_index do |field, index|
fields[field.to_sym] = index
end
csv.each do |row|
json['features'] << { 'type' => 'Feature',
'properties' => { 'id' => row[fields[:stop_id]],
'name' => row[fields[:stop_name]] },
'geometry' => { 'type' => 'Point',
'coordinates' => [ row[fields[:stop_lon]], row[fields[:stop_lat]] ] }}
end
end
puts JSON.pretty_generate(json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment