Skip to content

Instantly share code, notes, and snippets.

@milesgrimshaw
Created December 28, 2013 17:42
Show Gist options
  • Save milesgrimshaw/8161979 to your computer and use it in GitHub Desktop.
Save milesgrimshaw/8161979 to your computer and use it in GitHub Desktop.
require 'csv'
require 'json'
require 'pp'
i = 0
features = []
start_line = []
end_line = []
## Loop through each row of the data
CSV.foreach("paths_final.csv") do |row|
## Get the lat and lon and convert to float
lat = row[1].to_f
lon = row[2].to_f
## Get the week day, month, and day of the month
week_day = row[3]
month = row[4]
day = row[5]
## If the first row, then there is only start of the line
if (i <= 1 )
start_line = [lon,lat]
else
end_line = [lon,lat]
## Add the new geoJSON data to the features array
features << {type:'Feature', properties: {week_day: week_day, month: month, day: day }, geometry: {type:'LineString', coordinates: [start_line,end_line] } }
## The end of the line is not the start of the next line
start_line = end_line
end
i += 1
end
## Add it to the entire geoJSON set
geoJSON = {type: 'FeatureCollection', features: features}
## Write to JS file for web visualization
File.open("final.js","w") do |f|
f.write(geoJSON.to_json)
end
## Write to geoJSON for TileMill
File.open("final.geoJSON","w") do |f|
f.write(geoJSON.to_json)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment