Skip to content

Instantly share code, notes, and snippets.

@sharlagelfand
Created May 9, 2023 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharlagelfand/9585cd1e07467206c3c1a5bf802dcd1e to your computer and use it in GitHub Desktop.
Save sharlagelfand/9585cd1e07467206c3c1a5bf802dcd1e to your computer and use it in GitHub Desktop.
FSAs in Vancouver CMA
# Get v0.5.6 of cancensus for FSA geography
# remotes::install_github("mountainMath/cancensus", ref = "v0.5.6")
library(cancensus)
library(tongfen)
library(dplyr)
library(sf)
library(ggplot2)
dataset <- "CA21"
# Get Vancouver CMA
cma <- get_census(dataset,
regions = list(PR = 59), # BC province code
level = "CMA",
geo_format = "sf"
) %>%
filter(name == "Vancouver (B)") %>%
select(name, geometry)
# Get FSA geographies
fsa <- get_statcan_geographies(2021, "FSA") %>%
filter(PRUID == 59) %>%
select(fsa = CFSAUID, geometry)
# Reproject FSAs to match projection of CMA
st_crs(cma)
fsa <- fsa %>%
st_transform(crs = 4326)
# Get overlap of FSAs with CMA
fsa_cma_overlap <- tongfen_tag_largest_overlap(source = fsa, target = cma, target_id = "name")
# ...overlap_fraction field shows prop of overlap, usually it's pretty clear what's a genuine overlap:
hist(fsa_cma_overlap[["...overlap_fraction"]])
# Take > 0.8 as overlap
fsa_in_vancouver_cma <- fsa_cma_overlap %>%
filter(...overlap_fraction > 0.8)
ggplot() +
geom_sf(data = fsa_in_vancouver_cma, fill = "blue", colour = "blue", alpha = 0.25) +
geom_sf(data = cma, fill = NA, colour = "black")
# Bowen Island is missed here, part of V0N which is huge (includes more northern, island, etc) - but probably won't be part of our analysis anyways
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment