diego-ellis-soto commited on
Commit ·
1ca14ec
0
Parent(s):
Initial audited Biodiversity Access Score app
Browse files- .gitattributes +6 -0
- .github/workflows/deploy.yml +20 -0
- .gitignore +52 -0
- Dockerfile +21 -0
- README.md +209 -0
- Rscripts/iso_metrics.R +709 -0
- Rscripts/prep/build_cbg_greenspace_coverage.R +44 -0
- Rscripts/prep/build_equity_layers.R +34 -0
- Rscripts/prep/create_annotated_gbif_parquet.R +88 -0
- Rscripts/prep/implement_optimizations.R +62 -0
- Rscripts/prep/making-greenspace-raster.R +186 -0
- Rscripts/prep/making-rsfprogram-raster.R +173 -0
- Rscripts/prep/profile_startup.R +444 -0
- Rscripts/prep/run_all_prep.R +23 -0
- Rscripts/prep/upload_to_huggingface.R +48 -0
- Rscripts/setup_unified.R +284 -0
- SF_biodiv_access_shiny.Rproj +13 -0
- app.R +0 -0
- code/iso_metric_definitions.R +70 -0
- code/iso_metrics_AUDITED.R +688 -0
- code/setup_unified.R +287 -0
- install.r +51 -0
- outputs/results/gbif_cell_species_100m.rds +3 -0
- outputs/results/grid_results_100m.gpkg +3 -0
- outputs/results/isochrone_basi_100m.gpkg +3 -0
- outputs/results/isochrone_basi_100m.rds +3 -0
- outputs/results/muni_route_cells_100m.rds +0 -0
- rsconnect/shinyapps.io/diego-ellis-soto/RSF_Biodiversity_Access.dcf +12 -0
- rsconnect/shinyapps.io/diego-ellis-soto/SF_biodiv_access.dcf +13 -0
- www/California_academy_logo.png +0 -0
- www/Combined_logos.png +3 -0
- www/Reimagining_San_Francisco.png +3 -0
- www/UC_Berkeley_logo.png +0 -0
- www/app_pastel.css +89 -0
- www/hexbin_RSF_logo.png +0 -0
.gitattributes
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
outputs/results/isochrone_basi_100m.gpkg filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
outputs/results/isochrone_basi_100m.rds filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
www/Combined_logos.png filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
www/Reimagining_San_Francisco.png filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
outputs/results/gbif_cell_species_100m.rds filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
outputs/results/grid_results_100m.gpkg filter=lfs diff=lfs merge=lfs -text
|
.github/workflows/deploy.yml
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: Sync to Hugging Face hub
|
| 2 |
+
on:
|
| 3 |
+
push:
|
| 4 |
+
branches: [main]
|
| 5 |
+
|
| 6 |
+
# to run this workflow manually from the Actions tab
|
| 7 |
+
workflow_dispatch:
|
| 8 |
+
|
| 9 |
+
jobs:
|
| 10 |
+
sync-to-hub:
|
| 11 |
+
runs-on: ubuntu-latest
|
| 12 |
+
steps:
|
| 13 |
+
- uses: actions/checkout@v3
|
| 14 |
+
with:
|
| 15 |
+
fetch-depth: 0
|
| 16 |
+
lfs: true
|
| 17 |
+
- name: Push to hub
|
| 18 |
+
env:
|
| 19 |
+
HF_TOKEN: ${{ secrets.HF_TOKEN }}
|
| 20 |
+
run: git push -f https://cboettig:$HF_TOKEN@huggingface.co/spaces/boettiger-lab/sf_biodiv_access_shiny/ main
|
.gitignore
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# macOS system files
|
| 2 |
+
.DS_Store
|
| 3 |
+
data
|
| 4 |
+
.Rproj.user
|
| 5 |
+
.positai
|
| 6 |
+
old
|
| 7 |
+
startup_benchmarks.csv
|
| 8 |
+
startup_benchmarks.png
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# R session files
|
| 12 |
+
.Rhistory
|
| 13 |
+
.RData
|
| 14 |
+
.Ruserdata
|
| 15 |
+
.Rproj.user/
|
| 16 |
+
Renviron
|
| 17 |
+
.env
|
| 18 |
+
.env.*
|
| 19 |
+
.DS_Store
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# Secrets / local environment
|
| 23 |
+
.Renviron
|
| 24 |
+
.env
|
| 25 |
+
.env.*
|
| 26 |
+
*.key
|
| 27 |
+
*.pem
|
| 28 |
+
|
| 29 |
+
# macOS
|
| 30 |
+
.DS_Store
|
| 31 |
+
|
| 32 |
+
# RStudio
|
| 33 |
+
*.Rproj.user
|
| 34 |
+
|
| 35 |
+
# Temporary / cache
|
| 36 |
+
tmp/
|
| 37 |
+
temp/
|
| 38 |
+
cache/
|
| 39 |
+
*.log
|
| 40 |
+
|
| 41 |
+
# Docker/local build clutter
|
| 42 |
+
docker-compose.override.yml
|
| 43 |
+
|
| 44 |
+
# Large local analysis outputs not needed by deployed app
|
| 45 |
+
outputs/figures/
|
| 46 |
+
outputs/tables/
|
| 47 |
+
|
| 48 |
+
# Optional: raw or intermediate data not needed by Shiny
|
| 49 |
+
data/raw/
|
| 50 |
+
data/intermediate/
|
| 51 |
+
|
| 52 |
+
|
Dockerfile
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM quay.io/jupyter/minimal-notebook:ubuntu-24.04
|
| 2 |
+
|
| 3 |
+
USER root
|
| 4 |
+
|
| 5 |
+
# Install R
|
| 6 |
+
RUN curl -s https://raw.githubusercontent.com/boettiger-lab/repo2docker-r/refs/heads/main/install_r.sh | bash
|
| 7 |
+
|
| 8 |
+
WORKDIR /code
|
| 9 |
+
|
| 10 |
+
# Install R packages first.
|
| 11 |
+
# This layer only rebuilds when install.r changes.
|
| 12 |
+
COPY install.r .
|
| 13 |
+
RUN Rscript install.r
|
| 14 |
+
|
| 15 |
+
# Copy application and data after packages are installed.
|
| 16 |
+
COPY . .
|
| 17 |
+
|
| 18 |
+
# Hugging Face Spaces
|
| 19 |
+
EXPOSE 7860
|
| 20 |
+
|
| 21 |
+
CMD ["R", "--quiet", "-e", "shiny::runApp('app.R', host='0.0.0.0', port=7860)"]
|
README.md
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Sf Biodiv Access Shiny
|
| 3 |
+
emoji: 📚
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: yellow
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# SF Biodiversity Access Decision Support Tool
|
| 11 |
+
|
| 12 |
+
<img src="www/Combined_logos.png" width="60%">
|
| 13 |
+
|
| 14 |
+
An interactive Shiny app built for the **Reimagining San Francisco (RSF) Initiative** to explore how equitably San Francisco residents can access urban biodiversity across different transportation modes and socioeconomic contexts.
|
| 15 |
+
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
## What the App Does
|
| 19 |
+
|
| 20 |
+
Users select a location anywhere in San Francisco — either by clicking the map or geocoding an address — and choose one or more transportation modes and travel-time thresholds (5, 10, or 15 minutes). The app then:
|
| 21 |
+
|
| 22 |
+
1. **Generates travel-time isochrones** — polygons representing the area reachable within the chosen time budget — using Mapbox (driving, walking, cycling, traffic-aware driving) and SF Muni GTFS data (transit, walk + transit).
|
| 23 |
+
2. **Summarizes biodiversity** within each isochrone: GBIF occurrence records, unique species richness, and breakdowns by taxonomic group (birds, mammals, plants).
|
| 24 |
+
3. **Summarizes greenspace access**: OSM greenspace coverage (%), distance to nearest greenspace via raster, and NDVI from Sentinel-2.
|
| 25 |
+
4. **Summarizes socioeconomic and environmental justice context**: area-weighted median income and population from ACS Census block groups, CalEnviroScreen Cumulative Impact scores, and SF Environmental Justice community burden scores.
|
| 26 |
+
5. **Computes the Biodiversity Access Index (BAI)** — a composite score benchmarked against citywide empirical distributions — and displays it as a spider/radar plot across seven dimensions: mobility access, route access, biodiversity potential, observation intensity, environmental quality, greenspace cover, and equity context.
|
| 27 |
+
6. **Displays partner RSF Program Projects** as a toggleable map layer.
|
| 28 |
+
|
| 29 |
+
---
|
| 30 |
+
|
| 31 |
+
## App Tabs
|
| 32 |
+
|
| 33 |
+
| Tab | Description |
|
| 34 |
+
|-----|-------------|
|
| 35 |
+
| **Isochrone Explorer** | Interactive map, isochrone generation, BAI spider plot, summary table, and metric plots |
|
| 36 |
+
| **Isochrone Comparer** | Pick two locations (one mode + travel time each) and compare them side by side: two BAI spider plots plus a difference table (transit score, biodiversity score, BAI, closest greenspace). Loads only when opened. |
|
| 37 |
+
| **GBIF Summaries** | Filter GBIF records by taxonomic class and family within the isochrone; species richness vs. sampling effort plot |
|
| 38 |
+
| **Community Science** | Map and table of partner community organizations |
|
| 39 |
+
| **About** | Full methodology, data sources, transport mode descriptions, and BAI explanation |
|
| 40 |
+
|
| 41 |
+
---
|
| 42 |
+
|
| 43 |
+
## Transportation Modes
|
| 44 |
+
|
| 45 |
+
| Mode | Engine | Notes |
|
| 46 |
+
|------|--------|-------|
|
| 47 |
+
| Driving | Mapbox Navigation API | Free-flow road network |
|
| 48 |
+
| Walking | Mapbox Navigation API | Pedestrian paths and crossings |
|
| 49 |
+
| Cycling | Mapbox Navigation API | Dedicated cycle lanes where available |
|
| 50 |
+
| Driving with Traffic | Mapbox Navigation API | Real-time + historical congestion |
|
| 51 |
+
| Transit (GTFS) | gtfsrouter | SF Muni timetable-based stop-to-stop routing |
|
| 52 |
+
| Walk + Transit (Muni) | Mapbox + gtfsrouter | First-mile walk + Muni ride + last-mile walk, all within total time budget |
|
| 53 |
+
|
| 54 |
+
---
|
| 55 |
+
|
| 56 |
+
## Map Layers
|
| 57 |
+
|
| 58 |
+
All layers are toggleable in the map's layer control panel:
|
| 59 |
+
|
| 60 |
+
- **Income** — Median household income per census block group (ACS 5-yr)
|
| 61 |
+
- **Greenspace** — OSM parks and green areas
|
| 62 |
+
- **Greenspace Distance** — Raster showing distance (m) to nearest greenspace
|
| 63 |
+
- **RSF Program Distance** — Raster showing distance (m) to nearest RSF program polygon
|
| 64 |
+
- **RSF Program Projects** — Partner project areas from the RSF Initiative
|
| 65 |
+
- **Hotspots / Coldspots (KnowBR)** — Block groups with anomalously high/low species richness relative to sampling effort
|
| 66 |
+
- **Species Richness** — Unique GBIF species per census block group
|
| 67 |
+
- **Data Availability** — GBIF occurrence records per block group
|
| 68 |
+
- **CalEnviroScreen (CI Score)** — Cumulative environmental burden by census tract
|
| 69 |
+
- **SF EJ Communities** — SF Environmental Justice community burden scores
|
| 70 |
+
- **Transit Routes** — All SF Muni routes from GTFS shapes, colored by official SFMTA route color
|
| 71 |
+
- **Transit Stops** — All SF Muni stops with AM peak headway info
|
| 72 |
+
- **Isochrones** — Generated travel-time polygons
|
| 73 |
+
- **NDVI Raster** — Sentinel-2 NDVI cropped to the isochrone union
|
| 74 |
+
|
| 75 |
+
---
|
| 76 |
+
|
| 77 |
+
## Biodiversity Access Index (BAI)
|
| 78 |
+
|
| 79 |
+
The BAI is a composite indicator benchmarked against **citywide empirical distributions** (ECDFs across all SF census block groups), not just the current session. It comprises seven equally-weighted dimensions:
|
| 80 |
+
|
| 81 |
+
| Dimension | Variable | Direction |
|
| 82 |
+
|-----------|----------|-----------|
|
| 83 |
+
| Mobility Access | Transit stops / km² | Higher = better |
|
| 84 |
+
| Route Access | Unique Muni routes crossing isochrone | Higher = better |
|
| 85 |
+
| Biodiversity Potential | Unique GBIF species | Higher = better |
|
| 86 |
+
| Observation Intensity | GBIF records / km² | Higher = better |
|
| 87 |
+
| Environmental Quality | Mean NDVI | Higher = better |
|
| 88 |
+
| Greenspace Cover | % OSM greenspace area | Higher = better |
|
| 89 |
+
| Equity Context | SF EJ burden score (inverted) | Lower burden = better |
|
| 90 |
+
|
| 91 |
+
All axes scaled 0–1. BAI = unweighted mean of all seven standardized components.
|
| 92 |
+
|
| 93 |
+
---
|
| 94 |
+
|
| 95 |
+
## Repository Structure
|
| 96 |
+
|
| 97 |
+
```
|
| 98 |
+
SF_biodiv_access_shiny/
|
| 99 |
+
├── app.R # Main app (sources Rscripts/setup_unified.R at startup)
|
| 100 |
+
├── Dockerfile # HF Spaces: install.r + shiny::runApp('app.R', …)
|
| 101 |
+
├── install.r # R package list for Docker
|
| 102 |
+
├── www/ # Static assets (e.g. app_pastel.css, logos)
|
| 103 |
+
├── Rscripts/
|
| 104 |
+
│ ├── setup_unified.R # Loads all app data: local data/cached + HuggingFace fallback
|
| 105 |
+
│ ├── iso_metrics.R # Shared point→isochrone→metrics→BAI→radar functions (used by both isochrone tabs)
|
| 106 |
+
│ └── prep/ # One-off builds → data/output/ (see run_all_prep.R)
|
| 107 |
+
└── data/
|
| 108 |
+
├── cached/ # Downloaded / runtime cache (often gitignored)
|
| 109 |
+
├── output/ # Prep outputs (often gitignored)
|
| 110 |
+
└── source/ # Raw inputs for prep (e.g. RSF polygons, GTFS extract)
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
> **Note:** The `data/` directory is gitignored. See setup instructions below.
|
| 114 |
+
|
| 115 |
+
---
|
| 116 |
+
|
| 117 |
+
## Cyberinfrastructure & Data Sources
|
| 118 |
+
|
| 119 |
+
| Dataset | Source | Format | How Loaded |
|
| 120 |
+
|---------|--------|--------|-----------|
|
| 121 |
+
| GBIF occurrences (SF) | Global Biodiversity Information Facility | Parquet | DuckDB spatial queries in server |
|
| 122 |
+
| Census block groups + ACS | US Census / tidycensus | `.Rdata` | Downloaded from HuggingFace at startup |
|
| 123 |
+
| NDVI raster | Sentinel-2 (pre-processed) | GeoTIFF | Downloaded from HuggingFace at startup |
|
| 124 |
+
| OSM greenspace polygons | OpenStreetMap | Shapefile bundle | Downloaded from HuggingFace at startup |
|
| 125 |
+
| Greenspace distance rasters | Derived from OSM (see `making-greenspace-raster.R`) | GeoTIFF | Downloaded from HuggingFace at startup |
|
| 126 |
+
| RSF Program distance rasters | Derived from RSF polygons (see `making-rsfprogram-raster.R`) | GeoTIFF | Downloaded from HuggingFace at startup |
|
| 127 |
+
| Biodiversity hotspots/coldspots | KnowBR analysis on GBIF | Shapefile | Downloaded from HuggingFace at startup |
|
| 128 |
+
| SF Muni GTFS | SFMTA official GTFS feed | zip + rds + csv | Downloaded from HuggingFace at startup |
|
| 129 |
+
| CalEnviroScreen 4.0 | OEHHA | GeoPackage | Downloaded from HuggingFace at startup |
|
| 130 |
+
| SF EJ Communities | SF Environment | GeoPackage | Downloaded from HuggingFace at startup |
|
| 131 |
+
| RSF Program Projects | RSF Initiative | GeoPackage | Downloaded from HuggingFace at startup |
|
| 132 |
+
|
| 133 |
+
**Remote data** is hosted on HuggingFace at
|
| 134 |
+
[`boettiger-lab/sf_biodiv_access`](https://huggingface.co/datasets/boettiger-lab/sf_biodiv_access) and cached locally in `data/cached/` by `setup_unified.R`. The two most expensive products — the CBG × greenspace intersection and the transit-routing timetable — are precomputed in `Rscripts/prep/` and read at startup rather than recomputed, so the app starts quickly.
|
| 135 |
+
|
| 136 |
+
**GBIF queries** use [DuckDB](https://duckdb.org/) with the spatial extension, querying a local `.parquet` file directly via SQL `ST_Intersects` — avoiding loading the full dataset into memory.
|
| 137 |
+
|
| 138 |
+
---
|
| 139 |
+
|
| 140 |
+
## Local Development Setup
|
| 141 |
+
|
| 142 |
+
### Prerequisites
|
| 143 |
+
|
| 144 |
+
Install required R packages:
|
| 145 |
+
|
| 146 |
+
```r
|
| 147 |
+
install.packages(c(
|
| 148 |
+
"shiny", "shinydashboard", "leaflet", "mapboxapi", "tidyverse",
|
| 149 |
+
"tidycensus", "sf", "DT", "RColorBrewer", "terra", "data.table",
|
| 150 |
+
"mapview", "sjPlot", "sjlabelled", "bslib", "shinycssloaders",
|
| 151 |
+
"DBI", "duckdb", "dbplyr", "gtfsrouter", "tidytransit", "fmsb", "scales"
|
| 152 |
+
))
|
| 153 |
+
```
|
| 154 |
+
|
| 155 |
+
### First-Time Setup
|
| 156 |
+
|
| 157 |
+
**Step 1: Build GBIF parquets** (one-time; needs local `gbif.duckdb` path in script)
|
| 158 |
+
```r
|
| 159 |
+
source("Rscripts/prep/create_annotated_gbif_parquet.R")
|
| 160 |
+
# Creates: data/output/sf-gbif.parquet, data/output/gbif_census_ndvi_anno.parquet
|
| 161 |
+
```
|
| 162 |
+
|
| 163 |
+
**Step 2: Pre-compute GTFS timetable and greenspace / RSF rasters** (one-time, slow)
|
| 164 |
+
```r
|
| 165 |
+
source("Rscripts/prep/run_all_prep.R")
|
| 166 |
+
```
|
| 167 |
+
|
| 168 |
+
**Step 3: Run the app**
|
| 169 |
+
```r
|
| 170 |
+
shiny::runApp("app.R")
|
| 171 |
+
```
|
| 172 |
+
|
| 173 |
+
### Startup Performance
|
| 174 |
+
|
| 175 |
+
Typical startup time: **~6–12 seconds** depending on whether caches exist.
|
| 176 |
+
|
| 177 |
+
---
|
| 178 |
+
|
| 179 |
+
## Cloud Deployment (HuggingFace Spaces)
|
| 180 |
+
|
| 181 |
+
The app is deployed via Docker on HuggingFace Spaces using `app.R` + `Rscripts/setup_unified.R`. Data is downloaded from the HuggingFace dataset repository into `data/cached/` at startup — so no large files need to be bundled in the image.
|
| 182 |
+
|
| 183 |
+
See `Dockerfile` for the deployment configuration.
|
| 184 |
+
|
| 185 |
+
---
|
| 186 |
+
|
| 187 |
+
## Authors
|
| 188 |
+
|
| 189 |
+
**Diego Ellis Soto**, Avery Hill, Christopher J. Schell, Carl Boettiger, Rebecca Johnson
|
| 190 |
+
University of California Berkeley (ESPM) · California Academy of Sciences
|
| 191 |
+
Contact: [diego.ellissoto@berkeley.edu](mailto:diego.ellissoto@berkeley.edu)
|
| 192 |
+
|
| 193 |
+
---
|
| 194 |
+
|
| 195 |
+
## Status & Roadmap
|
| 196 |
+
|
| 197 |
+
This tool is a **decision-support prototype** co-developed with the RSF Data Working Group. The BAI should be treated as an exploratory indicator; variable weights and reference distributions are subject to revision through stakeholder co-development.
|
| 198 |
+
|
| 199 |
+
**Planned additions:**
|
| 200 |
+
- Impervious surface coverage
|
| 201 |
+
- National Walkability Index
|
| 202 |
+
- CDC Social Vulnerability Index
|
| 203 |
+
- NatureServe biodiversity / rarity maps
|
| 204 |
+
- Frequency-weighted multimodal transit accessibility
|
| 205 |
+
- Pre-cached transit isochrones for faster queries
|
| 206 |
+
|
| 207 |
+
---
|
| 208 |
+
|
| 209 |
+
<img src="www/hexbin_RSF_logo.png" width="80">
|
Rscripts/iso_metrics.R
ADDED
|
@@ -0,0 +1,709 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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2 |
+
# Shared isochrone + metric functions (sourced once by app.R at startup)
|
| 3 |
+
# ============================================================================
|
| 4 |
+
# These four functions are the single source of truth for turning a point into
|
| 5 |
+
# an isochrone, scoring it, and drawing its BAI spider plot. Both the main
|
| 6 |
+
# "Isochrone Explorer" tab and the "Isochrone Comparer" tab call them, so the
|
| 7 |
+
# scoring logic lives in exactly one place.
|
| 8 |
+
#
|
| 9 |
+
# build_isochrones() point + modes/times -> isochrone sf (mode,time,geom)
|
| 10 |
+
# compute_iso_metrics() isochrone sf + point -> per-isochrone metric data.frame
|
| 11 |
+
# add_bai() metric df + benchmarks -> df + 7 BAI axes + composite BAI
|
| 12 |
+
# draw_radar() BAI df -> spider/radar plot (base graphics)
|
| 13 |
+
#
|
| 14 |
+
# These functions read the *static* objects loaded by Rscripts/setup_unified.R
|
| 15 |
+
# directly as globals (cbg_vect_sf, osm_greenspace, the distance/NDVI rasters,
|
| 16 |
+
# rsf_projects, cbg_greenspace_coverage, gtfs_stops_sf, gtfs_routes_sf,
|
| 17 |
+
# gtfs_router, transit_iso_cache, cenv_sf, sf_ej_sf) plus the helpers/config
|
| 18 |
+
# defined at the top of app.R (mapbox_token, mode_palette, pretty_mode, ecdf01,
|
| 19 |
+
# standardize_iso_sf, build_walk_transit_isochrone, safe_biodiv_hotspots/coldspots).
|
| 20 |
+
#
|
| 21 |
+
# Two objects are created per-session inside server() and so are passed in as
|
| 22 |
+
# explicit arguments rather than read as globals:
|
| 23 |
+
# gbif_tab -- the session's DuckDB handle on the GBIF parquet
|
| 24 |
+
# city_benchmarks -- the citywide ECDF reference distributions for the BAI
|
| 25 |
+
#
|
| 26 |
+
# compute_iso_metrics() reports progress via withProgress(), so it must be
|
| 27 |
+
# called from within a Shiny reactive context (which both tabs satisfy).
|
| 28 |
+
# ============================================================================
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# ----------------------------------------------------------------------------
|
| 32 |
+
# build_isochrones(): point + chosen modes/times -> combined isochrone sf
|
| 33 |
+
# ----------------------------------------------------------------------------
|
| 34 |
+
# point: named numeric c(lon = , lat = ) (the shape chosen_point() stores), or NULL.
|
| 35 |
+
# modes: character vector from c("driving","walking","cycling","driving-traffic",
|
| 36 |
+
# "transit","walk_transit"). times: numeric minutes. The transit_* / walk_*
|
| 37 |
+
# arguments only matter when a transit mode is selected; they default to the
|
| 38 |
+
# main tab's defaults so the comparer can pass a single mode/time and ignore them.
|
| 39 |
+
# Returns an sf with columns mode, time, geometry (EPSG:4326), or NULL if nothing
|
| 40 |
+
# could be built.
|
| 41 |
+
build_isochrones <- function(point, modes, times,
|
| 42 |
+
transit_hour = 9,
|
| 43 |
+
walk_to_stop_min = 5,
|
| 44 |
+
walk_from_stop_min = 5,
|
| 45 |
+
transit_departure_window_min = 10) {
|
| 46 |
+
if (is.null(point) || length(modes) == 0 || length(times) == 0) return(NULL)
|
| 47 |
+
|
| 48 |
+
location_sf <- st_as_sf(
|
| 49 |
+
data.frame(lon = point["lon"], lat = point["lat"]),
|
| 50 |
+
coords = c("lon", "lat"),
|
| 51 |
+
crs = 4326
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
iso_list <- list()
|
| 55 |
+
times <- as.numeric(times)
|
| 56 |
+
|
| 57 |
+
# --- Mapbox modes (driving / walking / cycling / driving-traffic) ----------
|
| 58 |
+
mapbox_modes <- intersect(modes, c("driving", "walking", "cycling", "driving-traffic"))
|
| 59 |
+
for (mode in mapbox_modes) {
|
| 60 |
+
for (t in times) {
|
| 61 |
+
iso <- tryCatch(
|
| 62 |
+
mb_isochrone(location_sf, time = t, profile = mode, access_token = mapbox_token),
|
| 63 |
+
# Surface the Mapbox error to the log instead of silently dropping it --
|
| 64 |
+
# otherwise a failed call just looks like "no isochrone" with no clue why.
|
| 65 |
+
error = function(e) { warning("mb_isochrone failed (", mode, " ", t, " min): ", conditionMessage(e)); NULL }
|
| 66 |
+
)
|
| 67 |
+
if (!is.null(iso)) {
|
| 68 |
+
iso_std <- standardize_iso_sf(iso, mode_name = mode, time_min = t)
|
| 69 |
+
if (!is.null(iso_std)) iso_list <- append(iso_list, list(iso_std))
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
# --- Transit (GTFS) --------------------------------------------------------
|
| 75 |
+
if ("transit" %in% modes && !is.null(gtfs_router) && !is.null(gtfs_stops_sf)) {
|
| 76 |
+
stop_dists <- st_distance(location_sf, gtfs_stops_sf)
|
| 77 |
+
nearest_idx <- which.min(stop_dists)
|
| 78 |
+
nearest_id <- as.character(gtfs_stops_sf$stop_id[nearest_idx])
|
| 79 |
+
dep_secs <- as.numeric(transit_hour) * 3600
|
| 80 |
+
|
| 81 |
+
for (t in times) {
|
| 82 |
+
iso_poly <- NULL
|
| 83 |
+
|
| 84 |
+
if (!is.null(transit_iso_cache) &&
|
| 85 |
+
!is.null(transit_iso_cache[[nearest_id]]) &&
|
| 86 |
+
!is.null(transit_iso_cache[[nearest_id]][[as.character(t)]])) {
|
| 87 |
+
iso_poly <- transit_iso_cache[[nearest_id]][[as.character(t)]]
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
if (is.null(iso_poly)) {
|
| 91 |
+
iso_result <- tryCatch(
|
| 92 |
+
gtfsrouter::gtfs_isochrone(
|
| 93 |
+
gtfs = gtfs_router,
|
| 94 |
+
from = nearest_id,
|
| 95 |
+
start_time = dep_secs,
|
| 96 |
+
end_time = dep_secs + t * 60,
|
| 97 |
+
from_is_id = TRUE
|
| 98 |
+
),
|
| 99 |
+
error = function(e) NULL
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
if (!is.null(iso_result) && nrow(iso_result) > 2) {
|
| 103 |
+
reachable_sf <- gtfs_stops_sf |>
|
| 104 |
+
filter(stop_id %in% as.character(iso_result$stop_id))
|
| 105 |
+
|
| 106 |
+
if (nrow(reachable_sf) > 2) {
|
| 107 |
+
iso_poly <- st_convex_hull(st_union(reachable_sf))
|
| 108 |
+
} else if (nrow(reachable_sf) > 0) {
|
| 109 |
+
iso_poly <- st_union(st_buffer(st_transform(reachable_sf, 3857), 100)) |>
|
| 110 |
+
st_transform(4326)
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
if (!is.null(iso_poly)) {
|
| 116 |
+
iso_sf <- st_sf(
|
| 117 |
+
mode = "transit",
|
| 118 |
+
time = as.numeric(t),
|
| 119 |
+
geometry = st_geometry(st_as_sf(iso_poly)),
|
| 120 |
+
crs = 4326
|
| 121 |
+
)
|
| 122 |
+
iso_sf <- standardize_iso_sf(iso_sf, mode_name = "transit", time_min = t)
|
| 123 |
+
iso_list <- append(iso_list, list(iso_sf))
|
| 124 |
+
}
|
| 125 |
+
}
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
# --- Walk + Transit (Muni) -------------------------------------------------
|
| 129 |
+
if ("walk_transit" %in% modes && !is.null(gtfs_router) && !is.null(gtfs_stops_sf)) {
|
| 130 |
+
dep_secs <- as.numeric(transit_hour) * 3600
|
| 131 |
+
valid_times <- times[times > walk_to_stop_min]
|
| 132 |
+
|
| 133 |
+
for (t in valid_times) {
|
| 134 |
+
wt_iso <- tryCatch(
|
| 135 |
+
build_walk_transit_isochrone(
|
| 136 |
+
location_sf = location_sf,
|
| 137 |
+
total_time_min = t,
|
| 138 |
+
dep_secs = dep_secs,
|
| 139 |
+
walk_to_stop_min = walk_to_stop_min,
|
| 140 |
+
walk_from_stop_min = walk_from_stop_min,
|
| 141 |
+
gtfs_stops_sf = gtfs_stops_sf,
|
| 142 |
+
gtfs_router = gtfs_router,
|
| 143 |
+
mapbox_token = mapbox_token,
|
| 144 |
+
departure_window_min = transit_departure_window_min,
|
| 145 |
+
departure_step_min = 5,
|
| 146 |
+
max_last_mile_stops = 12,
|
| 147 |
+
include_first_mile_polygon = TRUE
|
| 148 |
+
),
|
| 149 |
+
error = function(e) NULL
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
if (!is.null(wt_iso) && nrow(wt_iso) > 0) {
|
| 153 |
+
iso_list <- append(iso_list, list(wt_iso))
|
| 154 |
+
}
|
| 155 |
+
}
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
if (length(iso_list) == 0) return(NULL)
|
| 159 |
+
|
| 160 |
+
dplyr::bind_rows(iso_list) |>
|
| 161 |
+
st_as_sf() |>
|
| 162 |
+
st_make_valid() |>
|
| 163 |
+
st_transform(4326)
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
# ----------------------------------------------------------------------------
|
| 168 |
+
# compute_iso_metrics(): isochrone sf -> per-isochrone metric data.frame
|
| 169 |
+
# ----------------------------------------------------------------------------
|
| 170 |
+
# iso_data: the sf returned by build_isochrones(). point: the c(lon, lat) the
|
| 171 |
+
# isochrones were built from (used for the nearest-greenspace / nearest-RSF
|
| 172 |
+
# lookups), or NULL. gbif_tab: the session's DuckDB handle on the GBIF parquet.
|
| 173 |
+
# Returns a data.frame (one row per isochrone) carrying aggregate summaries as
|
| 174 |
+
# attributes: bio_percentile, city/mean transit scores, closest_greenspace(+dist),
|
| 175 |
+
# closest_rsf_program(+dist).
|
| 176 |
+
compute_iso_metrics <- function(iso_data, point, gbif_tab) {
|
| 177 |
+
if (is.null(iso_data) || nrow(iso_data) == 0) return(data.frame())
|
| 178 |
+
|
| 179 |
+
hotspot_union <- safe_biodiv_hotspots()
|
| 180 |
+
coldspot_union <- safe_biodiv_coldspots()
|
| 181 |
+
|
| 182 |
+
if (!is.null(hotspot_union)) hotspot_union <- st_union(hotspot_union)
|
| 183 |
+
if (!is.null(coldspot_union)) coldspot_union <- st_union(coldspot_union)
|
| 184 |
+
|
| 185 |
+
acs_wide <- cbg_vect_sf |>
|
| 186 |
+
mutate(population = popE, med_income = medincE)
|
| 187 |
+
|
| 188 |
+
results <- data.frame()
|
| 189 |
+
n_isos <- nrow(iso_data)
|
| 190 |
+
|
| 191 |
+
user_point_sf <- NULL
|
| 192 |
+
if (!is.null(point)) {
|
| 193 |
+
user_point_sf <- st_as_sf(
|
| 194 |
+
data.frame(lon = point["lon"], lat = point["lat"]),
|
| 195 |
+
coords = c("lon", "lat"),
|
| 196 |
+
crs = 4326
|
| 197 |
+
)
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
min_dist_val_global <- NA_real_
|
| 201 |
+
osm_greenspace_name_global <- NA_character_
|
| 202 |
+
if (!is.null(user_point_sf) && exists("greenspace_dist_raster") && exists("greenspace_osmid_raster")) {
|
| 203 |
+
try({
|
| 204 |
+
# Distance from the selected point to its nearest greenspace, read straight
|
| 205 |
+
# from the distance raster. terra::extract() returns data.frame(ID, value),
|
| 206 |
+
# so the distance is column 2 -- pulling column 1 (the point ID) was the bug
|
| 207 |
+
# that made this come back as "1" instead of the real distance in metres.
|
| 208 |
+
min_dist_val_global <- (greenspace_dist_raster |> extract(vect(user_point_sf)) |> pull(2))[1]
|
| 209 |
+
user_point_osm_id <- (greenspace_osmid_raster |> extract(vect(user_point_sf)) |> pull(2))[1]
|
| 210 |
+
osm_greenspace_name_global <- osm_greenspace |>
|
| 211 |
+
mutate(osm_id = as.numeric(osm_id)) |>
|
| 212 |
+
filter(osm_id == user_point_osm_id) |>
|
| 213 |
+
pull(name)
|
| 214 |
+
if (length(osm_greenspace_name_global) == 0 || is.na(osm_greenspace_name_global[1])) {
|
| 215 |
+
osm_greenspace_name_global <- "Unnamed Greenspace"
|
| 216 |
+
} else {
|
| 217 |
+
osm_greenspace_name_global <- osm_greenspace_name_global[1]
|
| 218 |
+
}
|
| 219 |
+
}, silent = TRUE)
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
min_rsf_dist_global <- NA_real_
|
| 223 |
+
rsf_program_name_global <- NA_character_
|
| 224 |
+
if (!is.null(user_point_sf) && exists("rsfprogram_dist_raster") && exists("rsfprogram_id_raster") && exists("rsf_projects")) {
|
| 225 |
+
try({
|
| 226 |
+
# Distance is column 2 (column 1 is the point ID) -- same fix as greenspace above.
|
| 227 |
+
min_rsf_dist_global <- (rsfprogram_dist_raster |> extract(vect(user_point_sf)) |> pull(2))[1]
|
| 228 |
+
user_point_rsf_pid <- (rsfprogram_id_raster |> extract(vect(user_point_sf)) |> pull(2))[1]
|
| 229 |
+
rsf_program_name_global <- rsf_projects |>
|
| 230 |
+
dplyr::filter(as.numeric(.data$polygon_id) == as.numeric(user_point_rsf_pid)) |>
|
| 231 |
+
dplyr::pull(.data$prj_name)
|
| 232 |
+
if (length(rsf_program_name_global) == 0 || is.na(rsf_program_name_global[1])) {
|
| 233 |
+
rsf_program_name_global <- "Unknown RSF program"
|
| 234 |
+
} else {
|
| 235 |
+
rsf_program_name_global <- rsf_program_name_global[1]
|
| 236 |
+
}
|
| 237 |
+
}, silent = TRUE)
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
withProgress(message = "Analyzing isochrones...", value = 0, {
|
| 241 |
+
|
| 242 |
+
for (i in seq_len(n_isos)) {
|
| 243 |
+
poly_i <- iso_data[i, ]
|
| 244 |
+
vect_poly_i <- vect(poly_i)
|
| 245 |
+
|
| 246 |
+
incProgress(
|
| 247 |
+
1 / n_isos,
|
| 248 |
+
detail = paste0(
|
| 249 |
+
pretty_mode(as.character(poly_i$mode[[1]])),
|
| 250 |
+
" – ", poly_i$time[[1]], " min"
|
| 251 |
+
)
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
dist_hot_km <- if (!is.null(hotspot_union)) {
|
| 255 |
+
round(as.numeric(min(st_distance(poly_i, hotspot_union))) / 1000, 3)
|
| 256 |
+
} else NA_real_
|
| 257 |
+
|
| 258 |
+
dist_cold_km <- if (!is.null(coldspot_union)) {
|
| 259 |
+
round(as.numeric(min(st_distance(poly_i, coldspot_union))) / 1000, 3)
|
| 260 |
+
} else NA_real_
|
| 261 |
+
|
| 262 |
+
inter_acs <- tryCatch(intersect(vect(acs_wide), vect_poly_i) |> st_as_sf(), error = function(e) NULL)
|
| 263 |
+
|
| 264 |
+
pop_total <- 0
|
| 265 |
+
w_income <- NA_real_
|
| 266 |
+
if (!is.null(inter_acs) && nrow(inter_acs) > 0) {
|
| 267 |
+
inter_acs <- inter_acs |>
|
| 268 |
+
mutate(
|
| 269 |
+
area_num = as.numeric(st_area(st_transform(geometry, 3857))),
|
| 270 |
+
weighted_pop = population * (area_num / sum(area_num, na.rm = TRUE))
|
| 271 |
+
)
|
| 272 |
+
pop_total <- round(sum(inter_acs$weighted_pop, na.rm = TRUE))
|
| 273 |
+
w_income <- sum(inter_acs$med_income * inter_acs$area_num, na.rm = TRUE) /
|
| 274 |
+
sum(inter_acs$area_num, na.rm = TRUE)
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
iso_area_m2 <- as.numeric(st_area(st_transform(poly_i, 3857)))
|
| 278 |
+
iso_area_km2 <- round(iso_area_m2 / 1e6, 3)
|
| 279 |
+
|
| 280 |
+
# Greenspace area within this isochrone: use pre-cached per-CBG coverage
|
| 281 |
+
# (replaces slow per-isochrone raster cellSize() which took ~40-55 s each).
|
| 282 |
+
# Method: intersect isochrone with CBGs, scale each CBG's greenspace_m2 by
|
| 283 |
+
# the fraction of that CBG falling inside the isochrone, then sum.
|
| 284 |
+
gs_area_m2 <- tryCatch({
|
| 285 |
+
if (exists("cbg_greenspace_coverage") && !is.null(cbg_greenspace_coverage)) {
|
| 286 |
+
poly_proj <- st_transform(poly_i, 3857)
|
| 287 |
+
cbg_proj_i <- st_transform(cbg_vect_sf[, "GEOID"], 3857) |>
|
| 288 |
+
mutate(cbg_area_m2 = as.numeric(st_area(geometry))) |>
|
| 289 |
+
st_make_valid()
|
| 290 |
+
inter_df <- st_intersection(cbg_proj_i, poly_proj) |>
|
| 291 |
+
mutate(inter_area_m2 = as.numeric(st_area(geometry))) |>
|
| 292 |
+
st_drop_geometry() |>
|
| 293 |
+
left_join(
|
| 294 |
+
cbg_greenspace_coverage[, c("GEOID", "greenspace_m2", "cbg_area_m2")],
|
| 295 |
+
by = "GEOID", suffix = c("_iso", "_cbg")
|
| 296 |
+
) |>
|
| 297 |
+
mutate(
|
| 298 |
+
greenspace_m2 = tidyr::replace_na(greenspace_m2, 0),
|
| 299 |
+
cbg_area_m2 = dplyr::coalesce(cbg_area_m2_cbg, cbg_area_m2_iso),
|
| 300 |
+
contrib = ifelse(cbg_area_m2 > 0,
|
| 301 |
+
inter_area_m2 / cbg_area_m2 * greenspace_m2,
|
| 302 |
+
0)
|
| 303 |
+
)
|
| 304 |
+
sum(inter_df$contrib, na.rm = TRUE)
|
| 305 |
+
} else if (exists("greenspace_dist_raster")) {
|
| 306 |
+
# Fallback to raster method if cache is unavailable
|
| 307 |
+
dist_crop <- terra::crop(greenspace_dist_raster, vect_poly_i)
|
| 308 |
+
dist_mask <- terra::mask(dist_crop, vect_poly_i)
|
| 309 |
+
is_greenspace <- dist_mask == 0
|
| 310 |
+
cell_areas <- terra::cellSize(is_greenspace, unit = "m")
|
| 311 |
+
as.numeric(terra::global(cell_areas * is_greenspace, "sum", na.rm = TRUE)[1, 1])
|
| 312 |
+
} else {
|
| 313 |
+
0
|
| 314 |
+
}
|
| 315 |
+
}, error = function(e) 0)
|
| 316 |
+
gs_percent <- ifelse(iso_area_m2 > 0, 100 * gs_area_m2 / iso_area_m2, 0)
|
| 317 |
+
|
| 318 |
+
mean_ndvi <- NA_real_
|
| 319 |
+
if (exists("ndvi")) {
|
| 320 |
+
ndvi_vals <- tryCatch(values(terra::mask(terra::crop(ndvi, vect_poly_i), vect_poly_i)), error = function(e) NA)
|
| 321 |
+
ndvi_vals <- ndvi_vals[!is.na(ndvi_vals)]
|
| 322 |
+
mean_ndvi <- ifelse(length(ndvi_vals) > 0, round(mean(ndvi_vals, na.rm = TRUE), 3), NA_real_)
|
| 323 |
+
}
|
| 324 |
+
|
| 325 |
+
iso_wkt <- st_as_text(st_geometry(poly_i)[[1]])
|
| 326 |
+
gbif_summary <- tryCatch({
|
| 327 |
+
gbif_tab |>
|
| 328 |
+
filter(sql(glue("ST_Intersects(ST_GeomFromText(geom_wkt), ST_GeomFromText('{iso_wkt}'))"))) |>
|
| 329 |
+
summarise(
|
| 330 |
+
n_records = n(),
|
| 331 |
+
n_species = n_distinct(species),
|
| 332 |
+
n_birds = n_distinct(case_when(class == "Aves" ~ species, TRUE ~ NA_character_)),
|
| 333 |
+
n_mammals = n_distinct(case_when(class == "Mammalia" ~ species, TRUE ~ NA_character_)),
|
| 334 |
+
n_plants = n_distinct(case_when(
|
| 335 |
+
class %in% c("Magnoliopsida", "Liliopsida", "Pinopsida", "Polypodiopsida",
|
| 336 |
+
"Equisetopsida", "Bryopsida", "Marchantiopsida") ~ species,
|
| 337 |
+
TRUE ~ NA_character_
|
| 338 |
+
))
|
| 339 |
+
) |>
|
| 340 |
+
collect()
|
| 341 |
+
}, error = function(e) NULL)
|
| 342 |
+
n_records <- if (!is.null(gbif_summary) && nrow(gbif_summary) > 0) gbif_summary$n_records[[1]] else 0L
|
| 343 |
+
n_species <- if (!is.null(gbif_summary) && nrow(gbif_summary) > 0) gbif_summary$n_species[[1]] else 0L
|
| 344 |
+
n_birds <- if (!is.null(gbif_summary) && nrow(gbif_summary) > 0) gbif_summary$n_birds[[1]] else 0L
|
| 345 |
+
n_mammals <- if (!is.null(gbif_summary) && nrow(gbif_summary) > 0) gbif_summary$n_mammals[[1]] else 0L
|
| 346 |
+
n_plants <- if (!is.null(gbif_summary) && nrow(gbif_summary) > 0) gbif_summary$n_plants[[1]] else 0L
|
| 347 |
+
|
| 348 |
+
n_transit_stops <- NA_real_
|
| 349 |
+
transit_access_score <- NA_real_
|
| 350 |
+
freq_weighted_score <- NA_real_
|
| 351 |
+
mean_headway_iso <- NA_real_
|
| 352 |
+
nearest_stop_m <- NA_real_
|
| 353 |
+
nearest_stop_name <- NA_character_
|
| 354 |
+
|
| 355 |
+
if (!is.null(gtfs_stops_sf)) {
|
| 356 |
+
inter_transit <- tryCatch(st_intersection(gtfs_stops_sf, poly_i), error = function(e) NULL)
|
| 357 |
+
n_transit_stops <- if (!is.null(inter_transit)) nrow(inter_transit) else 0
|
| 358 |
+
|
| 359 |
+
dist_transit <- st_distance(poly_i, gtfs_stops_sf)
|
| 360 |
+
nearest_stop_m <- round(as.numeric(min(dist_transit)), 0)
|
| 361 |
+
nearest_stop_name <- gtfs_stops_sf$stop_name[which.min(dist_transit)]
|
| 362 |
+
|
| 363 |
+
transit_access_score <- ifelse(iso_area_km2 > 0, round(n_transit_stops / iso_area_km2, 2), NA_real_)
|
| 364 |
+
|
| 365 |
+
freq_weighted_score <- if (!is.null(inter_transit) &&
|
| 366 |
+
"mean_headway_min" %in% names(inter_transit) &&
|
| 367 |
+
nrow(inter_transit) > 0 &&
|
| 368 |
+
iso_area_km2 > 0) {
|
| 369 |
+
hw <- inter_transit$mean_headway_min
|
| 370 |
+
hw <- hw[!is.na(hw) & hw > 0]
|
| 371 |
+
if (length(hw) > 0) round(sum(60 / hw) / iso_area_km2, 2) else NA_real_
|
| 372 |
+
} else NA_real_
|
| 373 |
+
|
| 374 |
+
mean_headway_iso <- if (!is.null(inter_transit) &&
|
| 375 |
+
"mean_headway_min" %in% names(inter_transit) &&
|
| 376 |
+
nrow(inter_transit) > 0) {
|
| 377 |
+
round(mean(inter_transit$mean_headway_min, na.rm = TRUE), 1)
|
| 378 |
+
} else NA_real_
|
| 379 |
+
}
|
| 380 |
+
|
| 381 |
+
# Unique Muni route IDs whose shapes intersect this isochrone
|
| 382 |
+
n_unique_routes <- 0L
|
| 383 |
+
if (!is.null(gtfs_routes_sf)) {
|
| 384 |
+
routes_inter <- tryCatch(
|
| 385 |
+
st_intersection(gtfs_routes_sf[, "route_id"], poly_i),
|
| 386 |
+
error = function(e) NULL
|
| 387 |
+
)
|
| 388 |
+
n_unique_routes <- if (!is.null(routes_inter) && "route_id" %in% names(routes_inter)) {
|
| 389 |
+
length(unique(routes_inter$route_id))
|
| 390 |
+
} else 0L
|
| 391 |
+
}
|
| 392 |
+
|
| 393 |
+
sampling_density_km2 <- ifelse(iso_area_km2 > 0, round(n_records / iso_area_km2, 2), NA_real_)
|
| 394 |
+
|
| 395 |
+
mean_ciscore <- if (!is.null(cenv_sf)) {
|
| 396 |
+
tryCatch({
|
| 397 |
+
ce_inter <- st_intersection(cenv_sf, poly_i)
|
| 398 |
+
if (nrow(ce_inter) > 0) {
|
| 399 |
+
ce_inter$a <- as.numeric(st_area(st_transform(ce_inter, 3857)))
|
| 400 |
+
round(weighted.mean(ce_inter$CIscore, w = ce_inter$a, na.rm = TRUE), 1)
|
| 401 |
+
} else NA_real_
|
| 402 |
+
}, error = function(e) NA_real_)
|
| 403 |
+
} else NA_real_
|
| 404 |
+
|
| 405 |
+
mean_traffic_pctl <- if (!is.null(cenv_sf)) {
|
| 406 |
+
tryCatch({
|
| 407 |
+
ce_inter <- st_intersection(cenv_sf, poly_i)
|
| 408 |
+
if (nrow(ce_inter) > 0) {
|
| 409 |
+
ce_inter$a <- as.numeric(st_area(st_transform(ce_inter, 3857)))
|
| 410 |
+
round(weighted.mean(ce_inter$Traffic_Pctl, w = ce_inter$a, na.rm = TRUE), 1)
|
| 411 |
+
} else NA_real_
|
| 412 |
+
}, error = function(e) NA_real_)
|
| 413 |
+
} else NA_real_
|
| 414 |
+
|
| 415 |
+
mean_ej_score <- if (!is.null(sf_ej_sf)) {
|
| 416 |
+
tryCatch({
|
| 417 |
+
ej_inter <- st_intersection(sf_ej_sf, poly_i)
|
| 418 |
+
if (nrow(ej_inter) > 0) {
|
| 419 |
+
ej_inter$a <- as.numeric(st_area(st_transform(ej_inter, 3857)))
|
| 420 |
+
valid <- ej_inter[!is.na(ej_inter$score), ]
|
| 421 |
+
if (nrow(valid) > 0) {
|
| 422 |
+
round(weighted.mean(valid$score, w = valid$a, na.rm = TRUE), 1)
|
| 423 |
+
} else NA_real_
|
| 424 |
+
} else NA_real_
|
| 425 |
+
}, error = function(e) NA_real_)
|
| 426 |
+
} else NA_real_
|
| 427 |
+
|
| 428 |
+
row_i <- data.frame(
|
| 429 |
+
Mode = pretty_mode(as.character(poly_i$mode[[1]])),
|
| 430 |
+
Time = as.numeric(poly_i$time[[1]]),
|
| 431 |
+
IsochroneArea_km2 = iso_area_km2,
|
| 432 |
+
DistToHotspot_km = dist_hot_km,
|
| 433 |
+
DistToColdspot_km = dist_cold_km,
|
| 434 |
+
EstimatedPopulation = pop_total,
|
| 435 |
+
MedianIncome = round(w_income, 2),
|
| 436 |
+
MeanNDVI = mean_ndvi,
|
| 437 |
+
GBIF_Records = n_records,
|
| 438 |
+
GBIF_Species = n_species,
|
| 439 |
+
Bird_Species = n_birds,
|
| 440 |
+
Mammal_Species = n_mammals,
|
| 441 |
+
Plant_Species = n_plants,
|
| 442 |
+
SamplingDensity_km2 = sampling_density_km2,
|
| 443 |
+
Greenspace_percent = round(gs_percent, 2),
|
| 444 |
+
Transit_Stops = n_transit_stops,
|
| 445 |
+
Unique_Muni_Routes = n_unique_routes,
|
| 446 |
+
Transit_Access_Score = transit_access_score,
|
| 447 |
+
Freq_Weighted_Score = freq_weighted_score,
|
| 448 |
+
Mean_Headway_min = mean_headway_iso,
|
| 449 |
+
Nearest_Stop_m = nearest_stop_m,
|
| 450 |
+
Nearest_Stop_Name = nearest_stop_name,
|
| 451 |
+
CalEnviro_CIscore = mean_ciscore,
|
| 452 |
+
CalEnviro_Traffic_Pctl = mean_traffic_pctl,
|
| 453 |
+
SF_EJ_Score = mean_ej_score,
|
| 454 |
+
closest_greenspace = osm_greenspace_name_global,
|
| 455 |
+
closest_greenspace_dist_m = min_dist_val_global,
|
| 456 |
+
closest_rsf_program = rsf_program_name_global,
|
| 457 |
+
closest_rsf_program_dist_m = min_rsf_dist_global,
|
| 458 |
+
stringsAsFactors = FALSE
|
| 459 |
+
)
|
| 460 |
+
|
| 461 |
+
results <- rbind(results, row_i)
|
| 462 |
+
}
|
| 463 |
+
|
| 464 |
+
}) # end withProgress
|
| 465 |
+
|
| 466 |
+
union_wkt <- st_as_text(st_geometry(st_union(iso_data))[[1]])
|
| 467 |
+
union_n_species <- tryCatch({
|
| 468 |
+
gbif_tab |>
|
| 469 |
+
filter(sql(glue("ST_Intersects(ST_GeomFromText(geom_wkt), ST_GeomFromText('{union_wkt}'))"))) |>
|
| 470 |
+
summarise(n_species = n_distinct(species)) |>
|
| 471 |
+
collect() |>
|
| 472 |
+
pull(n_species)
|
| 473 |
+
}, error = function(e) 0L)
|
| 474 |
+
|
| 475 |
+
attr(results, "bio_percentile") <- round(100 * ecdf(cbg_vect_sf$unique_species)(union_n_species), 1)
|
| 476 |
+
|
| 477 |
+
if (!is.null(gtfs_stops_sf)) {
|
| 478 |
+
sf_city_area_km2 <- 121.4
|
| 479 |
+
attr(results, "city_transit_score") <- round(nrow(gtfs_stops_sf) / sf_city_area_km2, 2)
|
| 480 |
+
attr(results, "mean_transit_score") <- round(mean(results$Transit_Access_Score, na.rm = TRUE), 2)
|
| 481 |
+
attr(results, "mean_transit_stops") <- round(mean(results$Transit_Stops, na.rm = TRUE), 1)
|
| 482 |
+
attr(results, "mean_muni_routes") <- round(mean(results$Unique_Muni_Routes, na.rm = TRUE), 1)
|
| 483 |
+
} else {
|
| 484 |
+
attr(results, "city_transit_score") <- NA_real_
|
| 485 |
+
attr(results, "mean_transit_score") <- NA_real_
|
| 486 |
+
attr(results, "mean_transit_stops") <- NA_real_
|
| 487 |
+
attr(results, "mean_muni_routes") <- NA_real_
|
| 488 |
+
}
|
| 489 |
+
|
| 490 |
+
if (nrow(results) > 0) {
|
| 491 |
+
closest_gs <- results |>
|
| 492 |
+
filter(!is.na(closest_greenspace_dist_m)) |>
|
| 493 |
+
slice_min(closest_greenspace_dist_m, n = 1)
|
| 494 |
+
if (nrow(closest_gs) > 0) {
|
| 495 |
+
attr(results, "closest_greenspace") <- closest_gs$closest_greenspace[1]
|
| 496 |
+
attr(results, "closest_greenspace_dist_m") <- closest_gs$closest_greenspace_dist_m[1]
|
| 497 |
+
} else {
|
| 498 |
+
attr(results, "closest_greenspace") <- "None"
|
| 499 |
+
attr(results, "closest_greenspace_dist_m") <- NA_real_
|
| 500 |
+
}
|
| 501 |
+
|
| 502 |
+
closest_rsf <- results |>
|
| 503 |
+
filter(!is.na(closest_rsf_program_dist_m)) |>
|
| 504 |
+
slice_min(closest_rsf_program_dist_m, n = 1)
|
| 505 |
+
if (nrow(closest_rsf) > 0) {
|
| 506 |
+
attr(results, "closest_rsf_program") <- closest_rsf$closest_rsf_program[1]
|
| 507 |
+
attr(results, "closest_rsf_program_dist_m") <- closest_rsf$closest_rsf_program_dist_m[1]
|
| 508 |
+
} else {
|
| 509 |
+
attr(results, "closest_rsf_program") <- "None"
|
| 510 |
+
attr(results, "closest_rsf_program_dist_m") <- NA_real_
|
| 511 |
+
}
|
| 512 |
+
}
|
| 513 |
+
|
| 514 |
+
results
|
| 515 |
+
}
|
| 516 |
+
|
| 517 |
+
|
| 518 |
+
# ----------------------------------------------------------------------------
|
| 519 |
+
# add_bai(): metric df + citywide benchmarks -> df + 7 BAI axes + composite BAI
|
| 520 |
+
# ----------------------------------------------------------------------------
|
| 521 |
+
# df: a data.frame from compute_iso_metrics(). city_benchmarks: the list of
|
| 522 |
+
# citywide reference distributions (the ECDFs each axis is scored against).
|
| 523 |
+
# Returns df with the seven *_std columns (each 0-1) plus BAI (their row mean),
|
| 524 |
+
# or NULL if df is empty.
|
| 525 |
+
add_bai <- function(df, city_benchmarks) {
|
| 526 |
+
if (is.null(df) || nrow(df) == 0) return(NULL)
|
| 527 |
+
|
| 528 |
+
ref <- city_benchmarks
|
| 529 |
+
|
| 530 |
+
eq_ref_inverted <- if (length(ref$ej) > 0) max(ref$ej, na.rm = TRUE) - ref$ej else numeric(0)
|
| 531 |
+
eq_obs_inverted <- ifelse(is.na(df$SF_EJ_Score), NA_real_, max(ref$ej, na.rm = TRUE) - df$SF_EJ_Score)
|
| 532 |
+
|
| 533 |
+
tmp <- df |>
|
| 534 |
+
mutate(
|
| 535 |
+
Mobility_Access_std = ecdf01(Transit_Access_Score, ref$transit_density),
|
| 536 |
+
Biodiversity_Potential_std = ecdf01(GBIF_Species, ref$biodiversity),
|
| 537 |
+
Observation_Intensity_std = ecdf01(SamplingDensity_km2, ref$sampling),
|
| 538 |
+
Environmental_Quality_std = ecdf01(MeanNDVI, ref$ndvi),
|
| 539 |
+
Greenspace_Cover_std = ecdf01(Greenspace_percent, ref$greenspace_cover),
|
| 540 |
+
Equity_Context_std = ecdf01(eq_obs_inverted, eq_ref_inverted),
|
| 541 |
+
Route_Access_std = ecdf01(Unique_Muni_Routes, ref$route_access)
|
| 542 |
+
)
|
| 543 |
+
|
| 544 |
+
tmp$BAI <- rowMeans(
|
| 545 |
+
tmp[, c(
|
| 546 |
+
"Mobility_Access_std",
|
| 547 |
+
"Biodiversity_Potential_std",
|
| 548 |
+
"Observation_Intensity_std",
|
| 549 |
+
"Environmental_Quality_std",
|
| 550 |
+
"Greenspace_Cover_std",
|
| 551 |
+
"Equity_Context_std",
|
| 552 |
+
"Route_Access_std"
|
| 553 |
+
)],
|
| 554 |
+
na.rm = TRUE
|
| 555 |
+
)
|
| 556 |
+
|
| 557 |
+
tmp
|
| 558 |
+
}
|
| 559 |
+
|
| 560 |
+
|
| 561 |
+
# Axis order shared by both radar functions, and the column each axis reads from.
|
| 562 |
+
# Keep these two vectors aligned -- the group labels below assume this clockwise
|
| 563 |
+
# order (1-2 Urban Access, 3-4 Biodiversity, 5-6 Environment, 7 EJ).
|
| 564 |
+
RADAR_AXIS_COLS <- c("Mobility_Access_std", "Route_Access_std", "Biodiversity_Potential_std",
|
| 565 |
+
"Observation_Intensity_std", "Environmental_Quality_std",
|
| 566 |
+
"Greenspace_Cover_std", "Equity_Context_std")
|
| 567 |
+
RADAR_AXIS_LABELS <- c("Stop\nDensity", "Route\nDiversity", "Species\nRichness",
|
| 568 |
+
"Obs.\nIntensity", "Vegetation\n(NDVI)", "Greenspace\nCover", "EJ\nContext")
|
| 569 |
+
|
| 570 |
+
# Radial group subheaders placed just beyond the axis labels. Shared by both
|
| 571 |
+
# radar functions so the grouping (Urban Access / Biodiversity / Environment /
|
| 572 |
+
# EJ) is drawn identically. Call after fmsb::radarchart() has drawn the chart.
|
| 573 |
+
draw_radar_group_labels <- function() {
|
| 574 |
+
n_ax <- 7
|
| 575 |
+
angs <- pi/2 - (0:(n_ax - 1)) * (2 * pi / n_ax)
|
| 576 |
+
cat_r <- 1.48 # radius just outside axis labels (~1.2-1.3)
|
| 577 |
+
|
| 578 |
+
text(cat_r * cos(mean(angs[1:2])), cat_r * sin(mean(angs[1:2])),
|
| 579 |
+
"Urban Access", cex = 0.72, col = "#2166ac", font = 2, xpd = TRUE)
|
| 580 |
+
text(cat_r * cos(mean(angs[3:4])), cat_r * sin(mean(angs[3:4])),
|
| 581 |
+
"Biodiversity", cex = 0.72, col = "#1b7837", font = 2, xpd = TRUE)
|
| 582 |
+
text(cat_r * cos(mean(angs[5:6])), cat_r * sin(mean(angs[5:6])),
|
| 583 |
+
"Environment", cex = 0.72, col = "#762a83", font = 2, xpd = TRUE)
|
| 584 |
+
text(cat_r * cos(angs[7]), cat_r * sin(angs[7]),
|
| 585 |
+
"Environmental\nJustice", cex = 0.72, col = "#b2182b", font = 2, xpd = TRUE)
|
| 586 |
+
}
|
| 587 |
+
|
| 588 |
+
# ----------------------------------------------------------------------------
|
| 589 |
+
# draw_radar(): BAI df -> spider/radar plot (base graphics, via fmsb)
|
| 590 |
+
# ----------------------------------------------------------------------------
|
| 591 |
+
# bai_df: the data.frame returned by add_bai() (one row per isochrone). title:
|
| 592 |
+
# the chart title. Draws directly to the current graphics device, so call it
|
| 593 |
+
# from inside a renderPlot({ }). Lines are coloured by transport mode.
|
| 594 |
+
draw_radar <- function(bai_df, title = "Biodiversity Access Index Profile") {
|
| 595 |
+
if (is.null(bai_df) || nrow(bai_df) == 0) return(NULL)
|
| 596 |
+
|
| 597 |
+
radar_df <- bai_df |>
|
| 598 |
+
mutate(
|
| 599 |
+
ModeTime = paste0(Mode, "_", Time, "m"),
|
| 600 |
+
`Stop\nDensity` = Mobility_Access_std,
|
| 601 |
+
`Route\nDiversity` = Route_Access_std,
|
| 602 |
+
`Species\nRichness` = Biodiversity_Potential_std,
|
| 603 |
+
`Obs.\nIntensity` = Observation_Intensity_std,
|
| 604 |
+
`Vegetation\n(NDVI)`= Environmental_Quality_std,
|
| 605 |
+
`Greenspace\nCover` = Greenspace_Cover_std,
|
| 606 |
+
`EJ\nContext` = Equity_Context_std
|
| 607 |
+
) |>
|
| 608 |
+
select(
|
| 609 |
+
ModeTime,
|
| 610 |
+
`Stop\nDensity`,
|
| 611 |
+
`Route\nDiversity`,
|
| 612 |
+
`Species\nRichness`,
|
| 613 |
+
`Obs.\nIntensity`,
|
| 614 |
+
`Vegetation\n(NDVI)`,
|
| 615 |
+
`Greenspace\nCover`,
|
| 616 |
+
`EJ\nContext`
|
| 617 |
+
)
|
| 618 |
+
|
| 619 |
+
radar_mat <- as.data.frame(radar_df[, -1])
|
| 620 |
+
rownames(radar_mat) <- radar_df$ModeTime
|
| 621 |
+
|
| 622 |
+
radar_mat <- rbind(
|
| 623 |
+
rep(1, ncol(radar_mat)),
|
| 624 |
+
rep(0, ncol(radar_mat)),
|
| 625 |
+
radar_mat
|
| 626 |
+
)
|
| 627 |
+
|
| 628 |
+
labels <- rownames(radar_mat)[-(1:2)]
|
| 629 |
+
line_cols <- mode_palette[gsub("_(.*)$", "", labels)]
|
| 630 |
+
line_cols[is.na(line_cols)] <- "#666666"
|
| 631 |
+
|
| 632 |
+
# Extra margin so two-line axis labels aren't clipped
|
| 633 |
+
par(mar = c(2, 2, 3, 2))
|
| 634 |
+
|
| 635 |
+
fmsb::radarchart(
|
| 636 |
+
radar_mat,
|
| 637 |
+
axistype = 1,
|
| 638 |
+
pcol = line_cols,
|
| 639 |
+
plwd = 2,
|
| 640 |
+
plty = 1,
|
| 641 |
+
cglcol = "grey80",
|
| 642 |
+
cglty = 1,
|
| 643 |
+
cglwd = 0.8,
|
| 644 |
+
axislabcol = "grey40",
|
| 645 |
+
vlcex = 0.88,
|
| 646 |
+
title = title
|
| 647 |
+
)
|
| 648 |
+
|
| 649 |
+
draw_radar_group_labels()
|
| 650 |
+
|
| 651 |
+
legend(
|
| 652 |
+
"topright",
|
| 653 |
+
legend = labels,
|
| 654 |
+
col = line_cols,
|
| 655 |
+
lty = 1,
|
| 656 |
+
lwd = 2,
|
| 657 |
+
cex = 0.75,
|
| 658 |
+
bty = "n"
|
| 659 |
+
)
|
| 660 |
+
}
|
| 661 |
+
|
| 662 |
+
|
| 663 |
+
# ----------------------------------------------------------------------------
|
| 664 |
+
# draw_compare_radar(): overlay several BAI rows on ONE radar (Comparer tab)
|
| 665 |
+
# ----------------------------------------------------------------------------
|
| 666 |
+
# bai_rows: a list of single-row add_bai() data.frames (one per point).
|
| 667 |
+
# labels: legend text, one per row (e.g. "Point A: Walking 5 min").
|
| 668 |
+
# colors: one line colour per row -- this is where the A-vs-B colour scheme
|
| 669 |
+
# lives, so keep it in sync with the point markers / isochrones.
|
| 670 |
+
# Unlike draw_radar() (coloured by mode), this colours by *point* so two
|
| 671 |
+
# isochrones of the same mode are still distinguishable. Draws to the current
|
| 672 |
+
# device; call from inside renderPlot({ }).
|
| 673 |
+
draw_compare_radar <- function(bai_rows, labels, colors,
|
| 674 |
+
title = "Biodiversity Access Index — Point A vs Point B") {
|
| 675 |
+
if (length(bai_rows) == 0) return(NULL)
|
| 676 |
+
|
| 677 |
+
mat <- as.data.frame(do.call(rbind, lapply(bai_rows, function(d) as.numeric(d[1, RADAR_AXIS_COLS]))))
|
| 678 |
+
names(mat) <- RADAR_AXIS_LABELS
|
| 679 |
+
# fmsb wants the axis max (1) and min (0) as the first two rows.
|
| 680 |
+
mat <- rbind(rep(1, ncol(mat)), rep(0, ncol(mat)), mat)
|
| 681 |
+
|
| 682 |
+
par(mar = c(2, 2, 3, 2))
|
| 683 |
+
fmsb::radarchart(
|
| 684 |
+
mat,
|
| 685 |
+
axistype = 1,
|
| 686 |
+
pcol = colors,
|
| 687 |
+
pfcol = grDevices::adjustcolor(colors, alpha.f = 0.2), # translucent fills so overlaps read
|
| 688 |
+
plwd = 3,
|
| 689 |
+
plty = 1,
|
| 690 |
+
cglcol = "grey80",
|
| 691 |
+
cglty = 1,
|
| 692 |
+
cglwd = 0.8,
|
| 693 |
+
axislabcol = "grey40",
|
| 694 |
+
vlcex = 0.9,
|
| 695 |
+
title = title
|
| 696 |
+
)
|
| 697 |
+
|
| 698 |
+
draw_radar_group_labels()
|
| 699 |
+
|
| 700 |
+
legend(
|
| 701 |
+
"topright",
|
| 702 |
+
legend = labels,
|
| 703 |
+
col = colors,
|
| 704 |
+
lty = 1,
|
| 705 |
+
lwd = 3,
|
| 706 |
+
cex = 0.85,
|
| 707 |
+
bty = "n"
|
| 708 |
+
)
|
| 709 |
+
}
|
Rscripts/prep/build_cbg_greenspace_coverage.R
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================================================================
|
| 2 |
+
# Prep: Per-CBG greenspace coverage -> data/output
|
| 3 |
+
# ============================================================================
|
| 4 |
+
# Inputs: cbg_vect_sf + osm_greenspace (loaded via Rscripts/setup_unified.R,
|
| 5 |
+
# which pulls them from data/cached/ or HuggingFace)
|
| 6 |
+
# Output: data/output/cbg_greenspace_coverage.csv (GEOID, greenspace_m2, cbg_area_m2)
|
| 7 |
+
#
|
| 8 |
+
# The CBG x greenspace st_union + st_intersection is the most expensive single
|
| 9 |
+
# step at app startup. The inputs are static, so we precompute it here once and
|
| 10 |
+
# let setup_unified.R just read this small CSV instead of recomputing every run.
|
| 11 |
+
# Re-run when cbg_vect_sf or the greenspace polygons change, then upload
|
| 12 |
+
# data/output/cbg_greenspace_coverage.csv to HuggingFace (see upload_to_huggingface.R).
|
| 13 |
+
#
|
| 14 |
+
# Working directory must be the project root (folder containing data/ and Rscripts/).
|
| 15 |
+
# ============================================================================
|
| 16 |
+
|
| 17 |
+
# Reuse the app's canonical loader so coverage is computed from the exact same
|
| 18 |
+
# cbg_vect_sf + osm_greenspace the app uses (also gives us sf/dplyr, etc.).
|
| 19 |
+
source("Rscripts/setup_unified.R")
|
| 20 |
+
|
| 21 |
+
out_dir <- "data/output"
|
| 22 |
+
dir.create(out_dir, recursive = TRUE, showWarnings = FALSE)
|
| 23 |
+
|
| 24 |
+
cbg_proj <- st_transform(cbg_vect_sf[, "GEOID"], 3857) |>
|
| 25 |
+
mutate(cbg_area_m2 = as.numeric(st_area(geometry)))
|
| 26 |
+
gs_proj <- st_transform(osm_greenspace, 3857) |> st_make_valid()
|
| 27 |
+
gs_union <- st_union(gs_proj)
|
| 28 |
+
cbg_gs_inter <- st_intersection(cbg_proj, gs_union)
|
| 29 |
+
|
| 30 |
+
cbg_greenspace_coverage <- cbg_gs_inter |>
|
| 31 |
+
mutate(greenspace_m2 = as.numeric(st_area(geometry))) |>
|
| 32 |
+
st_drop_geometry() |>
|
| 33 |
+
group_by(GEOID) |>
|
| 34 |
+
summarise(greenspace_m2 = sum(greenspace_m2), .groups = "drop") |>
|
| 35 |
+
right_join(cbg_proj |> st_drop_geometry() |> dplyr::select(GEOID, cbg_area_m2), by = "GEOID") |>
|
| 36 |
+
mutate(
|
| 37 |
+
greenspace_m2 = tidyr::replace_na(greenspace_m2, 0),
|
| 38 |
+
GEOID = as.character(GEOID)
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
readr::write_csv(cbg_greenspace_coverage, file.path(out_dir, "cbg_greenspace_coverage.csv"))
|
| 42 |
+
message(glue::glue(
|
| 43 |
+
"[prep] wrote {nrow(cbg_greenspace_coverage)} CBG rows -> data/output/cbg_greenspace_coverage.csv"
|
| 44 |
+
))
|
Rscripts/prep/build_equity_layers.R
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================================================================
|
| 2 |
+
# Prep: CalEnviroScreen + SF EJ → GeoPackages in data/output
|
| 3 |
+
# ============================================================================
|
| 4 |
+
# Reads local inputs from data/source/, writes derived layers for HuggingFace upload.
|
| 5 |
+
|
| 6 |
+
library(sf)
|
| 7 |
+
library(dplyr)
|
| 8 |
+
|
| 9 |
+
out_dir <- "data/output"
|
| 10 |
+
dir.create(out_dir, recursive = TRUE, showWarnings = FALSE)
|
| 11 |
+
|
| 12 |
+
# --- CalEnviroScreen (SF tracts only) -----------------------------------------
|
| 13 |
+
|
| 14 |
+
sf::st_read("data/source/calenviroscreen40gdb_F_2021.gdb", quiet = TRUE) |>
|
| 15 |
+
filter(grepl("san francisco", County, ignore.case = TRUE), !is.na(CIscore)) |>
|
| 16 |
+
select(
|
| 17 |
+
Tract, CIscore, CIscoreP,
|
| 18 |
+
PM2_5, PM2_5_Pctl, Traffic, Traffic_Pctl,
|
| 19 |
+
Poverty, Poverty_Pctl, HousBurd, HousBurd_Pctl,
|
| 20 |
+
County
|
| 21 |
+
) |>
|
| 22 |
+
sf::st_transform(4326) |>
|
| 23 |
+
sf::st_make_valid() |>
|
| 24 |
+
sf::st_write(file.path(out_dir, "calenviro_sf.gpkg"), delete_dsn = TRUE, quiet = TRUE)
|
| 25 |
+
|
| 26 |
+
# --- SF Environmental Justice Communities -------------------------------------
|
| 27 |
+
|
| 28 |
+
sf::st_read(
|
| 29 |
+
"data/source/San Francisco Environmental Justice Communities Map_20260401/geo_export_e303e420-19f7-4166-9736-c1dbeda5b82e.shp",
|
| 30 |
+
quiet = TRUE
|
| 31 |
+
) |>
|
| 32 |
+
sf::st_transform(4326) |>
|
| 33 |
+
sf::st_make_valid() |>
|
| 34 |
+
sf::st_write(file.path(out_dir, "sf_ej_communities_map.gpkg"), delete_dsn = TRUE, quiet = TRUE)
|
Rscripts/prep/create_annotated_gbif_parquet.R
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================================================================
|
| 2 |
+
# Prep: GBIF (DuckDB) → sf-gbif.parquet → CBG + NDVI → gbif_census_ndvi_anno.parquet
|
| 3 |
+
# ============================================================================
|
| 4 |
+
# 1) COPY San Francisco iNaturalist occurrences from attached gbif.duckdb → parquet
|
| 5 |
+
# 2) Join census block groups, extract NDVI, write annotated parquet for the app
|
| 6 |
+
#
|
| 7 |
+
# Requires: local gbif.duckdb path below; data/source/cbg_vect_sf.Rdata,
|
| 8 |
+
# data/source/SF_EastBay_NDVI_Sentinel_10.tif
|
| 9 |
+
|
| 10 |
+
# library(tidyverse)
|
| 11 |
+
library(dplyr)
|
| 12 |
+
library(tidyr)
|
| 13 |
+
library(readr)
|
| 14 |
+
library(purrr)
|
| 15 |
+
library(tibble)
|
| 16 |
+
library(stringr)
|
| 17 |
+
library(forcats)
|
| 18 |
+
library(lubridate)
|
| 19 |
+
library(ggplot2)
|
| 20 |
+
library(duckdb)
|
| 21 |
+
library(sf)
|
| 22 |
+
library(terra)
|
| 23 |
+
library(arrow)
|
| 24 |
+
|
| 25 |
+
# ============================================================================
|
| 26 |
+
# 1) Export raw GBIF points
|
| 27 |
+
# ============================================================================
|
| 28 |
+
|
| 29 |
+
scon <- dbConnect(duckdb())
|
| 30 |
+
scon |> dbExecute("INSTALL spatial; LOAD spatial;")
|
| 31 |
+
scon |> dbExecute("ATTACH '~/Data/Occurrences/GBIF/gbif.duckdb' AS gbifmain;")
|
| 32 |
+
|
| 33 |
+
scon |> dbExecute(
|
| 34 |
+
"COPY
|
| 35 |
+
(SELECT DISTINCT year,kingdom,phylum,class,\"order\",family,genus,species,decimallatitude,decimallongitude,coordinateuncertaintyinmeters,institutioncode FROM gbifmain.gbif
|
| 36 |
+
NATURAL JOIN gbifmain.join_lookup
|
| 37 |
+
WHERE stateprovince='California'
|
| 38 |
+
AND county_name='San Francisco'
|
| 39 |
+
AND institutioncode='iNaturalist'
|
| 40 |
+
AND decimallatitude IS NOT NULL
|
| 41 |
+
AND decimallongitude IS NOT NULL)
|
| 42 |
+
TO
|
| 43 |
+
'data/output/sf-gbif.parquet'
|
| 44 |
+
(FORMAT parquet);
|
| 45 |
+
"
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
scon |> dbDisconnect(shutdown = TRUE)
|
| 49 |
+
|
| 50 |
+
# ============================================================================
|
| 51 |
+
# 2) Annotate: CBG + NDVI → gbif_census_ndvi_anno.parquet
|
| 52 |
+
# ============================================================================
|
| 53 |
+
|
| 54 |
+
gbif_in <- "data/output/sf-gbif.parquet"
|
| 55 |
+
cbg_rdata <- "data/source/cbg_vect_sf.Rdata"
|
| 56 |
+
ndvi_tif <- "data/source/SF_EastBay_NDVI_Sentinel_10.tif"
|
| 57 |
+
out_dir <- "data/output"
|
| 58 |
+
out_file <- file.path(out_dir, "gbif_census_ndvi_anno.parquet")
|
| 59 |
+
|
| 60 |
+
gbif <- arrow::read_parquet(gbif_in, as_data_frame = TRUE)
|
| 61 |
+
load(cbg_rdata)
|
| 62 |
+
ndvi <- terra::rast(ndvi_tif)
|
| 63 |
+
|
| 64 |
+
cbg_acs <- cbg_vect_sf |>
|
| 65 |
+
select(GEOID, medincE, popE, housingE)
|
| 66 |
+
|
| 67 |
+
pts <- gbif |>
|
| 68 |
+
filter(!is.na(decimallongitude), !is.na(decimallatitude)) |>
|
| 69 |
+
st_as_sf(coords = c("decimallongitude", "decimallatitude"), crs = 4326, remove = FALSE)
|
| 70 |
+
|
| 71 |
+
cbg_join <- st_join(pts, cbg_acs, join = st_within, left = TRUE)
|
| 72 |
+
|
| 73 |
+
cbg_join$ndvi_sentinel <- terra::extract(ndvi, cbg_join, ID = FALSE)$NDVI
|
| 74 |
+
|
| 75 |
+
out <- cbg_join |>
|
| 76 |
+
mutate(
|
| 77 |
+
geom_wkt = st_as_text(geometry),
|
| 78 |
+
longitude = decimallongitude,
|
| 79 |
+
latitude = decimallatitude
|
| 80 |
+
) |>
|
| 81 |
+
st_drop_geometry() |>
|
| 82 |
+
rename(
|
| 83 |
+
institutionCode = institutioncode,
|
| 84 |
+
coordinateUncertaintyInMeters = coordinateuncertaintyinmeters
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
dir.create(out_dir, recursive = TRUE, showWarnings = FALSE)
|
| 88 |
+
arrow::write_parquet(out, out_file)
|
Rscripts/prep/implement_optimizations.R
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================================================================
|
| 2 |
+
# Prep: GTFS timetable, stop headways, and feed zip → data/output
|
| 3 |
+
# ============================================================================
|
| 4 |
+
# Inputs: data/source/muni_gtfs-current/*.txt
|
| 5 |
+
# Outputs: data/output/gtfs_timetable_monday.rds
|
| 6 |
+
# data/output/gtfs_stop_headways.csv
|
| 7 |
+
# data/output/sf_muni_gtfs.zip
|
| 8 |
+
#
|
| 9 |
+
# Run after updating the GTFS extract. Then upload data/output/* to HuggingFace.
|
| 10 |
+
|
| 11 |
+
# library(tidyverse)
|
| 12 |
+
library(dplyr)
|
| 13 |
+
library(tidyr)
|
| 14 |
+
library(readr)
|
| 15 |
+
library(purrr)
|
| 16 |
+
library(tibble)
|
| 17 |
+
library(stringr)
|
| 18 |
+
library(forcats)
|
| 19 |
+
library(lubridate)
|
| 20 |
+
library(ggplot2)
|
| 21 |
+
library(gtfsrouter)
|
| 22 |
+
library(tidytransit)
|
| 23 |
+
library(glue)
|
| 24 |
+
|
| 25 |
+
out_dir <- "data/output"
|
| 26 |
+
dir.create(out_dir, recursive = TRUE, showWarnings = FALSE)
|
| 27 |
+
out_dir_abs <- normalizePath(out_dir, mustWork = TRUE)
|
| 28 |
+
|
| 29 |
+
gtfs_dir <- "data/source/muni_gtfs-current"
|
| 30 |
+
if (!dir.exists(gtfs_dir)) {
|
| 31 |
+
stop(glue("GTFS folder not found: {gtfs_dir}"))
|
| 32 |
+
}
|
| 33 |
+
gtfs_txt <- list.files(gtfs_dir, pattern = "\\.txt$", full.names = FALSE)
|
| 34 |
+
if (length(gtfs_txt) == 0L) {
|
| 35 |
+
stop(glue("No .txt files under {gtfs_dir}"))
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
# --- Zip feed (same bytes consumers will download from HuggingFace) ----------
|
| 39 |
+
# Absolute zip path before setwd(); normalizePath(zip) is NA if the file does not exist yet
|
| 40 |
+
zip_abs <- file.path(out_dir_abs, "sf_muni_gtfs.zip")
|
| 41 |
+
old_wd <- getwd()
|
| 42 |
+
setwd(gtfs_dir)
|
| 43 |
+
utils::zip(zip_abs, files = gtfs_txt)
|
| 44 |
+
setwd(old_wd)
|
| 45 |
+
|
| 46 |
+
# --- gtfsrouter Monday timetable ------------------------------------------------
|
| 47 |
+
gr <- gtfsrouter::extract_gtfs(zip_abs, quiet = TRUE)
|
| 48 |
+
tt <- gtfsrouter::gtfs_timetable(gr, day = "Monday")
|
| 49 |
+
saveRDS(tt, file.path(out_dir_abs, "gtfs_timetable_monday.rds"), compress = "gzip")
|
| 50 |
+
|
| 51 |
+
# --- AM peak headways (inspectable CSV) ---------------------------------------
|
| 52 |
+
gt <- tidytransit::read_gtfs(zip_abs)
|
| 53 |
+
hw <- tidytransit::get_stop_frequency(gt, start_time = 7 * 3600, end_time = 9 * 3600) |>
|
| 54 |
+
group_by(stop_id) |>
|
| 55 |
+
summarise(
|
| 56 |
+
mean_headway_min = mean(mean_headway, na.rm = TRUE) / 60,
|
| 57 |
+
n_departures_peak = sum(n_departures, na.rm = TRUE),
|
| 58 |
+
.groups = "drop"
|
| 59 |
+
) |>
|
| 60 |
+
mutate(stop_id = as.character(stop_id))
|
| 61 |
+
|
| 62 |
+
readr::write_csv(hw, file.path(out_dir_abs, "gtfs_stop_headways.csv"))
|
Rscripts/prep/making-greenspace-raster.R
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Here we make 2 raster layers related to the nearest greenspaces in San Francisco
|
| 2 |
+
# at 30m resolution
|
| 3 |
+
# - 1) The distance to the nearest greenspace
|
| 4 |
+
# - 2) The osm_id of the nearest greenspace (for looking up the polygon)
|
| 5 |
+
|
| 6 |
+
# Load libraries
|
| 7 |
+
# library(tidyverse)
|
| 8 |
+
library(terra)
|
| 9 |
+
library(tidyterra)
|
| 10 |
+
library(sf)
|
| 11 |
+
library(duckdb)
|
| 12 |
+
library(duckdbfs)
|
| 13 |
+
library(glue)
|
| 14 |
+
library(tictoc)
|
| 15 |
+
|
| 16 |
+
# Connect to DuckDB
|
| 17 |
+
tcon <- dbConnect(duckdb::duckdb())
|
| 18 |
+
|
| 19 |
+
# Load spatial extension
|
| 20 |
+
tcon |> dbExecute("
|
| 21 |
+
INSTALL spatial;
|
| 22 |
+
LOAD spatial;
|
| 23 |
+
SET memory_limit = '200GB';
|
| 24 |
+
SET preserve_insertion_order = false;
|
| 25 |
+
SET threads TO 8;
|
| 26 |
+
")
|
| 27 |
+
|
| 28 |
+
greenspace_shp <- "data/source/Greenspaces_osm_nad93/greenspaces_osm_nad83.shp"
|
| 29 |
+
|
| 30 |
+
greenspaces <- st_read(greenspace_shp, quiet = TRUE) |>
|
| 31 |
+
st_transform(3310)
|
| 32 |
+
|
| 33 |
+
# --- --- --- --- --- ---
|
| 34 |
+
# Commented out #
|
| 35 |
+
# --- --- --- --- --- ---
|
| 36 |
+
|
| 37 |
+
# # San Francisco county minus farallon islands
|
| 38 |
+
# sf <- st_read("data/source/CA_Counties/CA_Counties_TIGER2016.shp") %>%
|
| 39 |
+
# filter(NAME == "San Francisco") |>
|
| 40 |
+
# st_transform(3310) |>
|
| 41 |
+
# st_cast("POLYGON") %>%
|
| 42 |
+
# mutate(area = st_area(.)) |>
|
| 43 |
+
# slice_max(area)
|
| 44 |
+
#
|
| 45 |
+
library(tidycensus)
|
| 46 |
+
library(sf)
|
| 47 |
+
library(dplyr)
|
| 48 |
+
|
| 49 |
+
# requires a Census API key (install once via census_api_key())
|
| 50 |
+
sf <- get_acs(
|
| 51 |
+
geography = "county",
|
| 52 |
+
state = "CA",
|
| 53 |
+
variables = "B01003_001", # total population (dummy variable just to trigger geometry)
|
| 54 |
+
year = 2016,
|
| 55 |
+
geometry = TRUE
|
| 56 |
+
) %>%
|
| 57 |
+
filter(NAME == "San Francisco County, California") %>%
|
| 58 |
+
st_transform(3310) %>%
|
| 59 |
+
st_cast("POLYGON") %>%
|
| 60 |
+
mutate(area = st_area(geometry)) %>%
|
| 61 |
+
slice_max(area, n = 1) %>%
|
| 62 |
+
select(GEOID, NAME, geometry)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
empty.sr <- rast("data/source/slope.tif")
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
template.sr <- empty.sr %>%
|
| 69 |
+
# Give every cell an ID
|
| 70 |
+
mutate(cell_id = 1:ncell(.)) |>
|
| 71 |
+
# But filter to everything that isn't NA for slope values
|
| 72 |
+
filter(!is.na(prcnt_slope30)) |>
|
| 73 |
+
crop(sf |> st_transform(4326), mask = T) |>
|
| 74 |
+
crop(ext(c(-123, -122, 37.65, 37.85))) |>
|
| 75 |
+
trim()
|
| 76 |
+
|
| 77 |
+
# Convert template raster to points (centroids) for spatial joins
|
| 78 |
+
template_pts <- template.sr |>
|
| 79 |
+
as.points(na.rm = TRUE) |>
|
| 80 |
+
st_as_sf() |>
|
| 81 |
+
st_transform(4326) # Transform back to WGS84 for consistency
|
| 82 |
+
|
| 83 |
+
# Create directories if they don't exist
|
| 84 |
+
dir.create("data/intermediate", showWarnings = FALSE)
|
| 85 |
+
dir.create("data/output", showWarnings = FALSE)
|
| 86 |
+
|
| 87 |
+
# Write template to file for DuckDB
|
| 88 |
+
template_pts %>%
|
| 89 |
+
write_sf("data/intermediate/template_pts.gpkg")
|
| 90 |
+
|
| 91 |
+
# Load data into DuckDB and set up geometries
|
| 92 |
+
tcon %>% dbExecute(glue("
|
| 93 |
+
CREATE OR REPLACE TABLE greenspace_geo
|
| 94 |
+
AS
|
| 95 |
+
SELECT * EXCLUDE geom,
|
| 96 |
+
ST_TRANSFORM(geom, 'EPSG:4269', 'EPSG:3310', always_xy := true) AS geom3310,
|
| 97 |
+
ST_Centroid(geom) AS centroid_geom3310,
|
| 98 |
+
ST_SimplifyPreserveTopology(ST_TRANSFORM(geom, 'EPSG:4269', 'EPSG:3310', always_xy := true), 10) AS simple_geom3310
|
| 99 |
+
FROM ST_READ('{greenspace_shp}');
|
| 100 |
+
|
| 101 |
+
CREATE INDEX idx_grn_simple_geom ON greenspace_geo USING RTREE (simple_geom3310);
|
| 102 |
+
"))
|
| 103 |
+
|
| 104 |
+
# Load template grid into DuckDB
|
| 105 |
+
tcon %>% dbExecute("
|
| 106 |
+
CREATE OR REPLACE TABLE template_grid_geo
|
| 107 |
+
AS
|
| 108 |
+
SELECT
|
| 109 |
+
cell_id,
|
| 110 |
+
geom,
|
| 111 |
+
ST_TRANSFORM(geom, 'EPSG:4326', 'EPSG:3310', always_xy := true) AS geom3310
|
| 112 |
+
FROM ST_Read('data/intermediate/template_pts.gpkg');
|
| 113 |
+
|
| 114 |
+
CREATE INDEX idx_template_geom ON template_grid_geo USING RTREE (geom3310);
|
| 115 |
+
CREATE INDEX idx_template_cell_id ON template_grid_geo (cell_id);
|
| 116 |
+
")
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
# Calculate nearest greenspace distances
|
| 120 |
+
nn_query <- glue("
|
| 121 |
+
CREATE OR REPLACE TABLE grn_distance_complete AS
|
| 122 |
+
WITH distances AS (
|
| 123 |
+
SELECT
|
| 124 |
+
template.cell_id,
|
| 125 |
+
template.geom AS template_geom,
|
| 126 |
+
ST_AsText(template.geom) AS geom_wkt,
|
| 127 |
+
osm_id,
|
| 128 |
+
ST_Distance(template.geom3310, green.simple_geom3310) AS distance_meters
|
| 129 |
+
FROM template_grid_geo AS template, greenspace_geo AS green
|
| 130 |
+
)
|
| 131 |
+
SELECT
|
| 132 |
+
cell_id,
|
| 133 |
+
template_geom,
|
| 134 |
+
geom_wkt,
|
| 135 |
+
MIN(distance_meters) AS distance_to_greenspace_meters,
|
| 136 |
+
arg_min(osm_id, distance_meters) AS nearest_greenspace_osmid
|
| 137 |
+
FROM distances
|
| 138 |
+
GROUP BY cell_id, template_geom, geom_wkt;
|
| 139 |
+
")
|
| 140 |
+
|
| 141 |
+
cat("Calculating nearest greenspace distances...\n")
|
| 142 |
+
tic()
|
| 143 |
+
tcon %>% dbExecute(nn_query)
|
| 144 |
+
toc()
|
| 145 |
+
# Takes 138 seconds
|
| 146 |
+
# 287 seconds on mac
|
| 147 |
+
|
| 148 |
+
# Get results from database
|
| 149 |
+
sf_dist_complete <- tcon %>%
|
| 150 |
+
tbl("grn_distance_complete") %>%
|
| 151 |
+
select(cell_id, distance_to_greenspace_meters, nearest_greenspace_osmid) %>%
|
| 152 |
+
collect()
|
| 153 |
+
|
| 154 |
+
# Create empty raster based on template
|
| 155 |
+
empty_raster <- empty.sr
|
| 156 |
+
values(empty_raster) <- NA
|
| 157 |
+
|
| 158 |
+
# Join results back to preserve exact cell alignment
|
| 159 |
+
aligned_results <- tibble(cells = 1:ncell(empty.sr)) %>%
|
| 160 |
+
left_join(sf_dist_complete %>% rename(cells = cell_id), by = "cells")
|
| 161 |
+
|
| 162 |
+
# Create distance raster
|
| 163 |
+
greenspace_nearest_dist <- empty_raster
|
| 164 |
+
values(greenspace_nearest_dist) <- aligned_results$distance_to_greenspace_meters
|
| 165 |
+
names(greenspace_nearest_dist) <- "greenspace_nearest_dist"
|
| 166 |
+
cropped_greenspace_nearest_dist <- greenspace_nearest_dist |>
|
| 167 |
+
crop(sf |> st_transform(4326), mask = T) |>
|
| 168 |
+
crop(ext(c(-123, -122, 37.65, 37.85))) |>
|
| 169 |
+
trim()
|
| 170 |
+
|
| 171 |
+
# Create nearest greenspace ID raster
|
| 172 |
+
greenspace_nearest_osmid <- empty_raster
|
| 173 |
+
values(greenspace_nearest_osmid) <- as.numeric(aligned_results$nearest_greenspace_osmid)
|
| 174 |
+
names(greenspace_nearest_osmid) <- "greenspace_nearest_osmid"
|
| 175 |
+
cropped_greenspace_nearest_osmid <- greenspace_nearest_osmid |>
|
| 176 |
+
crop(sf |> st_transform(4326), mask = T) |>
|
| 177 |
+
crop(ext(c(-123, -122, 37.65, 37.85))) |>
|
| 178 |
+
trim()
|
| 179 |
+
|
| 180 |
+
plot(cropped_greenspace_nearest_dist)
|
| 181 |
+
plot(cropped_greenspace_nearest_osmid)
|
| 182 |
+
|
| 183 |
+
# Write rasters
|
| 184 |
+
writeRaster(cropped_greenspace_nearest_dist, "data/output/nearest_greenspace_dist.tif", overwrite = TRUE)
|
| 185 |
+
# Use INT8S (64-bit signed integer) to store OSM IDs without precision loss
|
| 186 |
+
writeRaster(cropped_greenspace_nearest_osmid, "data/output/nearest_greenspace_osmid.tif", overwrite = TRUE, datatype = "INT8S")
|
Rscripts/prep/making-rsfprogram-raster.R
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================================================================
|
| 2 |
+
# Prep: Nearest RSF Program rasters (30 m, same grid as greenspace)
|
| 3 |
+
# ============================================================================
|
| 4 |
+
# Produces:
|
| 5 |
+
# - nearest_rsfprogram_dist.tif — distance (m) to nearest RSF program polygon
|
| 6 |
+
# - nearest_rsfprogram_id.tif — polygon_id for lookup in RSF gpkg (prj_name)
|
| 7 |
+
#
|
| 8 |
+
# Source polygons: data/source/RSF_Program_Projects_polygons.gpkg
|
| 9 |
+
# Template grid: same slope.tif + SF county mask as making-greenspace-raster.R
|
| 10 |
+
# ============================================================================
|
| 11 |
+
|
| 12 |
+
# library(tidyverse)
|
| 13 |
+
library(dplyr)
|
| 14 |
+
library(tidyr)
|
| 15 |
+
library(readr)
|
| 16 |
+
library(purrr)
|
| 17 |
+
library(tibble)
|
| 18 |
+
library(stringr)
|
| 19 |
+
library(forcats)
|
| 20 |
+
library(lubridate)
|
| 21 |
+
library(ggplot2)
|
| 22 |
+
library(terra)
|
| 23 |
+
library(tidyterra)
|
| 24 |
+
library(sf)
|
| 25 |
+
library(duckdb)
|
| 26 |
+
library(glue)
|
| 27 |
+
library(tictoc)
|
| 28 |
+
library(tidycensus)
|
| 29 |
+
|
| 30 |
+
# ============================================================================
|
| 31 |
+
# DuckDB + spatial
|
| 32 |
+
# ============================================================================
|
| 33 |
+
tcon <- dbConnect(duckdb::duckdb())
|
| 34 |
+
|
| 35 |
+
tcon |> dbExecute("
|
| 36 |
+
INSTALL spatial;
|
| 37 |
+
LOAD spatial;
|
| 38 |
+
SET memory_limit = '200GB';
|
| 39 |
+
SET preserve_insertion_order = false;
|
| 40 |
+
SET threads TO 8;
|
| 41 |
+
")
|
| 42 |
+
|
| 43 |
+
rsf_gpkg <- "data/source/RSF_Program_Projects_polygons.gpkg"
|
| 44 |
+
|
| 45 |
+
# ============================================================================
|
| 46 |
+
# SF county boundary + template grid (match greenspace raster script)
|
| 47 |
+
# ============================================================================
|
| 48 |
+
sf <- get_acs(
|
| 49 |
+
geography = "county",
|
| 50 |
+
state = "CA",
|
| 51 |
+
variables = "B01003_001",
|
| 52 |
+
year = 2016,
|
| 53 |
+
geometry = TRUE
|
| 54 |
+
) |>
|
| 55 |
+
filter(NAME == "San Francisco County, California") |>
|
| 56 |
+
st_transform(3310) |>
|
| 57 |
+
st_cast("POLYGON") |>
|
| 58 |
+
mutate(area = st_area(geometry)) |>
|
| 59 |
+
slice_max(area, n = 1) |>
|
| 60 |
+
select(GEOID, NAME, geometry)
|
| 61 |
+
|
| 62 |
+
empty.sr <- rast("data/source/slope.tif")
|
| 63 |
+
|
| 64 |
+
template.sr <- empty.sr %>%
|
| 65 |
+
mutate(cell_id = 1:ncell(.)) %>%
|
| 66 |
+
filter(!is.na(prcnt_slope30)) %>%
|
| 67 |
+
crop(sf %>% st_transform(4326), mask = TRUE) %>%
|
| 68 |
+
crop(ext(c(-123, -122, 37.65, 37.85))) %>%
|
| 69 |
+
trim()
|
| 70 |
+
|
| 71 |
+
template_pts <- template.sr %>%
|
| 72 |
+
as.points(na.rm = TRUE) %>%
|
| 73 |
+
st_as_sf() %>%
|
| 74 |
+
st_transform(4326)
|
| 75 |
+
|
| 76 |
+
dir.create("data/intermediate", showWarnings = FALSE)
|
| 77 |
+
dir.create("data/output", showWarnings = FALSE)
|
| 78 |
+
|
| 79 |
+
template_pts |>
|
| 80 |
+
write_sf("data/intermediate/template_pts.gpkg")
|
| 81 |
+
|
| 82 |
+
# Escape single quotes in path for SQL (Windows paths unlikely here)
|
| 83 |
+
rsf_gpkg_sql <- gsub("'", "''", normalizePath(rsf_gpkg, winslash = "/", mustWork = TRUE))
|
| 84 |
+
|
| 85 |
+
tcon |> dbExecute(glue("
|
| 86 |
+
CREATE OR REPLACE TABLE rsf_geo AS
|
| 87 |
+
SELECT
|
| 88 |
+
polygon_id,
|
| 89 |
+
prj_name,
|
| 90 |
+
ST_TRANSFORM(geom, 'EPSG:4326', 'EPSG:3310', always_xy := true) AS geom3310,
|
| 91 |
+
ST_SimplifyPreserveTopology(
|
| 92 |
+
ST_TRANSFORM(geom, 'EPSG:4326', 'EPSG:3310', always_xy := true),
|
| 93 |
+
10
|
| 94 |
+
) AS simple_geom3310
|
| 95 |
+
FROM ST_Read('{rsf_gpkg_sql}');
|
| 96 |
+
|
| 97 |
+
CREATE INDEX idx_rsf_simple_geom ON rsf_geo USING RTREE (simple_geom3310);
|
| 98 |
+
"))
|
| 99 |
+
|
| 100 |
+
tcon |> dbExecute("
|
| 101 |
+
CREATE OR REPLACE TABLE template_grid_geo AS
|
| 102 |
+
SELECT
|
| 103 |
+
cell_id,
|
| 104 |
+
geom,
|
| 105 |
+
ST_TRANSFORM(geom, 'EPSG:4326', 'EPSG:3310', always_xy := true) AS geom3310
|
| 106 |
+
FROM ST_Read('data/intermediate/template_pts.gpkg');
|
| 107 |
+
|
| 108 |
+
CREATE INDEX idx_template_geom ON template_grid_geo USING RTREE (geom3310);
|
| 109 |
+
CREATE INDEX idx_template_cell_id ON template_grid_geo (cell_id);
|
| 110 |
+
")
|
| 111 |
+
|
| 112 |
+
nn_query <- glue("
|
| 113 |
+
CREATE OR REPLACE TABLE rsf_distance_complete AS
|
| 114 |
+
WITH distances AS (
|
| 115 |
+
SELECT
|
| 116 |
+
template.cell_id,
|
| 117 |
+
template.geom AS template_geom,
|
| 118 |
+
ST_AsText(template.geom) AS geom_wkt,
|
| 119 |
+
rsf.polygon_id,
|
| 120 |
+
ST_Distance(template.geom3310, rsf.simple_geom3310) AS distance_meters
|
| 121 |
+
FROM template_grid_geo AS template, rsf_geo AS rsf
|
| 122 |
+
)
|
| 123 |
+
SELECT
|
| 124 |
+
cell_id,
|
| 125 |
+
template_geom,
|
| 126 |
+
geom_wkt,
|
| 127 |
+
MIN(distance_meters) AS distance_to_rsf_meters,
|
| 128 |
+
arg_min(polygon_id, distance_meters) AS nearest_rsf_polygon_id
|
| 129 |
+
FROM distances
|
| 130 |
+
GROUP BY cell_id, template_geom, geom_wkt;
|
| 131 |
+
")
|
| 132 |
+
|
| 133 |
+
cat("Calculating nearest RSF program distances...\n")
|
| 134 |
+
tic()
|
| 135 |
+
tcon |> dbExecute(nn_query)
|
| 136 |
+
toc()
|
| 137 |
+
|
| 138 |
+
rsf_dist_complete <- tcon |>
|
| 139 |
+
tbl("rsf_distance_complete") |>
|
| 140 |
+
select(cell_id, distance_to_rsf_meters, nearest_rsf_polygon_id) |>
|
| 141 |
+
collect()
|
| 142 |
+
|
| 143 |
+
empty_raster <- empty.sr
|
| 144 |
+
values(empty_raster) <- NA
|
| 145 |
+
|
| 146 |
+
aligned_results <- tibble(cells = 1:ncell(empty.sr)) |>
|
| 147 |
+
left_join(rsf_dist_complete |> rename(cells = cell_id), by = "cells")
|
| 148 |
+
|
| 149 |
+
rsf_nearest_dist <- empty_raster
|
| 150 |
+
values(rsf_nearest_dist) <- aligned_results$distance_to_rsf_meters
|
| 151 |
+
names(rsf_nearest_dist) <- "rsfprogram_nearest_dist"
|
| 152 |
+
|
| 153 |
+
cropped_rsf_dist <- rsf_nearest_dist %>%
|
| 154 |
+
crop(sf %>% st_transform(4326), mask = TRUE) %>%
|
| 155 |
+
crop(ext(c(-123, -122, 37.65, 37.85))) %>%
|
| 156 |
+
trim()
|
| 157 |
+
|
| 158 |
+
rsf_nearest_id <- empty_raster
|
| 159 |
+
values(rsf_nearest_id) <- as.integer(round(as.numeric(aligned_results$nearest_rsf_polygon_id)))
|
| 160 |
+
names(rsf_nearest_id) <- "rsfprogram_nearest_id"
|
| 161 |
+
|
| 162 |
+
cropped_rsf_id <- rsf_nearest_id %>%
|
| 163 |
+
crop(sf %>% st_transform(4326), mask = TRUE) %>%
|
| 164 |
+
crop(ext(c(-123, -122, 37.65, 37.85))) %>%
|
| 165 |
+
trim()
|
| 166 |
+
|
| 167 |
+
plot(cropped_rsf_dist)
|
| 168 |
+
plot(cropped_rsf_id)
|
| 169 |
+
|
| 170 |
+
writeRaster(cropped_rsf_dist, "data/output/nearest_rsfprogram_dist.tif", overwrite = TRUE)
|
| 171 |
+
writeRaster(cropped_rsf_id, "data/output/nearest_rsfprogram_id.tif", overwrite = TRUE, datatype = "INT4S")
|
| 172 |
+
|
| 173 |
+
dbDisconnect(tcon, shutdown = TRUE)
|
Rscripts/prep/profile_startup.R
ADDED
|
@@ -0,0 +1,444 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================================================================
|
| 2 |
+
# Startup Performance Profiling
|
| 3 |
+
# ============================================================================
|
| 4 |
+
# This script benchmarks each data loading step in setup.R to identify
|
| 5 |
+
# bottlenecks and guide optimization decisions (precomputation, caching, etc.)
|
| 6 |
+
|
| 7 |
+
# library(tidyverse)
|
| 8 |
+
library(profvis)
|
| 9 |
+
|
| 10 |
+
# Create benchmark dataframe to track timings
|
| 11 |
+
benchmarks <- tibble(
|
| 12 |
+
step = character(),
|
| 13 |
+
description = character(),
|
| 14 |
+
time_sec = numeric(),
|
| 15 |
+
time_pct = numeric()
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
start_overall <- Sys.time()
|
| 19 |
+
|
| 20 |
+
# ============================================================================
|
| 21 |
+
# 1. Library Loading
|
| 22 |
+
# ============================================================================
|
| 23 |
+
t1 <- Sys.time()
|
| 24 |
+
|
| 25 |
+
library(shiny)
|
| 26 |
+
library(shinydashboard)
|
| 27 |
+
library(leaflet)
|
| 28 |
+
library(mapboxapi)
|
| 29 |
+
# library(tidyverse)
|
| 30 |
+
library(tidycensus)
|
| 31 |
+
library(sf)
|
| 32 |
+
library(DT)
|
| 33 |
+
library(RColorBrewer)
|
| 34 |
+
library(terra)
|
| 35 |
+
library(data.table)
|
| 36 |
+
library(mapview)
|
| 37 |
+
# library(sjPlot)
|
| 38 |
+
# library(sjlabelled)
|
| 39 |
+
library(bslib)
|
| 40 |
+
library(shinycssloaders)
|
| 41 |
+
library(DBI)
|
| 42 |
+
library(duckdb)
|
| 43 |
+
library(dbplyr)
|
| 44 |
+
library(gtfsrouter)
|
| 45 |
+
library(tidytransit)
|
| 46 |
+
library(fmsb)
|
| 47 |
+
library(scales)
|
| 48 |
+
|
| 49 |
+
time_libraries <- as.numeric(difftime(Sys.time(), t1, units = "secs"))
|
| 50 |
+
benchmarks <- bind_rows(benchmarks, tibble(
|
| 51 |
+
step = "00_libraries",
|
| 52 |
+
description = "Load all 23 packages",
|
| 53 |
+
time_sec = time_libraries,
|
| 54 |
+
time_pct = NA
|
| 55 |
+
))
|
| 56 |
+
|
| 57 |
+
# ============================================================================
|
| 58 |
+
# 2. Greenspace (OSM)
|
| 59 |
+
# ============================================================================
|
| 60 |
+
t1 <- Sys.time()
|
| 61 |
+
osm_greenspace <- st_read("/vsicurl/https://huggingface.co/datasets/boettiger-lab/sf_biodiv_access/resolve/main/greenspaces_osm_nad83.shp", quiet = TRUE) |>
|
| 62 |
+
st_transform(4326)
|
| 63 |
+
if (!"name" %in% names(osm_greenspace)) {
|
| 64 |
+
osm_greenspace$name <- "Unnamed Greenspace"
|
| 65 |
+
}
|
| 66 |
+
time_greenspace <- as.numeric(difftime(Sys.time(), t1, units = "secs"))
|
| 67 |
+
benchmarks <- bind_rows(benchmarks, tibble(
|
| 68 |
+
step = "01_greenspace",
|
| 69 |
+
description = "Load OSM greenspace polygons",
|
| 70 |
+
time_sec = time_greenspace,
|
| 71 |
+
time_pct = NA
|
| 72 |
+
))
|
| 73 |
+
|
| 74 |
+
# ============================================================================
|
| 75 |
+
# 3. Greenspace Distance Rasters
|
| 76 |
+
# ============================================================================
|
| 77 |
+
t1 <- Sys.time()
|
| 78 |
+
greenspace_dist_raster <- terra::rast("/vsicurl/https://huggingface.co/datasets/boettiger-lab/sf_biodiv_access/resolve/main/nearest_greenspace_dist.tif")
|
| 79 |
+
greenspace_osmid_raster <- terra::rast("/vsicurl/https://huggingface.co/datasets/boettiger-lab/sf_biodiv_access/resolve/main/nearest_greenspace_osmid.tif")
|
| 80 |
+
time_gs_rasters <- as.numeric(difftime(Sys.time(), t1, units = "secs"))
|
| 81 |
+
benchmarks <- bind_rows(benchmarks, tibble(
|
| 82 |
+
step = "02_gs_rasters",
|
| 83 |
+
description = "Load greenspace distance rasters",
|
| 84 |
+
time_sec = time_gs_rasters,
|
| 85 |
+
time_pct = NA
|
| 86 |
+
))
|
| 87 |
+
|
| 88 |
+
# ============================================================================
|
| 89 |
+
# 4. NDVI Raster
|
| 90 |
+
# ============================================================================
|
| 91 |
+
t1 <- Sys.time()
|
| 92 |
+
ndvi <- terra::rast("/vsicurl/https://huggingface.co/datasets/boettiger-lab/sf_biodiv_access/resolve/main/SF_EastBay_NDVI_Sentinel_10.tif")
|
| 93 |
+
time_ndvi <- as.numeric(difftime(Sys.time(), t1, units = "secs"))
|
| 94 |
+
benchmarks <- bind_rows(benchmarks, tibble(
|
| 95 |
+
step = "03_ndvi",
|
| 96 |
+
description = "Load NDVI raster",
|
| 97 |
+
time_sec = time_ndvi,
|
| 98 |
+
time_pct = NA
|
| 99 |
+
))
|
| 100 |
+
|
| 101 |
+
# ============================================================================
|
| 102 |
+
# 5. CBG Vector Data
|
| 103 |
+
# ============================================================================
|
| 104 |
+
t1 <- Sys.time()
|
| 105 |
+
download.file(
|
| 106 |
+
'https://huggingface.co/datasets/boettiger-lab/sf_biodiv_access/resolve/main/cbg_vect_sf.Rdata',
|
| 107 |
+
'/tmp/cbg_vect_sf.Rdata',
|
| 108 |
+
mode = 'wb',
|
| 109 |
+
quiet = TRUE
|
| 110 |
+
)
|
| 111 |
+
load('/tmp/cbg_vect_sf.Rdata')
|
| 112 |
+
|
| 113 |
+
if (!"unique_species" %in% names(cbg_vect_sf)) {
|
| 114 |
+
cbg_vect_sf$unique_species <- cbg_vect_sf$n_species
|
| 115 |
+
}
|
| 116 |
+
if (!"n_observations" %in% names(cbg_vect_sf)) {
|
| 117 |
+
cbg_vect_sf$n_observations <- cbg_vect_sf$n
|
| 118 |
+
}
|
| 119 |
+
if (!"median_inc" %in% names(cbg_vect_sf)) {
|
| 120 |
+
cbg_vect_sf$median_inc <- cbg_vect_sf$medincE
|
| 121 |
+
}
|
| 122 |
+
if (!"ndvi_mean" %in% names(cbg_vect_sf)) {
|
| 123 |
+
cbg_vect_sf$ndvi_mean <- cbg_vect_sf$ndvi_sentinel
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
time_cbg <- as.numeric(difftime(Sys.time(), t1, units = "secs"))
|
| 127 |
+
benchmarks <- bind_rows(benchmarks, tibble(
|
| 128 |
+
step = "04_cbg",
|
| 129 |
+
description = "Download + load census block groups",
|
| 130 |
+
time_sec = time_cbg,
|
| 131 |
+
time_pct = NA
|
| 132 |
+
))
|
| 133 |
+
|
| 134 |
+
# ============================================================================
|
| 135 |
+
# 6. Hotspots/Coldspots
|
| 136 |
+
# ============================================================================
|
| 137 |
+
t1 <- Sys.time()
|
| 138 |
+
biodiv_hotspots <- st_read("/vsicurl/https://huggingface.co/datasets/boettiger-lab/sf_biodiv_access/resolve/main/hotspots.shp", quiet = TRUE) |>
|
| 139 |
+
st_transform(4326)
|
| 140 |
+
biodiv_coldspots <- st_read("/vsicurl/https://huggingface.co/datasets/boettiger-lab/sf_biodiv_access/resolve/main/coldspots.shp", quiet = TRUE) |>
|
| 141 |
+
st_transform(4326)
|
| 142 |
+
time_hotcold <- as.numeric(difftime(Sys.time(), t1, units = "secs"))
|
| 143 |
+
benchmarks <- bind_rows(benchmarks, tibble(
|
| 144 |
+
step = "05_hotcold",
|
| 145 |
+
description = "Load hotspots and coldspots",
|
| 146 |
+
time_sec = time_hotcold,
|
| 147 |
+
time_pct = NA
|
| 148 |
+
))
|
| 149 |
+
|
| 150 |
+
# ============================================================================
|
| 151 |
+
# 7. RSF Projects
|
| 152 |
+
# ============================================================================
|
| 153 |
+
t1 <- Sys.time()
|
| 154 |
+
rsf_projects <- st_read("data/source/RSF_Program_Projects_polygons.gpkg", quiet = TRUE) |>
|
| 155 |
+
st_transform(4326)
|
| 156 |
+
time_rsf <- as.numeric(difftime(Sys.time(), t1, units = "secs"))
|
| 157 |
+
benchmarks <- bind_rows(benchmarks, tibble(
|
| 158 |
+
step = "06_rsf",
|
| 159 |
+
description = "Load RSF program projects",
|
| 160 |
+
time_sec = time_rsf,
|
| 161 |
+
time_pct = NA
|
| 162 |
+
))
|
| 163 |
+
|
| 164 |
+
# ============================================================================
|
| 165 |
+
# 8. GBIF Parquet Reference (just get path, don't load yet)
|
| 166 |
+
# ============================================================================
|
| 167 |
+
time_gbif_setup <- 0.001 # negligible
|
| 168 |
+
benchmarks <- bind_rows(benchmarks, tibble(
|
| 169 |
+
step = "07_gbif_setup",
|
| 170 |
+
description = "GBIF parquet path (lazy load in server)",
|
| 171 |
+
time_sec = time_gbif_setup,
|
| 172 |
+
time_pct = NA
|
| 173 |
+
))
|
| 174 |
+
|
| 175 |
+
# ============================================================================
|
| 176 |
+
# 9. GTFS Data
|
| 177 |
+
# ============================================================================
|
| 178 |
+
t1 <- Sys.time()
|
| 179 |
+
|
| 180 |
+
gtfs_path <- '/Users/diegoellis/Desktop/RSF_next_steps/GPFS_OSM_Transit/sf_muni_gtfs-current/'
|
| 181 |
+
|
| 182 |
+
# Stops
|
| 183 |
+
gtfs_stops_sf <- tryCatch({
|
| 184 |
+
read.csv(file.path(gtfs_path, 'stops.txt')) |>
|
| 185 |
+
st_as_sf(coords = c("stop_lon", "stop_lat"), crs = 4326)
|
| 186 |
+
}, error = function(e) {
|
| 187 |
+
warning("GTFS stops failed: ", e$message)
|
| 188 |
+
NULL
|
| 189 |
+
})
|
| 190 |
+
|
| 191 |
+
# Route shapes
|
| 192 |
+
gtfs_shapes_raw <- read.csv(file.path(gtfs_path, 'shapes.txt'))
|
| 193 |
+
gtfs_trips_raw <- read.csv(file.path(gtfs_path, 'trips.txt'))
|
| 194 |
+
gtfs_routes_raw <- read.csv(file.path(gtfs_path, 'routes.txt'))
|
| 195 |
+
|
| 196 |
+
shape_route_map <- gtfs_trips_raw |>
|
| 197 |
+
distinct(shape_id, route_id)
|
| 198 |
+
|
| 199 |
+
route_meta <- gtfs_routes_raw |>
|
| 200 |
+
select(route_id, route_short_name, route_long_name, route_color) |>
|
| 201 |
+
mutate(route_color_hex = paste0("#", trimws(route_color)))
|
| 202 |
+
|
| 203 |
+
shapes_split <- gtfs_shapes_raw |>
|
| 204 |
+
arrange(shape_id, shape_pt_sequence) |>
|
| 205 |
+
group_by(shape_id) |>
|
| 206 |
+
group_split()
|
| 207 |
+
|
| 208 |
+
shape_geoms <- lapply(shapes_split, function(s) {
|
| 209 |
+
st_linestring(cbind(s$shape_pt_lon, s$shape_pt_lat))
|
| 210 |
+
})
|
| 211 |
+
|
| 212 |
+
gtfs_routes_sf <- st_sf(
|
| 213 |
+
shape_id = sapply(shapes_split, function(s) s$shape_id[1]),
|
| 214 |
+
geometry = st_sfc(shape_geoms, crs = 4326)
|
| 215 |
+
) |>
|
| 216 |
+
left_join(shape_route_map, by = "shape_id") |>
|
| 217 |
+
left_join(route_meta, by = "route_id")
|
| 218 |
+
|
| 219 |
+
time_gtfs_basic <- as.numeric(difftime(Sys.time(), t1, units = "secs"))
|
| 220 |
+
benchmarks <- bind_rows(benchmarks, tibble(
|
| 221 |
+
step = "08_gtfs_basic",
|
| 222 |
+
description = "Load GTFS stops and route shapes",
|
| 223 |
+
time_sec = time_gtfs_basic,
|
| 224 |
+
time_pct = NA
|
| 225 |
+
))
|
| 226 |
+
|
| 227 |
+
# ============================================================================
|
| 228 |
+
# 10. gtfsrouter Initialization
|
| 229 |
+
# ============================================================================
|
| 230 |
+
t1 <- Sys.time()
|
| 231 |
+
|
| 232 |
+
gtfs_router <- tryCatch({
|
| 233 |
+
gtfs_zip_path <- tempfile(fileext = ".zip")
|
| 234 |
+
old_wd <- getwd()
|
| 235 |
+
setwd(gtfs_path)
|
| 236 |
+
utils::zip(gtfs_zip_path, files = list.files('.', pattern = "\\.txt$"))
|
| 237 |
+
setwd(old_wd)
|
| 238 |
+
|
| 239 |
+
gr <- gtfsrouter::extract_gtfs(gtfs_zip_path)
|
| 240 |
+
gtfsrouter::gtfs_timetable(gr, day = "Monday")
|
| 241 |
+
}, error = function(e) {
|
| 242 |
+
warning("gtfsrouter failed: ", e$message)
|
| 243 |
+
NULL
|
| 244 |
+
})
|
| 245 |
+
|
| 246 |
+
time_gtfs_router <- as.numeric(difftime(Sys.time(), t1, units = "secs"))
|
| 247 |
+
benchmarks <- bind_rows(benchmarks, tibble(
|
| 248 |
+
step = "09_gtfs_router",
|
| 249 |
+
description = "Initialize gtfsrouter + timetable",
|
| 250 |
+
time_sec = time_gtfs_router,
|
| 251 |
+
time_pct = NA
|
| 252 |
+
))
|
| 253 |
+
|
| 254 |
+
# ============================================================================
|
| 255 |
+
# 11. tidytransit Headways
|
| 256 |
+
# ============================================================================
|
| 257 |
+
t1 <- Sys.time()
|
| 258 |
+
|
| 259 |
+
gtfs_stop_headways <- tryCatch({
|
| 260 |
+
gt <- tidytransit::read_gtfs(gtfs_path)
|
| 261 |
+
tidytransit::get_stop_frequency(gt, start_time = 7 * 3600, end_time = 9 * 3600) |>
|
| 262 |
+
group_by(stop_id) |>
|
| 263 |
+
summarise(
|
| 264 |
+
mean_headway_min = mean(mean_headway, na.rm = TRUE) / 60,
|
| 265 |
+
n_departures_peak = sum(n_departures, na.rm = TRUE),
|
| 266 |
+
.groups = "drop"
|
| 267 |
+
) |>
|
| 268 |
+
mutate(stop_id = as.character(stop_id))
|
| 269 |
+
}, error = function(e) {
|
| 270 |
+
warning("tidytransit failed: ", e$message)
|
| 271 |
+
NULL
|
| 272 |
+
})
|
| 273 |
+
|
| 274 |
+
if (!is.null(gtfs_stop_headways) && !is.null(gtfs_stops_sf)) {
|
| 275 |
+
gtfs_stops_sf <- gtfs_stops_sf |>
|
| 276 |
+
mutate(stop_id = as.character(stop_id)) |>
|
| 277 |
+
left_join(gtfs_stop_headways, by = "stop_id")
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
time_gtfs_headways <- as.numeric(difftime(Sys.time(), t1, units = "secs"))
|
| 281 |
+
benchmarks <- bind_rows(benchmarks, tibble(
|
| 282 |
+
step = "10_gtfs_headways",
|
| 283 |
+
description = "Compute transit stop headways (AM peak)",
|
| 284 |
+
time_sec = time_gtfs_headways,
|
| 285 |
+
time_pct = NA
|
| 286 |
+
))
|
| 287 |
+
|
| 288 |
+
# ============================================================================
|
| 289 |
+
# 12. CalEnviroScreen
|
| 290 |
+
# ============================================================================
|
| 291 |
+
t1 <- Sys.time()
|
| 292 |
+
|
| 293 |
+
calenviro_path <- '/Users/diegoellis/Downloads/calenviroscreen40gdb_F_2021.gdb'
|
| 294 |
+
if (!file.exists(calenviro_path)) {
|
| 295 |
+
calenviro_path <- '/Users/diegoellis/Desktop/Projects/Presentations/Data_Schell_Lab_Tutorial/calenviroscreen40gdb_F_2021.gdb'
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
cenv_sf <- tryCatch({
|
| 299 |
+
sf::st_read(calenviro_path, quiet = TRUE) |>
|
| 300 |
+
dplyr::filter(grepl("san francisco", County, ignore.case = TRUE), !is.na(CIscore)) |>
|
| 301 |
+
dplyr::select(
|
| 302 |
+
Tract, CIscore, CIscoreP,
|
| 303 |
+
PM2_5, PM2_5_Pctl, Traffic, Traffic_Pctl,
|
| 304 |
+
Poverty, Poverty_Pctl, HousBurd, HousBurd_Pctl,
|
| 305 |
+
County
|
| 306 |
+
) |>
|
| 307 |
+
sf::st_transform(4326) |>
|
| 308 |
+
sf::st_make_valid()
|
| 309 |
+
}, error = function(e) {
|
| 310 |
+
warning("CalEnviroScreen failed: ", e$message)
|
| 311 |
+
NULL
|
| 312 |
+
})
|
| 313 |
+
|
| 314 |
+
time_calenviro <- as.numeric(difftime(Sys.time(), t1, units = "secs"))
|
| 315 |
+
benchmarks <- bind_rows(benchmarks, tibble(
|
| 316 |
+
step = "11_calenviro",
|
| 317 |
+
description = "Load CalEnviroScreen layer",
|
| 318 |
+
time_sec = time_calenviro,
|
| 319 |
+
time_pct = NA
|
| 320 |
+
))
|
| 321 |
+
|
| 322 |
+
# ============================================================================
|
| 323 |
+
# 13. SF EJ Communities
|
| 324 |
+
# ============================================================================
|
| 325 |
+
t1 <- Sys.time()
|
| 326 |
+
|
| 327 |
+
sf_ej_path <- '/Users/diegoellis/Downloads/San Francisco Environmental Justice Communities Map_20251217/geo_export_a21b0a0a-7306-46fd-8381-06581cdbe6e9.shp'
|
| 328 |
+
|
| 329 |
+
sf_ej_sf <- tryCatch({
|
| 330 |
+
sf::st_read(sf_ej_path, quiet = TRUE) |>
|
| 331 |
+
dplyr::mutate(
|
| 332 |
+
symbol_hex = stringr::str_split(symbol_rgb, ",\\s*") |>
|
| 333 |
+
lapply(function(x) sprintf("#%02X%02X%02X",
|
| 334 |
+
as.integer(x[1]), as.integer(x[2]), as.integer(x[3]))) |>
|
| 335 |
+
unlist(),
|
| 336 |
+
ej_label = dplyr::case_when(
|
| 337 |
+
is.na(score) ~ "Not EJ",
|
| 338 |
+
score >= 21 ~ "High EJ burden (21-30)",
|
| 339 |
+
score >= 11 ~ "Moderate EJ burden (11-20)",
|
| 340 |
+
score >= 1 ~ "Low EJ burden (1-10)",
|
| 341 |
+
score == 0 ~ "Score 0",
|
| 342 |
+
TRUE ~ "Unknown"
|
| 343 |
+
)
|
| 344 |
+
) |>
|
| 345 |
+
sf::st_transform(4326) |>
|
| 346 |
+
sf::st_make_valid()
|
| 347 |
+
}, error = function(e) {
|
| 348 |
+
warning("SF EJ layer failed: ", e$message)
|
| 349 |
+
NULL
|
| 350 |
+
})
|
| 351 |
+
|
| 352 |
+
time_ej <- as.numeric(difftime(Sys.time(), t1, units = "secs"))
|
| 353 |
+
benchmarks <- bind_rows(benchmarks, tibble(
|
| 354 |
+
step = "12_ej",
|
| 355 |
+
description = "Load SF EJ communities layer",
|
| 356 |
+
time_sec = time_ej,
|
| 357 |
+
time_pct = NA
|
| 358 |
+
))
|
| 359 |
+
|
| 360 |
+
# ============================================================================
|
| 361 |
+
# SUMMARY
|
| 362 |
+
# ============================================================================
|
| 363 |
+
time_overall <- as.numeric(difftime(Sys.time(), start_overall, units = "secs"))
|
| 364 |
+
|
| 365 |
+
benchmarks <- benchmarks |>
|
| 366 |
+
mutate(time_pct = round(100 * time_sec / time_overall, 1))
|
| 367 |
+
|
| 368 |
+
# Print results
|
| 369 |
+
cat("\n")
|
| 370 |
+
cat("================================================================================\n")
|
| 371 |
+
cat("STARTUP PERFORMANCE BENCHMARK\n")
|
| 372 |
+
cat("================================================================================\n\n")
|
| 373 |
+
|
| 374 |
+
print(benchmarks |> select(step, description, time_sec, time_pct))
|
| 375 |
+
|
| 376 |
+
cat("\n")
|
| 377 |
+
cat("TOTAL STARTUP TIME: ", round(time_overall, 2), " seconds\n")
|
| 378 |
+
cat("================================================================================\n\n")
|
| 379 |
+
|
| 380 |
+
# ============================================================================
|
| 381 |
+
# OPTIMIZATION RECOMMENDATIONS
|
| 382 |
+
# ============================================================================
|
| 383 |
+
cat("OPTIMIZATION RECOMMENDATIONS:\n")
|
| 384 |
+
cat("================================================================================\n\n")
|
| 385 |
+
|
| 386 |
+
# Flag slow steps (>5 seconds or >10% of total)
|
| 387 |
+
slow_steps <- benchmarks |>
|
| 388 |
+
filter(time_sec > 5 | time_pct > 10) |>
|
| 389 |
+
arrange(desc(time_sec))
|
| 390 |
+
|
| 391 |
+
if (nrow(slow_steps) > 0) {
|
| 392 |
+
cat("CRITICAL BOTTLENECKS (>5s or >10%):\n\n")
|
| 393 |
+
for (i in 1:nrow(slow_steps)) {
|
| 394 |
+
row <- slow_steps[i, ]
|
| 395 |
+
cat(" •", row$step, "(", row$time_sec, "s,", row$time_pct, "%)\n")
|
| 396 |
+
cat(" Description:", row$description, "\n")
|
| 397 |
+
|
| 398 |
+
if (grepl("libraries", row$step)) {
|
| 399 |
+
cat(" Recommendation: Load packages only in server() if possible. Use lazy loading.\n")
|
| 400 |
+
} else if (grepl("download", row$step)) {
|
| 401 |
+
cat(" Recommendation: Cache downloaded files locally or use precomputed versions.\n")
|
| 402 |
+
} else if (grepl("gtfsrouter|tidytransit", row$step)) {
|
| 403 |
+
cat(" Recommendation: Pre-compute and cache GTFS timetable. Consider lazy loading for session.\n")
|
| 404 |
+
} else if (grepl("calenviro|ej", row$step)) {
|
| 405 |
+
cat(" Recommendation: Pre-filter to SF boundary. Store as .gpkg or parquet locally.\n")
|
| 406 |
+
}
|
| 407 |
+
cat("\n")
|
| 408 |
+
}
|
| 409 |
+
} else {
|
| 410 |
+
cat("No critical bottlenecks detected (all steps < 5s).\n\n")
|
| 411 |
+
}
|
| 412 |
+
|
| 413 |
+
# Create visualization
|
| 414 |
+
p <- ggplot(benchmarks, aes(x = reorder(step, -time_sec), y = time_sec, fill = time_pct)) +
|
| 415 |
+
geom_col() +
|
| 416 |
+
geom_text(aes(label = paste0(round(time_sec, 2), "s")), vjust = -0.3, size = 3) +
|
| 417 |
+
scale_fill_gradient(low = "green", high = "red", name = "% of Total") +
|
| 418 |
+
labs(
|
| 419 |
+
title = "Shiny App Startup Performance Profile",
|
| 420 |
+
subtitle = paste0("Total time: ", round(time_overall, 2), " seconds"),
|
| 421 |
+
x = "Loading Step",
|
| 422 |
+
y = "Time (seconds)"
|
| 423 |
+
) +
|
| 424 |
+
theme_minimal() +
|
| 425 |
+
theme(
|
| 426 |
+
axis.text.x = element_text(angle = 45, hjust = 1, size = 9),
|
| 427 |
+
plot.title = element_text(face = "bold", size = 14),
|
| 428 |
+
legend.position = "right"
|
| 429 |
+
)
|
| 430 |
+
|
| 431 |
+
print(p)
|
| 432 |
+
|
| 433 |
+
ggsave("/Users/diegoellis/Desktop/Projects/Postdoc/Biodiversity_Access_Indicator/SF_biodiv_access_shiny/startup_benchmark.png",
|
| 434 |
+
p, width = 12, height = 6, dpi = 150)
|
| 435 |
+
|
| 436 |
+
cat("\nBenchmark plot saved to: startup_benchmark.png\n")
|
| 437 |
+
|
| 438 |
+
# ============================================================================
|
| 439 |
+
# Export benchmark data
|
| 440 |
+
# ============================================================================
|
| 441 |
+
write_csv(benchmarks,
|
| 442 |
+
"/Users/diegoellis/Desktop/Projects/Postdoc/Biodiversity_Access_Indicator/SF_biodiv_access_shiny/startup_benchmarks.csv")
|
| 443 |
+
|
| 444 |
+
cat("Benchmark data saved to: startup_benchmarks.csv\n\n")
|
Rscripts/prep/run_all_prep.R
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================================================================
|
| 2 |
+
# Prep: Run full data build → data/output
|
| 3 |
+
# ============================================================================
|
| 4 |
+
# Working directory must be the project root (folder containing data/ and Rscripts/).
|
| 5 |
+
#
|
| 6 |
+
# Order (fast steps first; heavy raster build last):
|
| 7 |
+
# 1. GBIF DuckDB → sf-gbif.parquet → gbif_census_ndvi_anno.parquet
|
| 8 |
+
# 2. CalEnviroScreen + SF EJ → GeoPackages
|
| 9 |
+
# 3. GTFS zip + timetable + stop headways (~20–30 s; cached for app startup)
|
| 10 |
+
# 4. Greenspace distance rasters (DuckDB; slow)
|
| 11 |
+
# 5. RSF Program nearest rasters (DuckDB; same grid as 4)
|
| 12 |
+
# 6. Per-CBG greenspace coverage CSV (precomputed so the app's first isochrone
|
| 13 |
+
# doesn't recompute the CBG x greenspace intersection)
|
| 14 |
+
#
|
| 15 |
+
# Then upload files from data/output/ to the HuggingFace dataset (manually or
|
| 16 |
+
# see comments in upload_to_huggingface.R).
|
| 17 |
+
|
| 18 |
+
source("Rscripts/prep/create_annotated_gbif_parquet.R")
|
| 19 |
+
source("Rscripts/prep/build_equity_layers.R")
|
| 20 |
+
source("Rscripts/prep/implement_optimizations.R")
|
| 21 |
+
source("Rscripts/prep/making-greenspace-raster.R")
|
| 22 |
+
source("Rscripts/prep/making-rsfprogram-raster.R")
|
| 23 |
+
source("Rscripts/prep/build_cbg_greenspace_coverage.R")
|
Rscripts/prep/upload_to_huggingface.R
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================================================================
|
| 2 |
+
# Prep: Stage data/output → data/hf_upload (optional mirror before upload)
|
| 3 |
+
# ============================================================================
|
| 4 |
+
# Canonical build artifacts live in data/output/ (from Rscripts/prep/* and
|
| 5 |
+
# run_all_prep.R). Copy them here, then upload data/hf_upload/ to HuggingFace:
|
| 6 |
+
#
|
| 7 |
+
# huggingface-cli upload boettiger-lab/sf_biodiv_access data/hf_upload/ . \
|
| 8 |
+
# --repo-type dataset
|
| 9 |
+
#
|
| 10 |
+
# Or upload files from data/output/ directly via the web UI.
|
| 11 |
+
#
|
| 12 |
+
# Already on HuggingFace (not staged from data/output): greenspaces_osm_nad83.{shp,...}
|
| 13 |
+
# Other manual assets: SF_EastBay_NDVI_Sentinel_10.tif, cbg_vect_sf.Rdata, hotspots/coldspots
|
| 14 |
+
#
|
| 15 |
+
# GTFS: sf_muni_gtfs.zip contains the feed; timetable .rds + headways .csv are
|
| 16 |
+
# precomputed (~20–30 s) so the app does not rebuild them every session.
|
| 17 |
+
# ============================================================================
|
| 18 |
+
|
| 19 |
+
library(glue)
|
| 20 |
+
|
| 21 |
+
out_dir <- "data/output"
|
| 22 |
+
upload_dir <- "data/hf_upload"
|
| 23 |
+
dir.create(upload_dir, recursive = TRUE, showWarnings = FALSE)
|
| 24 |
+
|
| 25 |
+
artifacts <- c(
|
| 26 |
+
"nearest_greenspace_dist.tif",
|
| 27 |
+
"nearest_greenspace_osmid.tif",
|
| 28 |
+
"nearest_rsfprogram_dist.tif",
|
| 29 |
+
"nearest_rsfprogram_id.tif",
|
| 30 |
+
"gbif_census_ndvi_anno.parquet",
|
| 31 |
+
"gtfs_timetable_monday.rds",
|
| 32 |
+
"gtfs_stop_headways.csv",
|
| 33 |
+
"sf_muni_gtfs.zip",
|
| 34 |
+
"calenviro_sf.gpkg",
|
| 35 |
+
"sf_ej_communities_map.gpkg",
|
| 36 |
+
"RSF_Program_Projects_polygons.gpkg",
|
| 37 |
+
"cbg_greenspace_coverage.csv"
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
for (f in artifacts) {
|
| 41 |
+
src <- file.path(out_dir, f)
|
| 42 |
+
dst <- file.path(upload_dir, f)
|
| 43 |
+
if (file.exists(src)) {
|
| 44 |
+
file.copy(src, dst, overwrite = TRUE)
|
| 45 |
+
} else {
|
| 46 |
+
warning(glue("Missing in data/output/: {f}"))
|
| 47 |
+
}
|
| 48 |
+
}
|
Rscripts/setup_unified.R
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================================================================
|
| 2 |
+
# Setup: load all data for the app (sourced once at startup by app.R)
|
| 3 |
+
# ============================================================================
|
| 4 |
+
# Checks local files in data/cached/ first, falling back to HuggingFace
|
| 5 |
+
# downloads (see hf_or_local()). Loads everything the app needs in one pass.
|
| 6 |
+
#
|
| 7 |
+
# The two most expensive products -- the CBG x greenspace intersection and the
|
| 8 |
+
# transit-routing timetable -- are precomputed in Rscripts/prep/ and just read
|
| 9 |
+
# here, so startup stays fast without splitting the load across files.
|
| 10 |
+
# ============================================================================
|
| 11 |
+
|
| 12 |
+
# ----------------------------------------------------------------------------
|
| 13 |
+
# Libraries
|
| 14 |
+
# ----------------------------------------------------------------------------
|
| 15 |
+
# require(shinyjs) # commented: useShinyjs() enabled features the app never calls
|
| 16 |
+
library(shiny)
|
| 17 |
+
library(shinydashboard)
|
| 18 |
+
library(leaflet)
|
| 19 |
+
library(mapboxapi)
|
| 20 |
+
# library(tidyverse)
|
| 21 |
+
library(dplyr)
|
| 22 |
+
library(tidyr)
|
| 23 |
+
library(readr)
|
| 24 |
+
library(purrr)
|
| 25 |
+
library(tibble)
|
| 26 |
+
library(stringr)
|
| 27 |
+
library(forcats)
|
| 28 |
+
library(lubridate)
|
| 29 |
+
library(ggplot2)
|
| 30 |
+
# library(tidycensus) # commented: unused in app (CBG data is precomputed) -- speeds startup
|
| 31 |
+
library(sf)
|
| 32 |
+
library(DT)
|
| 33 |
+
# library(RColorBrewer) # commented: brewer.pal only appears in dead if(FALSE) blocks
|
| 34 |
+
library(terra)
|
| 35 |
+
# Commented out: no detectable use in the app, and loading them added ~1-2 s to
|
| 36 |
+
# startup (mapview/sjPlot/sjlabelled each pull large dependency trees). Re-enable
|
| 37 |
+
# the relevant line if you start using one.
|
| 38 |
+
# library(data.table)
|
| 39 |
+
# library(mapview)
|
| 40 |
+
# library(sjPlot)
|
| 41 |
+
# library(sjlabelled)
|
| 42 |
+
library(bslib)
|
| 43 |
+
library(shinycssloaders)
|
| 44 |
+
library(glue)
|
| 45 |
+
|
| 46 |
+
# ----------------------------------------------------------------------------
|
| 47 |
+
# HuggingFace base URL + cache dir + download helper
|
| 48 |
+
# ----------------------------------------------------------------------------
|
| 49 |
+
HF_BASE <- "https://huggingface.co/datasets/boettiger-lab/sf_biodiv_access/resolve/main"
|
| 50 |
+
|
| 51 |
+
# Use data/cached/ when running locally (writable), otherwise fall back to
|
| 52 |
+
# /tmp/sf_biodiv_cache/ for read-only environments like HuggingFace Spaces.
|
| 53 |
+
cache_dir <- if (file.access(".", mode = 2) == 0) "data/cached" else "/tmp/sf_biodiv_cache"
|
| 54 |
+
dir.create(cache_dir, recursive = TRUE, showWarnings = FALSE)
|
| 55 |
+
|
| 56 |
+
# Helper: if the file already exists in cache_dir, return that path. Otherwise
|
| 57 |
+
# attempt to download from HuggingFace into cache_dir. Returns the destination
|
| 58 |
+
# path regardless -- caller must check file.exists() if the download may fail.
|
| 59 |
+
hf_or_local <- function(filename) {
|
| 60 |
+
dest <- file.path(cache_dir, filename)
|
| 61 |
+
if (!file.exists(dest)) {
|
| 62 |
+
tryCatch(
|
| 63 |
+
download.file(glue::glue("{HF_BASE}/{filename}"), dest, mode = "wb", quiet = TRUE),
|
| 64 |
+
error = function(e) warning(glue::glue("HuggingFace download failed for {filename}: {e$message}")),
|
| 65 |
+
warning = function(w) warning(glue::glue("HuggingFace download warning for {filename}: {w$message}"))
|
| 66 |
+
)
|
| 67 |
+
}
|
| 68 |
+
dest
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
message("[setup] loading greenspace, CBG, RSF, CalEnviroScreen, SF EJ…")
|
| 72 |
+
|
| 73 |
+
# ----------------------------------------------------------------------------
|
| 74 |
+
# Greenspace (OSM polygons) -- "Greenspace" map layer + coverage calc
|
| 75 |
+
# ----------------------------------------------------------------------------
|
| 76 |
+
greenspace_shp <- file.path(cache_dir, "greenspaces_osm_nad83.shp")
|
| 77 |
+
if (!file.exists(greenspace_shp)) {
|
| 78 |
+
for (ext in c("shp", "dbf", "prj", "shx")) {
|
| 79 |
+
hf_or_local(glue::glue("greenspaces_osm_nad83.{ext}"))
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
osm_greenspace <- st_read(greenspace_shp, quiet = TRUE) |> st_transform(4326)
|
| 83 |
+
if (!"name" %in% names(osm_greenspace)) osm_greenspace$name <- "Unnamed Greenspace"
|
| 84 |
+
|
| 85 |
+
# ----------------------------------------------------------------------------
|
| 86 |
+
# GBIF observations (parquet) -- path only; queried via DuckDB in app.R / server
|
| 87 |
+
# ----------------------------------------------------------------------------
|
| 88 |
+
gbif_parquet <- hf_or_local("gbif_census_ndvi_anno.parquet")
|
| 89 |
+
|
| 90 |
+
# ----------------------------------------------------------------------------
|
| 91 |
+
# Census block groups (CBG) -- Income / Richness / Data map layers
|
| 92 |
+
# ----------------------------------------------------------------------------
|
| 93 |
+
load(hf_or_local("cbg_vect_sf.Rdata"))
|
| 94 |
+
|
| 95 |
+
if (!"unique_species" %in% names(cbg_vect_sf)) cbg_vect_sf$unique_species <- cbg_vect_sf$n_species
|
| 96 |
+
if (!"n_observations" %in% names(cbg_vect_sf)) cbg_vect_sf$n_observations <- cbg_vect_sf$n
|
| 97 |
+
if (!"median_inc" %in% names(cbg_vect_sf)) cbg_vect_sf$median_inc <- cbg_vect_sf$medincE
|
| 98 |
+
if (!"ndvi_mean" %in% names(cbg_vect_sf)) cbg_vect_sf$ndvi_mean <- cbg_vect_sf$ndvi_sentinel
|
| 99 |
+
|
| 100 |
+
# ----------------------------------------------------------------------------
|
| 101 |
+
# RSF Program Projects -- "RSF Program Projects" map layer
|
| 102 |
+
# ----------------------------------------------------------------------------
|
| 103 |
+
rsf_projects <- st_read(hf_or_local("RSF_Program_Projects_polygons.gpkg"), quiet = TRUE) |>
|
| 104 |
+
st_transform(4326)
|
| 105 |
+
|
| 106 |
+
# ----------------------------------------------------------------------------
|
| 107 |
+
# CalEnviroScreen 4.0 (pre-filtered to SF) -- map overlay
|
| 108 |
+
# ----------------------------------------------------------------------------
|
| 109 |
+
cenv_sf <- tryCatch({
|
| 110 |
+
sf::st_read(hf_or_local("calenviro_sf.gpkg"), quiet = TRUE)
|
| 111 |
+
}, error = function(e) {
|
| 112 |
+
warning("CalEnviroScreen failed to load: ", e$message); NULL
|
| 113 |
+
})
|
| 114 |
+
|
| 115 |
+
# ----------------------------------------------------------------------------
|
| 116 |
+
# SF Environmental Justice Communities -- map overlay
|
| 117 |
+
# ----------------------------------------------------------------------------
|
| 118 |
+
sf_ej_sf <- tryCatch({
|
| 119 |
+
sf::st_read(hf_or_local("sf_ej_communities_map.gpkg"), quiet = TRUE) |>
|
| 120 |
+
dplyr::mutate(
|
| 121 |
+
symbol_hex = stringr::str_split(symbol_rgb, ",\\s*") |>
|
| 122 |
+
lapply(function(x) sprintf("#%02X%02X%02X",
|
| 123 |
+
as.integer(x[1]), as.integer(x[2]), as.integer(x[3]))) |>
|
| 124 |
+
unlist(),
|
| 125 |
+
ej_label = dplyr::case_when(
|
| 126 |
+
is.na(score) ~ "Not EJ",
|
| 127 |
+
score >= 21 ~ "High EJ burden (21-30)",
|
| 128 |
+
score >= 11 ~ "Moderate EJ burden (11-20)",
|
| 129 |
+
score >= 1 ~ "Low EJ burden (1-10)",
|
| 130 |
+
score == 0 ~ "Score 0",
|
| 131 |
+
TRUE ~ "Unknown"
|
| 132 |
+
)
|
| 133 |
+
)
|
| 134 |
+
}, error = function(e) {
|
| 135 |
+
warning("SF EJ layer failed to load: ", e$message); NULL
|
| 136 |
+
})
|
| 137 |
+
|
| 138 |
+
message("[setup] loading distance rasters + NDVI (terra lazy: headers only)…")
|
| 139 |
+
|
| 140 |
+
# ----------------------------------------------------------------------------
|
| 141 |
+
# Greenspace + RSF distance/id rasters + NDVI (terra is lazy: reads headers, not
|
| 142 |
+
# cells, so these open instantly; pixels are read during isochrone analysis).
|
| 143 |
+
# ----------------------------------------------------------------------------
|
| 144 |
+
greenspace_dist_raster <- terra::rast(hf_or_local("nearest_greenspace_dist.tif"))
|
| 145 |
+
greenspace_osmid_raster <- terra::rast(hf_or_local("nearest_greenspace_osmid.tif"))
|
| 146 |
+
rsfprogram_dist_raster <- terra::rast(hf_or_local("nearest_rsfprogram_dist.tif"))
|
| 147 |
+
rsfprogram_id_raster <- terra::rast(hf_or_local("nearest_rsfprogram_id.tif"))
|
| 148 |
+
ndvi <- terra::rast(hf_or_local("SF_EastBay_NDVI_Sentinel_10.tif"))
|
| 149 |
+
|
| 150 |
+
# ----------------------------------------------------------------------------
|
| 151 |
+
# Per-CBG greenspace overlap (used in the socio summary + BAI benchmark).
|
| 152 |
+
# Precomputed by Rscripts/prep/build_cbg_greenspace_coverage.R and cached as a
|
| 153 |
+
# small CSV (downloaded from HuggingFace like the other prep artifacts). If that
|
| 154 |
+
# file is unavailable, fall back to the (slow) st_union + st_intersection here.
|
| 155 |
+
# ----------------------------------------------------------------------------
|
| 156 |
+
cov_path <- hf_or_local("cbg_greenspace_coverage.csv")
|
| 157 |
+
cbg_greenspace_coverage <- if (file.exists(cov_path)) {
|
| 158 |
+
# GEOID forced to character so leading zeros survive (FIPS codes; join key).
|
| 159 |
+
readr::read_csv(cov_path, col_types = readr::cols(GEOID = readr::col_character()),
|
| 160 |
+
show_col_types = FALSE)
|
| 161 |
+
} else {
|
| 162 |
+
message("[setup] precomputed greenspace coverage not found; computing inline (slow)…")
|
| 163 |
+
cbg_proj <- st_transform(cbg_vect_sf[, "GEOID"], 3857) |>
|
| 164 |
+
mutate(cbg_area_m2 = as.numeric(st_area(geometry)))
|
| 165 |
+
gs_proj <- st_transform(osm_greenspace, 3857) |> st_make_valid()
|
| 166 |
+
gs_union <- st_union(gs_proj)
|
| 167 |
+
cbg_gs_inter <- st_intersection(cbg_proj, gs_union)
|
| 168 |
+
coverage <- cbg_gs_inter |>
|
| 169 |
+
mutate(greenspace_m2 = as.numeric(st_area(geometry))) |>
|
| 170 |
+
st_drop_geometry() |>
|
| 171 |
+
group_by(GEOID) |>
|
| 172 |
+
summarise(greenspace_m2 = sum(greenspace_m2), .groups = "drop") |>
|
| 173 |
+
right_join(cbg_proj |> st_drop_geometry() |> dplyr::select(GEOID, cbg_area_m2), by = "GEOID") |>
|
| 174 |
+
mutate(
|
| 175 |
+
greenspace_m2 = tidyr::replace_na(greenspace_m2, 0),
|
| 176 |
+
GEOID = as.character(GEOID)
|
| 177 |
+
)
|
| 178 |
+
readr::write_csv(coverage, file.path(cache_dir, "cbg_greenspace_coverage.csv"))
|
| 179 |
+
coverage
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
message("[setup] loading GTFS (stops, routes, headways, routing timetable)…")
|
| 183 |
+
|
| 184 |
+
# ----------------------------------------------------------------------------
|
| 185 |
+
# GTFS (SF Muni)
|
| 186 |
+
# ----------------------------------------------------------------------------
|
| 187 |
+
gtfs_zip_path <- hf_or_local("sf_muni_gtfs.zip")
|
| 188 |
+
|
| 189 |
+
# Unzip for read.csv(stops.txt, …); gtfsrouter/tidytransit read the .zip directly.
|
| 190 |
+
gtfs_unzip_dir <- file.path(cache_dir, "muni_gtfs")
|
| 191 |
+
dir.create(gtfs_unzip_dir, recursive = TRUE, showWarnings = FALSE)
|
| 192 |
+
if (!dir.exists(gtfs_unzip_dir) || length(list.files(gtfs_unzip_dir, pattern = "\\.txt$")) == 0L) {
|
| 193 |
+
unzip(gtfs_zip_path, exdir = gtfs_unzip_dir, overwrite = TRUE)
|
| 194 |
+
}
|
| 195 |
+
gtfs_path <- gtfs_unzip_dir
|
| 196 |
+
|
| 197 |
+
# --- Transit stops ("Transit Stops" overlay) --------------------------------
|
| 198 |
+
gtfs_stops_sf <- tryCatch({
|
| 199 |
+
read.csv(file.path(gtfs_path, "stops.txt")) |>
|
| 200 |
+
st_as_sf(coords = c("stop_lon", "stop_lat"), crs = 4326)
|
| 201 |
+
}, error = function(e) { warning("GTFS stops failed to load: ", e$message); NULL })
|
| 202 |
+
|
| 203 |
+
# --- Route shapes ("Transit Routes" overlay) --------------------------------
|
| 204 |
+
gtfs_routes_sf <- tryCatch({
|
| 205 |
+
gtfs_shapes_raw <- read.csv(file.path(gtfs_path, "shapes.txt"))
|
| 206 |
+
gtfs_trips_raw <- read.csv(file.path(gtfs_path, "trips.txt"))
|
| 207 |
+
gtfs_routes_raw <- read.csv(file.path(gtfs_path, "routes.txt"))
|
| 208 |
+
|
| 209 |
+
shape_route_map <- gtfs_trips_raw |> distinct(shape_id, route_id)
|
| 210 |
+
route_meta <- gtfs_routes_raw |>
|
| 211 |
+
select(route_id, route_short_name, route_long_name, route_color) |>
|
| 212 |
+
mutate(route_color_hex = paste0("#", trimws(route_color)))
|
| 213 |
+
|
| 214 |
+
shapes_split <- gtfs_shapes_raw |>
|
| 215 |
+
arrange(shape_id, shape_pt_sequence) |>
|
| 216 |
+
group_by(shape_id) |>
|
| 217 |
+
group_split()
|
| 218 |
+
|
| 219 |
+
shape_geoms <- lapply(shapes_split, function(s) {
|
| 220 |
+
st_linestring(cbind(s$shape_pt_lon, s$shape_pt_lat))
|
| 221 |
+
})
|
| 222 |
+
|
| 223 |
+
st_sf(
|
| 224 |
+
shape_id = sapply(shapes_split, function(s) s$shape_id[1]),
|
| 225 |
+
geometry = st_sfc(shape_geoms, crs = 4326)
|
| 226 |
+
) |>
|
| 227 |
+
left_join(shape_route_map, by = "shape_id") |>
|
| 228 |
+
left_join(route_meta, by = "route_id")
|
| 229 |
+
}, error = function(e) { warning("GTFS route shapes failed to load: ", e$message); NULL })
|
| 230 |
+
|
| 231 |
+
# --- Stop headways (AM peak 7-9am): cached as CSV (readable / diffable).
|
| 232 |
+
# Joined into gtfs_stops_sf below so they show in the stop popups on the map.
|
| 233 |
+
hw_csv <- file.path(cache_dir, "gtfs_stop_headways.csv")
|
| 234 |
+
hw_rds <- file.path(cache_dir, "gtfs_stop_headways.rds")
|
| 235 |
+
if (!file.exists(hw_csv) && file.exists(hw_rds)) {
|
| 236 |
+
readRDS(hw_rds) |> readr::write_csv(hw_csv)
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
gtfs_stop_headways <- tryCatch({
|
| 240 |
+
headways_path <- hf_or_local("gtfs_stop_headways.csv")
|
| 241 |
+
if (file.exists(headways_path)) {
|
| 242 |
+
readr::read_csv(headways_path, show_col_types = FALSE) |>
|
| 243 |
+
mutate(stop_id = as.character(stop_id))
|
| 244 |
+
} else {
|
| 245 |
+
gt <- tidytransit::read_gtfs(gtfs_zip_path)
|
| 246 |
+
hw <- tidytransit::get_stop_frequency(gt, start_time = 7 * 3600, end_time = 9 * 3600) |>
|
| 247 |
+
group_by(stop_id) |>
|
| 248 |
+
summarise(
|
| 249 |
+
mean_headway_min = mean(mean_headway, na.rm = TRUE) / 60,
|
| 250 |
+
n_departures_peak = sum(n_departures, na.rm = TRUE),
|
| 251 |
+
.groups = "drop"
|
| 252 |
+
) |>
|
| 253 |
+
mutate(stop_id = as.character(stop_id))
|
| 254 |
+
readr::write_csv(hw, hw_csv)
|
| 255 |
+
hw
|
| 256 |
+
}
|
| 257 |
+
}, error = function(e) { warning("tidytransit headway computation failed: ", e$message); NULL })
|
| 258 |
+
|
| 259 |
+
if (!is.null(gtfs_stop_headways) && !is.null(gtfs_stops_sf)) {
|
| 260 |
+
gtfs_stops_sf <- gtfs_stops_sf |>
|
| 261 |
+
mutate(stop_id = as.character(stop_id)) |>
|
| 262 |
+
left_join(gtfs_stop_headways, by = "stop_id")
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
# --- gtfsrouter timetable: routes transit isochrones (precomputed .rds) ------
|
| 266 |
+
gtfs_router <- tryCatch({
|
| 267 |
+
timetable_path <- hf_or_local("gtfs_timetable_monday.rds")
|
| 268 |
+
if (file.exists(timetable_path)) {
|
| 269 |
+
readRDS(timetable_path)
|
| 270 |
+
} else {
|
| 271 |
+
gr <- gtfsrouter::extract_gtfs(gtfs_zip_path)
|
| 272 |
+
result <- gtfsrouter::gtfs_timetable(gr, day = "Monday")
|
| 273 |
+
saveRDS(result, file.path(cache_dir, "gtfs_timetable_monday.rds"))
|
| 274 |
+
result
|
| 275 |
+
}
|
| 276 |
+
}, error = function(e) { warning("gtfsrouter failed to initialise: ", e$message); NULL })
|
| 277 |
+
|
| 278 |
+
# --- Pre-computed transit isochrone cache (keyed by nearest stop + time) -----
|
| 279 |
+
transit_iso_cache <- tryCatch({
|
| 280 |
+
p <- file.path(cache_dir, "transit_iso_cache.rds")
|
| 281 |
+
if (file.exists(p)) readRDS(p) else NULL
|
| 282 |
+
}, error = function(e) { NULL })
|
| 283 |
+
|
| 284 |
+
message("[setup] data load complete.")
|
SF_biodiv_access_shiny.Rproj
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Version: 1.0
|
| 2 |
+
|
| 3 |
+
RestoreWorkspace: Default
|
| 4 |
+
SaveWorkspace: Default
|
| 5 |
+
AlwaysSaveHistory: Default
|
| 6 |
+
|
| 7 |
+
EnableCodeIndexing: Yes
|
| 8 |
+
UseSpacesForTab: Yes
|
| 9 |
+
NumSpacesForTab: 2
|
| 10 |
+
Encoding: UTF-8
|
| 11 |
+
|
| 12 |
+
RnwWeave: Sweave
|
| 13 |
+
LaTeX: pdfLaTeX
|
app.R
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
code/iso_metric_definitions.R
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# =============================================================================
|
| 2 |
+
# Shared raw-metric definitions for SF Biodiversity Access isochrone scoring
|
| 3 |
+
# =============================================================================
|
| 4 |
+
# This file defines the scientific support used by BOTH:
|
| 5 |
+
# * Rscripts/iso_metrics.R (interactive user-generated isochrones)
|
| 6 |
+
# * 03_compute_isochrone_basi.R (citywide precomputed reference isochrones)
|
| 7 |
+
#
|
| 8 |
+
# NON-NEGOTIABLE SUPPORT DEFINITION
|
| 9 |
+
# ---------------------------------
|
| 10 |
+
# For each isochrone independently, the metric support is the set of SF 100 m
|
| 11 |
+
# grid cells whose projected grid-cell centroids fall within that single
|
| 12 |
+
# isochrone polygon. Metrics are then calculated from those selected cells.
|
| 13 |
+
# This deliberately makes the interactive and precomputed estimands identical.
|
| 14 |
+
#
|
| 15 |
+
# It is a 100 m grid-supported access statistic, not a claim of pixel-perfect
|
| 16 |
+
# clipping at the isochrone boundary.
|
| 17 |
+
# =============================================================================
|
| 18 |
+
|
| 19 |
+
ISO_METRIC_DEFINITION_VERSION <- "sf_bai_grid100m_centroid_support_v1"
|
| 20 |
+
# UTM Zone 10N is appropriate for San Francisco metric area/distance calculations.
|
| 21 |
+
# Do not use Web Mercator (EPSG:3857) for area-based density denominators.
|
| 22 |
+
ISO_METRIC_CRS <- 26910
|
| 23 |
+
|
| 24 |
+
ISO_PLANT_CLASSES <- c(
|
| 25 |
+
"Magnoliopsida", "Liliopsida", "Pinopsida", "Polypodiopsida",
|
| 26 |
+
"Equisetopsida", "Bryopsida", "Marchantiopsida"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
iso_weighted_mean <- function(x, w) {
|
| 30 |
+
x <- suppressWarnings(as.numeric(x))
|
| 31 |
+
w <- suppressWarnings(as.numeric(w))
|
| 32 |
+
ok <- is.finite(x) & is.finite(w) & w > 0
|
| 33 |
+
if (!any(ok)) return(NA_real_)
|
| 34 |
+
sum(x[ok] * w[ok]) / sum(w[ok])
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
iso_safe_sum <- function(x, na_as_zero = TRUE) {
|
| 38 |
+
x <- suppressWarnings(as.numeric(x))
|
| 39 |
+
if (na_as_zero) x[!is.finite(x)] <- 0
|
| 40 |
+
if (!any(is.finite(x))) return(NA_real_)
|
| 41 |
+
sum(x[is.finite(x)])
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
iso_safe_min <- function(x) {
|
| 45 |
+
x <- suppressWarnings(as.numeric(x))
|
| 46 |
+
x <- x[is.finite(x)]
|
| 47 |
+
if (length(x) == 0) return(NA_real_)
|
| 48 |
+
min(x)
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
# Clean, explicit reference schema. `direction` describes whether a larger raw
|
| 52 |
+
# value is better for the access score. Step 5 stores raw values plus pctile_*
|
| 53 |
+
# columns; the Shiny app uses the RAW columns for external-observation scoring.
|
| 54 |
+
ISO_REFERENCE_METRICS <- list(
|
| 55 |
+
n_species_accessed = list(direction = "higher", label = "Species richness"),
|
| 56 |
+
n_birds_accessed = list(direction = "higher", label = "Bird richness"),
|
| 57 |
+
n_mammals_accessed = list(direction = "higher", label = "Mammal richness"),
|
| 58 |
+
n_plants_accessed = list(direction = "higher", label = "Plant richness"),
|
| 59 |
+
n_records_accessed = list(direction = "higher", label = "GBIF records"),
|
| 60 |
+
sampling_density_km2 = list(direction = "higher", label = "GBIF records / km2"),
|
| 61 |
+
mean_ndvi = list(direction = "higher", label = "Mean NDVI"),
|
| 62 |
+
greenspace_pct = list(direction = "higher", label = "Greenspace cover"),
|
| 63 |
+
n_stops_accessed = list(direction = "higher", label = "Transit stops"),
|
| 64 |
+
transit_access_score = list(direction = "higher", label = "Transit stops / km2"),
|
| 65 |
+
unique_muni_routes = list(direction = "higher", label = "Unique Muni routes"),
|
| 66 |
+
nearest_stop_m = list(direction = "lower", label = "Nearest stop distance"),
|
| 67 |
+
calenviro_ci_score = list(direction = "lower", label = "CalEnviroScreen burden"),
|
| 68 |
+
calenviro_traffic_pct = list(direction = "lower", label = "Traffic burden percentile"),
|
| 69 |
+
sf_ej_score = list(direction = "lower", label = "SF EJ burden")
|
| 70 |
+
)
|
code/iso_metrics_AUDITED.R
ADDED
|
@@ -0,0 +1,688 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================================================================
|
| 2 |
+
# Shared isochrone + metric functions (sourced once by app.R at startup)
|
| 3 |
+
# ============================================================================
|
| 4 |
+
# These four functions are the single source of truth for turning a point into
|
| 5 |
+
# an isochrone, scoring it, and drawing its BAI spider plot. Both the main
|
| 6 |
+
# "Isochrone Explorer" tab and the "Isochrone Comparer" tab call them, so the
|
| 7 |
+
# scoring logic lives in exactly one place.
|
| 8 |
+
#
|
| 9 |
+
# build_isochrones() point + modes/times -> isochrone sf (mode,time,geom)
|
| 10 |
+
# compute_iso_metrics() isochrone sf + point -> per-isochrone metric data.frame
|
| 11 |
+
# draw_radar() BAI df -> spider/radar plot (base graphics)
|
| 12 |
+
#
|
| 13 |
+
# These functions read the *static* objects loaded by Rscripts/setup_unified.R
|
| 14 |
+
# directly as globals (cbg_vect_sf, osm_greenspace, the distance/NDVI rasters,
|
| 15 |
+
# rsf_projects, cbg_greenspace_coverage, gtfs_stops_sf, gtfs_routes_sf,
|
| 16 |
+
# gtfs_router, transit_iso_cache, cenv_sf, sf_ej_sf) plus the helpers/config
|
| 17 |
+
# defined at the top of app.R (mapbox_token, mode_palette, pretty_mode, ecdf01,
|
| 18 |
+
# standardize_iso_sf, build_walk_transit_isochrone, safe_biodiv_hotspots/coldspots).
|
| 19 |
+
#
|
| 20 |
+
# Two objects are created per-session inside server() and so are passed in as
|
| 21 |
+
# explicit arguments rather than read as globals:
|
| 22 |
+
# gbif_tab -- the session's DuckDB handle on the GBIF parquet
|
| 23 |
+
#
|
| 24 |
+
# compute_iso_metrics() reports progress via withProgress(), so it must be
|
| 25 |
+
# called from within a Shiny reactive context (which both tabs satisfy).
|
| 26 |
+
# ============================================================================
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# Shared scientific definition used by both interactive and Step-5 calculations.
|
| 30 |
+
if (!exists("ISO_METRIC_DEFINITION_VERSION", inherits = TRUE)) {
|
| 31 |
+
source("Rscripts/iso_metric_definitions.R", local = TRUE)
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
# ----------------------------------------------------------------------------
|
| 35 |
+
# 100 m grid-supported metric cache
|
| 36 |
+
# ----------------------------------------------------------------------------
|
| 37 |
+
# Every scored raw metric uses the same support as corrected Step 5: the set of
|
| 38 |
+
# SF 100 m grid cells whose projected centroids fall within ONE isochrone.
|
| 39 |
+
# This makes user and reference metrics mathematically identical at 100 m support.
|
| 40 |
+
.iso_metric_cache <- new.env(parent = emptyenv())
|
| 41 |
+
|
| 42 |
+
load_iso_metric_grid <- function() {
|
| 43 |
+
if (exists("grid", envir = .iso_metric_cache, inherits = FALSE)) {
|
| 44 |
+
return(get("grid", envir = .iso_metric_cache, inherits = FALSE))
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
path <- Sys.getenv(
|
| 48 |
+
"ISO_METRIC_GRID_GPKG",
|
| 49 |
+
unset = file.path("outputs", "results", "grid_results_100m.gpkg")
|
| 50 |
+
)
|
| 51 |
+
if (!file.exists(path)) {
|
| 52 |
+
stop("Harmonized isochrone metrics require: ", path)
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
g <- sf::st_read(path, quiet = TRUE) |> sf::st_make_valid()
|
| 56 |
+
required <- c(
|
| 57 |
+
"cell_id", "n_records", "mean_ndvi", "gs_area_m2", "n_stops",
|
| 58 |
+
"nearest_stop_m", "calenviro_ci_score", "calenviro_traffic_pct", "sf_ej_score"
|
| 59 |
+
)
|
| 60 |
+
missing <- setdiff(required, names(g))
|
| 61 |
+
if (length(missing) > 0) {
|
| 62 |
+
stop("grid_results_100m.gpkg lacks required metric columns: ", paste(missing, collapse = ", "))
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
gp <- sf::st_transform(g, ISO_METRIC_CRS)
|
| 66 |
+
gp$cell_area_m2 <- as.numeric(sf::st_area(gp))
|
| 67 |
+
cp <- sf::st_centroid(gp) |> dplyr::select(cell_id)
|
| 68 |
+
|
| 69 |
+
out <- list(
|
| 70 |
+
grid_metric = gp,
|
| 71 |
+
centroids_metric = cp,
|
| 72 |
+
attrs = gp |>
|
| 73 |
+
sf::st_drop_geometry() |>
|
| 74 |
+
dplyr::transmute(
|
| 75 |
+
cell_id, cell_area_m2,
|
| 76 |
+
n_records = tidyr::replace_na(as.numeric(n_records), 0),
|
| 77 |
+
mean_ndvi = as.numeric(mean_ndvi),
|
| 78 |
+
gs_area_m2 = tidyr::replace_na(as.numeric(gs_area_m2), 0),
|
| 79 |
+
n_stops = tidyr::replace_na(as.numeric(n_stops), 0),
|
| 80 |
+
nearest_stop_m = as.numeric(nearest_stop_m),
|
| 81 |
+
calenviro_ci_score = as.numeric(calenviro_ci_score),
|
| 82 |
+
calenviro_traffic_pct = as.numeric(calenviro_traffic_pct),
|
| 83 |
+
sf_ej_score = as.numeric(sf_ej_score)
|
| 84 |
+
)
|
| 85 |
+
)
|
| 86 |
+
assign("grid", out, envir = .iso_metric_cache)
|
| 87 |
+
out
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
load_iso_species_cell_lookup <- function() {
|
| 91 |
+
if (exists("species", envir = .iso_metric_cache, inherits = FALSE)) {
|
| 92 |
+
return(get("species", envir = .iso_metric_cache, inherits = FALSE))
|
| 93 |
+
}
|
| 94 |
+
path <- file.path("outputs", "results", "gbif_cell_species_100m.rds")
|
| 95 |
+
if (!file.exists(path)) {
|
| 96 |
+
warning(
|
| 97 |
+
"Missing ", path, ". Run corrected 03_compute_isochrone_basi.R before ",
|
| 98 |
+
"using definition-matched biodiversity percentiles."
|
| 99 |
+
)
|
| 100 |
+
out <- NULL
|
| 101 |
+
} else {
|
| 102 |
+
out <- readRDS(path)
|
| 103 |
+
ver <- attr(out, "iso_metric_definition_version")
|
| 104 |
+
if (is.null(ver) || !identical(ver, ISO_METRIC_DEFINITION_VERSION)) {
|
| 105 |
+
stop("GBIF cell lookup definition version does not match the Shiny metric definition.")
|
| 106 |
+
}
|
| 107 |
+
}
|
| 108 |
+
assign("species", out, envir = .iso_metric_cache)
|
| 109 |
+
out
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
load_iso_route_cell_lookup <- function() {
|
| 113 |
+
if (exists("routes", envir = .iso_metric_cache, inherits = FALSE)) {
|
| 114 |
+
return(get("routes", envir = .iso_metric_cache, inherits = FALSE))
|
| 115 |
+
}
|
| 116 |
+
path <- file.path("outputs", "results", "muni_route_cells_100m.rds")
|
| 117 |
+
if (!file.exists(path)) {
|
| 118 |
+
warning(
|
| 119 |
+
"Missing ", path, ". Run corrected 03_compute_isochrone_basi.R before ",
|
| 120 |
+
"using definition-matched route-access percentiles."
|
| 121 |
+
)
|
| 122 |
+
out <- NULL
|
| 123 |
+
} else {
|
| 124 |
+
out <- readRDS(path)
|
| 125 |
+
ver <- attr(out, "iso_metric_definition_version")
|
| 126 |
+
if (is.null(ver) || !identical(ver, ISO_METRIC_DEFINITION_VERSION)) {
|
| 127 |
+
stop("Muni route-cell lookup definition version does not match the Shiny metric definition.")
|
| 128 |
+
}
|
| 129 |
+
}
|
| 130 |
+
assign("routes", out, envir = .iso_metric_cache)
|
| 131 |
+
out
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
iso_metric_support <- function(poly_i, include_geometry = FALSE) {
|
| 135 |
+
cache <- load_iso_metric_grid()
|
| 136 |
+
poly_metric <- sf::st_transform(sf::st_make_valid(poly_i), ISO_METRIC_CRS)
|
| 137 |
+
hit <- lengths(sf::st_within(cache$centroids_metric, sf::st_geometry(poly_metric))) > 0
|
| 138 |
+
ids <- cache$centroids_metric$cell_id[hit]
|
| 139 |
+
attrs <- cache$attrs[cache$attrs$cell_id %in% ids, , drop = FALSE]
|
| 140 |
+
if (!include_geometry) return(attrs)
|
| 141 |
+
geom <- cache$grid_metric[cache$grid_metric$cell_id %in% ids, , drop = FALSE]
|
| 142 |
+
list(attrs = attrs, geometry = geom)
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
iso_metric_support_geometry <- function(poly_i) {
|
| 146 |
+
support <- iso_metric_support(poly_i, include_geometry = TRUE)
|
| 147 |
+
if (is.null(support$geometry) || nrow(support$geometry) == 0) {
|
| 148 |
+
return(sf::st_sfc(crs = ISO_METRIC_CRS))
|
| 149 |
+
}
|
| 150 |
+
sf::st_union(sf::st_geometry(support$geometry))
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
summarise_iso_metric_support <- function(poly_i) {
|
| 154 |
+
support <- iso_metric_support(poly_i, include_geometry = FALSE)
|
| 155 |
+
if (is.null(support) || nrow(support) == 0) {
|
| 156 |
+
return(list(
|
| 157 |
+
cell_ids = integer(0), n_cells = 0L, area_m2 = NA_real_, area_km2 = NA_real_,
|
| 158 |
+
n_records = NA_real_, n_species = NA_real_, n_birds = NA_real_,
|
| 159 |
+
n_mammals = NA_real_, n_plants = NA_real_, sampling_density = NA_real_,
|
| 160 |
+
mean_ndvi = NA_real_, greenspace_pct = NA_real_, n_stops = NA_real_,
|
| 161 |
+
transit_access = NA_real_, unique_routes = NA_real_, nearest_stop_m = NA_real_,
|
| 162 |
+
calenviro_ci = NA_real_, traffic_pct = NA_real_, sf_ej = NA_real_
|
| 163 |
+
))
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
ids <- unique(support$cell_id)
|
| 167 |
+
area_m2 <- sum(support$cell_area_m2[is.finite(support$cell_area_m2)], na.rm = TRUE)
|
| 168 |
+
area_km2 <- area_m2 / 1e6
|
| 169 |
+
n_records <- sum(tidyr::replace_na(support$n_records, 0), na.rm = TRUE)
|
| 170 |
+
n_stops <- sum(tidyr::replace_na(support$n_stops, 0), na.rm = TRUE)
|
| 171 |
+
|
| 172 |
+
spp <- load_iso_species_cell_lookup()
|
| 173 |
+
if (is.null(spp)) {
|
| 174 |
+
n_species <- n_birds <- n_mammals <- n_plants <- NA_real_
|
| 175 |
+
} else {
|
| 176 |
+
s <- spp[spp$cell_id %in% ids, , drop = FALSE]
|
| 177 |
+
n_species <- dplyr::n_distinct(s$species, na.rm = TRUE)
|
| 178 |
+
n_birds <- dplyr::n_distinct(s$species[s$class == "Aves"], na.rm = TRUE)
|
| 179 |
+
n_mammals <- dplyr::n_distinct(s$species[s$class == "Mammalia"], na.rm = TRUE)
|
| 180 |
+
n_plants <- dplyr::n_distinct(s$species[s$class %in% ISO_PLANT_CLASSES], na.rm = TRUE)
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
route_lookup <- load_iso_route_cell_lookup()
|
| 184 |
+
unique_routes <- if (is.null(route_lookup)) {
|
| 185 |
+
NA_real_
|
| 186 |
+
} else {
|
| 187 |
+
dplyr::n_distinct(route_lookup$route_id[route_lookup$cell_id %in% ids], na.rm = TRUE)
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
list(
|
| 191 |
+
cell_ids = ids,
|
| 192 |
+
n_cells = length(ids),
|
| 193 |
+
area_m2 = area_m2,
|
| 194 |
+
area_km2 = area_km2,
|
| 195 |
+
n_records = n_records,
|
| 196 |
+
n_species = n_species,
|
| 197 |
+
n_birds = n_birds,
|
| 198 |
+
n_mammals = n_mammals,
|
| 199 |
+
n_plants = n_plants,
|
| 200 |
+
sampling_density = if (is.finite(area_km2) && area_km2 > 0) n_records / area_km2 else NA_real_,
|
| 201 |
+
mean_ndvi = iso_weighted_mean(support$mean_ndvi, support$cell_area_m2),
|
| 202 |
+
greenspace_pct = if (is.finite(area_m2) && area_m2 > 0) {
|
| 203 |
+
pmin(100, 100 * sum(tidyr::replace_na(support$gs_area_m2, 0), na.rm = TRUE) / area_m2)
|
| 204 |
+
} else NA_real_,
|
| 205 |
+
n_stops = n_stops,
|
| 206 |
+
transit_access = if (is.finite(area_km2) && area_km2 > 0) n_stops / area_km2 else NA_real_,
|
| 207 |
+
unique_routes = unique_routes,
|
| 208 |
+
nearest_stop_m = iso_safe_min(support$nearest_stop_m),
|
| 209 |
+
calenviro_ci = iso_weighted_mean(support$calenviro_ci_score, support$cell_area_m2),
|
| 210 |
+
traffic_pct = iso_weighted_mean(support$calenviro_traffic_pct, support$cell_area_m2),
|
| 211 |
+
sf_ej = iso_weighted_mean(support$sf_ej_score, support$cell_area_m2)
|
| 212 |
+
)
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
# ----------------------------------------------------------------------------
|
| 216 |
+
# build_isochrones(): point + chosen modes/times -> combined isochrone sf
|
| 217 |
+
# ----------------------------------------------------------------------------
|
| 218 |
+
# point: named numeric c(lon = , lat = ) (the shape chosen_point() stores), or NULL.
|
| 219 |
+
# modes: character vector from c("driving","walking","cycling","driving-traffic",
|
| 220 |
+
# "transit","walk_transit"). times: numeric minutes. The transit_* / walk_*
|
| 221 |
+
# arguments only matter when a transit mode is selected; they default to the
|
| 222 |
+
# main tab's defaults so the comparer can pass a single mode/time and ignore them.
|
| 223 |
+
# Returns an sf with columns mode, time, geometry (EPSG:4326), or NULL if nothing
|
| 224 |
+
# could be built.
|
| 225 |
+
build_isochrones <- function(point, modes, times,
|
| 226 |
+
transit_hour = 9,
|
| 227 |
+
walk_to_stop_min = 5,
|
| 228 |
+
walk_from_stop_min = 5,
|
| 229 |
+
transit_departure_window_min = 10) {
|
| 230 |
+
if (is.null(point) || length(modes) == 0 || length(times) == 0) return(NULL)
|
| 231 |
+
|
| 232 |
+
location_sf <- st_as_sf(
|
| 233 |
+
data.frame(lon = point["lon"], lat = point["lat"]),
|
| 234 |
+
coords = c("lon", "lat"),
|
| 235 |
+
crs = 4326
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
iso_list <- list()
|
| 239 |
+
times <- as.numeric(times)
|
| 240 |
+
|
| 241 |
+
# --- Mapbox modes (driving / walking / cycling / driving-traffic) ----------
|
| 242 |
+
mapbox_modes <- intersect(modes, c("driving", "walking", "cycling", "driving-traffic"))
|
| 243 |
+
for (mode in mapbox_modes) {
|
| 244 |
+
for (t in times) {
|
| 245 |
+
iso <- tryCatch(
|
| 246 |
+
mb_isochrone(location_sf, time = t, profile = mode, access_token = mapbox_token),
|
| 247 |
+
# Surface the Mapbox error to the log instead of silently dropping it --
|
| 248 |
+
# otherwise a failed call just looks like "no isochrone" with no clue why.
|
| 249 |
+
error = function(e) { warning("mb_isochrone failed (", mode, " ", t, " min): ", conditionMessage(e)); NULL }
|
| 250 |
+
)
|
| 251 |
+
if (!is.null(iso)) {
|
| 252 |
+
iso_std <- standardize_iso_sf(iso, mode_name = mode, time_min = t)
|
| 253 |
+
if (!is.null(iso_std)) iso_list <- append(iso_list, list(iso_std))
|
| 254 |
+
}
|
| 255 |
+
}
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
# --- Transit (GTFS) --------------------------------------------------------
|
| 259 |
+
if ("transit" %in% modes && !is.null(gtfs_router) && !is.null(gtfs_stops_sf)) {
|
| 260 |
+
stop_dists <- st_distance(location_sf, gtfs_stops_sf)
|
| 261 |
+
nearest_idx <- which.min(stop_dists)
|
| 262 |
+
nearest_id <- as.character(gtfs_stops_sf$stop_id[nearest_idx])
|
| 263 |
+
dep_secs <- as.numeric(transit_hour) * 3600
|
| 264 |
+
|
| 265 |
+
for (t in times) {
|
| 266 |
+
iso_poly <- NULL
|
| 267 |
+
|
| 268 |
+
if (!is.null(transit_iso_cache) &&
|
| 269 |
+
!is.null(transit_iso_cache[[nearest_id]]) &&
|
| 270 |
+
!is.null(transit_iso_cache[[nearest_id]][[as.character(t)]])) {
|
| 271 |
+
iso_poly <- transit_iso_cache[[nearest_id]][[as.character(t)]]
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
if (is.null(iso_poly)) {
|
| 275 |
+
iso_result <- tryCatch(
|
| 276 |
+
gtfsrouter::gtfs_isochrone(
|
| 277 |
+
gtfs = gtfs_router,
|
| 278 |
+
from = nearest_id,
|
| 279 |
+
start_time = dep_secs,
|
| 280 |
+
end_time = dep_secs + t * 60,
|
| 281 |
+
from_is_id = TRUE
|
| 282 |
+
),
|
| 283 |
+
error = function(e) NULL
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
if (!is.null(iso_result) && nrow(iso_result) > 2) {
|
| 287 |
+
reachable_sf <- gtfs_stops_sf |>
|
| 288 |
+
filter(stop_id %in% as.character(iso_result$stop_id))
|
| 289 |
+
|
| 290 |
+
if (nrow(reachable_sf) > 2) {
|
| 291 |
+
iso_poly <- st_convex_hull(st_union(reachable_sf))
|
| 292 |
+
} else if (nrow(reachable_sf) > 0) {
|
| 293 |
+
iso_poly <- st_union(st_buffer(st_transform(reachable_sf, 3857), 100)) |>
|
| 294 |
+
st_transform(4326)
|
| 295 |
+
}
|
| 296 |
+
}
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
if (!is.null(iso_poly)) {
|
| 300 |
+
iso_sf <- st_sf(
|
| 301 |
+
mode = "transit",
|
| 302 |
+
time = as.numeric(t),
|
| 303 |
+
geometry = st_geometry(st_as_sf(iso_poly)),
|
| 304 |
+
crs = 4326
|
| 305 |
+
)
|
| 306 |
+
iso_sf <- standardize_iso_sf(iso_sf, mode_name = "transit", time_min = t)
|
| 307 |
+
iso_list <- append(iso_list, list(iso_sf))
|
| 308 |
+
}
|
| 309 |
+
}
|
| 310 |
+
}
|
| 311 |
+
|
| 312 |
+
# --- Walk + Transit (Muni) -------------------------------------------------
|
| 313 |
+
if ("walk_transit" %in% modes && !is.null(gtfs_router) && !is.null(gtfs_stops_sf)) {
|
| 314 |
+
dep_secs <- as.numeric(transit_hour) * 3600
|
| 315 |
+
valid_times <- times[times > walk_to_stop_min]
|
| 316 |
+
|
| 317 |
+
for (t in valid_times) {
|
| 318 |
+
wt_iso <- tryCatch(
|
| 319 |
+
build_walk_transit_isochrone(
|
| 320 |
+
location_sf = location_sf,
|
| 321 |
+
total_time_min = t,
|
| 322 |
+
dep_secs = dep_secs,
|
| 323 |
+
walk_to_stop_min = walk_to_stop_min,
|
| 324 |
+
walk_from_stop_min = walk_from_stop_min,
|
| 325 |
+
gtfs_stops_sf = gtfs_stops_sf,
|
| 326 |
+
gtfs_router = gtfs_router,
|
| 327 |
+
mapbox_token = mapbox_token,
|
| 328 |
+
departure_window_min = transit_departure_window_min,
|
| 329 |
+
departure_step_min = 5,
|
| 330 |
+
max_last_mile_stops = 12,
|
| 331 |
+
include_first_mile_polygon = TRUE
|
| 332 |
+
),
|
| 333 |
+
error = function(e) NULL
|
| 334 |
+
)
|
| 335 |
+
|
| 336 |
+
if (!is.null(wt_iso) && nrow(wt_iso) > 0) {
|
| 337 |
+
iso_list <- append(iso_list, list(wt_iso))
|
| 338 |
+
}
|
| 339 |
+
}
|
| 340 |
+
}
|
| 341 |
+
|
| 342 |
+
if (length(iso_list) == 0) return(NULL)
|
| 343 |
+
|
| 344 |
+
dplyr::bind_rows(iso_list) |>
|
| 345 |
+
st_as_sf() |>
|
| 346 |
+
st_make_valid() |>
|
| 347 |
+
st_transform(4326)
|
| 348 |
+
}
|
| 349 |
+
|
| 350 |
+
|
| 351 |
+
# ----------------------------------------------------------------------------
|
| 352 |
+
# compute_iso_metrics(): one independently calculated row per isochrone
|
| 353 |
+
# ----------------------------------------------------------------------------
|
| 354 |
+
# Scored metrics use the shared 100 m centroid-support definition above. This is
|
| 355 |
+
# intentionally identical to corrected Step 5. `gbif_tab` is retained in the
|
| 356 |
+
# signature for compatibility but is no longer used for scored richness/effort.
|
| 357 |
+
compute_iso_metrics <- function(iso_data, point, gbif_tab) {
|
| 358 |
+
if (is.null(iso_data) || nrow(iso_data) == 0) return(data.frame())
|
| 359 |
+
|
| 360 |
+
hotspot_union <- safe_biodiv_hotspots()
|
| 361 |
+
coldspot_union <- safe_biodiv_coldspots()
|
| 362 |
+
if (!is.null(hotspot_union)) hotspot_union <- st_union(hotspot_union)
|
| 363 |
+
if (!is.null(coldspot_union)) coldspot_union <- st_union(coldspot_union)
|
| 364 |
+
|
| 365 |
+
acs_wide <- cbg_vect_sf |>
|
| 366 |
+
mutate(population = popE, med_income = medincE)
|
| 367 |
+
|
| 368 |
+
user_point_sf <- NULL
|
| 369 |
+
if (!is.null(point)) {
|
| 370 |
+
user_point_sf <- st_as_sf(
|
| 371 |
+
data.frame(lon = point["lon"], lat = point["lat"]),
|
| 372 |
+
coords = c("lon", "lat"), crs = 4326
|
| 373 |
+
)
|
| 374 |
+
}
|
| 375 |
+
|
| 376 |
+
min_dist_val_global <- NA_real_
|
| 377 |
+
osm_greenspace_name_global <- NA_character_
|
| 378 |
+
if (!is.null(user_point_sf) && exists("greenspace_dist_raster") && exists("greenspace_osmid_raster")) {
|
| 379 |
+
try({
|
| 380 |
+
min_dist_val_global <- (greenspace_dist_raster |> extract(vect(user_point_sf)) |> pull(2))[1]
|
| 381 |
+
user_point_osm_id <- (greenspace_osmid_raster |> extract(vect(user_point_sf)) |> pull(2))[1]
|
| 382 |
+
osm_greenspace_name_global <- osm_greenspace |>
|
| 383 |
+
mutate(osm_id = as.numeric(osm_id)) |>
|
| 384 |
+
filter(osm_id == user_point_osm_id) |>
|
| 385 |
+
pull(name)
|
| 386 |
+
if (length(osm_greenspace_name_global) == 0 || is.na(osm_greenspace_name_global[1])) {
|
| 387 |
+
osm_greenspace_name_global <- "Unnamed Greenspace"
|
| 388 |
+
} else {
|
| 389 |
+
osm_greenspace_name_global <- osm_greenspace_name_global[1]
|
| 390 |
+
}
|
| 391 |
+
}, silent = TRUE)
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
min_rsf_dist_global <- NA_real_
|
| 395 |
+
rsf_program_name_global <- NA_character_
|
| 396 |
+
if (!is.null(user_point_sf) && exists("rsfprogram_dist_raster") && exists("rsfprogram_id_raster") && exists("rsf_projects")) {
|
| 397 |
+
try({
|
| 398 |
+
min_rsf_dist_global <- (rsfprogram_dist_raster |> extract(vect(user_point_sf)) |> pull(2))[1]
|
| 399 |
+
user_point_rsf_pid <- (rsfprogram_id_raster |> extract(vect(user_point_sf)) |> pull(2))[1]
|
| 400 |
+
rsf_program_name_global <- rsf_projects |>
|
| 401 |
+
dplyr::filter(as.numeric(.data$polygon_id) == as.numeric(user_point_rsf_pid)) |>
|
| 402 |
+
dplyr::pull(.data$prj_name)
|
| 403 |
+
if (length(rsf_program_name_global) == 0 || is.na(rsf_program_name_global[1])) {
|
| 404 |
+
rsf_program_name_global <- "Unknown RSF program"
|
| 405 |
+
} else {
|
| 406 |
+
rsf_program_name_global <- rsf_program_name_global[1]
|
| 407 |
+
}
|
| 408 |
+
}, silent = TRUE)
|
| 409 |
+
}
|
| 410 |
+
|
| 411 |
+
results <- data.frame()
|
| 412 |
+
n_isos <- nrow(iso_data)
|
| 413 |
+
|
| 414 |
+
withProgress(message = "Analyzing isochrones...", value = 0, {
|
| 415 |
+
for (i in seq_len(n_isos)) {
|
| 416 |
+
poly_i <- iso_data[i, ]
|
| 417 |
+
incProgress(
|
| 418 |
+
1 / n_isos,
|
| 419 |
+
detail = paste0(pretty_mode(as.character(poly_i$mode[[1]])), " – ", poly_i$time[[1]], " min")
|
| 420 |
+
)
|
| 421 |
+
|
| 422 |
+
support <- summarise_iso_metric_support(poly_i)
|
| 423 |
+
iso_area_m2 <- as.numeric(st_area(st_transform(poly_i, ISO_METRIC_CRS)))
|
| 424 |
+
iso_area_km2 <- iso_area_m2 / 1e6
|
| 425 |
+
|
| 426 |
+
dist_hot_km <- if (!is.null(hotspot_union)) {
|
| 427 |
+
round(as.numeric(min(st_distance(poly_i, hotspot_union))) / 1000, 3)
|
| 428 |
+
} else NA_real_
|
| 429 |
+
dist_cold_km <- if (!is.null(coldspot_union)) {
|
| 430 |
+
round(as.numeric(min(st_distance(poly_i, coldspot_union))) / 1000, 3)
|
| 431 |
+
} else NA_real_
|
| 432 |
+
|
| 433 |
+
# Population/income remain descriptive, non-BAI fields and therefore retain
|
| 434 |
+
# the existing polygon-intersection implementation.
|
| 435 |
+
inter_acs <- tryCatch(intersect(vect(acs_wide), vect(poly_i)) |> st_as_sf(), error = function(e) NULL)
|
| 436 |
+
pop_total <- 0
|
| 437 |
+
w_income <- NA_real_
|
| 438 |
+
if (!is.null(inter_acs) && nrow(inter_acs) > 0) {
|
| 439 |
+
inter_acs <- inter_acs |>
|
| 440 |
+
mutate(
|
| 441 |
+
area_num = as.numeric(st_area(st_transform(geometry, ISO_METRIC_CRS))),
|
| 442 |
+
weighted_pop = population * (area_num / sum(area_num, na.rm = TRUE))
|
| 443 |
+
)
|
| 444 |
+
pop_total <- round(sum(inter_acs$weighted_pop, na.rm = TRUE))
|
| 445 |
+
w_income <- sum(inter_acs$med_income * inter_acs$area_num, na.rm = TRUE) /
|
| 446 |
+
sum(inter_acs$area_num, na.rm = TRUE)
|
| 447 |
+
}
|
| 448 |
+
|
| 449 |
+
# Frequency/headway are descriptive only; they are not reference-scored.
|
| 450 |
+
freq_weighted_score <- NA_real_
|
| 451 |
+
mean_headway_iso <- NA_real_
|
| 452 |
+
nearest_stop_name <- NA_character_
|
| 453 |
+
if (!is.null(gtfs_stops_sf)) {
|
| 454 |
+
inter_transit <- tryCatch(st_intersection(gtfs_stops_sf, poly_i), error = function(e) NULL)
|
| 455 |
+
if (!is.null(inter_transit) && nrow(inter_transit) > 0 &&
|
| 456 |
+
"mean_headway_min" %in% names(inter_transit)) {
|
| 457 |
+
hw <- inter_transit$mean_headway_min
|
| 458 |
+
hw <- hw[is.finite(hw) & hw > 0]
|
| 459 |
+
if (length(hw) > 0 && is.finite(support$area_km2) && support$area_km2 > 0) {
|
| 460 |
+
freq_weighted_score <- round(sum(60 / hw) / support$area_km2, 2)
|
| 461 |
+
mean_headway_iso <- round(mean(hw), 1)
|
| 462 |
+
}
|
| 463 |
+
}
|
| 464 |
+
if (is.finite(support$nearest_stop_m)) {
|
| 465 |
+
support_geom <- iso_metric_support_geometry(poly_i)
|
| 466 |
+
if (length(support_geom) > 0) {
|
| 467 |
+
support_sf <- st_sf(geometry = support_geom)
|
| 468 |
+
d <- tryCatch(st_distance(support_sf, st_transform(gtfs_stops_sf, st_crs(support_sf))), error = function(e) NULL)
|
| 469 |
+
if (!is.null(d) && length(d) > 0) {
|
| 470 |
+
nearest_stop_name <- gtfs_stops_sf$stop_name[which.min(as.numeric(d))]
|
| 471 |
+
}
|
| 472 |
+
}
|
| 473 |
+
}
|
| 474 |
+
}
|
| 475 |
+
|
| 476 |
+
row_i <- data.frame(
|
| 477 |
+
Mode = pretty_mode(as.character(poly_i$mode[[1]])),
|
| 478 |
+
Time = as.numeric(poly_i$time[[1]]),
|
| 479 |
+
IsochroneArea_km2 = round(iso_area_km2, 3),
|
| 480 |
+
MetricSupportArea_km2 = round(support$area_km2, 3),
|
| 481 |
+
MetricSupportNCells = as.integer(support$n_cells),
|
| 482 |
+
MetricDefinitionVersion = ISO_METRIC_DEFINITION_VERSION,
|
| 483 |
+
DistToHotspot_km = dist_hot_km,
|
| 484 |
+
DistToColdspot_km = dist_cold_km,
|
| 485 |
+
EstimatedPopulation = pop_total,
|
| 486 |
+
MedianIncome = round(w_income, 2),
|
| 487 |
+
MeanNDVI = as.numeric(support$mean_ndvi),
|
| 488 |
+
GBIF_Records = as.numeric(support$n_records),
|
| 489 |
+
GBIF_Species = as.numeric(support$n_species),
|
| 490 |
+
Bird_Species = as.numeric(support$n_birds),
|
| 491 |
+
Mammal_Species = as.numeric(support$n_mammals),
|
| 492 |
+
Plant_Species = as.numeric(support$n_plants),
|
| 493 |
+
SamplingDensity_km2 = as.numeric(support$sampling_density),
|
| 494 |
+
Greenspace_percent = as.numeric(support$greenspace_pct),
|
| 495 |
+
Transit_Stops = as.numeric(support$n_stops),
|
| 496 |
+
Unique_Muni_Routes = as.numeric(support$unique_routes),
|
| 497 |
+
Transit_Access_Score = as.numeric(support$transit_access),
|
| 498 |
+
Freq_Weighted_Score = freq_weighted_score,
|
| 499 |
+
Mean_Headway_min = mean_headway_iso,
|
| 500 |
+
Nearest_Stop_m = as.numeric(support$nearest_stop_m),
|
| 501 |
+
Nearest_Stop_Name = nearest_stop_name,
|
| 502 |
+
CalEnviro_CIscore = as.numeric(support$calenviro_ci),
|
| 503 |
+
CalEnviro_Traffic_Pctl = as.numeric(support$traffic_pct),
|
| 504 |
+
SF_EJ_Score = as.numeric(support$sf_ej),
|
| 505 |
+
closest_greenspace = osm_greenspace_name_global,
|
| 506 |
+
closest_greenspace_dist_m = min_dist_val_global,
|
| 507 |
+
closest_rsf_program = rsf_program_name_global,
|
| 508 |
+
closest_rsf_program_dist_m = min_rsf_dist_global,
|
| 509 |
+
stringsAsFactors = FALSE
|
| 510 |
+
)
|
| 511 |
+
results <- rbind(results, row_i)
|
| 512 |
+
}
|
| 513 |
+
})
|
| 514 |
+
|
| 515 |
+
# No union-based or mean-across-isochrone scoring attributes are created here.
|
| 516 |
+
# Each row remains the sole unit of percentile and BAI scoring.
|
| 517 |
+
if (nrow(results) > 0) {
|
| 518 |
+
closest_gs <- results |>
|
| 519 |
+
filter(!is.na(closest_greenspace_dist_m)) |>
|
| 520 |
+
slice_min(closest_greenspace_dist_m, n = 1)
|
| 521 |
+
attr(results, "closest_greenspace") <- if (nrow(closest_gs) > 0) closest_gs$closest_greenspace[1] else "None"
|
| 522 |
+
attr(results, "closest_greenspace_dist_m") <- if (nrow(closest_gs) > 0) closest_gs$closest_greenspace_dist_m[1] else NA_real_
|
| 523 |
+
|
| 524 |
+
closest_rsf <- results |>
|
| 525 |
+
filter(!is.na(closest_rsf_program_dist_m)) |>
|
| 526 |
+
slice_min(closest_rsf_program_dist_m, n = 1)
|
| 527 |
+
attr(results, "closest_rsf_program") <- if (nrow(closest_rsf) > 0) closest_rsf$closest_rsf_program[1] else "None"
|
| 528 |
+
attr(results, "closest_rsf_program_dist_m") <- if (nrow(closest_rsf) > 0) closest_rsf$closest_rsf_program_dist_m[1] else NA_real_
|
| 529 |
+
}
|
| 530 |
+
|
| 531 |
+
results
|
| 532 |
+
}
|
| 533 |
+
|
| 534 |
+
|
| 535 |
+
# Legacy census-block-group add_bai() scoring was removed. The app now uses
|
| 536 |
+
# add_reference_bai() from app.R, which requires same-mode × same-time
|
| 537 |
+
# precomputed isochrone references and never falls back to CBG ECDFs.
|
| 538 |
+
|
| 539 |
+
|
| 540 |
+
# Axis order shared by both radar functions, and the column each axis reads from.
|
| 541 |
+
# Keep these two vectors aligned -- the group labels below assume this clockwise
|
| 542 |
+
# order (1-2 Urban Access, 3-4 Biodiversity, 5-6 Environment, 7 EJ).
|
| 543 |
+
RADAR_AXIS_COLS <- c("Mobility_Access_std", "Route_Access_std", "Biodiversity_Potential_std",
|
| 544 |
+
"Observation_Intensity_std", "Environmental_Quality_std",
|
| 545 |
+
"Greenspace_Cover_std", "Equity_Context_std")
|
| 546 |
+
RADAR_AXIS_LABELS <- c("Stop\nDensity", "Route\nDiversity", "Species\nRichness",
|
| 547 |
+
"Obs.\nIntensity", "Vegetation\n(NDVI)", "Greenspace\nCover", "EJ\nContext")
|
| 548 |
+
|
| 549 |
+
# Radial group subheaders placed just beyond the axis labels. Shared by both
|
| 550 |
+
# radar functions so the grouping (Urban Access / Biodiversity / Environment /
|
| 551 |
+
# EJ) is drawn identically. Call after fmsb::radarchart() has drawn the chart.
|
| 552 |
+
draw_radar_group_labels <- function() {
|
| 553 |
+
n_ax <- 7
|
| 554 |
+
angs <- pi/2 - (0:(n_ax - 1)) * (2 * pi / n_ax)
|
| 555 |
+
cat_r <- 1.48 # radius just outside axis labels (~1.2-1.3)
|
| 556 |
+
|
| 557 |
+
text(cat_r * cos(mean(angs[1:2])), cat_r * sin(mean(angs[1:2])),
|
| 558 |
+
"Urban Access", cex = 0.72, col = "#2166ac", font = 2, xpd = TRUE)
|
| 559 |
+
text(cat_r * cos(mean(angs[3:4])), cat_r * sin(mean(angs[3:4])),
|
| 560 |
+
"Biodiversity", cex = 0.72, col = "#1b7837", font = 2, xpd = TRUE)
|
| 561 |
+
text(cat_r * cos(mean(angs[5:6])), cat_r * sin(mean(angs[5:6])),
|
| 562 |
+
"Environment", cex = 0.72, col = "#762a83", font = 2, xpd = TRUE)
|
| 563 |
+
text(cat_r * cos(angs[7]), cat_r * sin(angs[7]),
|
| 564 |
+
"Environmental\nJustice", cex = 0.72, col = "#b2182b", font = 2, xpd = TRUE)
|
| 565 |
+
}
|
| 566 |
+
|
| 567 |
+
# ----------------------------------------------------------------------------
|
| 568 |
+
# draw_radar(): BAI df -> spider/radar plot (base graphics, via fmsb)
|
| 569 |
+
# ----------------------------------------------------------------------------
|
| 570 |
+
# bai_df: the data.frame returned by same-mode × same-time reference scoring (one row per isochrone). title:
|
| 571 |
+
# the chart title. Draws directly to the current graphics device, so call it
|
| 572 |
+
# from inside a renderPlot({ }). Lines are coloured by transport mode.
|
| 573 |
+
draw_radar <- function(bai_df, title = "Biodiversity Access Index Profile") {
|
| 574 |
+
if (is.null(bai_df) || nrow(bai_df) == 0) return(NULL)
|
| 575 |
+
|
| 576 |
+
radar_df <- bai_df |>
|
| 577 |
+
mutate(
|
| 578 |
+
ModeTime = paste0(Mode, "_", Time, "m"),
|
| 579 |
+
`Stop\nDensity` = Mobility_Access_std,
|
| 580 |
+
`Route\nDiversity` = Route_Access_std,
|
| 581 |
+
`Species\nRichness` = Biodiversity_Potential_std,
|
| 582 |
+
`Obs.\nIntensity` = Observation_Intensity_std,
|
| 583 |
+
`Vegetation\n(NDVI)`= Environmental_Quality_std,
|
| 584 |
+
`Greenspace\nCover` = Greenspace_Cover_std,
|
| 585 |
+
`EJ\nContext` = Equity_Context_std
|
| 586 |
+
) |>
|
| 587 |
+
select(
|
| 588 |
+
ModeTime,
|
| 589 |
+
`Stop\nDensity`,
|
| 590 |
+
`Route\nDiversity`,
|
| 591 |
+
`Species\nRichness`,
|
| 592 |
+
`Obs.\nIntensity`,
|
| 593 |
+
`Vegetation\n(NDVI)`,
|
| 594 |
+
`Greenspace\nCover`,
|
| 595 |
+
`EJ\nContext`
|
| 596 |
+
)
|
| 597 |
+
|
| 598 |
+
radar_mat <- as.data.frame(radar_df[, -1])
|
| 599 |
+
rownames(radar_mat) <- radar_df$ModeTime
|
| 600 |
+
|
| 601 |
+
radar_mat <- rbind(
|
| 602 |
+
rep(1, ncol(radar_mat)),
|
| 603 |
+
rep(0, ncol(radar_mat)),
|
| 604 |
+
radar_mat
|
| 605 |
+
)
|
| 606 |
+
|
| 607 |
+
labels <- rownames(radar_mat)[-(1:2)]
|
| 608 |
+
line_cols <- mode_palette[gsub("_(.*)$", "", labels)]
|
| 609 |
+
line_cols[is.na(line_cols)] <- "#666666"
|
| 610 |
+
|
| 611 |
+
# Extra margin so two-line axis labels aren't clipped
|
| 612 |
+
par(mar = c(2, 2, 3, 2))
|
| 613 |
+
|
| 614 |
+
fmsb::radarchart(
|
| 615 |
+
radar_mat,
|
| 616 |
+
axistype = 1,
|
| 617 |
+
pcol = line_cols,
|
| 618 |
+
plwd = 2,
|
| 619 |
+
plty = 1,
|
| 620 |
+
cglcol = "grey80",
|
| 621 |
+
cglty = 1,
|
| 622 |
+
cglwd = 0.8,
|
| 623 |
+
axislabcol = "grey40",
|
| 624 |
+
vlcex = 0.88,
|
| 625 |
+
title = title
|
| 626 |
+
)
|
| 627 |
+
|
| 628 |
+
draw_radar_group_labels()
|
| 629 |
+
|
| 630 |
+
legend(
|
| 631 |
+
"topright",
|
| 632 |
+
legend = labels,
|
| 633 |
+
col = line_cols,
|
| 634 |
+
lty = 1,
|
| 635 |
+
lwd = 2,
|
| 636 |
+
cex = 0.75,
|
| 637 |
+
bty = "n"
|
| 638 |
+
)
|
| 639 |
+
}
|
| 640 |
+
|
| 641 |
+
|
| 642 |
+
# ----------------------------------------------------------------------------
|
| 643 |
+
# draw_compare_radar(): overlay several BAI rows on ONE radar (Comparer tab)
|
| 644 |
+
# ----------------------------------------------------------------------------
|
| 645 |
+
# bai_rows: a list of single-row reference-scored BAI data.frames (one per point).
|
| 646 |
+
# labels: legend text, one per row (e.g. "Point A: Walking 5 min").
|
| 647 |
+
# colors: one line colour per row -- this is where the A-vs-B colour scheme
|
| 648 |
+
# lives, so keep it in sync with the point markers / isochrones.
|
| 649 |
+
# Unlike draw_radar() (coloured by mode), this colours by *point* so two
|
| 650 |
+
# isochrones of the same mode are still distinguishable. Draws to the current
|
| 651 |
+
# device; call from inside renderPlot({ }).
|
| 652 |
+
draw_compare_radar <- function(bai_rows, labels, colors,
|
| 653 |
+
title = "Biodiversity Access Index — Point A vs Point B") {
|
| 654 |
+
if (length(bai_rows) == 0) return(NULL)
|
| 655 |
+
|
| 656 |
+
mat <- as.data.frame(do.call(rbind, lapply(bai_rows, function(d) as.numeric(d[1, RADAR_AXIS_COLS]))))
|
| 657 |
+
names(mat) <- RADAR_AXIS_LABELS
|
| 658 |
+
# fmsb wants the axis max (1) and min (0) as the first two rows.
|
| 659 |
+
mat <- rbind(rep(1, ncol(mat)), rep(0, ncol(mat)), mat)
|
| 660 |
+
|
| 661 |
+
par(mar = c(2, 2, 3, 2))
|
| 662 |
+
fmsb::radarchart(
|
| 663 |
+
mat,
|
| 664 |
+
axistype = 1,
|
| 665 |
+
pcol = colors,
|
| 666 |
+
pfcol = grDevices::adjustcolor(colors, alpha.f = 0.2), # translucent fills so overlaps read
|
| 667 |
+
plwd = 3,
|
| 668 |
+
plty = 1,
|
| 669 |
+
cglcol = "grey80",
|
| 670 |
+
cglty = 1,
|
| 671 |
+
cglwd = 0.8,
|
| 672 |
+
axislabcol = "grey40",
|
| 673 |
+
vlcex = 0.9,
|
| 674 |
+
title = title
|
| 675 |
+
)
|
| 676 |
+
|
| 677 |
+
draw_radar_group_labels()
|
| 678 |
+
|
| 679 |
+
legend(
|
| 680 |
+
"topright",
|
| 681 |
+
legend = labels,
|
| 682 |
+
col = colors,
|
| 683 |
+
lty = 1,
|
| 684 |
+
lwd = 3,
|
| 685 |
+
cex = 0.85,
|
| 686 |
+
bty = "n"
|
| 687 |
+
)
|
| 688 |
+
}
|
code/setup_unified.R
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================================================================
|
| 2 |
+
# Setup: Libraries
|
| 3 |
+
# ============================================================================
|
| 4 |
+
# require(shinyjs)
|
| 5 |
+
library(shiny)
|
| 6 |
+
library(shinydashboard)
|
| 7 |
+
library(leaflet)
|
| 8 |
+
library(mapboxapi)
|
| 9 |
+
# library(tidyverse)
|
| 10 |
+
library(dplyr)
|
| 11 |
+
library(tidyr)
|
| 12 |
+
library(readr)
|
| 13 |
+
library(purrr)
|
| 14 |
+
library(tibble)
|
| 15 |
+
library(stringr)
|
| 16 |
+
library(forcats)
|
| 17 |
+
library(lubridate)
|
| 18 |
+
library(ggplot2)
|
| 19 |
+
# library(tidycensus)
|
| 20 |
+
library(sf)
|
| 21 |
+
library(DT)
|
| 22 |
+
library(RColorBrewer)
|
| 23 |
+
library(terra)
|
| 24 |
+
library(data.table)
|
| 25 |
+
# library(mapview)
|
| 26 |
+
# library(sjPlot)
|
| 27 |
+
# library(sjlabelled)
|
| 28 |
+
library(bslib)
|
| 29 |
+
library(shinycssloaders)
|
| 30 |
+
library(glue)
|
| 31 |
+
|
| 32 |
+
# ============================================================================
|
| 33 |
+
# Setup: HuggingFace base URL and cache directory
|
| 34 |
+
# ============================================================================
|
| 35 |
+
HF_BASE <- "https://huggingface.co/datasets/boettiger-lab/sf_biodiv_access/resolve/main"
|
| 36 |
+
|
| 37 |
+
# Use data/cached/ when running locally (writable), otherwise fall back to
|
| 38 |
+
# /tmp/sf_biodiv_cache/ for read-only environments like HuggingFace Spaces.
|
| 39 |
+
cache_dir <- if (file.access(".", mode = 2) == 0) "data/cached" else "/tmp/sf_biodiv_cache"
|
| 40 |
+
dir.create(cache_dir, recursive = TRUE, showWarnings = FALSE)
|
| 41 |
+
|
| 42 |
+
# Helper: if the file already exists in data/cached/, return that path.
|
| 43 |
+
# Otherwise attempt to download from HuggingFace into data/cached/.
|
| 44 |
+
# Returns the destination path regardless — caller must check file.exists() if
|
| 45 |
+
# the download may fail (e.g. file not yet uploaded to HF).
|
| 46 |
+
hf_or_local <- function(filename) {
|
| 47 |
+
dest <- file.path(cache_dir, filename)
|
| 48 |
+
if (!file.exists(dest)) {
|
| 49 |
+
tryCatch(
|
| 50 |
+
download.file(glue::glue("{HF_BASE}/{filename}"), dest, mode = "wb", quiet = TRUE),
|
| 51 |
+
error = function(e) warning(glue::glue("HuggingFace download failed for {filename}: {e$message}")),
|
| 52 |
+
warning = function(w) warning(glue::glue("HuggingFace download warning for {filename}: {w$message}"))
|
| 53 |
+
)
|
| 54 |
+
}
|
| 55 |
+
dest
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
message("[setup_unified] loading greenspace polygons (cache / HuggingFace)…")
|
| 59 |
+
|
| 60 |
+
# ============================================================================
|
| 61 |
+
# Load Data: Greenspace (OSM polygons)
|
| 62 |
+
# ============================================================================
|
| 63 |
+
# Shapefile bundle on HuggingFace — download sidecars into cache if needed.
|
| 64 |
+
greenspace_shp <- file.path(cache_dir, "greenspaces_osm_nad83.shp")
|
| 65 |
+
if (!file.exists(greenspace_shp)) {
|
| 66 |
+
for (ext in c("shp", "dbf", "prj", "shx")) {
|
| 67 |
+
hf_or_local(glue::glue("greenspaces_osm_nad83.{ext}"))
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
osm_greenspace <- st_read(greenspace_shp, quiet = TRUE) |> st_transform(4326)
|
| 71 |
+
if (!"name" %in% names(osm_greenspace)) osm_greenspace$name <- "Unnamed Greenspace"
|
| 72 |
+
|
| 73 |
+
message("[setup_unified] loading greenspace distance rasters + NDVI…")
|
| 74 |
+
|
| 75 |
+
# ============================================================================
|
| 76 |
+
# Load Data: Greenspace distance rasters
|
| 77 |
+
# ============================================================================
|
| 78 |
+
greenspace_dist_raster <- terra::rast(hf_or_local("nearest_greenspace_dist.tif"))
|
| 79 |
+
greenspace_osmid_raster <- terra::rast(hf_or_local("nearest_greenspace_osmid.tif"))
|
| 80 |
+
|
| 81 |
+
rsfprogram_dist_raster <- terra::rast(hf_or_local("nearest_rsfprogram_dist.tif"))
|
| 82 |
+
rsfprogram_id_raster <- terra::rast(hf_or_local("nearest_rsfprogram_id.tif"))
|
| 83 |
+
|
| 84 |
+
# ============================================================================
|
| 85 |
+
# Load Data: NDVI raster
|
| 86 |
+
# ============================================================================
|
| 87 |
+
ndvi <- terra::rast(hf_or_local("SF_EastBay_NDVI_Sentinel_10.tif"))
|
| 88 |
+
|
| 89 |
+
message("[setup_unified] loading GBIF parquet + CBG polygons…")
|
| 90 |
+
|
| 91 |
+
# ============================================================================
|
| 92 |
+
# Load Data: GBIF observations (parquet, queried via DuckDB in server)
|
| 93 |
+
# ============================================================================
|
| 94 |
+
gbif_parquet <- hf_or_local("gbif_census_ndvi_anno.parquet")
|
| 95 |
+
|
| 96 |
+
# ============================================================================
|
| 97 |
+
# Load Data: Census block groups (CBG)
|
| 98 |
+
# ============================================================================
|
| 99 |
+
load(hf_or_local("cbg_vect_sf.Rdata"))
|
| 100 |
+
|
| 101 |
+
if (!"unique_species" %in% names(cbg_vect_sf)) cbg_vect_sf$unique_species <- cbg_vect_sf$n_species
|
| 102 |
+
if (!"n_observations" %in% names(cbg_vect_sf)) cbg_vect_sf$n_observations <- cbg_vect_sf$n
|
| 103 |
+
if (!"median_inc" %in% names(cbg_vect_sf)) cbg_vect_sf$median_inc <- cbg_vect_sf$medincE
|
| 104 |
+
if (!"ndvi_mean" %in% names(cbg_vect_sf)) cbg_vect_sf$ndvi_mean <- cbg_vect_sf$ndvi_sentinel
|
| 105 |
+
|
| 106 |
+
message("[setup_unified] computing CBG × greenspace overlap (vector intersect)…")
|
| 107 |
+
|
| 108 |
+
# ============================================================================
|
| 109 |
+
# Per-CBG greenspace overlap (computed here; no separate CSV on HuggingFace)
|
| 110 |
+
# ============================================================================
|
| 111 |
+
cbg_proj <- st_transform(cbg_vect_sf[, "GEOID"], 3857) |>
|
| 112 |
+
mutate(cbg_area_m2 = as.numeric(st_area(geometry)))
|
| 113 |
+
gs_proj <- st_transform(osm_greenspace, 3857) |> st_make_valid()
|
| 114 |
+
gs_union <- st_union(gs_proj)
|
| 115 |
+
cbg_gs_inter <- st_intersection(cbg_proj, gs_union)
|
| 116 |
+
cbg_greenspace_coverage <- cbg_gs_inter |>
|
| 117 |
+
mutate(greenspace_m2 = as.numeric(st_area(geometry))) |>
|
| 118 |
+
st_drop_geometry() |>
|
| 119 |
+
group_by(GEOID) |>
|
| 120 |
+
summarise(greenspace_m2 = sum(greenspace_m2), .groups = "drop") |>
|
| 121 |
+
right_join(cbg_proj |> st_drop_geometry() |> dplyr::select(GEOID, cbg_area_m2), by = "GEOID") |>
|
| 122 |
+
mutate(
|
| 123 |
+
greenspace_m2 = tidyr::replace_na(greenspace_m2, 0),
|
| 124 |
+
GEOID = as.character(GEOID)
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
message("[setup_unified] loading biodiversity hotspots / coldspots…")
|
| 128 |
+
|
| 129 |
+
# ============================================================================
|
| 130 |
+
# Load Data: Biodiversity hotspots / coldspots
|
| 131 |
+
# ============================================================================
|
| 132 |
+
hotspots_shp <- file.path(cache_dir, "hotspots.shp")
|
| 133 |
+
if (!file.exists(hotspots_shp)) {
|
| 134 |
+
for (ext in c("shp", "dbf", "prj", "shx")) hf_or_local(glue::glue("hotspots.{ext}"))
|
| 135 |
+
}
|
| 136 |
+
biodiv_hotspots <- st_read(hotspots_shp, quiet = TRUE) |> st_transform(4326)
|
| 137 |
+
|
| 138 |
+
coldspots_shp <- file.path(cache_dir, "coldspots.shp")
|
| 139 |
+
if (!file.exists(coldspots_shp)) {
|
| 140 |
+
for (ext in c("shp", "dbf", "prj", "shx")) hf_or_local(glue::glue("coldspots.{ext}"))
|
| 141 |
+
}
|
| 142 |
+
biodiv_coldspots <- st_read(coldspots_shp, quiet = TRUE) |> st_transform(4326)
|
| 143 |
+
|
| 144 |
+
message("[setup_unified] loading RSF, CalEnviroScreen, SF EJ layers…")
|
| 145 |
+
|
| 146 |
+
# ============================================================================
|
| 147 |
+
# Load Data: RSF Program Projects
|
| 148 |
+
# ============================================================================
|
| 149 |
+
rsf_projects <- st_read(hf_or_local("RSF_Program_Projects_polygons.gpkg"), quiet = TRUE) |>
|
| 150 |
+
st_transform(4326)
|
| 151 |
+
|
| 152 |
+
# ============================================================================
|
| 153 |
+
# Load Data: CalEnviroScreen 4.0 (pre-filtered to SF)
|
| 154 |
+
# ============================================================================
|
| 155 |
+
cenv_sf <- tryCatch({
|
| 156 |
+
sf::st_read(hf_or_local("calenviro_sf.gpkg"), quiet = TRUE)
|
| 157 |
+
}, error = function(e) {
|
| 158 |
+
warning("CalEnviroScreen failed to load: ", e$message); NULL
|
| 159 |
+
})
|
| 160 |
+
|
| 161 |
+
# ============================================================================
|
| 162 |
+
# Load Data: SF Environmental Justice Communities
|
| 163 |
+
# ============================================================================
|
| 164 |
+
sf_ej_sf <- tryCatch({
|
| 165 |
+
sf::st_read(hf_or_local("sf_ej_communities_map.gpkg"), quiet = TRUE) |>
|
| 166 |
+
dplyr::mutate(
|
| 167 |
+
symbol_hex = stringr::str_split(symbol_rgb, ",\\s*") |>
|
| 168 |
+
lapply(function(x) sprintf("#%02X%02X%02X",
|
| 169 |
+
as.integer(x[1]), as.integer(x[2]), as.integer(x[3]))) |>
|
| 170 |
+
unlist(),
|
| 171 |
+
ej_label = dplyr::case_when(
|
| 172 |
+
is.na(score) ~ "Not EJ",
|
| 173 |
+
score >= 21 ~ "High EJ burden (21-30)",
|
| 174 |
+
score >= 11 ~ "Moderate EJ burden (11-20)",
|
| 175 |
+
score >= 1 ~ "Low EJ burden (1-10)",
|
| 176 |
+
score == 0 ~ "Score 0",
|
| 177 |
+
TRUE ~ "Unknown"
|
| 178 |
+
)
|
| 179 |
+
)
|
| 180 |
+
}, error = function(e) {
|
| 181 |
+
warning("SF EJ layer failed to load: ", e$message); NULL
|
| 182 |
+
})
|
| 183 |
+
|
| 184 |
+
message("[setup_unified] loading GTFS (zip, stops, shapes, timetable, headways)…")
|
| 185 |
+
|
| 186 |
+
# ============================================================================
|
| 187 |
+
# Load Data: GTFS (SF Muni)
|
| 188 |
+
# ============================================================================
|
| 189 |
+
|
| 190 |
+
gtfs_zip_path <- hf_or_local("sf_muni_gtfs.zip")
|
| 191 |
+
|
| 192 |
+
# Unzip for read.csv(stops.txt, …); tidytransit/gtfsrouter read the .zip (gtfsio needs a zip path)
|
| 193 |
+
gtfs_unzip_dir <- file.path(cache_dir, "muni_gtfs")
|
| 194 |
+
dir.create(gtfs_unzip_dir, recursive = TRUE, showWarnings = FALSE)
|
| 195 |
+
if (!dir.exists(gtfs_unzip_dir) || length(list.files(gtfs_unzip_dir, pattern = "\\.txt$")) == 0L) {
|
| 196 |
+
unzip(gtfs_zip_path, exdir = gtfs_unzip_dir, overwrite = TRUE)
|
| 197 |
+
}
|
| 198 |
+
gtfs_path <- gtfs_unzip_dir
|
| 199 |
+
|
| 200 |
+
# --- Transit stops -----------------------------------------------------------
|
| 201 |
+
gtfs_stops_sf <- tryCatch({
|
| 202 |
+
read.csv(file.path(gtfs_path, "stops.txt")) |>
|
| 203 |
+
st_as_sf(coords = c("stop_lon", "stop_lat"), crs = 4326)
|
| 204 |
+
}, error = function(e) { warning("GTFS stops failed to load: ", e$message); NULL })
|
| 205 |
+
|
| 206 |
+
# --- Route shapes ------------------------------------------------------------
|
| 207 |
+
gtfs_routes_sf <- tryCatch({
|
| 208 |
+
gtfs_shapes_raw <- read.csv(file.path(gtfs_path, "shapes.txt"))
|
| 209 |
+
gtfs_trips_raw <- read.csv(file.path(gtfs_path, "trips.txt"))
|
| 210 |
+
gtfs_routes_raw <- read.csv(file.path(gtfs_path, "routes.txt"))
|
| 211 |
+
|
| 212 |
+
shape_route_map <- gtfs_trips_raw |> distinct(shape_id, route_id)
|
| 213 |
+
route_meta <- gtfs_routes_raw |>
|
| 214 |
+
select(route_id, route_short_name, route_long_name, route_color) |>
|
| 215 |
+
mutate(route_color_hex = paste0("#", trimws(route_color)))
|
| 216 |
+
|
| 217 |
+
shapes_split <- gtfs_shapes_raw |>
|
| 218 |
+
arrange(shape_id, shape_pt_sequence) |>
|
| 219 |
+
group_by(shape_id) |>
|
| 220 |
+
group_split()
|
| 221 |
+
|
| 222 |
+
shape_geoms <- lapply(shapes_split, function(s) {
|
| 223 |
+
st_linestring(cbind(s$shape_pt_lon, s$shape_pt_lat))
|
| 224 |
+
})
|
| 225 |
+
|
| 226 |
+
st_sf(
|
| 227 |
+
shape_id = sapply(shapes_split, function(s) s$shape_id[1]),
|
| 228 |
+
geometry = st_sfc(shape_geoms, crs = 4326)
|
| 229 |
+
) |>
|
| 230 |
+
left_join(shape_route_map, by = "shape_id") |>
|
| 231 |
+
left_join(route_meta, by = "route_id")
|
| 232 |
+
}, error = function(e) { warning("GTFS route shapes failed to load: ", e$message); NULL })
|
| 233 |
+
|
| 234 |
+
# --- gtfsrouter timetable ----------------------------------------------------
|
| 235 |
+
gtfs_router <- tryCatch({
|
| 236 |
+
timetable_path <- hf_or_local("gtfs_timetable_monday.rds")
|
| 237 |
+
if (file.exists(timetable_path)) {
|
| 238 |
+
readRDS(timetable_path)
|
| 239 |
+
} else {
|
| 240 |
+
gr <- gtfsrouter::extract_gtfs(gtfs_zip_path)
|
| 241 |
+
result <- gtfsrouter::gtfs_timetable(gr, day = "Monday")
|
| 242 |
+
saveRDS(result, file.path(cache_dir, "gtfs_timetable_monday.rds"))
|
| 243 |
+
result
|
| 244 |
+
}
|
| 245 |
+
}, error = function(e) { warning("gtfsrouter failed to initialise: ", e$message); NULL })
|
| 246 |
+
|
| 247 |
+
# --- Pre-computed transit isochrone cache ------------------------------------
|
| 248 |
+
transit_iso_cache <- tryCatch({
|
| 249 |
+
p <- file.path(cache_dir, "transit_iso_cache.rds")
|
| 250 |
+
if (file.exists(p)) readRDS(p) else NULL
|
| 251 |
+
}, error = function(e) { NULL })
|
| 252 |
+
|
| 253 |
+
# --- Stop headways (AM peak 7-9am): cached as CSV (readable / diffable) -------
|
| 254 |
+
# gtfsrouter timetable stays .rds (opaque R object); this table is just columns.
|
| 255 |
+
hw_csv <- file.path(cache_dir, "gtfs_stop_headways.csv")
|
| 256 |
+
hw_rds <- file.path(cache_dir, "gtfs_stop_headways.rds")
|
| 257 |
+
if (!file.exists(hw_csv) && file.exists(hw_rds)) {
|
| 258 |
+
readRDS(hw_rds) |> readr::write_csv(hw_csv)
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
gtfs_stop_headways <- tryCatch({
|
| 262 |
+
headways_path <- hf_or_local("gtfs_stop_headways.csv")
|
| 263 |
+
if (file.exists(headways_path)) {
|
| 264 |
+
readr::read_csv(headways_path, show_col_types = FALSE) |>
|
| 265 |
+
mutate(stop_id = as.character(stop_id))
|
| 266 |
+
} else {
|
| 267 |
+
gt <- tidytransit::read_gtfs(gtfs_zip_path)
|
| 268 |
+
hw <- tidytransit::get_stop_frequency(gt, start_time = 7 * 3600, end_time = 9 * 3600) |>
|
| 269 |
+
group_by(stop_id) |>
|
| 270 |
+
summarise(
|
| 271 |
+
mean_headway_min = mean(mean_headway, na.rm = TRUE) / 60,
|
| 272 |
+
n_departures_peak = sum(n_departures, na.rm = TRUE),
|
| 273 |
+
.groups = "drop"
|
| 274 |
+
) |>
|
| 275 |
+
mutate(stop_id = as.character(stop_id))
|
| 276 |
+
readr::write_csv(hw, hw_csv)
|
| 277 |
+
hw
|
| 278 |
+
}
|
| 279 |
+
}, error = function(e) { warning("tidytransit headway computation failed: ", e$message); NULL })
|
| 280 |
+
|
| 281 |
+
if (!is.null(gtfs_stop_headways) && !is.null(gtfs_stops_sf)) {
|
| 282 |
+
gtfs_stops_sf <- gtfs_stops_sf |>
|
| 283 |
+
mutate(stop_id = as.character(stop_id)) |>
|
| 284 |
+
left_join(gtfs_stop_headways, by = "stop_id")
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
message("[setup_unified] data load complete.")
|
install.r
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
options(repos = c(CRAN = "https://cloud.r-project.org"))
|
| 2 |
+
|
| 3 |
+
install.packages(c(
|
| 4 |
+
|
| 5 |
+
# Shiny / UI
|
| 6 |
+
"shiny",
|
| 7 |
+
"shinydashboard",
|
| 8 |
+
"shinycssloaders",
|
| 9 |
+
"bslib",
|
| 10 |
+
"DT",
|
| 11 |
+
"leaflet",
|
| 12 |
+
"leaflet.extras",
|
| 13 |
+
"htmltools",
|
| 14 |
+
"base64enc",
|
| 15 |
+
|
| 16 |
+
# Core data wrangling / plotting
|
| 17 |
+
# Install components directly rather than tidyverse meta-package
|
| 18 |
+
"dplyr",
|
| 19 |
+
"tidyr",
|
| 20 |
+
"readr",
|
| 21 |
+
"purrr",
|
| 22 |
+
"tibble",
|
| 23 |
+
"stringr",
|
| 24 |
+
"forcats",
|
| 25 |
+
"lubridate",
|
| 26 |
+
"ggplot2",
|
| 27 |
+
"scales",
|
| 28 |
+
|
| 29 |
+
# Spatial
|
| 30 |
+
"sf",
|
| 31 |
+
"terra",
|
| 32 |
+
"mapboxapi",
|
| 33 |
+
|
| 34 |
+
# Database
|
| 35 |
+
"DBI",
|
| 36 |
+
"dbplyr",
|
| 37 |
+
"duckdb",
|
| 38 |
+
|
| 39 |
+
# Transit
|
| 40 |
+
"gtfsrouter",
|
| 41 |
+
"tidytransit",
|
| 42 |
+
"tidycensus",
|
| 43 |
+
|
| 44 |
+
# Radar plots
|
| 45 |
+
"fmsb",
|
| 46 |
+
|
| 47 |
+
# Helpers
|
| 48 |
+
"glue",
|
| 49 |
+
"jsonlite",
|
| 50 |
+
"data.table"
|
| 51 |
+
))
|
outputs/results/gbif_cell_species_100m.rds
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:35649dd22b7093bda327fc71d17eb0edb163160ca8e55810fcfd2cc1956d6467
|
| 3 |
+
size 895847
|
outputs/results/grid_results_100m.gpkg
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:77a11fe1a88ff9eb1fc862fb449c0ba740cd5e3b7bafdd0c5792eaa6ecc3cfa7
|
| 3 |
+
size 8544256
|
outputs/results/isochrone_basi_100m.gpkg
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fd37dc968f140ab9f89fd50acb1a340b775bc468465376fb5a511514d838ed98
|
| 3 |
+
size 88784896
|
outputs/results/isochrone_basi_100m.rds
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:adb323cfca3133dd3c8bd2e518fd4fed03ac4123259457aacd9d6a7a350b033d
|
| 3 |
+
size 23175655
|
outputs/results/muni_route_cells_100m.rds
ADDED
|
Binary file (61.9 kB). View file
|
|
|
rsconnect/shinyapps.io/diego-ellis-soto/RSF_Biodiversity_Access.dcf
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: RSF_Biodiversity_Access
|
| 2 |
+
title: SF_biodiv_access_shiny
|
| 3 |
+
username: diego-ellis-soto
|
| 4 |
+
account: diego-ellis-soto
|
| 5 |
+
server: shinyapps.io
|
| 6 |
+
hostUrl: https://api.shinyapps.io/v1
|
| 7 |
+
appId: 13693040
|
| 8 |
+
bundleId: 9634358
|
| 9 |
+
url: https://diego-ellis-soto.shinyapps.io/RSF_Biodiversity_Access/
|
| 10 |
+
version: 1
|
| 11 |
+
asMultiple: FALSE
|
| 12 |
+
asStatic: FALSE
|
rsconnect/shinyapps.io/diego-ellis-soto/SF_biodiv_access.dcf
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: SF_biodiv_access
|
| 2 |
+
title: SF_biodiv_access
|
| 3 |
+
username: diego-ellis-soto
|
| 4 |
+
account: diego-ellis-soto
|
| 5 |
+
server: shinyapps.io
|
| 6 |
+
hostUrl: https://api.shinyapps.io/v1
|
| 7 |
+
appId: 13693447
|
| 8 |
+
bundleId: 9631093
|
| 9 |
+
url: https://diego-ellis-soto.shinyapps.io/SF_biodiv_access/
|
| 10 |
+
version: 1
|
| 11 |
+
asMultiple: FALSE
|
| 12 |
+
asStatic: FALSE
|
| 13 |
+
ignoredFiles: app_old.R
|
www/California_academy_logo.png
ADDED
|
www/Combined_logos.png
ADDED
|
Git LFS Details
|
www/Reimagining_San_Francisco.png
ADDED
|
Git LFS Details
|
www/UC_Berkeley_logo.png
ADDED
|
www/app_pastel.css
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* SF Biodiversity Access — pastel chrome (shinydashboard overrides) */
|
| 2 |
+
|
| 3 |
+
.content-wrapper,
|
| 4 |
+
.right-side {
|
| 5 |
+
background-color: #f3f8f5 !important;
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
.box {
|
| 9 |
+
border-radius: 12px;
|
| 10 |
+
overflow: hidden;
|
| 11 |
+
border: 1px solid rgba(61, 92, 74, 0.1);
|
| 12 |
+
box-shadow: 0 2px 14px rgba(45, 74, 58, 0.07);
|
| 13 |
+
margin-bottom: 20px;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
.box .box-header {
|
| 17 |
+
font-weight: 600;
|
| 18 |
+
letter-spacing: 0.01em;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
.box.box-success > .box-header {
|
| 22 |
+
background: #c8e9d6 !important;
|
| 23 |
+
color: #1a4534 !important;
|
| 24 |
+
border-bottom: 1px solid rgba(26, 69, 52, 0.12);
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
.box.box-primary > .box-header {
|
| 28 |
+
background: #c9dcf5 !important;
|
| 29 |
+
color: #1f3d66 !important;
|
| 30 |
+
border-bottom: 1px solid rgba(31, 61, 102, 0.12);
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
.box.box-warning > .box-header {
|
| 34 |
+
background: #fce9c9 !important;
|
| 35 |
+
color: #5a4518 !important;
|
| 36 |
+
border-bottom: 1px solid rgba(90, 69, 24, 0.12);
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
.box.box-info > .box-header {
|
| 40 |
+
background: #c9ecf3 !important;
|
| 41 |
+
color: #1a4a55 !important;
|
| 42 |
+
border-bottom: 1px solid rgba(26, 74, 85, 0.12);
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
.box.box-danger > .box-header {
|
| 46 |
+
background: #f5d4d4 !important;
|
| 47 |
+
color: #5c2222 !important;
|
| 48 |
+
border-bottom: 1px solid rgba(92, 34, 34, 0.12);
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
.box-body {
|
| 52 |
+
background-color: #fcfdfc;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
.well {
|
| 56 |
+
background-color: #f7faf8 !important;
|
| 57 |
+
border: 1px solid #e3ece6 !important;
|
| 58 |
+
border-radius: 10px !important;
|
| 59 |
+
box-shadow: none !important;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
/* Main title bar: strong contrast; title uses Roboto (matches body), not Roboto Slab */
|
| 63 |
+
.skin-green .main-header {
|
| 64 |
+
border-bottom: 2px solid rgba(25, 55, 42, 0.35);
|
| 65 |
+
box-shadow: 0 3px 14px rgba(25, 50, 38, 0.18);
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
.skin-green .main-header .logo {
|
| 69 |
+
background: linear-gradient(135deg, #5a8568 0%, #3d6b52 100%) !important;
|
| 70 |
+
color: #f7fffa !important;
|
| 71 |
+
border-right: 1px solid rgba(0, 0, 0, 0.12);
|
| 72 |
+
font-family: "Roboto", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif !important;
|
| 73 |
+
font-weight: 700 !important;
|
| 74 |
+
font-size: 1.5rem !important;
|
| 75 |
+
letter-spacing: 0.02em !important;
|
| 76 |
+
text-shadow: 0 1px 3px rgba(0, 0, 0, 0.22);
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
.skin-green .main-header .navbar {
|
| 80 |
+
background: linear-gradient(135deg, #5a8568 0%, #3d6b52 100%) !important;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
.skin-green .main-header .navbar .sidebar-toggle {
|
| 84 |
+
color: #f7fffa !important;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
.skin-green .main-header .navbar .sidebar-toggle:hover {
|
| 88 |
+
background-color: rgba(255, 255, 255, 0.14) !important;
|
| 89 |
+
}
|
www/hexbin_RSF_logo.png
ADDED
|