|
| 1 | +## Utility functions for Canadian geographic shapefiles and filenames |
1 | 2 |
|
2 | 3 |
|
3 | 4 |
|
4 | 5 |
|
| 6 | +#' Download and unzip Canadian shapefiles |
| 7 | +#' |
| 8 | +#' Download and unzip Canadian shapefiles from Statistics Canada |
| 9 | +#' |
| 10 | +#' @param geo_path Full path/url to zipped shapefile |
| 11 | +#' @param geo_dir Directory to download to --- may not keep this. |
| 12 | +#' |
| 13 | +#' |
| 14 | +#' @export |
| 15 | +#' |
| 16 | +#' @return A tibble of shapefiles along with metadata, including |
| 17 | +#' \code{filepath}, \code{size}, \code{path}, |
| 18 | +#' \code{ref_date}, \code{geo_code}, \code{geo_level}, |
| 19 | +#' \code{file_type}, \code{format}, \code{projection}, |
| 20 | +#' and \code{language}. |
| 21 | +#' |
| 22 | +#' @examples |
| 23 | +#' \donttest{ |
| 24 | +#' download_geography("http://www12.statcan.gc.ca/census-recensement/2011/ |
| 25 | +#' geo/bound-limit/files-fichiers/2016/lpr_000a16a_e.zip") |
| 26 | +#' } |
| 27 | +download_geography <- function(geo_path, geo_dir = NULL) { |
5 | 28 |
|
| 29 | + file_name <- fs::path_file(geo_path) |
| 30 | + geo_folder_name <- fs::path_ext_remove(file_name) |
| 31 | + |
| 32 | + if (is.null(geo_dir)) { |
| 33 | + geo_dir <- here::here("geography", geo_folder_name) |
| 34 | + } |
| 35 | + |
| 36 | + temp <- tempfile() |
| 37 | + |
| 38 | + utils::download.file(geo_path, temp) |
| 39 | + |
| 40 | + unzipped_files <- utils::unzip(temp, exdir = geo_dir) |
| 41 | + |
| 42 | + unlink(temp) |
| 43 | + |
| 44 | + shp_path <- stringr::str_subset(unzipped_files, pattern = ".shp") |
| 45 | + |
| 46 | + shp_path |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +#' Get metadata associated with geographic filename. |
| 52 | +#' |
| 53 | +#' Get metadata associated with geographic filename. |
| 54 | +#' |
| 55 | +#' @param filename Name of geography file |
| 56 | +#' |
| 57 | +#' @export |
| 58 | +#' |
| 59 | +#' @return A one-row tibble of metadata associated with the filename |
| 60 | +#' of Statistics Canada's shapefiles. |
| 61 | +#' |
| 62 | +#' @examples |
| 63 | +#' \donttest{ |
| 64 | +#' get_geoinfo("lpr_000a16a_e") |
| 65 | +#' } |
| 66 | +get_geoinfo <- function(filename) { |
| 67 | + |
| 68 | + # yy <- canmap::code_pos %>% |
| 69 | + # dplyr::mutate(code = stringr::str_sub(filename, start_chr, stop_chr)) %>% |
| 70 | + # dplyr::select(-start_chr, -stop_chr) |
| 71 | + # |
| 72 | + # z <- yy %>% |
| 73 | + # dplyr::left_join(canmap::code_book %>% dplyr::select(code_type, code, code_desc, long_desc), by = c("code_type", "code")) %>% |
| 74 | + # dplyr::select(code_type, code_desc) %>% |
| 75 | + # tidyr::spread(code_type, code_desc) |
| 76 | + # |
| 77 | + # z$geo_code <- yy %>% |
| 78 | + # dplyr::filter(code_type == "geo_level") %>% |
| 79 | + # dplyr::pull(code) |
| 80 | + # |
| 81 | + # z %>% |
| 82 | + # dplyr::mutate(filename = filename) %>% |
| 83 | + # dplyr::select(filename, ref_date, geo_code, geo_level, file_type, format, projection, geo_coverage, language) |
| 84 | + extract_codes <- code_pos |
| 85 | + |
| 86 | + extract_codes$code <- stringr::str_sub(filename, |
| 87 | + code_pos$start_chr, |
| 88 | + code_pos$stop_chr) |
| 89 | + |
| 90 | + extract_codes[, c("start_chr", "stop_chr")] <- NULL |
| 91 | + |
| 92 | + code_descriptions <- dplyr::left_join(extract_codes, |
| 93 | + canmap::code_book, |
| 94 | + by = c("code_type", "code")) |
| 95 | + code_descriptions[, "code"] <- NULL |
| 96 | + code_descriptions <- tidyr::spread(code_descriptions, |
| 97 | + .data$code_type, |
| 98 | + .data$code_desc) |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + code_descriptions$geo_code <- extract_codes[extract_codes$code_type == "geo_level", ]$code |
| 103 | + code_descriptions$filename <- filename |
| 104 | + |
| 105 | + dplyr::select(code_descriptions, .data$filename, .data$ref_date, |
| 106 | + .data$geo_code, .data$geo_level, .data$file_type, |
| 107 | + .data$format, .data$projection, .data$geo_coverage, |
| 108 | + .data$language) |
| 109 | + |
| 110 | +} |
0 commit comments