generated from UBICenter/analysis-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNational_Local_Child_Allowance_Mapping_CA.py
319 lines (312 loc) · 11.3 KB
/
National_Local_Child_Allowance_Mapping_CA.py
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# -*- coding: utf-8 -*-
"""National_Local_Child_Allowance_Mapping_colab-CA.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1OUQEeExzQLRygV-PkpznU9Bj9O0TdAnY
"""
# Commented out IPython magic to ensure Python compatibility.
# %%time
#
# # Important library for many geopython libraries
# !apt install gdal-bin python-gdal python3-gdal
# # Install rtree - Geopandas requirment
# !apt install python3-rtree
# # Install Geopandas
# !pip install git+git://github.com/geopandas/geopandas.git
# # Install descartes - Geopandas requirment
# !pip install descartes
# # Install Folium for Geographic data visualization
# !pip install folium
# # Install plotlyExpress
# !pip install plotly_express
# pip install -U plotly
# pip install git+https://github.com/UBICenter/ubicenter.py
import pandas as pd
import numpy as np
import geopandas as gpd
from shapely.geometry import Point
import folium
import plotly_express as px
from urllib.request import urlopen
import json
from matplotlib.colors import LightSource
from geojson_rewind import rewind
import plotly.io as pio
import ubicenter
##Lower Chamber for CA
assembly = gpd.read_file("cb_2020_us_sldl_500k/cb_2020_us_sldl_500k.shp")
print(assembly.columns)
assembly = assembly[(assembly.STATEFP != "72")]
assembly["st_and_dist"] = assembly["STATEFP"] + assembly["SLDLST"]
##Upper Chamber for CA
senate = gpd.read_file("cb_2020_us_sldu_500k/cb_2020_us_sldu_500k.shp")
senate.columns
senate = senate[(senate.STATEFP != "72") & (senate.STATEFP != "11")]
senate["st_and_dist"] = senate["STATEFP"] + senate["SLDUST"]
assembly_pov = pd.read_csv("pov_all_lower_leg_district.csv")
assembly_pov.rename(
columns={"lower_leg_district": "SLDLST", "st": "STATEFP"}, inplace=True
)
assembly_pov["STATEFP"] = assembly_pov["STATEFP"].astype(int)
assembly_pov["st_and_dist"] = assembly_pov["STATEFP"].astype(str).str.zfill(
2
) + assembly_pov["SLDLST"].astype(str)
assembly["in_both"] = assembly.st_and_dist.isin(assembly_pov.st_and_dist)
assembly[(assembly["in_both"] == False)]
assembly_clean = assembly[(assembly["in_both"] == True)]
senate_pov = pd.read_csv("pov_all_upper_leg_district.csv")
senate_pov.rename(
columns={"upper_leg_district": "SLDUST", "st": "STATEFP"}, inplace=True
)
senate_pov["STATEFP"] = senate_pov["STATEFP"].astype(int)
senate_pov["st_and_dist"] = senate_pov["STATEFP"].astype(str).str.zfill(2) + senate_pov[
"SLDUST"
].astype(str)
senate["in_both"] = senate.st_and_dist.isin(senate_pov.st_and_dist)
senate_clean = senate[(senate.in_both == True)]
# put mapping and poverty rates in same order so they they having matching index numbers
assembly_clean = assembly_clean.sort_values("st_and_dist")
assembly_pov = assembly_pov.sort_values("st_and_dist")
senate_clean = senate_clean.sort_values("st_and_dist")
senate_pov = senate_pov.sort_values("st_and_dist")
# index for mapping
assembly_clean["ind"] = assembly_clean.reset_index().index
assembly_clean["ind"] = assembly_clean["ind"] + 1
assembly_pov["ind"] = assembly_pov.reset_index().index
assembly_pov["ind"] = assembly_pov["ind"] + 1
senate_clean["ind"] = senate_clean.reset_index().index
senate_clean["ind"] = senate_clean["ind"] + 1
senate_pov["ind"] = senate_pov.reset_index().index
senate_pov["ind"] = senate_pov["ind"] + 1
# make GEOJSON file
assembly_clean.to_file("GeoJSON_file", driver="GeoJSON")
with open("GeoJSON_file") as geofile:
j_file_assembly = json.load(geofile)
senate_clean.to_file("GeoJSON_file1", driver="GeoJSON")
with open("GeoJSON_file1") as geofile:
j_file_senate = json.load(geofile)
# id in GEOJSON
i = 1
for feature in j_file_assembly["features"]:
feature["id"] = str(i)
i += 1
i = 1
for feature in j_file_senate["features"]:
feature["id"] = str(i)
i += 1
assembly_pov["pct_change_100"] = (assembly_pov["pct_chg"] * -100).round(1)
assembly_pov["poverty_base_100"] = (assembly_pov["poverty_base"] * 100).round(1)
assembly_pov["poverty_reform_100"] = (assembly_pov["poverty_reform"] * 100).round(1)
assembly_pov["child_pct_change_100"] = (assembly_pov["child_pct_chg"] * -100).round(1)
assembly_pov["child_poverty_base_100"] = (
assembly_pov["child_poverty_base"] * 100
).round(1)
assembly_pov["child_poverty_reform_100"] = (
assembly_pov["child_poverty_reform"] * 100
).round(1)
assembly_pov["assembly_string"] = assembly_pov.SLDLST.astype(str)
senate_pov["pct_change_100"] = (senate_pov["pct_chg"] * -100).round(1)
senate_pov["poverty_base_100"] = (senate_pov["poverty_base"] * 100).round(1)
senate_pov["poverty_reform_100"] = (senate_pov["poverty_reform"] * 100).round(1)
senate_pov["child_pct_change_100"] = (senate_pov["child_pct_chg"] * -100).round(1)
senate_pov["child_poverty_base_100"] = (senate_pov["child_poverty_base"] * 100).round(1)
senate_pov["child_poverty_reform_100"] = (
senate_pov["child_poverty_reform"] * 100
).round(1)
senate_pov["senate_string"] = senate_pov.SLDUST.astype(str)
state_names = pd.read_csv("us_state_fips.csv")
state_names["st"] = state_names[" st"]
assembly_pov.rename(columns={"STATEFP": "st"}, inplace=True)
assembly_pov_names = assembly_pov.merge(state_names, on="st")
senate_pov.rename(columns={"STATEFP": "st"}, inplace=True)
senate_pov_names = senate_pov.merge(state_names, on="st")
title_str_1 = "Change in child poverty from a $100 child allowance"
title_str_2 = "By Lower Legislative District"
title_str_3 = "By Upper Legislative District"
title_str_1_CA = "Change in child poverty from a $100 child allowance in California"
title_str_2_CA = "By Lower Legislative District"
title_str_3_CA = "By Upper Legislative District"
pov_child_assembly_map = px.choropleth(
assembly_pov_names,
color="child_pct_change_100",
geojson=j_file_assembly,
locations="ind",
labels={
"child_pct_change_100": "% change in child poverty",
"child_poverty_base_100": "Base child poverty rate",
"child_poverty_reform_100": "Reform child poverty rate",
"SLDLST": "Assembly District Number",
},
color_continuous_scale="blues_r",
scope="usa",
hover_data={
"child_pct_change_100",
"child_poverty_base_100",
"child_poverty_reform_100",
},
)
pov_child_assembly_map.update_layout(hoverlabel=dict(bgcolor="white", font_size=16,))
pov_child_assembly_map.update_layout(
title_text=title_str_1 + "<br>" + title_str_2, title_x=0.225
)
pov_child_assembly_map.update_traces(
hovertemplate="<b>"
+ assembly_pov_names.stname
+ " "
+ assembly_pov_names.assembly_string
+ "</b>"
+ "<br>"
+ "Change in child poverty: "
+ assembly_pov_names.child_pct_change_100.astype(str)
+ "%"
+ "<br>"
+ "Child poverty base: "
+ assembly_pov_names.child_poverty_base_100.astype(str)
+ "%"
+ "<br>"
+ "Child poverty reform: "
+ assembly_pov_names.child_poverty_reform_100.astype(str)
+ "%"
)
pov_child_assembly_map.update(layout_coloraxis_showscale=False)
pov_child_assembly_map = ubicenter.format_fig(pov_child_assembly_map, show=False)
pov_child_assembly_map.write_html("lower_district.html")
assembly_pov_CA = assembly_pov_names[(assembly_pov_names.st == 6)]
pov_child_assembly_map_CA = px.choropleth(
assembly_pov_CA,
color="child_pct_change_100",
geojson=j_file_assembly,
locations="ind",
labels={
"child_pct_change_100": "% change in child poverty",
"child_poverty_base_100": "Base child poverty rate",
"child_poverty_reform_100": "Reform child poverty rate",
"SLDLST": "Assembly District Number",
},
color_continuous_scale="blues_r",
scope="usa",
hover_data={
"child_pct_change_100",
"child_poverty_base_100",
"child_poverty_reform_100",
},
)
pov_child_assembly_map_CA.update_layout(
hoverlabel=dict(bgcolor="white", font_size=16,),
title_text=title_str_1_CA + "<br>" + title_str_2_CA,
title_x=0.225,
)
pov_child_assembly_map_CA.update_traces(
hovertemplate="<b>"
+ assembly_pov_CA.assembly_string
+ "</b>"
+ "<br>"
+ "Change in child poverty: "
+ assembly_pov_CA.child_pct_change_100.astype(str)
+ "%"
+ "<br>"
+ "Child poverty base: "
+ assembly_pov_CA.child_poverty_base_100.astype(str)
+ "%"
+ "<br>"
+ "Child poverty reform: "
+ assembly_pov_CA.child_poverty_reform_100.astype(str)
+ "%"
)
pov_child_assembly_map_CA.update(layout_coloraxis_showscale=False)
pov_child_assembly_map_CA.update_geos(fitbounds="locations", visible=False)
pov_child_assembly_map_CA = ubicenter.format_fig(pov_child_assembly_map_CA, show=False)
pov_child_assembly_map_CA.write_html("lower_district_CA.html")
pov_child_senate_map = px.choropleth(
senate_pov_names,
color="child_pct_change_100",
geojson=j_file_senate,
locations="ind",
labels={
"child_pct_change_100": "% change in child poverty",
"child_poverty_base_100": "Base child poverty rate",
"child_poverty_reform_100": "Reform child poverty rate",
"SLDUST": "Senate District Number",
},
color_continuous_scale="blues_r",
scope="usa",
hover_data={
"child_pct_change_100",
"child_poverty_base_100",
"child_poverty_reform_100",
},
)
pov_child_senate_map.update_layout(
hoverlabel=dict(bgcolor="white", font_size=16,),
title_text=title_str_1 + "<br>" + title_str_3,
title_x=0.225,
)
pov_child_senate_map.update_traces(
hovertemplate="<b>"
+ senate_pov_names.stname
+ " "
+ senate_pov_names.senate_string
+ "</b>"
+ "<br>"
+ "Change in child poverty: "
+ senate_pov_names.child_pct_change_100.astype(str)
+ "%"
+ "<br>"
+ "Child poverty base: "
+ senate_pov_names.child_poverty_base_100.astype(str)
+ "%"
+ "<br>"
+ "Child poverty reform: "
+ senate_pov_names.child_poverty_reform_100.astype(str)
+ "%"
)
pov_child_senate_map.update(layout_coloraxis_showscale=False)
pov_child_senate_map = ubicenter.format_fig(pov_child_senate_map, show=False)
pov_child_senate_map.write_html("upper_district.html")
senate_pov_CA = senate_pov_names[(senate_pov_names.st == 6)]
pov_child_senate_map_CA = px.choropleth(
senate_pov_CA,
color="child_pct_change_100",
geojson=j_file_senate,
locations="ind",
labels={
"child_pct_change_100": "% change in child poverty",
"child_poverty_base_100": "Base child poverty rate",
"child_poverty_reform_100": "Reform child poverty rate",
"SLDUST": "Senate District Number",
},
color_continuous_scale="blues_r",
scope="usa",
hover_data={
"child_pct_change_100",
"child_poverty_base_100",
"child_poverty_reform_100",
},
)
pov_child_senate_map_CA.update_layout(
hoverlabel=dict(bgcolor="white", font_size=16,),
title_text=title_str_1_CA + "<br>" + title_str_3_CA,
title_x=0.225,
)
pov_child_senate_map_CA.update_traces(
hovertemplate="<b>"
+ senate_pov_CA.stname
+ " "
+ senate_pov_CA.senate_string
+ "</b>"
+ "<br>"
+ "Change in child poverty: "
+ senate_pov_CA.child_pct_change_100.astype(str)
+ "%"
+ "<br>"
+ "Child poverty base: "
+ senate_pov_CA.child_poverty_base_100.astype(str)
+ "%"
+ "<br>"
+ "Child poverty reform: "
+ senate_pov_CA.child_poverty_reform_100.astype(str)
+ "%"
)
pov_child_senate_map_CA.update(layout_coloraxis_showscale=False)
pov_child_senate_map_CA = ubicenter.format_fig(pov_child_senate_map_CA, show=False)
pov_child_senate_map_CA.write_html("upper_district_CA.html")