@@ -76,11 +76,11 @@ unzip( zip_file, exdir = dir_raw)
76
76
# #### 4.1 read shape files -------------------
77
77
78
78
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" ),
80
80
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" ),
82
82
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" ),
84
84
options = " ENCODING=WINDOWS-1252" )
85
85
}
86
86
@@ -105,29 +105,33 @@ if(year==2015){
105
105
# Make sure all data sets have the same columns (in the same order)
106
106
107
107
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
+ }
127
134
128
- # pile them up
129
- urb_2005 <- rbind(ACP_urban_05 , cemk_urban_05 , cost_urban_05 )
130
- }
131
135
132
136
133
137
if (year == 2015 ){
@@ -160,13 +164,14 @@ if(year==2015){
160
164
if (year == 2005 ){
161
165
temp_sf <- dplyr :: select(temp_sf ,
162
166
code_urb = GEOC_URB ,
163
- pop_2005 = POP_2005 ,
164
- density = Tipo ,
165
167
code_muni = GEOCODIGO ,
166
- name_muni = NOME_MUNIC ,
167
168
code_acp = COD_ACP ,
168
169
name_acp = ACP ,
170
+ name_muni = NOME_MUNIC ,
169
171
abbrev_state = UF ,
172
+ pop_2005 = POP_2005 ,
173
+ density = Tipo ,
174
+ area_km2 = Area_Km2 ,
170
175
dataset = dataset ,
171
176
geometry = geometry
172
177
)
@@ -213,15 +218,17 @@ temp_sf <- to_multipolygon(temp_sf)
213
218
temp_sf <- fix_topoly(temp_sf )
214
219
215
220
# 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
+ }
216
227
if (year == 2015 ){
217
228
col_order <- c(" fid_1" , " code_muni" , ' name_muni' , " code_state" , " name_state" ,
218
229
" abbrev_state" , " code_region" , " name_region" , " type" , " density" ,
219
230
" area_km2" , " geometry" )
220
231
}
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
- }
225
232
226
233
temp_sf <- dplyr :: select(temp_sf , all_of(col_order ))
227
234
0 commit comments