Lab 5: Maps & More Maps

In this lab, you’ll be using everything you’ve learned about tables (and some of what you’ve learned about lists) to create a data visualization tool. Specifically, you’ll use a dataset of active Providence business licenses published by the Rhode Island government to mark locations on a map.

Learning Objective

A key goal of this lab is to give you practice breaking large problems into separate tasks, each of which gets handled with its own collection of functions, expressions, and operators. As you work on the problems, practice writing out the lists of tasks and using those to guide your work.

Part 1: Making a Simple Map

Question 1.1: Loading the data

Before you start working with the data, take a look at the Providence Business License dataset. The first sheet (inside the file) is the original dataset, and the second sheet is a pruned version with only the columns you’ll need for this lab. Load the pruned sheet into Pyret by pasting the following code into the definitions window:

include shared-gdrive("dcic-2021", "1wyQZj_L0qqV9Ekgr9au6RX2iqt2Ga8Ep")
include gdrive-sheets
import data-source as ds
include shared-gdrive("stencil.arr", "1W61RYJDms4y-1tP_oQ9igydaYr-nmHCr")
include image-structs
import lists as L

##############
# SETUP CODE #
##############

ssid = "18eKc_o-eZ8TWno6ePYkcMGKhx-Y2Fe8LEERqV5gClaU"
data-sheet = load-spreadsheet(ssid)
pvd-businesses = 
  load-table: license-id, name, extended-hours, hotel, parking, 
    restaurant, food-and-liquor, food-dispenser, lat, long
    source: data-sheet.sheet-by-name("Licenses_Pruned", true)
    sanitize license-id using ds.string-sanitizer
    sanitize name using ds.string-sanitizer
    sanitize extended-hours using ds.bool-sanitizer
    sanitize hotel using ds.bool-sanitizer
    sanitize parking using ds.bool-sanitizer
    sanitize restaurant using ds.bool-sanitizer
    sanitize food-and-liquor using ds.bool-sanitizer
    sanitize food-dispenser using ds.bool-sanitizer
    sanitize lat using ds.num-sanitizer
    sanitize long using ds.num-sanitizer
  end

Question 1.2: Generate a Map

We have provided you with a function table-to-map(t :: Table) -> Image, which takes in a table and plots rows as dots on a map of Providence, based on their "x", "y", and "color" columns. Treat table-to-map like a built-in function; you can call it just like you called filter-by or any other built-in function in previous labs.

To use table-to-map to map the locations of the businesses listed in pvd-businesses, you will need to convert the longitude and latitude values in the table to x- and y-coordinate values. Only locations that fit within the bounds of the Providence map can be included (table-to-map will account for this automatically). We’ve defined the following values for you in the stencil.arr file that the setup code includes:

  • LAT-MIN - the minimum latitude that fits the map
  • LAT-MAX - the maximum latitude that fits the map
  • LON-MIN - the minimum longitude that fits the map
  • LON-MAX - the maximum longitude that fits the map
  • HEIGHT - the height of the map
  • WIDTH - the width of the map

Try typing LAT-MIN or any of these values in the interactions window, you should see they are predefined values that you can use in your functions.

Use these values to write functions that scale latitudes to y values and longitudes to x values relative to the map dimensions. You must proportionally scale the businesses' (long, lat) coordinates to (x, y) coordinates in order for the rows to be read by the table-to-map function correctly.

To make things easier for you, we've included scaling formulas!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

TASK: Use these scaling functions to add columns named x and y to the pvd-businesses table with these scaled values.

TASK: Add a column labeled color and set each row’s color to "black." After adding these columns, the computed table can then be used as an input to table-to-map.

Complete the tasks and pass the transformed table into table-to-map. The output should look something like this:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


CHECKPOINT: Call over a TA once you reach this point.


Part 2: Analyzing the Data

Now that you have a map of Providence (albeit not a very pretty one), we need to put it to good use. Your friend Zach has taken you up on your invitation to visit and has just arrived in Providence. He wants to know what he should do while he's here!

Question 2.1: Generate a tourist map

