This repository contains the source code for an interactive app showing live location of public modes of transportation (trams and buses) for city of Wroclaw, Poland.
The app is build using R and Shiny web framework (with shinydashboard package), utilizing Leaflet maps with custom tile from [Mapbox] (http://www.mapbox.com).
I took the inspiration from two other interactive apps from RStudio folks:
- Twin Cities Buses (source code: https://github.com/rstudio/shiny-examples/tree/master/086-bus-dashboard)
- SuperZip example (source code: https://github.com/rstudio/shiny-examples/tree/master/063-superzip-example)
Publicly available data from Municipal Transport Company offering (not so straighforward) API for vehicle tracking.
The data is fetched from a live feed which gathers GPS location from all vehicles around Wroclaw area.
Example POST request takes the following form (where '104' is tram line number):
curl --data "busList[bus][]=104" http://pasazer.mpk.wroc.pl/position.php
which gives the following array with coordinates (y = longitude , x = latitude) and unique vehicle identifier (k).
{name: "104", type:"bus", y:16.948914, x:51.15738, k:"8667099"}
In R we can use RCurl
and jsonlite
packages to get the same output
library(RCurl)
library(jsonlite)
busline <- 104
names(busline) <- "busList[bus][]"
dat <- postForm("http://pasazer.mpk.wroc.pl/position.php", .params = busline)
fromJSON(dat)
name type y x k
1 104 bus 16.94891 51.15739 8667099
2 104 bus 17.01786 51.11339 8667137
3 104 bus 16.95299 51.14373 8667158
Online version is now up and running here: https://holek10.shinyapps.io/PublicTransport/
To run the app locally you should install required packages: shiny, shinydashboard, leaflet, RCurl, jsonlite and XML in R, for example:
if (!require('shiny')) install.packages("shiny")
and use the function runGithub()
with specified repository name under my username:
shiny::runGitHub("PublicTransport", "holek10")
Once the app is loaded select bus or tram line for the left hand side to see the real-time position on the map.
You can zoom-in the map and/or adjust refresh interval.
The app is provided for free under GNU General Public License