-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcensus.rb
52 lines (40 loc) · 1.03 KB
/
census.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require 'json'
require 'httparty'
incidents_file = File.read('incidents.json')
incidents = JSON.parse(incidents_file)
resolved = []
errors = []
incidents.each do |incident|
next if incident['id'].to_i < 407
puts "getting #{incident['id']}"
lat = incident['lat']
lng = incident['lng']
next if lat.nil? || lng.nil?
census_url = "http://data.fcc.gov/api/block/find?latitude=#{lat}&longitude=#{lng}&showall=false&format=json"
census_resp = HTTParty.get(census_url)
census_data = JSON.parse(census_resp.body)
if census_data['status'] == 'OK'
puts "fips = #{census_data['Block']['FIPS']}"
i = {}
i[:id] = incident['id']
i[:fips] = census_data['Block']['FIPS']
resolved << i
else
puts "error"
i = {}
i[:id] = incident['id']
i[:data] = census_data
errors << i
end
puts ''
end
File.open('census_resolved.json', 'w') do |f|
f.puts JSON.pretty_generate(resolved)
end
puts "wrote resolved"
if errors.count > 0
File.open('census_errors.json', 'w') do |f|
f.puts JSON.pretty_generate(errors)
end
puts "wrote errors"
end