Skip to content

Commit 384b76b

Browse files
committed
Improve docs and organization
1 parent 182ea9b commit 384b76b

27 files changed

+710
-1194
lines changed

DESCRIPTION

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: canmap
2-
Title: Standard Canadian geography shapefiles.
2+
Title: Standard Canadian geography shapefiles
33
Version: 0.0.0.9000
44
Authors@R:
55
person(given = "Jesse",
@@ -11,6 +11,7 @@ Description: What the package does (one paragraph).
1111
License: MIT + file LICENSE
1212
Encoding: UTF-8
1313
LazyData: true
14+
Depends: R (>= 3.4)
1415
Imports:
1516
here,
1617
fs,
@@ -23,5 +24,6 @@ RoxygenNote: 7.0.2
2324
Suggests:
2425
knitr,
2526
rmarkdown,
26-
sf
27+
sf,
28+
testthat (>= 2.1.0)
2729
VignetteBuilder: knitr

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export("%>%")
44
export(download_geography)
55
export(get_geoinfo)
66
importFrom(magrittr,"%>%")
7+
importFrom(rlang,.data)

R/canmap-package.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#' @keywords internal
2+
"_PACKAGE"
3+
4+
#' @importFrom rlang .data
5+
6+
# The following block is used by usethis to automatically manage
7+
# roxygen namespace tags. Modify with care!
8+
## usethis namespace: start
9+
## usethis namespace: end
10+
NULL

R/code_book.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#' Metadata directory for Canadian geographic datasets
2+
#'
3+
#' Code positions, names and definitions of geographic metadata
4+
#'
5+
#'
6+
#' @format A data frame.
7+
#' \describe{
8+
#' \item{code_type}{The type of code, (e.g., \code{geo_level})}
9+
#' \item{code}{Code occurring in shapefile filenames (e.g., \code{geo_level})}
10+
#' \item{code_desc}{Short description of the code value}
11+
#' }
12+
#' @source \url{https://www150.statcan.gc.ca/n1/pub/92-196-x/92-196-x2016001-eng.htm}
13+
"code_book"
14+

R/code_pos.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#' Directory of metadata character positions for Canadian geographic datasets
2+
#'
3+
#' Code positions of geographic metadata in filenames
4+
#'
5+
#'
6+
#' @format A data frame.
7+
#' \describe{
8+
#' \item{code_type}{The type of code, (e.g., \code{geo_level})}
9+
#' \item{start_chr}{Start position of the code in the filename (e.g., 2 = start of \code{geo_level})}
10+
#' \item{stop_chr}{End position of the code in the filename (e.g., 4 = end of \code{geo_level})}
11+
#' }
12+
#' @source \url{https://www150.statcan.gc.ca/n1/pub/92-196-x/92-196-x2016001-eng.htm}
13+
"code_pos"
14+

R/download_geography.R

Lines changed: 0 additions & 38 deletions
This file was deleted.

R/get_geoinfo.R

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
#' @format A data frame.
77
#' \describe{
88
#' \item{filepath}{The name of the file.}
9-
#' \item{ext}{Extension: .zip}
10-
#' \item{type}{File or directory.}
119
#' \item{size}{Zipped file size.}
1210
#' \item{path}{Url (external) or filepath (internal.)}
1311
#' \item{ref_date}{Year of the geography, usually Census year.}
@@ -17,9 +15,8 @@
1715
#' \item{file_type}{Cartographic or digital boundary file.}
1816
#' \item{format}{Arcgis (.shp), or MapInfo or some other one.}
1917
#' \item{projection}{lat/long or Lambert conformal conic.}
20-
#' \item{geo_coverage}{ = Canada.}
2118
#' \item{language}{English or French.}
2219
#' }
2320
#' @source \url{https://www150.statcan.gc.ca/n1/pub/92-196-x/92-196-x2016001-eng.htm}
24-
"shapefile_info"
21+
"shapefile_paths"
2522

R/utils.R

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,110 @@
1+
## Utility functions for Canadian geographic shapefiles and filenames
12

23

34

45

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) {
528

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+
}

README.Rmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ remotes::install_github("tweed1e/canmap")
3333
```{r example}
3434
library(canmap)
3535
36-
tibble::as_tibble(shapefile_info)
36+
tibble::as_tibble(shapefile_paths)
3737
3838
# then pick a shapefile and get the link:
39-
shapefile_info[1, c("filepath", "path")]
39+
shapefile_paths[1, c("filepath", "path")]
4040
```
4141

4242

0 commit comments

Comments
 (0)