Homework Assignment #6 - Mapping Statistical Data



Homework 6 Assigned: October 6, 2014Due: October 13, 2014, 6:00 PM00CS-1004, Introduction to Programming for Non-majors, A-term 201400CS-1004, Introduction to Programming for Non-majors, A-term 2014 Homework 6 — Mapping Statistical DataObjectivesLearn to work with CSV filesPractice working with dictionariesPlot some statistical data on a mapOverview — Show Crime Data and Real Estate Prices on a MapWrite a program that reads two sets of public data from Sacramento, California — one regarding crime statistics and one regarding real estate prices. Plot the locations of crimes and properties of the real estate transactions on a map of the Sacramento region.Both sets of data are in csv format — that is, comma-separated-values. This is a common format for exchanging data among spreadsheet applicationss such as Excel. Each data set consists of a header row that defines what the columns mean, followed by “records” containing actual data items. Each record corresponds to a row of a spreadsheet; it is represented in the csv file as a string containing a number of “fields,” the fields being separated from each other by commas. Each field contains a piece of information of the category defined by the corresponding position in the header row.One of the data sets for this assignment shows real estate purchases recorded in the Sacramento area for a particular week in May 2008. Each record includes the address, type, size, price, latitude, and longitude for one specific property. There are about 985 transactions in this data set. The second data set lists crime records from the Sacramento police for the month of January 2006. Each record shows the location, time and date, type of crime, latitude and longitude, and other information. There are about 7585 crimes recorded in this data set. Your task is to read each data set separately and then plot each record as a little circle at the correct latitude and longitude on the Sacramento map. Circles should be color-coded by price for real estate transactions and by type of crime for crime records.The data sets and map can be found here:– on a MapThe graphics package of our textbook includes a method for displaying images. The file SacramentoMap.png listed above is such an image. Open it in a browser and see what it looks like.This particular map, obtained from Google Maps, is 707 pixels wide and 774 pixel high. It depicts an area from 38.24 degrees to 39.03 degrees N latitude, and from 121.56 degrees W to 120.60 degrees W longitude. It is easy to display a map image in the graphics package. See the Displaying Images discussion in §4.8.4 of the textbook. You simply need to do something along the following lines in a function:–import graphicsdef initializeMap(mapFile, xPixels, yPixels): win = graphics.GraphWin('Sacramento Area', xPixels, yPixels) win.setCoords(0, 0, xPixels, yPixels) center = graphics.Point(xPixels/2, yPixels/2) image = graphics.Image(center, mapFile) image.draw(win) return winwhere mapfile is the name of the file containing the map image. Create a Python module called HW6_Map.py, and type or paste this code into it. In a wrapper module, test this function to make sure that it works correctly.It will be necessary to convert latitude and longitude into pixel positions for the purpose of drawing little circles on the map. It would seem that win.setCoords() would provide this conversion automatically. However, it apparently does not work in the case of images. Therefore, you need to create a helper function in your HW6_Map.py module:–def LatLongToPixels(latitude, longitude)For extra credit, you may develop this yourself and submit it before the end of the day on Wednesday, October 8. On October 9, a template file will be published for those who need it.Finally, you will need to write a function in HW6_Map.py to draw the little circles — for example, plotCircle() that takes latitudes and longitudes and places the center of a circle at that point on the map. The assignment below requires different colors. Therefore, plotCircle() should also have a color parameter.Data for this AssignmentFor this assignment, you need to read each csv file. For each record in a file, plot its location on the map of the Sacramento region according to its latitude and longitude. A colored circle of 4-5 pixels in radius is suggested.Reading csv DataTo read csv data, first open the file using open(filename, ...). Next, you have two options — create a csv.reader() object or create a csv.DictReader() object. The difference is in the way the information is presented. The csv.reader() object will read the file row-by-row and return a list of strings for each row. Each string within the list represents a field in the csv file. The following shows an outline of reading and processing a csv file using this method.rdr = csv.reader(file)for row in rdr:#row is a list of strings#data within row indexed by row[i] where i is integerThat is, you have to know which “column” to access by column number. The column number is then built into your program as a constant.A better alternative is to create a csv.DictReader() object. This will also read the csv file row-by-row, but it returns a little dictionary for each row. Therefore, you will be able to refer to the separate fields by name. For example,dRdr = csv.DictReader(file, ...):for row in dRdr:#row is a dictionary#data within row is accessed by row['key'], where 'key'#is one of the fields in the header rowThe advantage of the second approach is that your program does not have to know which particular column contains which particular data, because it is determined at run time from the header row of the csv file.Real Estate DataThe step of this part of the assignment is to create a map of real estate purchases. Plot each transaction onto the map in the form of a colored circle centered at the latitude and longitude of the property. Adjust the colors based on purchase price. For example, you might assign colors according to the following table:–< $100,000Red$100,000–200,000Orange$200,000–300,000Yellow$300,000-400,000Green$400,000–500,000Blue$500,000-600,000Indigo>$600,000VioletYou may choose a different color assignment that helps to convey the information. Superimpose a key in the lower right of the map showing which colors correspond to which price ranges. Capture an image of your map and submit it as RealEstate.png or RealEstate.jpg. Crime DataThe second step of this part of the assignment is to create a map of crimes. Plot each crime report on the Sacramento map with a small circle. You may choose the colors to correspond to the types of crimes — e.g., assaults, burglaries, thefts, etc. Also, create a key showing which colors go with which kinds of crimes. Superimpose this key on the lower right corner of the map. Capture an image of the map and submit it as CrimeData.png or CrimeData.jpg.Submit the module containing the functions that create the maps as HW6_Data.py.Filtering dataMany times, an analyst of csv data needs to filter it to select a subset of records meeting certain criteria. To do so, the program needs to go through all of the rows of the data set, and select the subset according to the criteria, and put the selected records into a separate list. It can then process this list to extract other information.For this part of the assignment, write a function filter(longitude, latitude, crimes, distance)that selects only the crimes occurring within a specified distance of a particular location. The location is specified by latitude and longitude, crimes is list representing a (subset of) the types of crimes in the data set, and distance is in miles. Also create a function called report() that invokes filter and generates a text file report of the filtered data. An example output might be:–Sample Investigations that you can run with this functionHome in downtown SacramentoNote that all of these crimes are within ? mile of the given long/lat position>>> report (-121.434, 38.632, listOfFilteredRecords, 0.25)4 459 PC BURGLARY RESIDENCE1 BATTERY - I RPT1 MISCELLANEOUS I RPT (ZMISC)1 TRESPASS OR PROWLER- I RPT2 NARCOTICS SUSP/EVID/ACT- I RPT2 243(E)1 BATTERY NONCOHAB SPOUS5 FOUND PROPERTY - I RPT1 11357(B)HS POSS -28.5GR MARIJ1 487(A) GRAND THEFT-INSIDE1 SUSP PERS-NO CRIME - I RPT6 TOWED/STORED VEH-14602.62 10851(A)VC TAKE VEH W/O OWNER1 O/S AGENCY -ASSISTANCE- I RPT1 23152(A) VC DUI-ALCOHOL/DRUGS1 TRAFFIC-ACCIDENT INJURY1 12025(A)1)CONCLD GUN VEH/FEL2 TOWED/STORED VEHICLE1 3056 PAROLE VIO - I RPT1 484 PETTY THEFT/LICENSE PLATE1 TRAFFIC - I RPT1 484G(B) PC ACCESS CARD FRAUD2 MISSING PERSON1 451(D) PC ARSON OF PROPERTYHome in Wealthy SuburbsNote that you have to go 11 miles to find a close crime in the data set>>> report (-121.219142, 38.732096, listOfFilteredRecords, 11)1 VANDALISM - I RPT1 5150 WI DANGER SELF/OTHERS1 TRAFFIC - I RPT1 459 PC BURGLARY VEHICLE1 594(B)(1)PC VANDALISM +$4001 594(B)(2)(A) VANDALISM/ -$4001 242 PC BATTERY CIVILIAN43 MISSING PERSON1 1203.2 PC VIOLATION OF PROBATI1 CHILD WELFARE - I RPTFor this part of the assignment, submit a module named HW6_Filter.py that contains the filter() and report() functions. Also, submit a text file containing a command line and the report for a selected, non-trivial location.For this part of the assignment, you will need a helper function to calculate the distance between two points specified by their latitudes and longitudes. You may copy and paste the following function for that purpose. (Note that the line that calculates the variable a is a very long line that spills over two lines in this document.)# toRad(x): """Convert given numeric degrees into radians""" return x*math.pi/180.0;def distanceMiles(lat1, long1, lat2, long2): """ Uses Haversine formula to calculate the great-circle distance between two points on the earth's surface expressed in (lat,long) coordinates """ R = 3958.7558657440545 dLat = toRad(lat2-lat1) dLon = toRad(long2-long1) lat1 = toRad(lat1) lat2 = toRad(lat2)a = math.sin(dLat/2) * math.sin(dLat/2) + math.sin(dLon/2) * math.sin(dLon/2) * math.cos(lat1) * math.cos(lat2) c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) d = R * c return (d)Extra creditFor extra credit, make a simple interactive tool that displays the map of the Sacramento region and asks the user to click on a location and then a distance (indicated by a second click). Using the filter function from above, plot all of the crimes within that distance on the map. For this purpose, you probably need the helper functiondef PixelsToLatLong(xPosition, yPosition)that returns the latitude and longitude corresponding to the pixel addresses of the mouse click.Experiment with other interactive applications of the data and the map and describe how to use them in your README file (see below).SubmissionZip together all your files into a single zip file named HW6-teamName.zip. Also include in your zip file the screenshots of the crime and real estate price maps, the output of the filter, and a file called README.txt explaining anything that the graders need to know to grade your submission.There are two extra credit options:–Submitting an your own version of HW6_Map.py containing the helper function def LatLongToPixels(latitude, longitude). This must be submitted by Wednesday evening before midnight. In addition, this file must be included in your zip file for the final submission, even if it is identical to the previously submitted version.An interactive tool for plotting data on the map in response to mouse clicks.GradingThis project is to be carried out in two-person teams. Both team members will receive the same project grade.The basic assignment is worth 100 points, and each extra credit option is worth up to 25 pointsPoints for the main assignment are allocated as follows:–HW6_Map.py (30 points):–Ability to display the Sacramento map using the graphics.Image() method from the textbook – 10 pointsCorrectly drawing a colored circle as a specified latitude and longitude – 10 pointsAbility to capture an image of a map from a graphics window for submission – 10 pointsHW6_Data.py (30 points):–Ability to open csv file, read and process it row-by-row. May use csv.reader() or csv.DictReader() – 10 pointsAbility to extract fields from a data set and use for some purpose – 10 pointsCorrectly plot crime statistics and real estate transactions on Sacramento map and export map for submission to Turnin – 10 pointsHW6_Filter.py (20 points):–Ability to write a filter function to extract data from data set – 10 pointsReport of crimes from a selected location (determined by graders) – 10 pointsREADME.txt file explaining how everything works and how graders should run your functions. – 20 points.PenaltiesPenalty for any function longer than 25 lines – 10 points per function ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download