본문 바로가기

U of T: Computer Science/First Year: CSC108

CSC108 Assignment 4

CSC108H/A08H Assignment 4

Overview

The purpose of this assignment is to give you practice in a number of areas:

  • Creating, modifying, and extracting data from dictionaries.
  • Working with both image and text files.
  • Modular development and testing.

Part 1: Warm-up Exercises

Before diving into the assignment, take some time to review the concepts you will be using. The following functions involve tasks related to the rest of the assignment, and you may find them useful as starter code (though probably not as helper functions). Save these functions in a file called warmup.py.

Function Name Description
file_to_dictionary(file) Given an open file that contains a unique string and three floating point numbers on each line (separated by colons), return a dictionary that contains the file data with the string as the key and the list of numbers as the value. Here is a sample file.
increment_count(dict, str) Given a dictionary where the keys are strings and the values are integers, and a string key, increment the integer associated with the key if the key is already in the dictionary. Else, add the key to the dictionary with the value 1.
collapse_dictionaries(dict, dict) Given a dictionary where the keys are strings and the values are integers and a dictionary where the keys are the integers from the previous dictionary and the values are tuples of two floating point numbers, produce a new dictionary that results from transitively associating the key from the first dictionary to the sum of the two values from the second dictionary.

For example, given the dictionaries {'a': 9, 'b': 1, 'c': 5} and {1: (3.1, 2.4), 5: (0.3, 1.6), 9: (-1.3, 4.7)}, collapse_dictionaries should return {'a': 3.4, 'b': 5.5, 'c': 1.9}.
draw_line(Picture, int, int) Given a Picture and two integers that represent a coordinate pair (x and y, in that order), return the picture with a blue line drawn from the midpoint of the picture to that coordinate.

Part 2: Geotagging and Canadian Airports

Geotagging is the addition of location data to images, websites, or other electronic media. For example, sites like Google Maps provide methods for linking physical addresses to maps and, beyond that, allow users to make geographically related searches. If you were to ask for pizza parlors near your house or apartment, you could get a map of your surrounding area with your house and the nearest ten establishments marked.

In this assignment, you will be adding the locations of airports to a map of Canada. We are providing a "database" (in this case, a text file) of information about airports, including name and latitude/longitude, and a map of Canada in the Mercator projection. Your task, described in more detail below, is to write functions to read the airport information from a file, relate the lat/long data to the map, and produce maps with various airports tagged. The image below is the result of the program you will write.

Canadian Airports

First, though, a word about the map you will be using. The Mercator projection is convenient for our task because both latitude and longitude lines are straight and equally spaced. If you have the latitude of the top and bottom of the map and longitude of the right and left, you can compute the size of a pixel and translate a lat/long pair into x and y coordinates on the map. For example, the map we will be using is bounded by 75 degrees latitude above and 40.25 degrees latitude below. The map is 354 pixels high, so each pixel is (75 - 40.25) / 354.0 = .0982 degrees latitude.

Of course, this convenience has a price. The Mercator projection distorts distances close the poles, since longitude lines converge at the poles while the projection attempts to keep them equally spaced. For example, this results in Greenland looking as large as Africa, despite being less than a tenth the size. For this reason, most mapping sites use other projections with complex mapping functions.

The Task:

Download the text file of airport data and map of Canada for this project. The map is bounded by 75.0 and 40.25 latitude and 144.0 and 47.25 longitude. These numbers will not result in a perfect fit -- the map is not perfectly to scale. Feel free to experiment and come up with a better fit, but when you submit your file, you must submit code that uses the latitudes and longitudes specified here.

Implement the following functions in airport_plotter.py:

Function Name Description
read_airports(file)

Given an open text file that contains airport data, return two dictionaries, both keyed on the Airport ID (str). The values in the first will be the precise (accurate to the second) latitude and longitude of every airport in the file (stored as a tuple of two floats). The values in the second will be the Status (str) of only the airports that serve an international destination. If a line does not have a Status field, that airport does not serve an international destination.

The file contains data for one airport on each line, and the format of each line is:

Airport ID:Name:Location:Degrees Lat:Minutes Lat:Seconds Lat:Degrees Long:Minutes Long:Seconds Long:Status

The last field, Status, is optional. If it exists, the airport has flights to an international destination -- either just the United States (US) or other countries (INT).

plot_airports(Picture, dict, dict)

Given the map of Canada being used, a dictionary where the keys are airport IDs of type str and the values are a tuple of a float latitude and a float longitude, and a dictionary where the keys are airport IDs that contain only international airports, return the map with all airports in the dictionary marked as three pixel by three pixel squares centered on the location of the airport. Domestic airports should be blue squares. International airports should be red squares.

For example, if one of the airports to plot is an international airport located at pixel (5, 7), the pixel (5, 7) should be colored red, as should all 8 pixels surrounding it.

You may define as many helper functions in airport_plotter.py as you find useful. Make sure to read the Marking Guidelines below: the design you choose and the readability of your code are both worth marks.

Remember to test your code thoroughly. Like in Assignment 3, you are being asked to submit a file test_airport_plotter.py that contains your test module. Every function you write should have at least one test in the test module, and each test should be specific (only testing one or a small number of conditions), unique (not redundant), and well-documented. You will find it useful to create short airport data files (with only 1 or 2 airports for which you know the location) for testing.

Part 3: Connecting Airports with Flights

In the last section, you read location data for a set of airports and tagged a map with them. Now, you will extend this functionality by linking airports that have daily scheduled flights between them. This functionality is very similar to a geotagged search function; for example, a user might state that they live near Toronto and ask for all flights leaving the area. Your code, if provided with relevant data, would produce the map showing where the user may go. The image below is an example of what you will be able to produce.

Flights from Vancouver and Toronto

We are providing a text file that contains two airport names per line. Each line represents a daily flight between the airports named. You will write functions that process this data (placing it in a useful structure) and draw lines on the map between airports connected by flights.

The Task:

Download the text file of selected flight data and implement the following functions in airport_plotter.py:

Function Name Description
read_flights(file) Given an open text file that contains flight data in the format used by daily_flights.txt, return a dictionary with airport IDs of type str as keys and lists of airport IDs as values. For each key, the associated list contains all airports connected to it by daily flights. For this assignment, don't worry about duplicates.
plot_flights(Picture, dict, dict, str) Given the map of Canada being used, a dictionary of coordinates where the keys are airport IDs of type str and the values are a tuple of a float latitude and a float longitude, a dictionary of flights where the keys are airport IDs and the values are a list of destination airport IDs, and an airport ID, return the map with all flights from the requested airport represented as green lines between the source and destination airport.

As in Part 2, you may use as many helper functions as you wish. Style and design count, and we expect each function to have at least one test in the test module.

What to Hand In

Hand in the following files:

  • warmup.py
  • airport_plotter.py
  • test_airport_plotter.py
Student's Solution Teacher's Solution


'U of T: Computer Science > First Year: CSC108' 카테고리의 다른 글

CSC108 Project  (0) 2007.12.16
CSC108 Assignment 3  (0) 2007.12.16
CSC108 Assignment 2  (0) 2007.12.16
CSC108 Assignment 1  (0) 2007.12.16
CSC 108 Course 소개.  (0) 2007.12.16