Created
July 2, 2020 18:21
-
-
Save paleolimbot/2d6411a01140b08d9d5e3670c9ddf5a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
library(sf) | |
library(s2) # remotes::install_github("r-spatial/s2") | |
counties_sf <- readRDS(url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_USA_2_sf.rds")) %>% | |
select(state = NAME_1, county = NAME_2) %>% | |
# USA atlas projection | |
st_transform(2163) | |
counties_s2 <- counties_sf %>% | |
st_transform(4326) %>% | |
as_tibble() %>% | |
mutate(geometry = as_s2_geography(sf::st_as_binary(geometry))) | |
# neighbouring counties using intersects | |
system.time( | |
neighbouring_county_int_indices_sf <- | |
st_intersects(counties_sf$geometry, counties_sf$geometry) | |
) | |
system.time( | |
neighbouring_county_int_indices_s2 <- | |
s2_intersects_matrix(counties_s2$geometry, counties_s2$geometry, options = s2_options(model = 2)) | |
) | |
attributes(neighbouring_county_int_indices_sf) <- NULL | |
all.equal(neighbouring_county_int_indices_s2, neighbouring_county_int_indices_sf) | |
# union states together | |
system.time( | |
states_sf <- counties_sf %>% | |
group_by(state) %>% | |
summarise() | |
) | |
system.time( | |
states_s2 <- counties_s2 %>% | |
group_by(state) %>% | |
summarise(geometry = s2_union_agg(geometry)) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Created on 2020-07-02 by the reprex package (v0.3.0)