Skip to content

Commit

Permalink
add gtfsr example to README
Browse files Browse the repository at this point in the history
  • Loading branch information
asiripanich committed Nov 19, 2020
1 parent 7812d1c commit 8a531be
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 7 deletions.
47 changes: 44 additions & 3 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ knitr::opts_chunk$set(
# tfnswapi

<!-- badges: start -->
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
[![R build status](https://github.com/asiripanich/tfnswapi/workflows/R-CMD-check/badge.svg)](https://github.com/asiripanich/tfnswapi/actions)
<!-- badges: end -->

Expand All @@ -34,9 +33,11 @@ You can install the `tfnswapi` package from [GitHub](https://github.com/asiripan
install.packages("tfnswapi")
```

## Example
## Examples

```{r example}
### Carpark API

```{r carpark-example, dpi=300}
library(tfnswapi)
# See what facilities are available
Expand Down Expand Up @@ -68,3 +69,43 @@ ggplot(data = tidied_carpark, aes(x = zone_id)) +
y = "# Spots"
)
```
### GTFS Realtime API

See TfNSW GTFS Realtime documentation [here](https://opendata.transport.nsw.gov.au/sites/default/files/TfNSW_GTFS_Realtime_Buses_Technical_Doc.pdf).

```{r gtfsr-example, dpi=300, message=FALSE}
library(tfnswapi)
library(ggplot2)
library(ggmap)
library(sf)
# remove `if (FALSE)` to register your own API key
if (FALSE) {
tfnswapi_register("<your-api-key>")
}
bus_response = tfnswapi_get("gtfs/vehiclepos/buses")
bus_position_table = bus_response$content$entity$vehicle$position
bus_position_table =
bus_position_table %>%
sf::st_as_sf(coords = c("longitude", "latitude")) %>%
sf::st_set_crs(value = sf::st_crs("WGS84"))
# Convert momentary speed measured by the vehicle in meters per second to
# kilometers per hour
bus_position_table$speed = 3.6 * bus_position_table$speed
# get base map
sydney_bbox = sf::st_geometry(bus_position_table) %>% sf::st_bbox()
names(sydney_bbox) <- c("left", "bottom", "right", "top")
sydney_map = get_stamenmap(sydney_bbox, maptype = "toner-lite", messaging = FALSE)
ggmap(sydney_map) +
coord_sf(crs = sf::st_crs("WGS84")) +
geom_sf(data = bus_position_table, aes(color = speed), inherit.aes = FALSE) +
labs(title = "Realtime positions of buses in Sydney, Australia",
color = "Speed (km/hr)",
subtitle = bus_response$date)
```

50 changes: 46 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

<!-- badges: start -->

[![Lifecycle:
experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
[![R build
status](https://github.com/asiripanich/tfnswapi/workflows/R-CMD-check/badge.svg)](https://github.com/asiripanich/tfnswapi/actions)
<!-- badges: end -->
Expand All @@ -29,7 +27,9 @@ You can install the `tfnswapi` package from
install.packages("tfnswapi")
```

## Example
## Examples

### Carpark API

``` r
library(tfnswapi)
Expand Down Expand Up @@ -76,4 +76,46 @@ ggplot(data = tidied_carpark, aes(x = zone_id)) +
)
```

<img src="man/figures/README-example-1.png" width="100%" />
<img src="man/figures/README-carpark-example-1.png" width="100%" />
\#\#\# GTFS Realtime API

See TfNSW GTFS Realtime documentation
[here](https://opendata.transport.nsw.gov.au/sites/default/files/TfNSW_GTFS_Realtime_Buses_Technical_Doc.pdf).

``` r
library(tfnswapi)
library(ggplot2)
library(ggmap)
library(sf)

# remove `if (FALSE)` to register your own API key
if (FALSE) {
tfnswapi_register("<your-api-key>")
}

bus_response = tfnswapi_get("gtfs/vehiclepos/buses")
bus_position_table = bus_response$content$entity$vehicle$position

bus_position_table =
bus_position_table %>%
sf::st_as_sf(coords = c("longitude", "latitude")) %>%
sf::st_set_crs(value = sf::st_crs("WGS84"))

# Convert momentary speed measured by the vehicle in meters per second to
# kilometers per hour
bus_position_table$speed = 3.6 * bus_position_table$speed

# get base map
sydney_bbox = sf::st_geometry(bus_position_table) %>% sf::st_bbox()
names(sydney_bbox) <- c("left", "bottom", "right", "top")
sydney_map = get_stamenmap(sydney_bbox, maptype = "toner-lite", messaging = FALSE)

ggmap(sydney_map) +
coord_sf(crs = sf::st_crs("WGS84")) +
geom_sf(data = bus_position_table, aes(color = speed), inherit.aes = FALSE) +
labs(title = "Realtime positions of buses in Sydney, Australia",
color = "Speed (km/hr)",
subtitle = bus_response$date)
```

<img src="man/figures/README-gtfsr-example-1.png" width="100%" />

0 comments on commit 8a531be

Please sign in to comment.