Lab 14: Bus mapping
In this lab, you’ll make a Bloomington bus map that updates in real time.
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://bloomington.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://bloomington.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) (make-posn -86.5272 39.15055) (make-posn -86.49569 39.15507) (make-posn -86.48167 39.17184) (make-posn -86.59177 39.16332) (make-posn -86.55335 39.18318) (make-posn -86.47729 39.16422) (make-posn -86.51201 39.17162) (make-posn -86.51638 39.17156) (make-posn -86.49038 39.15782) (make-posn -86.5503 39.16526) (make-posn -86.5235 39.17134) (make-posn -86.56342 39.15484) (make-posn -86.53346 39.16404) (make-posn -86.51655 39.16581) (make-posn -86.49082 39.15452) (make-posn -86.53774 39.18613) (make-posn -86.54202 39.17886) (make-posn -86.52083 39.15037) (make-posn -86.53324 39.16425) (make-posn -86.53633 39.16023) (make-posn -86.53523 39.17327) (make-posn -86.50841 39.12392) (make-posn -86.5334 39.16417)))
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 same area as circumscribed by bloomington-view from Problem set 9: Plotting functions.
You can draw each bus differently depending on its direction (“heading”) 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.