1-
2-
3-
41# ' Read the geosuite data
52# '
63# ' @description
7- # ' Reads the geosuite data for the given level and census year. Data gets cached after first download.
4+ # ' Reads the geosuite data for the given level and census year. Data gets cached after first download if the
5+ # ' cancensus cache path has been set. For older
6+ # ' years `get_statcan_geographic_attributes()` can fill in most of the information
87# '
98# ' @param level geographic level to return the data for, valid choices are
10- # ' "DB", "DA", "ADA", "CT", "CSD", "CMA", "CD", "PR"
9+ # ' "DB", "DA", "ADA", "CT", "CSD", "CMA", "CD", "PR", "FED", "DPL", "ER", "PN", "POPCTR"
1110# ' @param census_year census year to get the data for, right now only 2021 is supported
1211# ' @param refresh (logical) refresh the cache if true
1312# ' @return tibble with the geosuite data
1413# '
15- # ' @keywords internal
1614# '
1715# ' @examples
1816# ' # list add the cached census data
1917# ' \dontrun{
20- # ' get_geo_suite ("DA","2021")
18+ # ' get_statcan_geo_suite ("DA","2021")
2119# ' }
2220# ' @export
23- get_geo_suite <- function (level ,census_year = " 2021" ,refresh = FALSE ){
21+ get_statcan_geo_suite <- function (level ,census_year = " 2021" ,refresh = FALSE ){
2422 valid_years <- c(" 2021" ) # seq(2001,2021,5) %>% as.character()
25- valid_levels <- c(" DB" , " DA" , " CT" , " ADA" , " CSD" , " CMA" , " CD" , " PR" )
23+ valid_levels <- c(" DB" , " DA" , " CT" , " ADA" , " CSD" , " CMA" , " CD" , " PR" , " FED " , " DPL " , " ER " , " PN " , " POPCTR " )
2624 if (! (as.character(census_year ) %in% valid_years )) {
2725 stop(paste0(" Only census years " ,paste0(valid_years ,collapse = " , " ),
2826 " are supported for GeoSuite" ))
@@ -77,10 +75,12 @@ get_geo_suite <- function(level,census_year="2021",refresh=FALSE){
7775# '
7876# ' @description
7977# ' Reads the Dissemination Geographies Relationship File for the given census year. The table contains
80- # ' the information on how all the geographic levels are related for each area. A reference guide is available
78+ # ' the information on how all the geographic levels are related for each area. Data gets cached after first download if the
79+ # ' cancensus cache path has been set. A reference guide is available
8180# ' at https://www150.statcan.gc.ca/n1/en/catalogue/982600032021001
8281# '
83- # ' @param census_year census year to get the data for, right now only 2021 is supported
82+ # ' @param census_year census year to get the data for, right now only 2021 is supported, for older
83+ # ' years `get_statcan_geographic_attributes()` can fill in most of the information
8484# ' @param refresh (logical) refresh the cache if true
8585# ' @return tibble with the relationship data
8686# '
@@ -89,10 +89,10 @@ get_geo_suite <- function(level,census_year="2021",refresh=FALSE){
8989# ' @examples
9090# ' # list add the cached census data
9191# ' \dontrun{
92- # ' get_geography_relationship ("2021")
92+ # ' get_statcan_geography_relationships ("2021")
9393# ' }
9494# ' @export
95- get_geography_relationship <- function (census_year = " 2021" , refresh = FALSE ){
95+ get_statcan_geography_relationships <- function (census_year = " 2021" , refresh = FALSE ){
9696 valid_years <- c(" 2021" )
9797 if (! (as.character(census_year ) %in% valid_years )) {
9898 stop(paste0(" Only census years " ,paste0(valid_years ,collapse = " , " ),
@@ -113,28 +113,30 @@ get_geography_relationship <- function(census_year="2021", refresh=FALSE){
113113# ' @description
114114# ' Reads the Geographies Attributes File for the given census year. The table contains
115115# ' the information on how all the geographic levels are related for each area, and population, dwelling and household counts.
116- # ' A reference guide is available
116+ # ' Data gets cached after first download if the
117+ # ' cancensus cache path has been set. A reference guide is available
117118# ' at https://www150.statcan.gc.ca/n1/en/catalogue/92-151-G2021001
118119# '
119- # ' @param census_year census year to get the data for, right now only 2006, 2011 and 2016 are supported
120+ # ' @param census_year census year to get the data for, right now only 2006, 2011, 2016, 2021 are supported
120121# ' @param refresh (logical) refresh the cache if true
121122# ' @return tibble with the relationship data
122123# '
123124# ' @examples
124125# ' # list add the cached census data
125- # ' get_geographic_attributes("2016")
126- # '
126+ # ' \dontrun{
127+ # ' get_statcan_geographic_attributes("2021")
128+ # ' }
127129# ' @export
128- get_geographic_attributes <- function (census_year = " 2016 " ,refresh = FALSE ){
130+ get_statcan_geographic_attributes <- function (census_year = " 2021 " ,refresh = FALSE ){
129131 census_year <- as.character(census_year )[1 ]
130- valid_years <- seq(2006 ,2016 ,5 ) %> % as.character
132+ valid_years <- seq(2006 ,2021 ,5 ) %> % as.character
131133 if (! (as.character(census_year ) %in% valid_years )) {
132134 stop(paste0(" Only census years " ,paste0(valid_years ,collapse = " , " ),
133135 " are supported for the geographic relationship file." ))
134136 }
135- urls <- c(" 2016" = " https://www12.statcan.gc.ca/census-recensement/2016/geo/ref/gaf/files-fichiers/2016_92-151_XBB_txt.zip" ,
137+ urls <- c(" 2021" = " https://www12.statcan.gc.ca/census-recensement/2021/geo/aip-pia/attribute-attribs/files-fichiers/2021_92-151_X.zip" ,
138+ " 2016" = " https://www12.statcan.gc.ca/census-recensement/2016/geo/ref/gaf/files-fichiers/2016_92-151_XBB_txt.zip" ,
136139 " 2011" = " https://www12.statcan.gc.ca/census-recensement/2011/geo/ref/files-fichiers/2011_92-151_XBB_txt.zip" ,
137- # "2011"="https://www12.statcan.gc.ca/census-recensement/2011/geo/ref/files-fichiers/2011_92-151_XBB_xlsx.zip",
138140 " 2006" = " https://www12.statcan.gc.ca/census-recensement/2011/geo/ref/files-fichiers/2006_92-151_XBB_txt.zip" )
139141
140142 base_path <- cache_path(" attribute_files" )
@@ -147,8 +149,9 @@ get_geographic_attributes <- function(census_year="2016",refresh=FALSE){
147149 utils :: download.file(urls [[census_year ]],tmp )
148150 utils :: unzip(tmp ,exdir = base_path_year )
149151 }
150- file <- dir(base_path_year ,pattern = " \\ .txt" ,full.names = TRUE )
151- if (census_year == " 2016" ) {
152+ if (census_year == " 2021" ) file <- dir(base_path_year ,pattern = " \\ .csv" ,full.names = TRUE )
153+ else file <- dir(base_path_year ,pattern = " \\ .txt" ,full.names = TRUE )
154+ if (census_year %in% c(" 2016" ," 2021" )) {
152155 result <- readr :: read_csv(file ,col_types = readr :: cols(.default = " c" ),
153156 locale = readr :: locale(encoding = " Windows-1252" ))
154157 } else {
@@ -175,3 +178,7 @@ get_geographic_attributes <- function(census_year="2016",refresh=FALSE){
175178 dplyr :: mutate(dplyr :: across(dplyr :: matches(" DBpop\\ d{4}|DBtdwell\\ d{4}|DBurdwell\\ d{4}|DBarea" ),as.numeric ))
176179}
177180
181+
182+
183+
184+
0 commit comments