Skip to content

Instantly share code, notes, and snippets.

@jenningsanderson
Last active August 29, 2015 13:56
Show Gist options
  • Save jenningsanderson/8965160 to your computer and use it in GitHub Desktop.
Save jenningsanderson/8965160 to your computer and use it in GitHub Desktop.
Ruby script for extracting the coordinates from the Colorado Flood Tweets. Generates file ~ 1mb. Includes integer dates for Tilemill styling.
require 'json'
require 'csv'
require 'time'
file_path = '/path/to/2013ColoradoFloods.json'
#Open the CSV
CSV.open("/path/to/where/you/want/csv/geo_tagged_for_TM.csv", "w") do |csv|
csv << ["Lon", "Lat", "Date", "SortDate", "User", "Text"]
#Open the file and print the lines.
cnt=0
File.open( file_path ).each do |line|
tweet = JSON.load(line)
has_coords = tweet['coordinates'] or false
if has_coords
coords=has_coords['coordinates']
sort_time = Time.parse(tweet['created_at'])
csv << coords + [tweet['created_at'], sort_time.tv_sec-1378000000, tweet['user']['screen_name'], tweet['text']]
end
if cnt%1000==0
puts "#{cnt}: #{ coords }, #{tweet['user']['screen_name']}"
end
cnt+=1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment