CS21 Lab 9: Zip Codes

Due 11:59pm Tuesday, November 16, 2010

For this program you may work with one partner (you may not work in groups larger than two).

If you work with a partner:

The best way to work with a partner is to work together in the lab on all parts of the program. If you need to share a file, here are two ways to do this:

fennel[~]$ cd cs21/labs/09
fennel[09]$ mail knerr < zipcodes.py 

fennel[09]$ scp zipcodes.py knerr@lab:cs21/labs/09/.
knerr@lab's password: 
zipcodes.py                      100%   88     0.1KB/s   00:00    

In the first example the user emails the file to knerr. In the second example scp (secure copy) is used to copy the file from the user to knerr's cs21/labs/09 directory. This obviously requires knerr's password.

Run update21, if you haven't already, to create the cs21/labs/09 directory. Then cd into your cs21/labs/09 directory and create the python program for the lab in this directory (handin21 looks for your solutions here).

$ update21
$ cd cs21/labs/09

Zip Code Database

What US city has the zip code 12345? What is the zip code for Truth or Consequences, NM? Your assignment this week is to create a program (zipcodes.py) that allows the user to explore a zip code database.

We have a file, /usr/local/doc/zipcodes.txt, which contains zip code data for most of the United States. Each line of the file contains seven fields separated by commas:

The entry for Swarthmore is shown here:


Your program should prompt the user for either a zip code or at least part of a city name. Depending on what the user types, your program should do one of the following:

Sample run of the program
$ python zipcodes.py 
Welcome to the zip codes database.

This program will look up data based on zip code or city name.
It will display the state boundaries with the locations of
major cities in blue, and the location of the city of interest
in red.

Enter zip code or city: 10901

10901 Suffern, NY Rockland County Pop: 21760

Cities in NY with populations greater than 150000
    Schenectady  Pop: 156661
        Astoria  Pop: 165859
        Yonkers  Pop: 176600
       Flushing  Pop: 214473
        Jamaica  Pop: 216876
       Syracuse  Pop: 231585
  Staten Island  Pop: 443728
      Rochester  Pop: 488602
        Buffalo  Pop: 598640
          Bronx  Pop: 1327690
       New York  Pop: 1529375
       Brooklyn  Pop: 2465326
ny image

Enter zip or city: london

Here are the cities that match your query
03053 Londonderry, NH Rockingham County Pop: 23148
05148 Londonderry, VT Windham County Pop: 792
25126 London, WV Kanawha County Pop: 305
40741 London, KY Laurel County Pop: 36324
40742 London, KY Laurel County Pop: 36324
40743 London, KY Laurel County Pop: 36324
40744 London, KY Laurel County Pop: 36324
40745 London, KY Laurel County Pop: 36324
43140 London, OH Madison County Pop: 22135
45647 Londonderry, OH Ross County Pop: 1889
61544 London Mills, IL Fulton County Pop: 706
72847 London, AR Pope County Pop: 2631
76854 London, TX Kimble County Pop: 322

Enter zip or city: 76854

76854 London, TX Kimble County Pop: 322

Cities in TX with populations greater than 150000
    Brownsville  Pop: 159481
         Laredo  Pop: 190759
         Irving  Pop: 192610
        Garland  Pop: 215555
        Lubbock  Pop: 219631
          Plano  Pop: 225287
 Corpus Christi  Pop: 278829
      Arlington  Pop: 338858
        El Paso  Pop: 634240
     Fort Worth  Pop: 678401
         Austin  Pop: 747080
         Dallas  Pop: 1261999
    San Antonio  Pop: 1308200
        Houston  Pop: 2571090
tx image

Enter zip or city: 145555

Sorry no zip code matching your query was found.

Enter zip or city: pickles

Sorry no cities matching your query were found.

Enter zip or city: vien

Here are the cities that match your query
04360 Vienna, ME Kennebec County Pop: 515
07880 Vienna, NJ Warren County Pop: 0
21869 Vienna, MD Dorchester County Pop: 1039
22180 Vienna, VA Fairfax County Pop: 59531
22181 Vienna, VA Fairfax County Pop: 59531
22182 Vienna, VA Fairfax County Pop: 59531
22183 Vienna, VA Fairfax County Pop: 59531
22184 Vienna, VA Fairfax County Pop: 59531
22185 Vienna, VA Fairfax County Pop: 59531
26105 Vienna, WV Wood County Pop: 11923
31092 Vienna, GA Dooly County Pop: 5680
44473 Vienna, OH Trumbull County Pop: 4215
57271 Vienna, SD Clark County Pop: 463
62995 Vienna, IL Johnson County Pop: 5953
65582 Vienna, MO Maries County Pop: 2241

Enter zip or city: 31092

31092 Vienna, GA Dooly County Pop: 5680

Cities in GA with populations greater than 150000
          Macon  Pop: 163507
  Lawrenceville  Pop: 165459
       Columbus  Pop: 169136
        Decatur  Pop: 176163
        Augusta  Pop: 198378
       Savannah  Pop: 218659
       Marietta  Pop: 302216
        Atlanta  Pop: 673017
ga image

Enter zip or city: <Enter>

Requirements and Tips

You should practice good top-down design, incrementally implement and test your solution, and document your code with comments. We encourage you to finish the text part of this program before you add the graphics. This makes testing easier and faster. Once you have the text portion working, then add the graphics. While much of the design is up to you, your program should follow the guidelines given below.

  1. When you read the data from zipcodes.txt, store the data in a python dictionary, with the key being the zip code, and the value being a python list containing the other data for the zip code:
    1. latitude
    2. longitude
    3. city name
    4. county name
    5. state
    6. population
  2. It is best to store the zip codes as strings. Some zip codes begin with a zero, and python removes leading zeros from integers.
  3. Some of the data (latitude, longitude, population) will need to be converted from strings to floats or integers.
  4. When you prompt the user to enter a location, the user can enter either a zip code or a city (or part of a city). Your program must determine if the user input looks like a zip code, a city, or garbage, and do the right thing (Hint: use str methods like isdigit() and/or isalpha()).
  5. If you cannot find an entry for a particular city or zip code, inform the user that you cannot find that location and prompt them to enter another location.
  6. For reporting all cities that match a given name, you should sort the results by zip code. The entire data set will be stored in a dictionary. You will need to create a list of all the zip codes that have city names that match the requested name. Then sort that list using one of the methods we discussed in class (such as selection sort). You may not use python's built in sort method. Then use the sorted list to look up the data in the dictionary and report the matching cities in sorted order.
  7. For reporting the biggest cities in the state, use some threshold population (e.g., 150000) and print information about all cities in the state with populations above the threshold. Most big cities have more than one zip code. Note in the examples above that the big cities are listed only once, even if they have multiple zip codes.
  8. To plot the state boundaries we have written some functions to create the graphics window already scaled to the appropriate dimensions for the given state. You should be able to use the code below to create the graphics window with the state boundary already drawn. Your program must then draw points for all zip codes (using the longitude as the x coordinate and the lattitude as the y coordinate). You should highlight the user-specified zip code by making it red and highlight all of the points associated with the largest cities by making them blue.
    from boundaries import *
    state = "VA"
    w = getStateGraphWin(state)
    if w != None:
      # plot all zip codes for this state here
      # use points based on the longitude and lattitude for each zip code

Missing Data

The zip code data file we have provided you is by no means complete. Some zip codes were missing or did not have lat/long data and were removed. For some cities, we did not have population data, so we set the population arbitrarily to 0. If your favorite US city or hometown in the US is missing and you know all the info (zip code, city name, county name, state, lat, long, and population), let us know and we will be happy to add a few cities, but we are not trying to maintain a comprehensive list.

Optional Extension

This is not required and should only be attempted after you have completed the lab. When reporting the largest cities in a state, list them in sorted order by population.


Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.