Skip to content

Commit

Permalink
Push prelimnary version of map + test CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
suobset committed Nov 20, 2023
1 parent 33f9ebd commit bb9d19c
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 53 deletions.
Empty file.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<div id="leaflet-map" alt="leaflet-map">
<!-- placeholder img for design purposes-->
<!-- <img src="images/map.png"> -->
<iframe src="./map.html"></iframe>
</div>
<script src="main.js"></script>
</body>
Expand Down
Empty file added leaflet.ipynb
Empty file.
75 changes: 22 additions & 53 deletions leaflet.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,35 @@
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 19 18:59:36 2023
@author: kushd
"""

import pandas as pd
import folium
from geopy.geocoders import Nominatim
from geopy.exc import GeocoderTimedOut
from folium import plugins
from folium.plugins import HeatMap
import geopandas as gpd
from shapely.geometry import Point

data1 = pd.read_csv("./data/counties-age.csv")

def geocode_address(address):
geolocator = Nominatim(user_agent="my_geocoder")
try:
location = geolocator.geocode(address)
if location:
return location.latitude, location.longitude
else:
return None
except GeocoderTimedOut:
return geocode_address(address)

data1["Location"] = data1["NAMELSAD"] + ", " + data1["STUSPS"]
data1["Latitude"], data1["Longitude"] = zip(*data1["Location"].apply(geocode_address).dropna())

# Geocode the county names to get coordinates
data1["geometry"] = data1["Location"].apply(geocode_address).apply(Point)
gdf = gpd.GeoDataFrame(data1, geometry="geometry")

# Download U.S. counties shapefile from the Census Bureau
counties_shapefile_url = "https://www2.census.gov/geo/tiger/GENZ2021/shp/cb_2021_us_county_20m.zip"
counties_gdf = gpd.read_file(counties_shapefile_url)
import pandas as pd
from urllib.request import urlopen
import json

# Merge the county geometries with your data based on county names
merged_gdf = pd.merge(counties_gdf, gdf, left_on="NAME", right_on="Location", how="inner")
# Load GeoJSON data
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
counties = json.load(response)

# Save the GeoJSON file
merged_gdf.to_file("us-counties.json", driver="GeoJSON")
# Load unemployment data
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
dtype={"fips": str})

m = folium.Map(location=[center_lat, center_lon], zoom_start=6)
# Create a Folium map centered at a location of your choice (e.g., USA center)
map_center = [37.0902, -95.7129]
m = folium.Map(location=map_center, zoom_start=4)

# Choropleth map
# Add choropleth layer to the map with blue to red color scale
folium.Choropleth(
geo_data='us-counties.json', # Replace with the path to a US counties GeoJSON file
geo_data=counties,
name='choropleth',
data=data1,
columns=['Location', 'Population Age 65 and Older'],
key_on='feature.properties.NAME', # Use the county name as the key
fill_color='YlGnBu',
data=df,
columns=['fips', 'unemp'],
key_on='feature.id',
fill_color='RdBu',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Population Age 65 and Older',
legend_name='Unemployment Rate (%)'
).add_to(m)

# Display the map
# Add layer control to toggle visibility
folium.LayerControl().add_to(m)

# Save the map
m.save("choropleth_map.html")
# Save the map as an HTML file
m.save('./map.html')
Loading

0 comments on commit bb9d19c

Please sign in to comment.