---
title: "Propensity to Cycle Tool Advanced Workshop"
output:
  bookdown::html_document2:
    number_sections: true
vignette: >
  %\VignetteIndexEntry{Propensity to Cycle Tool Advanced Workshop}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
  %\VignetteDepends{tmap} 
bibliography: refs_training.bib
pkgdown:
  as_is: true
  set_null_theme: false
# Reformatted the Rmd doc
editor_options:
  markdown:
    wrap: sentence
---

```{r, eval=FALSE, echo=FALSE}
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[CO,CE]{Propensity to Cycle Tool Training Course}
- \fancyfoot[CO,CE]{For source code and support see github.com/ITSLeeds and github.com/npct}
- \fancyfoot[LE,RO]{\thepage}
output: pdf_document
# output as .R script
knitr::purl("vignettes/pct_training.Rmd")
```

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  out.width = "50%",
  cache = TRUE,
  eval = FALSE
)
```

```{r, eval=FALSE, echo=FALSE}
rbbt::bbt_update_bib(path_rmd = "vignettes/pct_training.Rmd")
```

# Introduction

This guide supports workshops on advanced usage and development of the Propensity to Cycle Tool (PCT).
Beginner and intermediate PCT events focus on using the PCT via the web application hosted at [www.pct.bike](https://www.pct.bike/) and the data provided by the PCT in [QGIS](https://www.qgis.org/en/site/).

The focus here is on analysing cycling potential in the open source statistical programming language R.
We use R because the PCT was developed in, and can be extended with, R code.
Using open source software with a command-line interface reduces barriers to entry, enabling the development of [open access transport models](https://doi.org/10.1016/j.tranpol.2020.06.015) for more citizen-led and participatory transport planning, including integration with the [A/B Street](https://github.com/a-b-street/abstreet) city simulation and editing software [@lovelace_open_2021].

The guide covers:

-   How to download and get data from the PCT
-   How to compare PCT data with cycle infrastructure data to identify gaps in the network
-   How the code underlying the PCT works
-   How to generate new scenarios of cycling uptake

See the marked-up version of this vignette online at the URL [`https://github.com/ITSLeeds/pct/releases/download/0.8.1/Propensity.to.Cycle.Tool.Advanced.Workshop.html`](https://github.com/ITSLeeds/pct/releases/download/0.8.1/Propensity.to.Cycle.Tool.Advanced.Workshop.html).

## Preparation

<!-- There will be 2 courses: -->

<!-- # Prerequisites -->

<!-- This is an advanced training course with specific pre-requisites. -->

<!-- Please only register if you: -->

<!-- - Have experience analysing geographic area and route data of the type used in the PCT -->

<!-- - Have experience using software for geographic data, including working knowledge of the statistical programming language R -->

<!-- - Can bring a laptop that has up-to-date versions of R, RStudio and the necessary R packages installed (see next section) -->

<!-- In addition, if you want to do routing on your computer:  -->

<!-- - Register for a CycleStreets.net API key from: https://www.cyclestreets.net/api/ -->

<!-- If you are new to R this course may not be appropriate for you. -->

