-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_09_preparation_add_variables_wm.R
198 lines (161 loc) · 4.68 KB
/
01_09_preparation_add_variables_wm.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
################################################################
# Load Data #
################################################################
# load housing data
red <- qs::qread(
file.path(
data_path,
"housing/WM_contour.qs"
)
)
# regional centers
regional_center <- readRDS(
file.path(
data_path,
"raumzentren/raumzentren_nach_gemeinde_prep.rds"
)
)
# industrial noise
industry_noise <- st_read(
file.path(
data_path,
"umgebungslaerm/industrie/Ballungsraeume/Lden/Aggind_Lden_17.shp"
),
quiet = TRUE
)
# railroad noise
rail_noise <- st_read(
file.path(
data_path,
"umgebungslaerm/schiene/Basisdaten/Mrail_Source_17.shp"
),
quiet = TRUE
)
# street noise
streets <- st_read(
file.path(
data_path,
"umgebungslaerm/strasse/Hauptverkehrsstrassen/Basisdaten/Mroad_Source_17.shp"
),
quiet = TRUE
)
################################################################
# Lockdown indicator #
################################################################
red <- red |>
mutate(
fir_lockdown = case_when(
year_mon_end >= "2020-03" ~ 1,
TRUE ~ 0
)
)
################################################################
# Adding regional centers #
################################################################
#----------------------------------------------
# prepare regional centers
regional_center <- st_set_geometry(
regional_center,
regional_center$geometry
)
# transform
regional_center <- st_transform(
regional_center,
crs = st_crs(red)
)
# drop if there is no geometry
regional_center <- regional_center |>
filter(!st_is_empty(geometry))
#----------------------------------------------
# subset for different regional centers
# large center (Oberzentrum == 1)
largcenter <- regional_center |>
filter(center_identifier == 1)
# medium center (Mittelzentrum == 2)
medcenter <- regional_center |>
filter(center_identifier == 2)
# small center (Grundzentrum == 3)
smalcenter <- regional_center |>
filter(center_identifier == 3)
#----------------------------------------------
# distance to the nearest regional centers
red$distance_largcenter <- as.numeric(
apply(st_distance(red, largcenter), 1, min)
) / 1000
red$distance_medcenter <- as.numeric(
apply(st_distance(red, medcenter), 1, min)
) / 1000
red$distance_smalcenter <- as.numeric(
apply(st_distance(red, smalcenter), 1, min)
) / 1000
################################################################
# Adding industry noise #
################################################################
#----------------------------------------------
# prepare noise
# transform
industry_noise <- st_transform(
industry_noise,
crs = st_crs(red)
)
# find nearest
nearest_ind <- st_nearest_feature(red, industry_noise)
#----------------------------------------------
# calculate distance
red$distance_industry <- as.numeric(
st_distance(
red,
industry_noise[nearest_ind, ],
by_element = TRUE
) / 1000
)
################################################################
# Adding railroad noise #
################################################################
#----------------------------------------------
# prepare noise
# transform
rail_noise <- st_transform(
rail_noise,
crs = st_crs(red)
)
# find nearest
nearest_rail <- st_nearest_feature(red, rail_noise)
#----------------------------------------------
# calculate distance
red$distance_railroads <- as.numeric(
st_distance(
red,
rail_noise[nearest_rail, ],
by_element = TRUE
) / 1000
)
################################################################
# Adding street noise #
################################################################
#----------------------------------------------
# prepare noise
# transform
streets <- st_transform(
streets,
crs = st_crs(red)
)
# find nearest
nearest <- st_nearest_feature(red, streets)
#----------------------------------------------
# calculate distance
red$distance_streets <- as.numeric(
st_distance(red,
streets[nearest, ],
by_element = TRUE
) / 1000
)
################################################################
# Export #
################################################################
qs::qsave(
red,
file.path(
data_path, "housing/WM_complete.qs"
)
)