Skip to content

Commit 1eec960

Browse files
author
rafapereirabr
committed
closes #275
1 parent f463bd8 commit 1eec960

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

data_prep/R/census_tract_2022.R

-6
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,7 @@ gc()
9292
save_state <- function(code_uf){ # code_uf <- 33
9393

9494
temp_sf2 <- subset(temp_sf, code_state == code_uf)
95-
<<<<<<< HEAD
9695
# temp_sf2 <- subset(temp_sf2, code_muni == '3304557')
97-
=======
98-
99-
# temp_sf2 <- subset(temp_sf2, code_muni == '3304557')
100-
101-
>>>>>>> e8fd7687ce69803eb31daab82f51a60a25f91525
10296

10397
# convert to MULTIPOLYGON
10498
temp_sf2 <- to_multipolygon(temp_sf2)

data_prep/R/muni_clean.R

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ clean_muni <- function( muni_raw_paths ){
1111
# all_muni_raw_paths <- list.files(path = paste0('./data_raw/municipios/', year),
1212
# pattern = '.rds',
1313
# full.names = TRUE)
14-
# f <- all_muni_raw_paths[16]
14+
# f <- all_muni_raw_paths[20]
1515
#
1616
# # 6666
1717
# muni_raw_paths <- all_muni_raw_paths[1]
@@ -116,12 +116,12 @@ clean_muni <- function( muni_raw_paths ){
116116
# remove geometries with area == 0
117117
temp_sf <- temp_sf[ as.numeric(sf::st_area(temp_sf)) != 0, ]
118118

119+
# convert to MULTIPOLYGON
120+
temp_sf <- to_multipolygon(temp_sf)
121+
119122
# simplify
120123
temp_sf_simplified <- simplify_temp_sf(temp_sf)
121124

122-
# convert to MULTIPOLYGON
123-
temp_sf <- to_multipolygon(temp_sf)
124-
temp_sf_simplified <- to_multipolygon(temp_sf_simplified)
125125

126126
# Make any invalid geom valid # st_is_valid( sf)
127127
temp_sf <- fix_topoly(temp_sf)
@@ -151,11 +151,12 @@ clean_muni <- function( muni_raw_paths ){
151151
sf::st_write(temp2_simplified, i,
152152
overwrite = TRUE, append = FALSE,
153153
delete_dsn = T, delete_layer = T, quiet = T)
154-
}
154+
}
155+
155156
}
156157

157158
# clean all files
158-
pbapply::pblapply(X = muni_raw_paths, FUN = clean_file)
159+
pbapply::pblapply(X = all_muni_raw_paths, FUN = clean_file)
159160

160161
# requiredPackages <- c('data.table', 'sf','dplyr', 'sfheaders', 'units',
161162
# 'lwgeom', 'rgeos', 'sp', 'maptools', 'stringr', 'stringi')

data_prep/R/support_fun.R

+13-4
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,20 @@ to_multipolygon <- function(temp_sf){
287287
temp_sf <- sf::st_cast(temp_sf, "POLYGON")
288288
temp_sf <- sf::st_collection_extract(temp_sf, "POLYGON")
289289
temp_sf <- sf::st_cast(temp_sf, "MULTIPOLYGON")
290-
return(temp_sf)
290+
}
291+
292+
# merge polygons into single multiploygon
293+
col_names <- names(temp_sf)
294+
col_names <- col_names[ !col_names %like% 'geometry|geom']
295+
296+
temp_sf <- temp_sf |>
297+
group_by(across(all_of(col_names))) |>
298+
summarise()
299+
300+
301+
return(temp_sf)
302+
}
291303

292-
} else {
293-
return(temp_sf) }
294-
}
295304

296305
###### Make valid topology -----------------
297306

0 commit comments

Comments
 (0)