Skip to content

Commit

Permalink
update fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Jun 15, 2024
1 parent 23215e7 commit 44c08a4
Show file tree
Hide file tree
Showing 43 changed files with 213 additions and 14,611 deletions.
13 changes: 7 additions & 6 deletions 2.1_installation.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: "Installation"
from: markdown+emoji
---

::: {.callout-important appearance="simple"}
Expand All @@ -14,16 +15,16 @@ See instructions below:

# Installing R and RStudio

We assume you already have R and RStudio installed in your machine. In case you don't, [follow these simple instructions here](https://posit.co/download/rstudio-desktop/#download).
We assume you already have R and RStudio installed on your machine. In case you don't, [follow these simple instructions here](https://posit.co/download/rstudio-desktop/#download).



# Installing R packages

The workshop uses a few R packages that need to be installed in your machine. The simplest way to do this is running the code below. This might take a few minutes if this is the first time you install these packages.
The workshop uses a few R packages that need to be installed on your machine. The simplest way to do this is running the code below. This might take a few minutes if this is the first time you install these packages.

```{R eval = FALSE, message = FALSE}
pkgs_to_install <- c(
pkgs <- c(
'r5r',
'accessibility',
'ggplot2',
Expand All @@ -33,10 +34,10 @@ pkgs_to_install <- c(
'sf'
)
install.packages(pkgs_to_install)
install.packages(pkgs)
```

The safest way to replicate the code of this workshop, though, is cloning its [repository](https://github.com/ipeaGIT/access_workshop_WSTLUR_2024) locally. This is because this repository uses the `{renv}` R package to manage the workshop dependencies, which is important to make sure we are using the intended versions of the packages dependencies.
The safest way to replicate the code of this workshop, though, is cloning its [repository](https://github.com/ipeaGIT/access_workshop_WSTLUR_2024) locally. This is because this repository uses the `{renv}` R package to manage the package dependencies of the code we use. This is important to make sure we are using the same versions of the relevant packages.

Once you have cloned the repo and you've opened the `access_workshop_bogota_2024.Rproj` file, you can simply run `renv::restore()` to install all the package dependencies used in this workshop.

Expand All @@ -50,7 +51,7 @@ renv::restore()

# Installing Java JDK

To use the `{r5r}` package (version v2.0 or higher), you will need to have *Java Development Kit (JDK) 21* installed on your computer. There are numerous open-source JDK implementations. The easiest way to install JDK is using the new [{rJavaEnv}](https://github.com/e-kotov/rJavaEnv) package in R, developed by Egor Kotov (thanks Egor!)
To use the `{r5r}` package (version v2.0 or higher), you will need to have *Java Development Kit (JDK) 21* installed on your computer. There are numerous open-source JDK implementations. The easiest way to install JDK is using the new [{rJavaEnv}](https://github.com/e-kotov/rJavaEnv) package in R, developed by Egor Kotov (thanks Egor!).

```{R eval = FALSE}
# install dev version of {rJavaEnv}
Expand Down
4 changes: 2 additions & 2 deletions 2.2_data_requirements.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ title: "Data requirements"

In order to conduct transport routing and accessibility analysis using the `{r5r}` package, you will need a few input data sets:

- A road network from OpenStreetMap (OSM) in `.pbf `format (*mandatory*).
- A road network from OpenStreetMap (OSM) in `.pbf` format (*mandatory*).
- A public transport feed in` GTFS.zip` format (optional).
- A raster file with Digital Elevation Model data in `.tif` format (optional).
- Some data on the spatial distribution of population and/or activities such as employment, schools, health care facilities.


All the data we'll use in this workshop is provided with the code exercises. However, here is where you can download these data sets for other places. See also slides below.
The data sets we'll use in this workshop are provided with the code exercises. If you would like to find similar data for you other regions in the world, here are a few data sources. See also slides below.

- **Street network** (mandatory):
- [{osmextract}](https://docs.ropensci.org/osmextract/), R package;
Expand Down
43 changes: 22 additions & 21 deletions 3.1_quick_approach.qmd
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
---
title: "Quick approach"
code-annotations: hover
from: markdown+emoji
---

<img align="right" src="/images/r5r_logo.png?raw=true" alt="logo" width="140">
**In this first hands-on section of the workshop, we'll learn a very quick and simple way to calculate spatial accessibility using the `{r5r}` package**. In the next section, we'll see a more flexible and robust way to do the same thing. Here we'll be calculating the number of schools accessible by public transport within a travel time of up to 20 minutes.
**In this first hands-on section of the workshop, we'll learn a very quick and simple way to calculate spatial accessibility using the `{r5r}` package**. In the next section, we'll see a more flexible and robust way to do the same thing. Here we'll be calculating the number of schools accessible by public transport within a travel time of 20 minutes.


### 1. Allocating memory to Java & loading packages

First, let's increase the memory available to run Java, which is used by the underlying R5 routing engine. By default, `R` allocates only 512 MB of memory to Java processes. To increase the available memory to 2 GB, for example, we use the following command. Note that this needs to be run ***before*** loading the packages that will be used in our analysis.
First, let's increase the memory available to run Java, which is used by the underlying R5 routing engine. To increase the available memory to 2 GB, for example, we use the following command. Note that this needs to be run ***before*** loading the packages that will be used in our analysis.

```{r}
options(java.parameters = "-Xmx2G")
Expand All @@ -26,14 +27,14 @@ library(ggplot2)
```


# 2. Quick pick in our sample data
# 2. A quick look at our sample data

The case study we'll the city of Porto Alegre (Brazil). The `{r5r}` package brings a small sample data for this city, including the following files:
Our case study is the city of Porto Alegre, Brazil. The `{r5r}` package brings a small sample data for this city, including the following files:

* An OpenStreetMap network: `poa_osm.pbf`
* Two public transport GTFS feeds: `poa_eptc.zip` (buses) and `poa_trensurb.zip` (trains)
* A raster elevation data: `poa_elevation.tif`
* A data frame with land use data: `poa_hexgrid.csv` file with the centroids of a regular hexagonal grid covering the sample area. The data frame also indicates the number of residents and schools in each cell. We'll use these points as origins and destinations in our analysis.
- An OpenStreetMap network: `poa_osm.pbf`
- Two public transport GTFS feeds: `poa_eptc.zip` (buses) and `poa_trensurb.zip` (trains)
- A raster elevation data: `poa_elevation.tif`
- A data frame with land use data: `poa_hexgrid.csv` file with the centroids of a regular hexagonal grid covering the sample area. The data frame also indicates the number of residents and schools in each cell. We'll use these points as origins and destinations in our analysis.

These data sets should be saved in a single directory (our `data_path`). Here's how the land use data looks like:

Expand All @@ -46,7 +47,7 @@ points <- read.csv(file.path(data_path, "poa_hexgrid.csv"))
head(points)
```

To visualize the spatial distribution of these data, we can retrieve the geometry of the H3 hexagonal grid explore it using an interactive map:
:mag_right: To visualize the spatial distribution of these data, we can retrieve the geometry of the H3 hexagonal grid and explore it using an interactive map:

```{r}
# retrieve polygons of H3 spatial grid
Expand All @@ -69,26 +70,28 @@ mapview(grid_poa, zcol = 'population')



# 3. Building routable transport network
# 3. Building a routable transport network

This quick approach to calculate accessibility only involves 2 steps. The first step is to build the multimodal transport network using the `r5r::setup_r5()` function.
This quick approach to calculate accessibility involves only 2 steps. The first step is to build the multimodal transport network using the `r5r::setup_r5()` function.


```{r, message=FALSE}
r5r_core <- r5r::setup_r5(data_path,
verbose = FALSE)
```
As you can see, we only need to pass the path to our data directory to the `r5r::setup_r5()` function. The function then combines all the data saved in this directory to create a graph that is used for routing trips between origin-destination pairs and, consequently, for calculating travel time matrices and accessibility.

As you can see, we only need to pass the path to our data directory to the `r5r::setup_r5()` function. The function then combines the OSM, GTFS and elevation data in this directory to create a graph that is used for routing trips between origin-destination pairs and, consequently, for calculating travel time matrices and accessibility.


# 4. Calculate access: quick and easy

In the second step, you can calculate accessibility estimates in a single call using the `r5r::accessibility()` function. It includes different options of decay functions to calculate cumulative accessibility measures and different gravity-based metrics.
# 4. Calculating access: quick approach

In the second step, you can calculate accessibility estimates in a single call using the `r5r::accessibility()` function. It includes different options of decay functions to compute cumulative accessibility measures and different gravity-based metrics.

In this example, we calculate the cumulative accessibility of the number of schools and hospitals accessible in less than 20 minutes by public transport. Thus, we'll be using `decay_function = "step"`.

Note that to use the `r5r::accessibility()`, the input of `points` must be a `data.frame` with columns indicating:
Note that to use `r5r::accessibility()`, the input of `points` must be a `data.frame` with columns indicating:

- the `id` of each location
- spatial coordinates `lat` and `lon`
- the number of activities in each location. The name of this column has to be passed to the `opportunities_colnames` parameter.
Expand Down Expand Up @@ -118,17 +121,15 @@ access1 <- r5r::accessibility(
```
1. In minutes
2. Note you can pass the column of more than one type of opportunity.
2. Note you can pass the columns of more than one type of opportunity.
3. Similarly, you could pass more than one time threshold.

::: {.callout-tip}
## Tip
Note that the `r5r::accessibility()` function has several additional parameters. These allow you to specify different characteristics of trips, including a maximum trip duration, walking and cycling speed, level of traffic stress (LTS), etc. For more info, check the documentation of the function by calling `?r5r::accessibility` in your R Console or check the documentation on [{r5r} website](https://ipeagit.github.io/r5r/reference/accessibility.html).
Note that the `r5r::accessibility()` function has several additional parameters that allow you to specify different characteristics of trips, including a maximum trip duration, walking and cycling speed, level of traffic stress (LTS), etc. For more info, check the documentation of the function by calling `?r5r::accessibility` in your R Console or check the documentation on [{r5r} website](https://ipeagit.github.io/r5r/reference/accessibility.html).
:::



The output is a `data.frame` and it looks like this. It shows for every origin `id` the number of opportunities that can be reached:
The output is a `data.frame` that shows for every origin `id` the number of opportunities that can be reached:

```{r}
head(access1)
Expand All @@ -151,7 +152,7 @@ access_sf <- left_join(
ggplot() +
geom_sf(data = access_sf, aes(fill = accessibility), color= NA) +
scale_fill_viridis_c(direction = -1, option = 'B') +
labs(subtitle = 'Number of schools and hospitals accessible by transit in 20 minutes',
labs(title = 'Number of schools and hospitals accessible by public transport in 20 minutes',
fill = 'Number of\nfacilities') +
theme_minimal() +
theme(axis.title = element_blank()) +
Expand Down
25 changes: 13 additions & 12 deletions 3.2_flexible_approach.qmd
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
---
title: "Flexible approach"
code-annotations: hover
from: markdown+emoji
---

In the the previous section, we learned a 'quick approach' to calculate accessibility directly from within `{r5r}`. The shortcoming of this approach it that is limited to only a few predefined accessibility metrics available in `{r5r}`. Oftentimes, we want to analyze other types of accessibility metrics and/or to have more control of the different steps of the analysis.
In the the previous section, we learned a 'quick approach' to calculate accessibility directly from within `{r5r}`. A shortcoming of this approach is that it is limited to only a few predefined accessibility metrics available in `{r5r}`. Oftentimes, we want to analyze other types of accessibility metrics and/or to have more control of the different steps of the analysis.

**In this section, we'll learn how to calculate a travel time matrix and how to use it to compute different types of accessibility metrics.** This is a more 'flexible' approach that combines the capabilities of the `{r5r}` and the `{accessibility}` packages. We'll use again the sample data for the city of Porto Alegre (Brazil) that comes with the `{r5r}` package.
**In this section, we'll learn how to calculate travel time matrices and to use them to compute different types of accessibility metrics.** This is a more 'flexible' approach that combines the capabilities of the `{r5r}` and the `{accessibility}` packages. We'll use again the sample data for the city of Porto Alegre, Brazil, that comes with the `{r5r}` package.

<!-- package logos side by side -->
<div style="text-align: center">
Expand All @@ -15,9 +16,9 @@ In the the previous section, we learned a 'quick approach' to calculate accessib

This flexible approach to calculate accessibility only involves 3 steps:

1. Build a routable transport network
2. Calculate a travel time matrix
3. Calculate accessibility
1. Building a routable transport network
2. Computing a travel time matrix
3. Calculating accessibility

Now let's start loading the packages we need:

Expand Down Expand Up @@ -48,7 +49,7 @@ r5r_core <- r5r::setup_r5(data_path,
```

# 2. Calculate a travel time matrix
# 2. Computing a travel time matrix

The second step is to calculate a travel time matrix with door-to-door travel time estimates from all origins to all destinations. To do this, we use the `r5r::travel_time_matrix()` function.

Expand Down Expand Up @@ -80,7 +81,7 @@ ttm <- r5r::travel_time_matrix(

::: {.callout-tip}
## Tip
Note that the `r5r::travel_time_matrix()` includes several additional parameters, which allows you to specify some trip characteristics, such as maximum walking time, a departure time window, the maximum number of public transport legs allowed, etc. For more info, check the documentation of the function by calling `?r5r::travel_time_matrix` in your R Console or check the documentation on [{r5r} website](https://ipeagit.github.io/r5r/reference/travel_time_matrix.html).
Note that the `r5r::travel_time_matrix()` includes several additional parameters that allow you to specify some trip characteristics, such as maximum walking time, a departure time window, the maximum number of public transport legs allowed, etc. For more info, check the documentation of the function by calling `?r5r::travel_time_matrix` in your R Console or check the documentation on [{r5r} website](https://ipeagit.github.io/r5r/reference/travel_time_matrix.html).
:::

The output is a `data.frame`, which looks like this. It is a matrix in long format with the columns `from_id`, `to_id` and a third column indicating the travel time (in minutes) for each origin-destination pair.
Expand All @@ -94,11 +95,11 @@ head(ttm)
::: {.callout-note appearance="simple"}
## Processing speed

The `travel_time_matrix()` function uses an extension of the RAPTOR routing algorithm [@conway2017evidencebased], making R5 extremely fast. Depending on the number of origin-destination pairs, `{r5r}` can calculate travel time matrices between 6 and 200 times faster than other multimodal routing softwares [@higgins2022calculating].
The `travel_time_matrix()` function uses an extension of the RAPTOR routing algorithm [@conway2017evidencebased], making R5 extremely fast. Depending on the number of origin-destination pairs, `{r5r}` can calculate travel time matrices between 6 and 200 times faster than other multimodal routing engines [@higgins2022calculating].
:::


# 3. Calculate accessibility
# 3. Calculating accessibility

<img align="right" src="/images/accessibility_logo.png" alt="logo" width="140">

Expand Down Expand Up @@ -130,7 +131,7 @@ Now let's calculate a few accessibility indicators.

### Threshold-based cumulative accessibility

To calculate a traditional cumulative accessibility measure, we can use the `accessibility::cumulative_cutoff()` function. Along with the travel matrix and land use datasets, we only need to pass the name of the column with the travel time values, the name of the column with the count of opportunities, and a travel time cutoff.
To calculate a traditional cumulative accessibility measure, we can use the `accessibility::cumulative_cutoff()` function. Along with the travel matrix and land use data sets, we only need to pass the name of the column with the travel time values, the name of the column with the count of opportunities, and a travel time cutoff.

Here, we calculate the number of schools accessible in 20 minutes.

Expand Down Expand Up @@ -162,7 +163,7 @@ access_cum_i <- accessibility::cumulative_interval(
land_use_data = points,
travel_cost = 'travel_time_p50',
opportunity = 'schools',
interval = c(20,30),
interval = c(15,25),
summary_function = mean
)
Expand Down Expand Up @@ -288,7 +289,7 @@ Plot map
ggplot() +
geom_sf(data = access_sf, aes(fill = schools), color= NA) +
scale_fill_viridis_c(direction = -1, option = 'B') +
labs(fill = "Access\nscore") +
labs(title = "Access to schools by public transport using different accessibility metrics", fill = "Access\nscore") +
theme_minimal() +
theme(axis.title = element_blank()) +
facet_wrap(~metric) +
Expand Down
Loading

0 comments on commit 44c08a4

Please sign in to comment.