library(tidyverse)
library(sf)
library(units)
source("00_functions.R")
<- read_rds("Data/Datasets/base_map.rds")
base_map <- read_rds("Data/Datasets/bcr_map.rds") bcr
Get hex grid
Create hex grid
We want
- Small enough grid size to detect relevant patterns
- Large enough to smooth over eBird spatial/temporal sampling issues
- Grid size similar to breeding territory size ~ 12ha –> Too small
- Try 10x10km or 20x20km
Setup
# range <- st_bbox(map)
#
# full_sf <- read_rds(here("Data/Intermediate/complete.rds")) |>
# mutate(
# observation_count = if_else(observation_count == "X", "1", observation_count),
# observation_count = as.numeric(observation_count)) |>
# group_by(longitude, latitude, year) |>
# summarize(presence = any(species_observed),
# n_obs = sum(observation_count),
# n_list = sum(species_observed),
# n_list_total = n(), .groups = "drop") |>
# st_as_sf(coords = c("longitude", "latitude"), crs = 4326) |>
# st_transform(3347)
Create grids
- We want grids with dimensions of 10x10 or 20x20 km.
- 10x10 = 100 km2; 20x20 = 400 km2
- This gives 10,000 or 40,000 hectares
as_units(c(10, 20)^2, "km2") |>
set_units("ha")
Units: [ha]
[1] 10000 40000
Using functions from 00_functions.R
…
First we’ll create the larger grid (20x20km)
<- bcr |> # Starting area to make grid over
grid_20 make_grid(size = 20 * 1000) |> # 20km in meters
mutate(grid_id = 1:n(),
area_ha = 40000) # reminder (20x20km = 400km2 = 40,000 ha)
And now we’ll split this grid into to the smaller 10x10 km grid
<- grid_filter(grid_20, bcr, size = 10 * 1000) |> # 10km in meters
grid_10 bind_rows() |>
mutate(grid_id = 1:n(),
area_ha = 10 * 10 * 100) # reminder (10x10km = 100km2 = 10,000 ha)
Double check
20x20km grid
ggplot() +
geom_sf(data = base_map) +
geom_sf(data = bcr, aes(fill = bcr), show.legend = FALSE) +
geom_sf(data = grid_20, fill = NA) +
scale_fill_viridis_d()
10x10km grid showing BC border subsection
- dotted grid are 10x10
- solid grid are 20x20
ggplot() +
geom_sf(data = base_map) +
geom_sf(data = bcr, aes(fill = bcr), show.legend = FALSE) +
geom_sf(data = grid_10, fill = NA, linetype = "dotted") +
geom_sf(data = grid_20, fill = NA, colour = "grey30") +
coord_sf(crs = 4326, xlim = c(-125, -118), ylim = c(48, 52)) +
scale_fill_viridis_d()
Then for both grids - merge in the checklist/observation data - keep only cells that have at least one checklist (i.e., use an inner join with left = FALSE
)
grid_10 <- grid_filter(grid_20, full_sf, size = size[3]) |> mutate(grid_id = 1:n(), area = 500)
write_rds(grid_10, here(“Data/Intermediate/grid_10km.rds”))
grid_500_coords <- grid_500 |> st_filter(full_sf) |> st_centroid() |> write_rds(here(“Data/Intermediate/grid_coords_10km.rds”))
# Add grid_ids to count data grid_500 |> st_join(full_sf, left = FALSE) |> st_drop_geometry() |> write_rds(here(“Data/Intermediate/lobcur_01_grid_500ha.rds”)) }
if(!file.exists(here(“Data/Intermediate/grid_100ha.rds”))) { grid_100 <- grid_filter(grid_med, full_sf, size = size[3]) |> bind_rows() |> mutate(grid_id = 1:n(), area = 100) write_rds(grid_100, here(“Data/Intermediate/grid_100ha.rds”))
grid_100_coords <- grid_100 |> st_filter(full_sf) |> st_centroid() |> write_rds(here(“Data/Intermediate/grid_coords_100ha.rds”))
# Add grid_ids to count data grid_100 |> st_join(full_sf, left = FALSE) |> st_drop_geometry() |> write_rds(here(“Data/Intermediate/lobcur_01_grid_100ha.rds”)) }
if(!file.exists(here(“Data/Intermediate/grid_12ha.rds”))) { grid_12 <- grid_filter(grid_med, full_sf, size = size[1]) |> bind_rows() |> mutate(grid_id = 1:n(), area = 12) write_rds(grid_12, here(“Data/Intermediate/grid_12ha.rds”))
grid_12_coords <- grid_12 |> st_filter(full_sf) |> st_centroid() |> write_rds(here(“Data/Intermediate/grid_coords_12ha.rds”))
# Add grid_ids to count data grid_12 |> st_join(full_sf, left = FALSE) |> st_drop_geometry() |> write_rds(here(“Data/Intermediate/lobcur_01_grid_12ha.rds”)) }
#+ echo = FALSE # Reproducible ————————————————————— #’ # Reproducible #+ results = “asis” print(citation(“sf”), style = “html”) print(citation(“wk”), style = “html”)
#’ ## Session Info #+ R.options = list(width = 100) devtools::session_info()