├── README ├── to_yaml.rb └── README.md /README: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /to_yaml.rb: -------------------------------------------------------------------------------- 1 | # Convert the raw .csv file to a yaml file for easier processing 2 | 3 | require 'yaml' 4 | require 'csv' 5 | 6 | def row_to_hash(row) 7 | { 8 | "state_fips" => row["state_fips"], 9 | "state" => row["state"], 10 | "state_abbr" => row["state_abbr"], 11 | "county" => row["county"], 12 | "city" => row["city"] 13 | } 14 | end 15 | 16 | list = { } 17 | CSV.foreach("geo-data.csv", :headers => true) do |row| 18 | list[row["zipcode"]] = row_to_hash(row) 19 | end 20 | 21 | puts list.to_yaml 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # US zipcode/city/county/state mapping 2 | 3 | ## Background 4 | 5 | This is a download of us geographic information based on the 2000 census. Read through the [article here](http://mcdc.missouri.edu/webrepts/geography/ZIP.resources.html) for a better understanding of what you're getting. You can preview the data [here](https://www.getsyndetic.com/publish/datasets/0b2c7d29-8c07-4a44-8160-8138889bf158). 6 | 7 | These are "ZCTA"s, which are based on USPS zip codes but more directly relate to spacial information (I.E. you can get lat/lon boundaries for them). From the article: 8 | 9 | > ZIP codes are a very messy kind of geography. They were created by the 10 | > U.S. Postal Service as a tool to help deliver the mail more 11 | > efficiently. ("ZIP" is actually an acronym for "Zone Improvement 12 | > Plan", where "Zone" is a reference to the 2-digit postal zones that 13 | > were used by the post office prior to implementing nationwide ZIP 14 | > codes back in the early 1960's. Because it is an acronym we always use 15 | > the uppercase for it.) ZIP codes have been adopted by marketing people 16 | > and by all kinds of other researchers as a standard geographic area, 17 | > like a city or a county. We see maps of ZIP codes in telephone books 18 | > and from commercial vendors that make us think of them as spatially 19 | > defined areas with precise boundaries, similar to counties. But, from 20 | > the perspective of the agency that defines them, the U.S. Postal 21 | > Service, ZIP codes are not and never have been such spatial 22 | > entities. They are simply categories for grouping mailing 23 | > addresses. As such, ZIP codes do in most cases resemble spatial areas 24 | > since they are comprised of spatially clustered street ranges. But not 25 | > always. In rural areas, ZIP codes can be collections of lines (rural 26 | > delivery routes) that in reality do no look much like a closed spatial 27 | > area. In areas where there is no mail delivery (deserts, mountains, 28 | > lakes, much of Nevada and Utah) ZIP codes are not really defined. You 29 | > may see maps that show ZIP code boundaries that include such areas, 30 | > but these are not post-office-defined official definitions. An area 31 | > will not be assigned a ZIP code until there is a reason for it, 32 | > i.e. until there needs to be mail delivered there. So the actual 33 | > definition of a ZIP code "boundary" is quite fuzzy at best, and a 34 | > purely extrapolated guess (at what it would be if someone were to 35 | > start receiving mail there) at worst. If you have an application that 36 | > requires extreme geographic precision, especially in sparsely 37 | > populated areas, then you need to avoid using ZIP codes. 38 | 39 | ## Usage 40 | 41 | Data is available in .csv and .yaml format. 42 | 43 | Example usage of the yaml data: 44 | 45 | require 'yaml' 46 | 47 | zipdata = YAML::load( File.open( 'geo-data.yaml' ) ) 48 | puts zipdata["18337"].inspect 49 | 50 | --------------------------------------------------------------------------------