Skip to content

Commit f48f1cc

Browse files
committed
data_prep census_tract_2022
1 parent d8b1bea commit f48f1cc

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

data_prep/R/census_tract_2022.R

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
library(sf)
2+
library(data.table)
3+
library(dplyr)
4+
5+
dest_dir <- 'C:/Users/user/Downloads/BR_Malha_Preliminar_2022/'
6+
7+
df <- sf::st_read('C:/Users/user/Downloads/BR_Malha_Preliminar_2022/BR_Malha_Preliminar_2022.gpkg')
8+
saveRDS(df, 'C:/Users/user/Downloads/BR_Malha_Preliminar_2022/BR_Malha_Preliminar_2022.rds')
9+
10+
df$AREA_KM2 <- NULL
11+
12+
temp_sf <- dplyr::rename(df,
13+
code_tract = CD_SETOR,
14+
code_muni = CD_MUN,
15+
name_muni = NM_MUN,
16+
code_subdistrict = CD_SUBDIST,
17+
name_subdistrict = NM_SUBDIST,
18+
code_district = CD_DIST,
19+
name_district = NM_DIST,
20+
code_urban_concentration = CD_CONCURB,
21+
name_urban_concentration = NM_CONCURB,
22+
code_state = CD_UF,
23+
name_state = NM_UF,
24+
code_micro = CD_MICRO,
25+
name_micro = NM_MICRO,
26+
code_meso = CD_MESO,
27+
name_meso = NM_MESO,
28+
code_immediate = CD_RGI,
29+
name_immediate = NM_RGI,
30+
code_intermediate = CD_RGINT,
31+
name_intermediate = NM_RGINT,
32+
code_region = CD_REGIAO,
33+
name_region = NM_REGIAO
34+
)
35+
head(temp_sf)
36+
37+
38+
# remove P from code tract
39+
temp_sf <- mutate(temp_sf, code_tract = gsub("P","", code_tract))
40+
head(temp_sf)
41+
42+
43+
# make all columns as character
44+
char_cols <- names(temp_sf)
45+
char_cols <- char_cols[char_cols %like% 'code_|name_']
46+
temp_sf <- mutate(temp_sf, across(all_of(char_cols), as.character))
47+
48+
49+
# Use UTF-8 encoding
50+
temp_sf <- use_encoding_utf8(temp_sf)
51+
52+
# Harmonize spatial projection CRS, using SIRGAS 2000 epsg (SRID): 4674
53+
temp_sf <- harmonize_projection(temp_sf)
54+
55+
save_state <- function(code_uf){ # code_uf <- 11
56+
57+
temp_sf2 <- subset(temp_sf, code_state == code_uf)
58+
59+
# temp_sf2 <- subset(temp_sf2, code_muni == '3304557')
60+
61+
62+
63+
64+
# convert to MULTIPOLYGON
65+
temp_sf2 <- to_multipolygon(temp_sf2)
66+
67+
# simplify
68+
temp_sf_simplified <- simplify_temp_sf(temp_sf2)
69+
70+
# Save cleaned sf in the cleaned directory
71+
sf::st_write(temp_sf2, paste0(dest_dir, code_uf,".gpkg") )
72+
sf::st_write(temp_sf_simplified, paste0(dest_dir, code_uf,"_simplified.gpkg"),)
73+
74+
}
75+
76+
77+
all_states <- unique(temp_sf$code_state)
78+
79+
# Apply function to save the data
80+
gc(reset = T)
81+
82+
tictoc::tic()
83+
future::plan(strategy = 'multisession')
84+
furrr::future_map(.x=all_states, .f=save_state, .progress = T)
85+
tictoc::toc()
86+
87+
88+
tictoc::tic()
89+
pbapply::pblapply(X=all_states, FUN=save_state)
90+
tictoc::toc()

data_prep/R/support_fun.R

+1
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ to_multipolygon <- function(temp_sf){
278278
temp_sf <- subset(temp_sf, sf::st_geometry_type(temp_sf) |> as.character() != "LINESTRING")
279279

280280
# get polyons
281+
temp_sf <- sf::st_cast(temp_sf, "POLYGON")
281282
temp_sf <- sf::st_collection_extract(temp_sf, "POLYGON")
282283
temp_sf <- sf::st_cast(temp_sf, "MULTIPOLYGON")
283284
return(temp_sf)

0 commit comments

Comments
 (0)