Skip to content

Commit

Permalink
Merge pull request #22 from acteng/21-rewrite-using-od2net-package
Browse files Browse the repository at this point in the history
Update README to use od2net R package, close #21
  • Loading branch information
Robinlovelace authored Oct 1, 2024
2 parents 7caeb66 + 0ea6734 commit 163107a
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,5 @@ docs
intermediate/
*.pbf
*.tif
output/output.geojson
output/*.pmtiles
71 changes: 55 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,31 @@ use in the reproducible code below.

# Setup

To run the code below you need to have R and Docker installed. After you
have installed R, you can install the packages we’ll use as follows:

``` r
# # Install geopandas:
# reticulate::py_install("geopandas")
# Install pak if not already installed:
if (!requireNamespace("pak", quietly = TRUE)) {
install.packages("pak")
}
pkgs = c("sf", "tidyverse", "tmap", "pak")
pkgs_to_install = pkgs[!pkgs %in% installed.packages()]
if (length(pkgs_to_install) > 0) {
pak::pkg_install(pkgs_to_install)
}
# Load the packages with vapply:
vapply(pkgs, require, logical(1), character.only = TRUE)
```

sf tidyverse tmap pak
TRUE TRUE TRUE TRUE

We will also install a couple of package that are not on CRAN:

``` r
pak::pkg_install("acteng/netgen")
pak::pkg_install("Urban-Analytics-Technology-Platform/od2net/r")
```

<div class="panel-tabset" group="language">
Expand Down Expand Up @@ -85,19 +107,24 @@ od_geo |>

# od2net

Building on code in the `od2net` and
[nptscot/od2net-tests](https://github.com/nptscot/od2net-tests) repos,
the code below prepares the input datasets and runs the
network-generation code, generating output.geojson and output.pmtiles
outputs:
Building on code in the
[od2net](https://urban-analytics-technology-platform.github.io/od2net/r/)
R package, we can generate the files needed for the network generation
stage.

After you have installed the package, prepare the input datasets and run
the network-generation code to generate output.geojson and
output.pmtiles outputs:

``` r
source("R/setup.R")
make_zones("input/zones_york.geojson")
make_osm()
make_origins()
make_elevation()
destinations = destinations_york # Provided in the R package
origin_zones = netgen::zones_york
names(origin_zones)
names(origin_zones)[1] = "name"
sf::write_sf(zones, "input/zones_york.geojson", delete_dsn = TRUE)
od2net::make_osm(zones_file = "input/zones_york.geojson")
od2net::make_origins()
netgen:::make_elevation()
destinations = netgen::destinations_york # Provided in the R package
names(destinations)[1] = "name"
destinations = destinations[1]
class(destinations$name) = "character"
Expand Down Expand Up @@ -125,9 +152,22 @@ fs::dir_tree("output")
```

output
├── counts.csv
├── output.geojson
├── rnet.pmtiles
└── rnet_output_osrm_overline.geojson

From that point you can visualise the pmtiles in the website at
[od2net.org](https://od2net.org) or other front-end application, as
shown below.

## Adjusting uptake functions

The example above visualises *all* trips to school, not just active
travel trips.

## Other approaches to network generation

An advantage of `od2net` is that it can generate the network and routes
in a single step. For other approaches, we need to calculate the routes
first, as shown below.
Expand Down Expand Up @@ -181,8 +221,6 @@ osmextract::oe_download(
)
```

[1] "/home/robin/github/acteng/netgen/geofabrik_osm.pbf"

Then with the system shell:

``` bash
Expand Down Expand Up @@ -262,5 +300,6 @@ plot(rnet)
A disadvantage of this approach is that it’s computational
resource-intensive and takes a long time. An in-progress is `od2net`.

<!-- ## Comparing the outputs -->
</details>

# Comparing od2net and overline outputs
68 changes: 47 additions & 21 deletions README.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,32 @@ There are many sources of OD data, see [odgen](odgen.qmd) for a way to generate

# Setup

To run the code below you need to have R and Docker installed.
After you have installed R, you can install the packages we'll use as follows:

```{r}
#| include: false
pkgs = c("sf", "tidyverse", "tmap")
# Install pak if not already installed:
if (!requireNamespace("pak", quietly = TRUE)) {
install.packages("pak")
}
pkgs = c("sf", "tidyverse", "tmap", "pak")
pkgs_to_install = pkgs[!pkgs %in% installed.packages()]
if (length(pkgs_to_install) > 0) {
install.packages(pkgs_to_install)
pak::pkg_install(pkgs_to_install)
}
# Load the packages with vapply:
vapply(pkgs, require, logical(1), character.only = TRUE)
```

We will also install a couple of package that are not on CRAN:

```{r}
pak::pkg_install("acteng/netgen")
pak::pkg_install("Urban-Analytics-Technology-Platform/od2net/r")
```

```{r}
#| include: false
# # Install geopandas:
# reticulate::py_install("geopandas")
```
Expand Down Expand Up @@ -84,17 +100,21 @@ od_geo |>

# od2net

Building on code in the `od2net` and [nptscot/od2net-tests](https://github.com/nptscot/od2net-tests) repos, the code below prepares the input datasets and runs the network-generation code, generating output.geojson and output.pmtiles outputs:
Building on code in the [od2net](https://urban-analytics-technology-platform.github.io/od2net/r/) R package, we can generate the files needed for the network generation stage.

After you have installed the package, prepare the input datasets and run the network-generation code to generate output.geojson and output.pmtiles outputs:


```{r}
#| eval: false
source("R/setup.R")
make_zones("input/zones_york.geojson")
make_osm()
make_origins()
make_elevation()
destinations = destinations_york # Provided in the R package
origin_zones = netgen::zones_york
names(origin_zones)
names(origin_zones)[1] = "name"
sf::write_sf(zones, "input/zones_york.geojson", delete_dsn = TRUE)
od2net::make_osm(zones_file = "input/zones_york.geojson")
od2net::make_origins()
netgen:::make_elevation()
destinations = netgen::destinations_york # Provided in the R package
names(destinations)[1] = "name"
destinations = destinations[1]
class(destinations$name) = "character"
Expand All @@ -112,15 +132,7 @@ Run the tool with Docker as follows:
```{bash}
#| eval: false
# On Linux:
sudo docker run -v $(pwd):/app ghcr.io/urban-analytics-technology-platform/od2net:main /app/config.json
# or in Windows:
sudo docker run -v ${pwd}:/app ghcr.io/urban-analytics-technology-platform/od2net:main /app/config.json
```

```{r}
#| eval: false
#| echo: false
system("docker run -v $(pwd):/app ghcr.io/urban-analytics-technology-platform/od2net:main /app/config.json")
sudo docker run -v $(pwd):/app ghcr.io/urban-analytics-technology-platform/od2net:main /app/config.json --rng-seed 42 elevation_geotiff /app/input/elevation.tif
```

After that you should see the following in the output folder:
Expand All @@ -129,6 +141,19 @@ After that you should see the following in the output folder:
fs::dir_tree("output")
```

From that point you can visualise the pmtiles in the website at [od2net.org](https://od2net.org) or other front-end application, as shown below.

## Minimal od2net example

For the purposes of testing it's worth

## Adjusting uptake functions

The example above visualises *all* trips to school, not just active travel trips.


## Other approaches to network generation

An advantage of `od2net` is that it can generate the network and routes in a single step.
For other approaches, we need to calculate the routes first, as shown below.

Expand Down Expand Up @@ -191,6 +216,7 @@ routes_osrm_minimal |>
We can spin-up a local OSRM server to calculate routes as [follows](https://github.com/Project-OSRM/osrm-backend#using-docker):

```{r}
#| eval: false
location = osmextract::oe_match(
od_geo_top_100 |> sf::st_union()
)
Expand Down Expand Up @@ -285,6 +311,6 @@ plot(rnet)
A disadvantage of this approach is that it's computational resource-intensive and takes a long time.
An in-progress is `od2net`.

<!-- ## Comparing the outputs -->
</details>

</details>
# Comparing od2net and overline outputs
3 changes: 1 addition & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@
},
"cost": "Distance",
"uptake": "Identity",
"lts": "BikeOttawa",
"elevation_geotiff": "elevation.tif"
"lts": "BikeOttawa"
}
1 change: 0 additions & 1 deletion output/output.geojson

This file was deleted.

0 comments on commit 163107a

Please sign in to comment.