8.6.0.14

Lab 14: Bus mapping

In this lab, you’ll make an IU bus map that updates in real time. You will use a data source created here at our school by Prof Mehmet Dalkilic and his students in collaboration with IU Campus Bus Service.

Exercise 1. Take a look at the data using your Web browser:
(If the server is down, or the time is late so there are no buses, use this static snapshot from the past.) This data is posted in the recursive format JSON. If you look closely, you can see the longitude and latitude of each bus, its direction, and even its route.

Refresh the page, and you should see the coordinates change slightly. What is one thing that changed? What is one thing that didn’t change?

Exercise 2. To retrieve data in the recursive format JSON, we provide a small library. Install it as follows:
  • Open DrRacket

  • In the File menu, choose “Install Package”

  • In the dialog, enter htdp-json

  • Press the button that says “Install” or “Update”

  • Wait a couple of minutes for the message “post-installing collections”

Now put this in your definitions:
(require 2htdp/json)
 
; read-json/web : String -> JSON
; Retrieves the remote file at the given URL and returns JSON data
 
; read-json/file : String -> JSON
; Retrieves the local file with the given name and returns JSON data
Hit the “Run” button in DrRacket. Then, in the Interactions Window, enter
(read-json/web "http://iub.doublemap.com/map/v2/buses")
and examine the result. Does it seem to contain longitudes and latitudes? It should. If not, either download this static snapshot from the past into the folder where you are completing this assignment and enter
(read-json/file "buses")
or, without downloading, try
(read-json/web "https://cs.indiana.edu/classes/c211/buses")
and examine the result. Does it seem to contain longitudes and latitudes? It should. If not, get help.

Exercise 3. Examine the result from Exercise 2. How does it correspond to what your Web browser showed in Exercise 1?

As the signatures of read-json/web and read-json/file indicate, this result should be a JSON. Here is the data definition for a JSON:
; A JSON is one of:
; - String
; - Number
; - [ListOf JSON]
; - (make-object [ListOf Member])
(define-struct object [members])
 
; A Member is (make-member String JSON)
(define-struct member [name value])

The structures object and member shown above are already defined by the 2htdp/json library, so your code should not duplicate those structure definitions.

Exercise 4. In the result from Exercise 2, what parts are JSONs, and what parts are not JSONs? Give two examples of parts that are JSONs, and give two examples of parts that are not JSONs.

Exercise 5. To make it easier to retrieve JSON data, define a function called parse like this:
; parse : Anything -> JSON
; Ignore the input. Retrieve bus data.
(define (parse whatever)
  (read-json/web "http://iub.doublemap.com/map/v2/buses"))
Try this function out in the Interactions Window. Does it produce a JSON that contains longitudes and latitudes? If not, use the static snapshot as described above.

Exercise 6. Design a function draw that takes a [ListOf Posn] representing bus locations and creates an Image showing where the buses are. Use draw-lop, cartesian->screen, and bloomington-view from Problem set 9: Plotting functions.

(define sample-bus-posns
  (list
   (make-posn -86.52805 39.18395)
   (make-posn -86.52836 39.18398)
   (make-posn -86.50961 39.17321)
   (make-posn -86.52168 39.16847)
   (make-posn -86.51697 39.18409)
   (make-posn -86.52782 39.1801)))

Exercise 7. Use your function to draw sample-bus-posns on a map.

Exercise 8. Examine the result of the parse function. Note how the result is not just any JSON, but a list of objects. Moreover, each object contains two Members of the form (make-member "lon" Number) and (make-member "lat" Number). What other Members are there?

To help retrieve the longitude and latitude, design a function lookup that looks up a given String name in a given [ListOf Member] and returns the corresponding JSON value. If there is no Member whose name is the given String, there should be an error, which you should test using check-error.

Exercise 9. Design a function project that takes such a JSON (i.e., not just any JSON, but one of the form just described) as input and returns a [ListOf Posn]. The Earth coordinates in each input object should become an output Posn: the longitude coordinate in the input should become the x coordinate in the output, and the latitude coordinate in the input should become the y coordinate in the output.

Use the function lookup from Exercise 8. You’ll probably want to use map also.

Exercise 10. Combine the functions parse, draw, and project to make a big-bang animation of where the buses are in real time. As usual when using big-bang, the central question is, what is a world?

Watch out! Don’t break the Internet! Because parse retrieves the latest data, it should not run more often than every 3 seconds. So you should only use parse in your on-tick function with the tick duration set to 3 or greater, like this:
(big-bang ... [on-tick ...parse... 5] ...)
In particular, don’t use parse in your to-draw function.

For extra fun, add more information to your map.
  • You can show a street map or a route map in the background. To start, you can download this map image provided by OpenStreetMap. It shows the following area: west to longitude -86.5352, east to longitude -86.5000, north to latitude 39.1863, south to latitude 39.1600.

  • You can draw each bus differently depending on its direction or route. The data format is documented online. Search for "v2", especially "v2/buses" to start.

  • You can show a trail of where the bus has been.

  • You can reveal more information by mouse or keyboard interaction.