If you are new to R, you should install R and RStudio before the course.
For instructions on that, see the download links at [cran.r-project.org](https://cran.r-project.org/) and [RStudio.com](https://www.rstudio.com/products/rstudio/download/#download).

R is a powerful statistical programming language for data science and a wide range of other applications and, like any language, takes time to learn.
To get started we recommend the following free resources:

-   Beginner: A free online course such as those provided by DataCamp or those listed at [education.rstudio.com](https://education.rstudio.com/learn/beginner/)
-   Recommended for people with knowledge of transport and road safety data: getting started with R in the context of road safety data, a tutorial hosted at <https://itsleeds.github.io/rrsrr/>
-   For people interested in geographic data, which is at the heart of the PCT: [Chapter 2 of the open source book *Geocomputation with R*](https://geocompr.robinlovelace.net/spatial-class.html).

If you want to calculate cycle routes from within R, you are recommended to sign-up for a CycleStreets API key.
See [here](https://www.cyclestreets.net/api/apply/) to apply and see [here](https://rpackage.cyclestreets.net/reference/journey.html#details) for instructions on creating a 'environment variable' (recommended for experienced R users only).

It may also be worth taking a read about the PCT if you're not familiar with it before the course starts.

## Prior reading

In addition to computer hardware (a laptop) and software (an up-to-date R set-up and experience using R) pre-requisites, you should have read, or at least have working knowledge of the contents of, the following publications, all of which are freely available online:

-   The original paper on the Propensity to Cycle Tool [@lovelace_propensity_2017]
-   The transport chapter ([12](https://geocompr.robinlovelace.net/transport.html)) in the open source book [*Geocomputation with R*](https://geocompr.robinlovelace.net/) [@lovelace_geocomputation_2019]
-   The vignette that describes the pct R package, which can be found here: <https://itsleeds.github.io/pct/articles/pct.html>

## Prerequisites

**To ensure your computer is ready for the course, you should be able to run the following lines of R code on your computer:**

```{r, eval=FALSE}
install.packages("remotes")
pkgs = c(
  "cyclestreets",
  "mapview",
  "pct",
  "sf",
  "stats19",
  "stplanr",
  "tidyverse",
  "devtools"
)
remotes::install_cran(pkgs)
# remotes::install_github("ITSLeeds/pct")
```

To test your computer is ready to work with PCT data in R, you can also try running the code hosted at <https://raw.githubusercontent.com/ITSLeeds/pct/master/inst/test-setup.R> to check everything is working:

<!-- method for helping people set up their computers. Type this single line into the console and follow the instructions.  -->

```{r testcode, eval = FALSE }
source("https://github.com/ITSLeeds/pct/raw/master/inst/test-setup.R") 
```

If you have any questions before the workshop, feel free to ask a question on the package's issue tracker (requires a GitHub login): <https://github.com/ropensci/slopes/issues>

<!-- \newpage -->

<!-- # Agenda -->

<!-- - 09:30 - 10:00: Arrival and set-up -->

<!-- - 10:00 - 11:00: Data and methods underlying the PCT -->

<!-- - 11:00 - 12:30: Getting and analysing PCT data -->

<!--     - Comparing PCT results with existing road infrastructure -->

<!--         - Getting data from the CyIPT -->

<!--         - Identfying 'weak links' -->

<!--         - Identify gaps in the network -->

<!-- Lunch break -->

<!-- - 13:30 - 15:30: Extending the PCT: minihack -->

<!--     - Alternative scenarios of cycling uptake -->

<!--         - A 'replace short car trips' scenario -->

<!--     - New input data -->

<!--         - Travel to stations -->

<!--     - Other extensions of the PCT -->

<!-- Break and presentation of results -->

<!-- - 16:00 - 16:30: Policy implementation -->

<!--     - Building open source web applications -->

<!--     - Community-building -->

<!--     - User interface -->

<!--     - Policy uptake -->

<!-- ## Agenda -->

<!-- - The venue + facilities -->

<!-- Part 1: how the Propensity to Cycle Tool works + demo: 'I do' -->

<!--   - Introductions (14:00 - 14:15) -->

<!--   - Walk through of [`test-setup.R`](https://github.com/ITSLeeds/pct/blob/master/inst/test-setup.R) (14:15 - 14:30) -->

<!--   - Demonstration of calculating cycling potential in a new context (14:30 - 14:40) -->

<!--   <!-- I will write an adapted version based on: https://github.com/ITSLeeds/pct/blob/master/data-raw/test-setup2.R -->

<!--   - Questions, break and debugging the test code in breakout rooms  (14:40 - 15:00) -->

<!--      - Room 1: technical questions and debugging code (Joey) -->

<!--      - Room 2: methodological questions about the PCT (Robin) -->

<!--      - Room 3: applying the PCT in new contexts (Rosa) -->

<!--      - Room 4: networking  -->

<!-- Part 2: Co-coding session: getting started with transport data in R: 'we do' -->

<!-- - Working through the code in [`pct_training.R`](https://github.com/ITSLeeds/pct/blob/master/inst/pct_training.R) (15:00 - 15:30) -->

<!--   - Breakout rooms 2 and 3 open for people who have questions -->

<!-- - Live demo: overlaying PCT data with data from the Rapid tool and OSM (15:30 - 15:45) -->

<!--    ☕☕☕ 15 minute break  ☕☕☕ -->

<!-- Part 3: using PCT data for local transport planning: 'you do' -->

<!-- - Getting set-up with RStudio and input data (16:00 - 16:15, Robin) -->

<!-- - Break-out rooms (16:15 - 17:00) -->

<!--    - Getting and visualising PCT data (e.g. `get_pct_rnet`) -->

<!--    - Running the PCT for new areas -->

<!--    - Advanced topics (different routing, uptake and route network summary methods - developments in **stplanr**) -->

<!-- - Presentation of work and next steps (17:00 - 17:15) -->

<!-- - Networking and 'ideas with beers' 🍻🍻🍻 (17:20 - 18:00) -->

# How the PCT works and what you can use it for

-   Take a look at the image below from [@woodcock_health_2021a] and shown in Figure \@ref(fig:flow), which builds on an earlier diagram in @lovelace_propensity_2017. Which parts of the process are of most interest for your work?

```{r flow, echo=FALSE, out.width="75%", fig.cap="Flow diagram illustrating key processesing stages involved in creating the datasets underlying the PCT."}
# u = "https://raw.githubusercontent.com/npct/pct-team/master/flow-model/flow-diag2.png"
# f = "vignettes/flow-diag2.png"
# download.file(u, f)
# knitr::include_graphics("flow-diag2.png")
# knitr::include_graphics("https://raw.githubusercontent.com/npct/pct-team/master/flow-model/flow-diag2.png")
knitr::include_graphics("https://user-images.githubusercontent.com/1825120/130079051-378fcaf1-9cac-4768-a42e-28c287497618.png")
```

<!-- - In groups of 2, identify 3 real world questions/problems that you could use the PCT to help answer/solve -->

<!-- Communicating a vision -->

<!-- Deciding on network scale/size/shape -->

<!-- Generating business case for investment -->

<!-- Feed into consulation -->

<!-- Extend it to look at cycling integraton -->

The PCT provides data at 4 geographic levels:

-   Zones
-   Desire lines
-   Routes
-   Route networks

Which types of data are most appropriate to tackle each of the questions/problems you identified?

-   Name 3 limitations of the data currently provided by the PCT and discuss how you could overcome them

<!-- OD data is old -> use new OD data -->

<!-- Only single stage journeys -> Model travel to rail, for example -->

<!-- It does not predict future levels of cycling following new infrastructure -> -->

<!--   Wait until follow-on work building CyIPT is developed -->

# Getting and exploring PCT data

<!-- From ccac 2021 -->

In this section you will learn about the open datasets provided by the PCT project and how to use them.
While the most common use of the PCT is via the interactive web application hosted at www.pct.bike, there is much value in downloading the data, e.g. to identify existing cycling infrastructure in close proximity to routes with high potential, and to help identify roads in need of interventions from a safety perspective, using data from the constantly evolving and community-driven global geographic database OpenStreetMap (OSM) [@barrington-leigh_world_2017].

In this session, which assumes you know how to have experience using QGIS, or R, you will learn how to:

-   Find data on travel behaviour from the 2011 Census and from the School Census
-   How to download and import data from the PCT into QGIS
-   How to process the data alongside infrastructure data, to help find gaps in the network

## Getting PCT data

We will get PCT data at the MSOA level and plot the result in a simple map.

-   G1: The first stage is to load packages we will use:

```{r}
library(pct)
library(sf)      # key package for working with spatial vector data
library(dplyr)   # in the tidyverse
library(tmap)    # installed alongside mapview
```

-   G2: After setting the region name (to avoid re-typing it many times) run the following commands to download data at the four main levels used in the PCT:

```{r}
region_name = "isle-of-wight"
zones_all = get_pct_zones(region_name, geography = "msoa")
lines_all = get_pct_lines(region_name, geography = "msoa")
routes_all = get_pct_routes_fast(region_name, geography = "msoa")
rnet_all = get_pct_rnet(region_name)
```

-   G3: Check the downloads worked by plotting the result as follows:

```{r plotall, out.width="70%"}
plot(zones_all$geometry)
plot(lines_all$geometry, col = "blue", add = TRUE)
plot(routes_all$geometry, col = "green", add = TRUE)
plot(rnet_all$geometry, col = "red", lwd = sqrt(rnet_all$bicycle), add = TRUE)
```

## Visualising PCT data

At its heart, the PCT is a data visualisation tool.

-   V1: Create a static plot showing the route network layer with the `tmap` package

```{r}
tm_shape(rnet_all) +
  tm_lines(lwd = "dutch_slc", scale = 9)
```

```{r, eval=FALSE, echo=FALSE}
# tmap_mode("plot")
tm_shape(zones_all) +
  tm_polygons("all") +
  tm_shape(rnet_all) +
  tm_lines(lwd = c("bicycle", "dutch_slc"), scale = 9)
```

-   V2: Create an interactive map showing the route network layer with the `tmap` package

```{r}
# interactive plot
tmap_mode("view")
tm_shape(rnet_all) +
  tm_lines(lwd = "dutch_slc", scale = 9)
```

-   V3 (Bonus): Run the following command to show the % of short trips in Isle of Wight made by active modes.

```{r setup, out.width="100%", message=FALSE}
# basic plot
max_distance = 7
# plot(zones_all$geometry)
# plot(lines_all$geometry[lines_all$all > 500], col = "red", add = TRUE)

# create 'active' desire lines (less than 5 km)
active = lines_all %>% 
  mutate(`Percent Active` = (bicycle + foot) / all * 100) %>% 
  filter(e_dist_km < max_distance)

tm_shape(active) +
  tm_lines("Percent Active", palette = "RdYlBu", lwd = "all", scale = 9)
```

-   V4 (Bonus): Use the same technique to to identify short distance trips with a high mode share by car, as follows:

```{r, out.width="100%"}
# Create car dependent desire lines
car_dependent = lines_all %>% 
  mutate(`Percent Drive` = (car_driver) / all * 100) %>% 
  filter(e_dist_km < max_distance)
tm_shape(car_dependent) +
  tm_lines("Percent Drive", palette = "-RdYlBu", lwd = "all", scale = 9)
```

Advanced: visualise the PCT data using a range of visualisation techniques.
For inspiration, check out the [Making maps with R](https://geocompr.robinlovelace.net/adv-map.html) chapter of Geocomputation with R.

## Exploring PCT data

-   E1: Using the PCT's online interface, hosted at [www.pct.bike/m/?r=isle-of-wight](https://www.pct.bike/m/?r=isle-of-wight), identify the MSOA **zone** that has the highest number of people who cycle to work.

-   E2: Using data downloaded with the command `get_pct_zones()`, identify the zone that has highest level of cycling with the function `top_n()` and save the result as an object called `z_highest_cycling` (hint: you may want to start by 'cleaning' the data you have downloaded to include only a few key columns with the function `select()`, as follows):

```{r}
z = zones_all %>% 
  select(geo_code, geo_name, all, foot, bicycle, car_driver)
```

```{r, echo=FALSE}
# the solution:
# View(z)
z_highest_cycling = z %>% 
  top_n(n = 1, wt = bicycle)
```

-   E3: Use the `plot()` command to visualise where on the Isle of Wight this 'high cycling' zone is (hint: you will need to use the `plot()` function twice, once to plot `z$geometry`, and again with the argument `add = TRUE` and a `col` argument to add the layer on top of the base layer and give it a colour). The result should look something like something this:

```{r, echo=FALSE}
plot(z$geometry)
plot(z_highest_cycling$geometry, col = "red", add = TRUE)
```

-   E4: Using the online interface, identify the top 5 MSOA to MSOA **desire lines** that have the highest number of people who cycle to work.

-   E5: Using the function `top_n(n = 5, wt = bicycle)`, identify the top 5 MSOA to MSOA **desire lines** that have the highest number of people who cycle to work (hint: you might want to start with the code shown below).

    -   Bonus: also find the 5 desire lines with the highest number of people driving to work. Plot them and find the straight line distance of these lines with the function `st_length()`.

```{r get routes}
# Aim: get top 5 cycle routes
l_msoa = lines_all %>% 
  select(geo_code1, geo_code2, all, foot, bicycle, car_driver, rf_avslope_perc, rf_dist_km)
```

```{r, echo=FALSE, warning=FALSE, fig.show='hold', fig.cap="Top 5 MSOA to MSOA desire lines with highest number of people cycling (left) and driving (right) in the Isle of Wight."}
# View(l)
l = l_msoa
l_top_cycling = l %>% 
  top_n(n = 5, wt = bicycle)
plot(z$geometry)
plot(st_geometry(l_top_cycling), add = TRUE, lwd = 5, col = "green")
 
# top 5 driving routes
l_top_driving = l %>% 
  top_n(n = 5, wt = car_driver)
plot(z$geometry)
plot(st_geometry(l_top_driving), add = TRUE, lwd = 5, col = "red")

# summary(sf::st_length(l_top_cycling))
# summary(sf::st_length(l_top_driving))

# library(tmap)
# tmap_mode("view")
# tm_shape(l_top_cycling) + tm_lines("green", lwd = 7) + tm_basemap(server = leaflet::providers$OpenStreetMap.BlackAndWhite)
# tm_shape(l_top_driving) + tm_lines("red", lwd = 7) + tm_basemap(server = leaflet::providers$OpenStreetMap.BlackAndWhite)
```

-   E6 (Bonus): Repeat the exercise but calculate the top 10 LSOA to LSOA desire lines (by setting the argument `geography = "lsoa"`, remember to change the names of the objects you create). The results should look like this:

```{r, echo=FALSE, warning=FALSE, fig.show='hold', fig.cap="Top 10 LSOA-LSOA desire lines with highest number of people cycling (left) and driving (right) in the Isle of Wight."}
# at the lsoa level
l_original_lsoa = get_pct_lines("isle-of-wight", geography = "lsoa")
l_lsoa = l_original_lsoa %>% 
  select(geo_code1, geo_code2, all, bicycle, car_driver, rf_avslope_perc, rf_dist_km)
l_top_cycling = l_lsoa %>% 
  top_n(n = 10, wt = bicycle)
l_top_driving = l_lsoa %>% 
  top_n(n = 10, wt = car_driver)

plot(z$geometry)
plot(st_geometry(l_top_cycling), add = TRUE, lwd = 5, col = "green")
plot(z$geometry)
plot(st_geometry(l_top_driving), add = TRUE, lwd = 5, col = "red")
```

-   E7: Why are the results different? What are the advantages and disadvantages of using smaller zones, as represented by the LSOA data above?

<!-- - E8 (bonus): do the same analysis but with the top **30** routes with a  cycled and driven. Hint: set the line width with `lwd = l_top_cycling$bicycle / mean(l_top_cycling$bicycle)` to portray the relative importance of each route. -->

```{r, echo=FALSE, warning=FALSE, fig.show='hold', fig.cap="Top 300 LSOA-LSOA desire lines with highest number of people cycling (left) and driving (right) in the Isle of Wight.", eval=FALSE}
# at the lsoa level
l_top_cycling = l %>% 
  top_n(n = 300, wt = bicycle)
plot(z$geometry)
plot(st_geometry(l_top_driving), add = TRUE, lwd = l_top_cycling$bicycle / mean(l_top_cycling$bicycle), col = "green")

# top 5 driving routes
l_top_driving = l %>% 
  top_n(n = 300, wt = car_driver)
plot(z$geometry)
plot(st_geometry(l_top_driving), add = TRUE, lwd = l_top_driving$car_driver / mean(l_top_driving$car_driver), col = "red")
```

## Modifying PCT data to identify routes/roads of interest

-   M1: Building on the MSOA examples above, add a new column called `pcycle` to the object `l_msoa` that contains the % who cycle to work (hint: you might want to start this by typing `l_msoa$pcycle = ...`) and plot the results (shown in left hand panel in plot below).

```{r p2}
l_msoa$pcycle = l_msoa$bicycle / l_msoa$all * 100
# plot(l_msoa["pcycle"], lwd = l_msoa$all / mean(l_msoa$all), breaks = c(0, 5, 10, 20, 50))
```

-   M2 (bonus): identify road segments with the highest estimated number of people cycling currently, and under the Go Dutch scenario (hint: you can download the route network with `get_pct_rnet("isle-of-wight")`)

```{r eval=FALSE, echo=FALSE}
rnet = get_pct_rnet("isle-of-wight")
```

-   M3 Calculate the proportion of trips in the Isle of Wight that are less than 10 km in length and (bonus) plot the cumulative distribution graph for the fastest route distances

```{r, echo=FALSE}
l_less_than_10km = l %>% 
  filter(rf_dist_km < 10)
sum(l_less_than_10km$all) / sum(l$all)
```

-   M4: subset and then plot all the MSOA-MSOA desire lines that have a route distance of less than 10 KM *and* more than 5 people travelling by mode, for each of the following modes:

    -   walking
    -   cycling
    -   driving

```{r, fig.show='hold', out.width="33%"}
plot(l_less_than_10km %>% filter(foot > 5) %>% select(foot))
plot(l_less_than_10km %>% filter(bicycle > 5) %>% select(bicycle))
plot(l_less_than_10km %>% filter(car_driver > 5) %>% select(car_driver))
```

This section is designed for people with experience with the PCT and cycling uptake estimates who want to learn more about how uptake models work.

# PCT scenarios

-   S1: Generate a 'Go Dutch' scenario for the Isle of Wight using the function `uptake_pct_godutch()` (hint: the following code chunk will create a 'Government Target' scenario):

```{r}
l_msoa$euclidean_distance = as.numeric(sf::st_length(l_msoa))
l_msoa$pcycle_govtarget = uptake_pct_govtarget_2020(
  distance = l_msoa$rf_dist_km,
  gradient = l_msoa$rf_avslope_perc
  ) * 100 + l_msoa$pcycle
```

```{r change, echo=FALSE}
l_msoa$pcycle_dutch = uptake_pct_godutch_2020(
  distance = l_msoa$rf_dist_km,
  gradient = l_msoa$rf_avslope_perc
  ) * 100 + l_msoa$pcycle
```

```{r dutch_pcycle, echo=FALSE, warning=FALSE, fig.show='hold', fig.cap="Percent cycling currently (left) and under a 'Go Dutch' scenario (right) in the Isle of Wight."}
plot(l_msoa["pcycle"], lwd = l_msoa$all / mean(l_msoa$all), breaks = c(0, 5, 10, 20, 50))
plot(l_msoa["pcycle_dutch"], lwd = l_msoa$all / mean(l_msoa$all), breaks = c(0, 5, 10, 20, 50))
# cor(l_original_msoa$dutch_slc / l_original_msoa$all, l_msoa$pcycle_dutch)
# cor(l_original_msoa$govtarget_slc / l_original_msoa$all, l_msoa$pcycle_govtarget)
# plot(l_original_msoa$dutch_slc / l_original_msoa$all, l_msoa$pcycle_dutch)
```

-   S2: Think of alternative scenarios that would be useful for your work
-   S3 (bonus): look inside the function [`pct_uptake_godutch()`](https://github.com/ITSLeeds/pct/blob/master/R/uptake.R#L36) - how could it be modified?

# Routing and route networks

A key aspect of the PCT is routing.
This section demonstrates how to calculate cycling routes in R, to support evidence-based transport planning.

## Routes

-   R1: Using the function `route()` find the route associated with the most cycled desire line in the Isle of Wight. If you use the arguments `route_fun = osrmRoute` `returnclass = "sf"`, the result should look similar to that displayed in the map below (hint: you may want to start your answer with the following lines of code):

```{r}
l_top = l_msoa %>% 
  top_n(n = 1, wt = bicycle)
```

```{r, eval=FALSE, echo=FALSE}
library(osrm)
r_top = route(l = l_top, route_fun = osrmRoute, returnclass = "sf")
# r_top = route(l = l_top, route_fun = cyclestreets::journey)
mapview::mapview(r_top)
sf::write_sf(r_top, "r_top.geojson")
piggyback::pb_upload("r_top.geojson")
piggyback::pb_download_url("r_top.geojson")
```

```{r, echo=FALSE}
r_top = sf::read_sf("https://github.com/ITSLeeds/pct/releases/download/0.0.1/r_top.geojson")
tm_shape(r_top) +
  tm_lines(lwd = 5)
```

-   R2: What are the problems associated with this route from a cycling perspective?
    Take a look at the help page opened by entering `?route` to identify the reason why the route is not particularly useful from a cycling perspective.

-   R3: Regenerate the route using the following command: `route(l = l_top, route_fun = cyclestreets::journey)`.
    What is the difference in the length between each route, and what other differences can you spot?
    **Note: this exercise requires an API Key from CycleStreets.net.**

```{r, echo=FALSE, eval=FALSE}
r_cs = route(l = l_top, route_fun = cyclestreets::journey)
library(leaflet)
leaflet() %>% 
  addTiles() %>% 
  addPolylines(data = r_cs)
```

-   R4 (bonus): what features of a routing service would be most useful for your work and why?

## Route networks

-   RN1: Generate a 'route network' showing number of people walking in the top 30 routes in the Isle of Wight, allocated to the transport network (hint: use the `overline()` function and begin the script as follows, the results should look similar to the results below):

```{r}
route_data = sf::st_sf(wight_lines_30, geometry = wight_routes_30$geometry)
```

```{r, echo=FALSE, message=FALSE}
library(stplanr) # required for the overline function
rnet_walk = overline(route_data, attrib = "foot")
tm_shape(rnet_walk) +
  tm_lines(lwd = "foot", scale = 9)
```

-   RN2: Download the travel to school route network and compare the results with the route network created for RN1.

    -   Which roads have greatest overlap between the two route networks?
    -   For more information on the travel to school layer, see @goodman_scenarios_2019. What other trip purposes would you like to see in tools for cycle planning?

```{r, eval=FALSE, echo=FALSE}
rnet_school = get_pct_rnet(region = "isle-of-wight", purpose = "school")
plot(rnet_school)
```

```{r, echo=FALSE, eval=FALSE}
# Demo PCT Analysis#
# Make a commuting quiet route network for Isle of Wight
# and combine it with the travel to school route network

# Step 1: Load Library
library(tidyverse)
library(sf)
library(pct)
library(stplanr)

# Step 2: Get Data
routes_commute = get_pct_routes_quiet(region = "isle-of-wight",
                              purpose = "commute",
                              geography = "lsoa")

lines_commute = get_pct_lines(region = "isle-of-wight",
                              purpose = "commute",
                              geography = "lsoa")

rnet_school = get_pct_rnet(region = "isle-of-wight",
                           purpose = "school",
                           geography = "lsoa")

# Step 3: Prepare Data
lines_commute = lines_commute %>%
  st_drop_geometry() %>%
  select(id, bicycle, dutch_slc)

routes_commute = routes_commute %>%
  select(id)

# Join Cycling Levels to Routes
routes_commute = left_join(routes_commute, lines_commute)
plot(routes_commute["bicycle"])

# Make a commuting Rnet
rnet_commute = overline2(routes_commute, 
                         attrib = c("bicycle","dutch_slc"))
plot(rnet_commute["bicycle"])

# Combine commuting and travel to schools
rnet_school = rnet_school %>%
  select(dutch_slc)
rnet_commute = rnet_commute %>%
  select(dutch_slc)
rnet_commute$bicycle = NULL


rnet_both = rbind(rnet_commute, rnet_school)
rnet_both = overline2(rnet_both, 
                         attrib = c("dutch_slc"))
mapview::mapview(rnet_both, at = c(50,100,200,500,1000))

```

# Ideas for further work

-   Create a route network reflecting where you would invest if the priority was reducing car trips of less than 5 km
-   Design interventions to replace short car trips across London (or another region of your choice) using the PCT methods/data to support your decisions
-   Identify quiet routes and design a quiet route network for city/region of your choice, e.g. Westminter
-   Import alternative origin-destination datasets and use those as the basis for propensity to cycle analysis for trip purposes other than single stage commutes, encapsulated in the commut layer in the PCT
-   Any other layers/scenarios/hacks: welcome! Comments in this repo's [issue tracker](https://github.com/ITSLeeds/pct/issues) also welcome.

<!-- - Identify priority routes to improve for bike share users by using OD data from TfL's bike share (if we get time to do this) -->

<!-- Demonstrate routing with cyclestreets.net -->

```{r, echo=FALSE, eval=FALSE}
# old code - create with leaflet
pal = colorBin(palette = "RdYlBu", domain = active$`Percent Active`, bins = c(0, 2, 4, 10, 15, 20, 30, 40, 90))
leaflet(data = active) %>% 
  addProviderTiles(providers$OpenStreetMap.BlackAndWhite) %>% 
  addPolylines(color = ~pal(`Percent Active`), weight = active$all / 100) %>% 
  addLegend(pal = pal, values = ~`Percent Active`)

pal = colorBin(palette = "RdYlBu", domain = car_dependent$`Percent Active`, bins = c(0, 20, 40, 60, 80, 100), reverse = TRUE)
leaflet(data = car_dependent) %>% 
  addProviderTiles(providers$OpenStreetMap.BlackAndWhite) %>% 
  addPolylines(color = ~pal(`Percent Drive`), weight = active$all / 100) %>% 
  addLegend(pal = pal, values = ~`Percent Drive`)
```

# Useful links

These links may be useful when working through the exercises:

-   The codebook describing each variable in the PCT route: [od_l\_rf_codebook.csv](https://github.com/npct/pct-shiny/blob/master/regions_www/www/static/02_codebooks/commute/od_l_rf_codebook.csv)
-   Folder containing all [codebooks, including variables in zone, line and centroid datasets](https://github.com/npct/pct-shiny/tree/master/regions_www/www/static/02_codebooks)
-   The PCT tool itself, which can be used to cross-compare results from the analysis steps below: [www.pct.bike](https://www.pct.bike/)

# References
