diff --git a/enhanced_data_center_cluster_map.ipynb b/enhanced_data_center_cluster_map.ipynb
index 829fea8..134d1b9 100644
--- a/enhanced_data_center_cluster_map.ipynb
+++ b/enhanced_data_center_cluster_map.ipynb
@@ -94,6 +94,8 @@
"SHOW_CLIMATE_LAYER = True\n",
"SHOW_BROADBAND_LAYER = True\n",
"SHOW_ELECTION_LAYER = True\n",
+ "SHOW_ELECTION_2020_LAYER = True\n",
+ "SHOW_ELECTION_2024_LAYER = False\n",
"\n",
"OUTPUT_DIR.mkdir(exist_ok=True)\n",
"print('points:', POINTS_CSV)\n",
@@ -356,39 +358,42 @@
" )\n",
" select\n",
" master_id, election_state_code, election_join_method, election_match_distance_m,\n",
- " feature_id, layer_id, election_latitude, election_longitude,\n",
+ " feature_id, layer_id as election_layer_id, election_latitude, election_longitude, properties,\n",
" coalesce((properties->>'LOCALITY'), '') as election_locality,\n",
" coalesce((properties->>'PRECINCT'), '') as election_precinct,\n",
- " nullif(properties->>'G20PREDBID','')::double precision as election_biden_votes,\n",
- " nullif(properties->>'G20PRERTRU','')::double precision as election_trump_votes,\n",
- " case\n",
- " when (coalesce(nullif(properties->>'G20PREDBID','')::double precision,0)\n",
- " + coalesce(nullif(properties->>'G20PRERTRU','')::double precision,0)) > 0\n",
- " then 100.0 * coalesce(nullif(properties->>'G20PREDBID','')::double precision,0)\n",
- " / (coalesce(nullif(properties->>'G20PREDBID','')::double precision,0)\n",
- " + coalesce(nullif(properties->>'G20PRERTRU','')::double precision,0))\n",
- " end as election_biden_share_pct,\n",
- " case\n",
- " when (coalesce(nullif(properties->>'G20PREDBID','')::double precision,0)\n",
- " + coalesce(nullif(properties->>'G20PRERTRU','')::double precision,0)) > 0\n",
- " then 100.0 * coalesce(nullif(properties->>'G20PRERTRU','')::double precision,0)\n",
- " / (coalesce(nullif(properties->>'G20PREDBID','')::double precision,0)\n",
- " + coalesce(nullif(properties->>'G20PRERTRU','')::double precision,0))\n",
- " end as election_trump_share_pct\n",
+ " nullif(properties->>'G20PREDBID','')::double precision as election_2020_dem_votes,\n",
+ " nullif(properties->>'G20PRERTRU','')::double precision as election_2020_rep_votes\n",
" from best_match\n",
" \"\"\"\n",
" election_context = pd.read_sql(election_sql, conn)\n",
" if not election_context.empty:\n",
- " election_context['election_trump_margin_pct'] = (\n",
- " election_context['election_trump_share_pct'] - election_context['election_biden_share_pct']\n",
+ " election_context['election_2020_total_votes'] = (\n",
+ " election_context['election_2020_dem_votes'].fillna(0) + election_context['election_2020_rep_votes'].fillna(0)\n",
" )\n",
+ " election_context.loc[election_context['election_2020_total_votes'].eq(0), 'election_2020_total_votes'] = pd.NA\n",
+ "\n",
+ "\n",
+ " election_context['election_2020_dem_share_pct'] = 100.0 * election_context['election_2020_dem_votes'] / election_context['election_2020_total_votes']\n",
+ " election_context['election_2020_rep_share_pct'] = 100.0 * election_context['election_2020_rep_votes'] / election_context['election_2020_total_votes']\n",
+ " election_context['election_2020_rep_margin_pct'] = (\n",
+ " election_context['election_2020_rep_share_pct'] - election_context['election_2020_dem_share_pct']\n",
+ " )\n",
+ "\n",
+ "\n",
+ " election_context['election_biden_votes'] = election_context['election_2020_dem_votes']\n",
+ " election_context['election_trump_votes'] = election_context['election_2020_rep_votes']\n",
+ " election_context['election_biden_share_pct'] = election_context['election_2020_dem_share_pct']\n",
+ " election_context['election_trump_share_pct'] = election_context['election_2020_rep_share_pct']\n",
+ " election_context['election_trump_margin_pct'] = election_context['election_2020_rep_margin_pct']\n",
+ " election_context = election_context.drop(columns=['properties'])\n",
+ "\n",
" print(f'election_context rows: {len(election_context):,}')\n",
" if not election_context.empty:\n",
" cols = [c for c in election_context.columns if c != 'master_id']\n",
" points = points.merge(election_context[['master_id'] + cols], on='master_id', how='left')\n",
"\n",
"\n",
- "load_optional_db_layers()"
+ "load_optional_db_layers()\n"
]
},
{
@@ -615,17 +620,27 @@
" '''\n",
"\n",
" election_lines = ''\n",
- " if hasattr(row, 'election_biden_share_pct') and pd.notna(row.election_biden_share_pct):\n",
+ " has_2020 = hasattr(row, 'election_2020_dem_share_pct') and pd.notna(row.election_2020_dem_share_pct)\n",
+ " if has_2020:\n",
+ " election_2020_lines = ''\n",
+ " if has_2020:\n",
+ " election_2020_lines = f'''\n",
+ " 2020
\n",
+ " Dem share: {fmt_number(row.election_2020_dem_share_pct, 1, suffix='%')}
\n",
+ " Rep share: {fmt_number(row.election_2020_rep_share_pct, 1, suffix='%')}
\n",
+ " Rep margin: {fmt_number(row.election_2020_rep_margin_pct, 1, suffix=' pp')}
\n",
+ " Dem / Rep votes: {fmt_number(row.election_2020_dem_votes)} / {fmt_number(row.election_2020_rep_votes)}
\n",
+ " Two-party votes: {fmt_number(row.election_2020_total_votes)}
\n",
+ " '''\n",
" election_lines = f'''\n",
"