Write a function generate-tourist-map(needs-hotel :: Boolean, has-car :: Boolean, stays-up-late :: Boolean, eats-out :: Boolean, businesses :: Table) -> Image that takes in a series of Booleans and the original pvd-businesses table and returns a map marked with the places that Zach might want to visit.

  • needs-hotel: If this Boolean is true, Zach needs a hotel to stay at! Mark all of the inns on the map with a color of your choice.
  • has-car: If true, Zach has decided to drive and will need to find places with parking. Mark all places with parking with a second color.
  • stays-up-late: If true, Zach is ready for a night out! Mark all places with extended hours (Look at the column entitled extended-hours) with a third color.
  • eats-out: If true, Zach has given up on cooking for himself and wants to eat out. Mark all eateries with a fourth color.

Remember that your generate-tourist-map function should take in the original pvd-businesses table, not the modified one made in Part 1.

If a place has more than one relevant characteristic, it should be colored according to the characteristic that's most important to Zach. Any non-relevant places should be colored black. Starting with the most important characteristic, Zach cares first about hotels, then eating out, then places that have parking, and lastly, places that stay open late.

Depending on the values of the parameters (needs-hotel, has-car, stays-up-late, eats-out), the computed colors for places will be different. Think about how you can accomplish this behavior with a nested function.

NOTE: Look at the list of predefined colors in the Pyret Documentation to see what options you have.


CHECKPOINT: Call over a TA once you reach this point.


Part 3: Telescopes!

It turns out that your space-enthusiast friend Zach wants nothing more than to spot UFOs. In order to do so, Zach needs a telescope. However, before he can buy a telescope, Zach would like a doughnut (everyone gets hungry!). Good thing Zach is a big advocate for local businesses and wants to visit all the local doughnut shops in Providence!

How do we distinguish local shops from those in national chains? One way is to find all the doughnut shops and see which ones have names that appear many times in the data-local shops will have fewer entries in the list. A frequency-bar chart would show us this answer visually, but what if we had to compute this information from a list of names of doughnut shops, rather than read it off of a frequency-bar-chart?

Question 3.1

Here’s a collection of operations on tables and lists (assuming we imported lists as L). Write a function using operations from the following collection that produces a list of names of local doughnut shops. We'll consider a doughnut shop to be local if it has no more than 2 locations in the pvd-businesses table.

Write a function to determine whether a row describes a doughnut shop (meaning the shop has "doughnut" or "donut" in the name), and then filter the pvd-businesses table to contain only those doughnut shops. After filtering for only doughnut shops, find the chain that isn't local (hint hint ;) :P ). Note that you can do this by eye. Then, remove all shops from this chain from the table using your list functions.

Something to keep in mind: There are two spellings for the tasty dessert of interest: donut and doughnut. Be aware of this when you are looking for businesses.

  • L.member
  • L.distinct
  • L.map
  • L.filter
  • L.length
  • get-column (tables)
  • filter-with (tables)
  • order-by (tables)

Remember to write out your list of tasks first and use that to guide your work.

Question 3.2

Given the list of names of local doughnut shops, generate a map that highlights all doughnut shops, marking local stores with a red dot and shops from the non-local chain (from 3.1) with a black dot.


CHECKPOINT: Call over a TA once you reach this point.


Part 4: A Different Kind of Map

Now that Zach got his doughnut, he is ready to purchase his telescope. Unsatisfied with how few local telescope shops there are in Providence, Zach decides to open his own store: "Comet Me, Store for Telescopes!" After conducting a survey of existing options, he's created a list of the most popular telescope options. With the telescopes, Zach can finally spot UFOs!

Question 4.1

Write a function create-ufo(ufo-colors :: List<String>) -> List<Image> that takes in a list of strings representing valid colors and produces a list of UFOs of these colors. Each UFO should have this shape, but with its index-specific color:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Each UFO should also have a randomly generated size, such that its radius is between 15-35 units.

HINT: Take a look at the List operations from part 3.1.

Question 4.2

Zach has discovered that the ideal UFO size has a radius within 20-30. He wants to discard all of the UFO images that are outside of that range and make display signs out of the rest.

Write a function generate-ufo-signs(ufo-images :: List<Image>) -> List<Image> that takes in the output list from create-ufo. This function should remove all of the UFOs that lie outside of the size range and put the remaining UFOs on rectangular backgrounds. This is what an output list item should look like:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


CHECKPOINT: Call over a TA once you reach this point.


Success!

Zach finally saw his first UFOs! Hooray! As a gift, he has provided you with a $5 gift card for his telescope store! Business at Zach's "Comet Me, Store for Telescopes!" is booming, all thanks to you.


Brown University CSCI 0111 (Fall 2020)
Do you have feedback? Fill out this form.