Created
January 17, 2010 22:13
-
-
Save tmcw/279618 to your computer and use it in GitHub Desktop.
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
| # prototype of crime mapping app | |
| require 'csv' | |
| require 'date' | |
| require 'rubygems' | |
| require 'sparklines' | |
| require 'RMagick' | |
| require 'uuid' | |
| ONLY_HOMICIDES = false | |
| street_parts = /(\d+) B\/O (\d+)(TH|ND|RD) ST (NW|SW|NE|SE)/ | |
| start = Time.now | |
| crimes = [] | |
| # This loop is probably very slow because | |
| # CSV isn't speedy and neither is date parsing | |
| # strptime and Date.parse are basically identical | |
| # performance-wise | |
| def dateagram(dates, year) | |
| out = [] | |
| (1..12).each do |month| | |
| daycounter = Date.strptime("#{month}/1/#{year}", "%m/%d/%Y") | |
| o = 1 | |
| while daycounter.month == month | |
| if dates.include? daycounter | |
| o = 2 | |
| break | |
| end | |
| daycounter += 1 | |
| end | |
| out << o | |
| end | |
| id = UUID.new.generate | |
| line = Sparklines.plot_to_image(out, | |
| :type => 'whisker', | |
| :step => 1, | |
| :whisker_color => "#eeeeee", | |
| :exception_color => "red", | |
| :height => 20) | |
| line.write("graphs/#{id}.png") | |
| "<img src='graphs/#{id}.png' />" | |
| end | |
| CSV.open('crime_2009a.csv', 'r') do |row| | |
| type = row[4] | |
| place = row[7] | |
| date = Date.strptime(row[2], '%m/%d/%Y') | |
| stp = street_parts.match(row[7]) | |
| if stp.nil? | |
| next | |
| end | |
| if ONLY_HOMICIDES and row[4] != 'HOMICIDE' || | |
| stp[4] != 'NW' | |
| next | |
| end | |
| crimes << [date, stp[1].to_i, stp[2].to_i] | |
| end | |
| grid = {} | |
| crimes.each do |c| | |
| k = "#{c[2]}-#{c[1]}" | |
| if grid.has_key?(k) | |
| grid[k] << c[0] | |
| else | |
| grid[k] = [c[0]] | |
| end | |
| end | |
| sorted_grid = grid.sort_by do |a| | |
| [a[0].split('-')[0].to_i,a[0].split('-')[1].to_i] | |
| end | |
| street = 0 | |
| puts "<table>" | |
| sorted_grid.each do |k, v| | |
| if k.split('-')[0].to_i != street | |
| puts "</table><table>" | |
| puts "<th colspan='2' class='header'>#{k.split('-')[0]}th</th>" | |
| street = k.split('-')[0].to_i | |
| end | |
| puts "<tr><th><span class='block'>#{k.split('-')[1]}</span></th><td>#{dateagram(v, 2009)}</td></tr>" | |
| end | |
| puts "</body></html>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment