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.
Compare the two snapshots closely. They were taken a few minutes apart. 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 ; web-address : String ; The location of the latest bus data (define web-address "https://www.busgenius.com/api/v1/get_updated_data/10?routes=[195,196,197,198,199,200,201,202,203,206,242]&updateAnnouncements=true")
(read-json/web web-address)
(read-json/file "buses1.json")
(read-json/web "https://cs.indiana.edu/classes/c211/buses1.json")
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.
Give two examples of parts that are JSONs.
Give two examples of parts that are Members rather than JSONs.
Give two examples of parts that are neither JSONs nor Members.
; parse : Anything -> JSON ; Ignore the input. Retrieve bus data. (define (parse whatever) (read-json/web web-address))
(define iub-view (make-world -86.536 -86.496 39.16 39.19))
(define sample-bus-posns (list (make-posn -86.5268283 39.1798933) (make-posn -86.5220229 39.1631958) (make-posn -86.5013583 39.1760933) (make-posn -86.52699 39.1802317) (make-posn -86.5162083 39.1680917) (make-posn -86.5266717 39.1643233) (make-posn -86.518595 39.1686967) (make-posn -86.5161778 39.1684192) (make-posn -86.527125 39.1809483) (make-posn -86.5269733 39.1800917) (make-posn -86.5169983 39.1839933) (make-posn -86.5212888 39.1716147) (make-posn -86.5261614 39.1790335) (make-posn -86.5236612 39.1763609) (make-posn -86.5200923 39.1642786) (make-posn -86.5167244 39.1716118) (make-posn -86.5270583 39.1805333)))
Note how the result is not just any JSON, but an object whose members include a Member whose name is "buses".
Moreover, note how the value of this Member is not just any JSON, but a list of objects.
Finally, note how each object in this list contains two Members of the form (make-member "lng" 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 lng (longitude) coordinate in the input should become the x coordinate in the output, and the lat (latitude) coordinate in the input should become the y coordinate in the output.
Instead of first or rest, use the function lookup from Exercise 8, and the built-in function map.
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 same area as circumscribed by iub-view above.
You can draw each bus differently depending on its direction (smoothed_heading) or route.
You can show a trail of where the bus has been.
You can reveal more information by mouse or keyboard interaction.