cassiebuhler commited on
Commit
6284181
1 Parent(s): a47ab28

adding SVI layer

Browse files
Files changed (1) hide show
  1. app.py +70 -13
app.py CHANGED
@@ -78,7 +78,7 @@ party = (con
78
 
79
 
80
  # generate altair chart with df
81
- def create_chart(df, y_column, title, color):
82
  # color encoding - color is a list or single value
83
  color_encoding = (
84
  alt.Color('party:N', scale=alt.Scale(domain=["DEMOCRAT", "REPUBLICAN"], range=color))
@@ -87,7 +87,7 @@ def create_chart(df, y_column, title, color):
87
 
88
  return alt.Chart(df).mark_line(strokeWidth=3).encode(
89
  x=alt.X('year:N', title='Year'),
90
- y=alt.Y(f'{y_column}:Q', title=title),
91
  color=color_encoding
92
  ).properties(
93
  title=title
@@ -158,17 +158,17 @@ paint_extrusion = {
158
  def get_style_status(jurisdiction):
159
  if jurisdiction == "State":
160
  name = "state"
161
- label = "states"
162
  paint_type = paint_fill
163
  layer_type = "fill"
164
  elif jurisdiction == "County":
165
  name = "county"
166
- label = "counties"
167
  paint_type = paint_extrusion
168
  layer_type = "fill-extrusion"
169
  else: # Municipal
170
  name = "municipal"
171
- label = "cities"
172
  paint_type = paint_extrusion
173
  layer_type = "fill-extrusion"
174
 
@@ -190,13 +190,13 @@ def get_style_status(jurisdiction):
190
  def get_style_party(jurisdiction):
191
  if jurisdiction == "State":
192
  name = "state"
193
- label = "states"
194
  elif jurisdiction == "County":
195
  name = "county"
196
- label = "counties"
197
  else: # Municipal
198
  name = "municipal"
199
- label = "cities"
200
 
201
  # Return style dictionary for political party
202
  return {
@@ -224,27 +224,84 @@ def get_style_party(jurisdiction):
224
  }
225
 
226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
  m = leafmap.Map(style="positron", center=(-100, 40), zoom=3)
 
228
  color_choice = st.radio("Color by:", ["Measure Status", "Political Party"])
 
 
 
 
 
 
 
 
229
 
230
  if color_choice == "Measure Status":
231
- m.add_pmtiles(votes_pmtiles, style=get_style_status("State"), visible=True, opacity=0.4, tooltip=True)
232
  m.add_pmtiles(votes_pmtiles, style=get_style_status("County"), visible=True, opacity=1.0, tooltip=True)
233
  m.add_pmtiles(votes_pmtiles, style=get_style_status("Municipal"), visible=True, opacity=1.0, tooltip=True)
234
 
235
  elif color_choice == "Political Party":
236
- m.add_pmtiles(votes_pmtiles, style=get_style_party("State"), visible=True, opacity=0.6, tooltip=True)
237
  m.add_pmtiles(votes_pmtiles, style=get_style_party("County"), visible=True, opacity=1.0, tooltip=True)
238
  m.add_pmtiles(votes_pmtiles, style=get_style_party("Municipal"), visible=True, opacity=1.0, tooltip=True)
239
 
240
- #
 
241
  m.add_layer_control()
242
  m.to_streamlit()
243
 
244
 
245
  # display charts
246
  df_passes = get_passes(party)
247
- st.altair_chart(create_chart(df_passes, "percent_passed", "% of Measures Passed", [COLORS["dem_blue"], COLORS["rep_red"]]), use_container_width=True)
248
 
249
  df_funding = funding_chart(party)
250
- st.altair_chart(create_chart(df_funding, "cumulative_funding", "Cumulative Funding (Billions of Dollars)", COLORS["dark_green"]))
 
 
78
 
79
 
80
  # generate altair chart with df
81
+ def create_chart(df, y_column, ylab, title, color):
82
  # color encoding - color is a list or single value
83
  color_encoding = (
84
  alt.Color('party:N', scale=alt.Scale(domain=["DEMOCRAT", "REPUBLICAN"], range=color))
 
87
 
88
  return alt.Chart(df).mark_line(strokeWidth=3).encode(
89
  x=alt.X('year:N', title='Year'),
90
+ y=alt.Y(f'{y_column}:Q', title=ylab),
91
  color=color_encoding
92
  ).properties(
93
  title=title
 
158
  def get_style_status(jurisdiction):
159
  if jurisdiction == "State":
160
  name = "state"
161
+ label = "States"
162
  paint_type = paint_fill
163
  layer_type = "fill"
164
  elif jurisdiction == "County":
165
  name = "county"
166
+ label = "Counties"
167
  paint_type = paint_extrusion
168
  layer_type = "fill-extrusion"
169
  else: # Municipal
170
  name = "municipal"
171
+ label = "Cities"
172
  paint_type = paint_extrusion
173
  layer_type = "fill-extrusion"
174
 
 
190
  def get_style_party(jurisdiction):
191
  if jurisdiction == "State":
192
  name = "state"
193
+ label = "States"
194
  elif jurisdiction == "County":
195
  name = "county"
196
+ label = "Counties"
197
  else: # Municipal
198
  name = "municipal"
199
+ label = "Cities"
200
 
201
  # Return style dictionary for political party
202
  return {
 
224
  }
225
 
226
 
227
+ sv_pmtiles = "https://data.source.coop/cboettig/social-vulnerability/svi2020_us_county.pmtiles"
228
+ sv_style = {
229
+ "layers": [
230
+ {
231
+ "id": "SVI",
232
+ "source": "Social Vulnerability Index",
233
+ "source-layer": "SVI2020_US_county",
234
+ "type": "fill",
235
+ "paint": {
236
+ "fill-color":
237
+ ["interpolate", ["linear"], ["get", "RPL_THEMES"],
238
+ 0, "#FFE6EE",
239
+ 1, "#850101"]
240
+
241
+ }
242
+ }
243
+ ]
244
+ }
245
+
246
+ party_pmtiles = "https://huggingface.co/datasets/boettiger-lab/landvote/resolve/main/party_polygons.pmtiles"
247
+
248
+ recent_election_year = year - year%4
249
+ party_style = {
250
+ "layers": [
251
+ {
252
+ "id": "Party",
253
+ "source": "Political Parties",
254
+ "source-layer": "county",
255
+ "type": "fill",
256
+ "filter": [
257
+ "==", ["get", "year"], recent_election_year
258
+ ],
259
+ "paint": {
260
+ "fill-color": {
261
+ "property": "party",
262
+ "type": "categorical",
263
+ "stops": [
264
+ ["DEMOCRAT", COLORS["dem_blue"]],
265
+ ["REPUBLICAN", COLORS["rep_red"]]
266
+ ]
267
+ }
268
+ }
269
+ }
270
+ ]
271
+ }
272
+
273
  m = leafmap.Map(style="positron", center=(-100, 40), zoom=3)
274
+
275
  color_choice = st.radio("Color by:", ["Measure Status", "Political Party"])
276
+ social_toggle = st.toggle("Social Vulnerability Index")
277
+ party_toggle = st.toggle("Political Parties")
278
+
279
+ if social_toggle:
280
+ m.add_pmtiles(sv_pmtiles, style = sv_style ,visible=True, opacity=0.3, tooltip=True)
281
+
282
+ if party_toggle:
283
+ m.add_pmtiles(party_pmtiles, style = party_style ,visible=True, opacity=0.3, tooltip=True)
284
 
285
  if color_choice == "Measure Status":
286
+ m.add_pmtiles(votes_pmtiles, style=get_style_status("State"), visible=True, opacity=0.8, tooltip=True)
287
  m.add_pmtiles(votes_pmtiles, style=get_style_status("County"), visible=True, opacity=1.0, tooltip=True)
288
  m.add_pmtiles(votes_pmtiles, style=get_style_status("Municipal"), visible=True, opacity=1.0, tooltip=True)
289
 
290
  elif color_choice == "Political Party":
291
+ m.add_pmtiles(votes_pmtiles, style=get_style_party("State"), visible=True, opacity=0.8, tooltip=True)
292
  m.add_pmtiles(votes_pmtiles, style=get_style_party("County"), visible=True, opacity=1.0, tooltip=True)
293
  m.add_pmtiles(votes_pmtiles, style=get_style_party("Municipal"), visible=True, opacity=1.0, tooltip=True)
294
 
295
+
296
+
297
  m.add_layer_control()
298
  m.to_streamlit()
299
 
300
 
301
  # display charts
302
  df_passes = get_passes(party)
303
+ st.altair_chart(create_chart(df_passes, "percent_passed", "Percent Passed","% of Measures Passed", [COLORS["dem_blue"], COLORS["rep_red"]]), use_container_width=True)
304
 
305
  df_funding = funding_chart(party)
306
+ st.altair_chart(create_chart(df_funding, "cumulative_funding", "Billions of Dollars", "Cumulative Funding", COLORS["dark_green"]), use_container_width=True)
307
+