Skip to content

Commit 92b3520

Browse files
committed
prep_urban_area 2005
1 parent 45e4349 commit 92b3520

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

r-package/prep_data/prep_urban_area.R

+39-32
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ unzip( zip_file, exdir = dir_raw)
7676
##### 4.1 read shape files -------------------
7777

7878
if(year==2005){
79-
ACP_urban_05 <- st_read( paste0(dir_2005,"/AreasUrbanizadas_MunicipiosACP_porMunicipio.shp"),
79+
ACP_urban_05 <- st_read( paste0(dir_raw,"/AreasUrbanizadas_MunicipiosACP_porMunicipio.shp"),
8080
options = "ENCODING=WINDOWS-1252")
81-
cemk_urban_05 <- st_read( paste0(dir_2005,"/AreasUrbanizadas_MunicipiosAcima100k_porMunicipio.shp"),
81+
cemk_urban_05 <- st_read( paste0(dir_raw,"/AreasUrbanizadas_MunicipiosAcima100k_porMunicipio.shp"),
8282
options = "ENCODING=WINDOWS-1252")
83-
cost_urban_05 <- st_read( paste0(dir_2005,"/AreasUrbanizadas_MunicipiosCosteiros_porMunicipio.shp"),
83+
cost_urban_05 <- st_read( paste0(dir_raw,"/AreasUrbanizadas_MunicipiosCosteiros_porMunicipio.shp"),
8484
options = "ENCODING=WINDOWS-1252")
8585
}
8686

@@ -105,29 +105,33 @@ if(year==2015){
105105
# Make sure all data sets have the same columns (in the same order)
106106

107107
if(year==2005){
108-
ACP_urban_05$POP_2005 <- NA
109-
ACP_urban_05$dataset <- "population concentration area"
110-
111-
cost_urban_05$POP_2005 <- NA
112-
cost_urban_05$ACP <- NA
113-
cost_urban_05$COD_ACP <- NA
114-
cost_urban_05$dataset <- "coastal area"
115-
116-
cemk_urban_05$ACP <- NA
117-
cemk_urban_05$COD_ACP <- NA
118-
cemk_urban_05$dataset <- "population above 100k"
119-
120-
# columns in the same order
121-
setDT(ACP_urban_05)
122-
setDT(cost_urban_05)
123-
setDT(cemk_urban_05)
124-
125-
setcolorder(cost_urban_05, neworder= c(names(ACP_urban_05)) )
126-
setcolorder(cemk_urban_05, neworder= c(names(ACP_urban_05)) )
108+
ACP_urban_05 <- mutate(ACP_urban_05,
109+
POP_2005 = NA,
110+
dataset = "population concentration area")
111+
112+
cost_urban_05 <- mutate(cost_urban_05,
113+
POP_2005 = NA,
114+
ACP = NA,
115+
COD_ACP = NA,
116+
dataset = "coastal area")
117+
118+
cemk_urban_05 <- mutate(cemk_urban_05,
119+
ACP = NA,
120+
COD_ACP = NA,
121+
dataset = "population above 100k")
122+
123+
# if they come with the same projection, reorder and rbind
124+
if (st_crs(ACP_urban_05) == st_crs(cost_urban_05) &
125+
st_crs(cost_urban_05) == st_crs(cemk_urban_05)) {
126+
127+
col_order <- names(ACP_urban_05)
128+
cost_urban_05 <- dplyr::select(cost_urban_05, all_of(col_order))
129+
cemk_urban_05 <- dplyr::select(cemk_urban_05, all_of(col_order))
130+
131+
temp_sf <- rbind(ACP_urban_05, cemk_urban_05, cost_urban_05)
132+
} else{stop('cannot rbind 2005 data sets')}
133+
}
127134

128-
# pile them up
129-
urb_2005 <- rbind(ACP_urban_05, cemk_urban_05, cost_urban_05)
130-
}
131135

132136

133137
if(year==2015){
@@ -160,13 +164,14 @@ if(year==2015){
160164
if(year==2005){
161165
temp_sf <- dplyr::select(temp_sf,
162166
code_urb = GEOC_URB,
163-
pop_2005 = POP_2005,
164-
density = Tipo,
165167
code_muni = GEOCODIGO,
166-
name_muni = NOME_MUNIC,
167168
code_acp = COD_ACP,
168169
name_acp = ACP,
170+
name_muni = NOME_MUNIC,
169171
abbrev_state = UF,
172+
pop_2005 = POP_2005,
173+
density = Tipo,
174+
area_km2 = Area_Km2,
170175
dataset = dataset,
171176
geometry = geometry
172177
)
@@ -213,15 +218,17 @@ temp_sf <- to_multipolygon(temp_sf)
213218
temp_sf <- fix_topoly(temp_sf)
214219

215220
# reoder columns
221+
if(year == 2005){
222+
col_order <- c("code_urb", "code_acp", "name_acp", "code_muni", "name_muni",
223+
"code_state", "name_state", "abbrev_state", "code_region",
224+
"name_region", "pop_2005", "density", "dataset", "area_km2",
225+
"geometry")
226+
}
216227
if(year == 2015){
217228
col_order <- c("fid_1", "code_muni", 'name_muni', "code_state", "name_state",
218229
"abbrev_state", "code_region", "name_region", "type", "density",
219230
"area_km2", "geometry")
220231
}
221-
if(year == 2005){
222-
col_order <- c("code_urb", "pop_2005", "density", "code_muni", "name_muni", "code_acp", "name_acp",
223-
"code_state", "abbrev_state", "name_state", "dataset", "geometry")
224-
}
225232

226233
temp_sf <- dplyr::select(temp_sf, all_of(col_order))
227234

0 commit comments

Comments
 (0)