Created
June 4, 2025 21:37
-
-
Save elipousson/f4c7ab715dda305748723df8a5927249 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| st_join_list <- function( | |
| x, | |
| y, | |
| ..., | |
| join = sf::st_intersects, | |
| .id = "name" | |
| ) { | |
| if (!inherits(y, "list") && inherits(y, "sf")) { | |
| y <- list(y) | |
| } | |
| y_names <- names(y) %||% .id | |
| .id <- vctrs::vec_recycle( | |
| .id, | |
| size = length(y_names) | |
| ) | |
| for (i in seq_along(y_names)) { | |
| nm <- y_names[[i]] | |
| x <- sf::st_join( | |
| x = x, | |
| y = dplyr::select( | |
| y[[nm]], | |
| dplyr::all_of(set_names(.id[[i]], nm)) | |
| ), | |
| join = join | |
| ) | |
| } | |
| x | |
| } | |
| sf_count_list <- function( | |
| x, | |
| y, | |
| ..., | |
| join = sf::st_intersects, | |
| names_to = "name", | |
| values_to = "value", | |
| .id = "name") { | |
| stopifnot( | |
| rlang::is_named(y) | |
| ) | |
| x |> | |
| dplyr::select( | |
| tidyselect::all_of( | |
| attr(x, "sf_column") | |
| ) | |
| ) |> | |
| st_join_list( | |
| y = y | |
| ) |> | |
| sf::st_drop_geometry() |> | |
| tidyr::pivot_longer( | |
| cols = names(y), | |
| names_to = names_to, | |
| values_to = values_to | |
| ) |> | |
| dplyr::count( | |
| .data[[names_to]], | |
| .data[[values_to]], | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment