Skip to content

Instantly share code, notes, and snippets.

@jpmckinney
Created March 18, 2014 03:51
Show Gist options
  • Save jpmckinney/9613246 to your computer and use it in GitHub Desktop.
Save jpmckinney/9613246 to your computer and use it in GitHub Desktop.
require 'csv'
require 'open-uri'
data_catalogs = {}
rows = CSV.parse(open('https://raw.github.com/opencivicdata/ocd-division-ids/master/identifiers/country-ca/ca_municipal_subdivisions-data_catalog.csv').read)
rows.shift
rows.each do |id,data_catalog|
data_catalogs[id[/[^:]+\z/]] = data_catalog
end
# @see http://www12.statcan.gc.ca/census-recensement/2011/dp-pd/hlt-fst/pd-pl/index-eng.cfm
file = open("http://www12.statcan.gc.ca/census-recensement/2011/dp-pd/hlt-fst/pd-pl/FullFile.cfm?T=301&LANG=Eng&OFT=CSV&OFN=98-310-XWE2011002-301.CSV")
# The CSV has an extra header row.
file.gets
# The CSV is in ISO-8859-1.
text = file.read.force_encoding("ISO-8859-1").encode("UTF-8")
puts CSV.generate_line(['Geographic code', 'Geographic name', 'Geographic type', 'Population, 2011', 'Data catalog'])
CSV.parse(text, headers: true, skip_blanks: true).each do |row|
code = row.fetch("Geographic code")
name = row.fetch("Geographic name")
type = row.fetch("Geographic type")
population = row.fetch("Population, 2011")
# Skip "Canada" row.
next if code == "01"
# Stop before footer.
break if code == "Note:"
puts CSV.generate_line([code, name, type, population, data_catalogs[code]])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment