{ "cells": [ { "cell_type": "markdown", "id": "4b449c51", "metadata": {}, "source": [ "# r5r: Access to opportunities\n", "\n", "```{admonition} Credits:\n", "\n", "This tutorial is a direct copy from [r5r -documentation](https://ipeagit.github.io/r5r/articles/calculating_accessibility.html) made by r5r contributors.\n", "```\n", "\n", "## Getting started\n", "\n", "### Run these codes in Binder\n", "\n", "Before you can run this Notebook, and/or do any programming, you need to launch the Binder instance. You can find buttons for activating the python environment at the top-right of this page which look like this:\n", "\n", "\n", "\n", "### Working with Jupyter Notebooks\n", "\n", "Jupyter Notebooks are documents that can be used and run inside the JupyterLab programming environment containing the computer code and rich text elements (such as text, figures, tables and links). \n", "\n", "**A couple of hints**:\n", "\n", "- You can **execute a cell** by clicking a given cell that you want to run and pressing Shift + Enter (or by clicking the \"Play\" button on top)\n", "- You can **change the cell-type** between `Markdown` (for writing text) and `Code` (for writing/executing code) from the dropdown menu above. \n", "\n", "See **further details and help for** [**using Notebooks and JupyterLab from here**](https://pythongis.org/part1/chapter-01/nb/04-using-jupyterlab.html). \n", "\n", "\n", "## 1. Introduction \n", "\n", "Accessibility indicators measure the ease with which opportunities, such as jobs,\n", "can be reached by a traveler from a particular location [@levinson2020manual]. One\n", "of the simplest forms of accessibility metrics is the cumulative-opportunities, \n", "which counts all of the opportunities accessible from each location in less than a \n", "cutoff time. Using a travel time matrix and information on the number of opportunities available at each location, we can calculate and map accessibility. This vignette shows how to do that in R using the [`r5r` package](https://ipeagit.github.io/r5r/index.html).\n", "\n", "In this reproducible example, we will be using a sample data set for the city of \n", "Porto Alegre (Brazil) included in `r5r`. We can compute accessibility in 5 quick \n", "steps:\n", "\n", "1. Increase Java memory and load libraries\n", "2. Build routable transport network\n", "3. Calculate Accessibility\n", "4. Map Accessibility\n", "\n", "\n", "\n", "## 2. Build routable transport network with `setup_r5()`\n", "\n", "### Increase Java memory and load libraries\n", "\n", "Before we start, we need to increase the memory available to Java and load the \n", "packages used in this vignette" ] }, { "cell_type": "code", "execution_count": 3, "id": "8693b6b0", "metadata": {}, "outputs": [], "source": [ "options(java.parameters = \"-Xmx2G\")\n", "\n", "library(r5r)\n", "library(sf)\n", "library(data.table)\n", "library(ggplot2)\n", "library(interp)\n", "library(dplyr)" ] }, { "cell_type": "markdown", "id": "9ca722b9", "metadata": {}, "source": [ "To build a routable transport network with r5r and load it into memory, the user \n", "needs to call `setup_r5` with the path to the directory where OpenStreetMap and \n", "GTFS data are stored." ] }, { "cell_type": "code", "execution_count": 5, "id": "0077b9bf", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Using cached R5 version from /home/hentenka/.conda/envs/mamba/envs/r5/lib/R/library/r5r/jar/r5-v6.7-all.jar\n", "\n", "\n", "Using cached network.dat from /home/hentenka/.conda/envs/mamba/envs/r5/lib/R/library/r5r/extdata/poa/network.dat\n", "\n" ] } ], "source": [ "data_path <- system.file(\"extdata/poa\", package = \"r5r\")\n", "\n", "r5r_core <- setup_r5(data_path)" ] }, { "cell_type": "markdown", "id": "d112af0b", "metadata": {}, "source": [ "## 3. Calculate Accessibility\n", "\n", "In this example, we will be calculating the number of schools and public healthcare facilities accessible by public transport within a travel time of up to 20 minutes. The sample data provided contains information on the spatial distribution of schools in Porto Alegre in the `points$schools` column, and healthcare facilities in the `points$healthcare` column. \n", "\n", "With the code below we compute the number of schools and healthcare accessible considering median of multiple travel time estimates departing every minute over a 60-minute time window, between 2pm and 3pm. The `accessibility()` function can calculate access to multiple opportunities in a single call, which is much more efficient and convenient than producing a travel time matrix of the study area and manually computing accessibility.\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "4c86f616", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| id | opportunity | percentile | cutoff | accessibility |
|---|---|---|---|---|
| <chr> | <chr> | <int> | <int> | <dbl> |
| 89a901291abffff | schools | 50 | 21 | 3 |
| 89a901291abffff | healthcare | 50 | 21 | 6 |
| 89a9012a3cfffff | schools | 50 | 21 | 0 |
| 89a9012a3cfffff | healthcare | 50 | 21 | 0 |
| 89a901295b7ffff | schools | 50 | 21 | 6 |
| 89a901295b7ffff | healthcare | 50 | 21 | 4 |