Created
July 8, 2010 16:00
-
-
Save konklone/468215 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
## Methods dealing with Govtrack's KML exporter for states and congressional districts. | |
# Used to generate map files for a couple of Sunlight's projects, Politiwidgets and the | |
# Congress app for Android. | |
## You can also just download individual maps at GovTrack's page about all this: | |
# http://www.govtrack.us/embed/googlemaps.xpd | |
# This script is useful if you want to generate many programmatically. | |
# It also swaps out the default '0' placemark name for something minimally descriptive. | |
## This file isn't runnable, and just contains some methods. | |
# Most useful by opening up IRB in the same directory as this file, requiring this file, | |
# then running whichever methods are useful. | |
require 'rubygems' | |
require 'nokogiri' | |
@export = "http://www.govtrack.us/perl/wms/export.cgi" | |
@state_dataset = "http://www.rdfabout.com/rdf/usgov/us/states" | |
@state_region = "http://www.rdfabout.com/rdf/usgov/geo/us/%s" # state (e.g. CA) | |
@cd_dataset = "http://www.rdfabout.com/rdf/usgov/congress/house/%s" # session (e.g. 110) | |
@cd_region = "http://www.rdfabout.com/rdf/usgov/geo/us/%s/cd/%s/%s" # [state, session, district (e.g. 16)] | |
def get_state(state, filename = nil) | |
name = filename || "states/#{state}" | |
get_kml name, @state_dataset, (@state_region % state) | |
change_placemark_names name | |
end | |
def get_cd(session, state, district) | |
name = "cds/#{session}/#{state}-#{district}" | |
get_kml name, (@cd_dataset % session), (@cd_region % [state, session, district]) | |
change_placemark_names name | |
end | |
def get_kml(path, dataset, region) | |
system "wget -O \"#{path}.kml\" \"#{@export}?dataset=#{dataset}®ion=#{region}&format=kml&maxpoints=1000\"" | |
end | |
# update the default '0' placemark name to be the base name of the file (e.g. AK, CA-9, etc.) | |
def change_placemark_names(filename) | |
# filename will be like "states/AK" or "cds/CA-16" | |
place_name = File.basename filename | |
# read file into Nokogiri | |
file = File.open "#{filename}.kml" | |
doc = Nokogiri::XML file | |
file.close | |
# find all the placemark name items and switch their names | |
doc.css("Placemark name").each do |name| | |
name.inner_html = place_name | |
end | |
# write over file | |
out = File.open "#{filename}.kml", "w" | |
out.write doc.to_xml | |
out.close | |
end | |
# all 56 states, districts, and territories | |
def get_states | |
@states.keys.each {|state| get_state state} | |
end | |
# all 441 districts for a given session of Congress | |
def get_districts(session) | |
@states.each do |state, districts| | |
if districts == 1 | |
get_state state, "cds/#{session}/#{state}-0" | |
else # if districts > 1 | |
districts.times do |i| # 0 indexed | |
get_cd session, state, i+1 | |
#sleep 1 | |
end | |
end | |
end | |
end | |
# state codes and number of districts apiece | |
@states = { | |
"AL" => 7, | |
"AK" => 1, | |
"AZ" => 8, | |
"AR" => 4, | |
"AS" => 1, | |
"CA" => 53, | |
"CO" => 7, | |
"CT" => 5, | |
"DE" => 1, | |
"DC" => 1, | |
"FL" => 25, | |
"GA" => 13, | |
"GU" => 1, | |
"HI" => 2, | |
"ID" => 2, | |
"IL" => 19, | |
"IN" => 9, | |
"IA" => 5, | |
"KS" => 4, | |
"KY" => 6, | |
"LA" => 7, | |
"ME" => 2, | |
"MD" => 8, | |
"MA" => 10, | |
"MI" => 15, | |
"MN" => 8, | |
"MP" => 1, | |
"MS" => 4, | |
"MO" => 9, | |
"MT" => 1, | |
"NE" => 3, | |
"NV" => 3, | |
"NH" => 2, | |
"NJ" => 13, | |
"NM" => 3, | |
"NY" => 29, | |
"NC" => 13, | |
"ND" => 1, | |
"OH" => 18, | |
"OK" => 5, | |
"OR" => 5, | |
"PA" => 19, | |
"PR" => 1, | |
"RI" => 2, | |
"SC" => 6, | |
"SD" => 1, | |
"TN" => 9, | |
"TX" => 32, | |
"UT" => 3, | |
"VT" => 1, | |
"VA" => 11, | |
"VI" => 1, | |
"WA" => 9, | |
"WV" => 3, | |
"WI" => 8, | |
"WY" => 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment