-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Push prelimnary version of map + test CSS
- Loading branch information
Showing
6 changed files
with
231 additions
and
53 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Oops, something went wrong.