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.
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?
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”
(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
(read-json/web "http://iub.doublemap.com/map/v2/buses")
(read-json/file "buses")
(read-json/web "https://cs.indiana.edu/classes/c211/buses")
Exercise 3. Examine the result from Exercise 2. How does it correspond to what your Web browser showed in Exercise 1?
; 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.
; parse : Anything -> JSON ; Ignore the input. Retrieve bus data. (define (parse whatever) (read-json/web "http://iub.doublemap.com/map/v2/buses"))
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?
(big-bang ... [on-tick ...parse... 5] ...)
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.