From bb9d19c44a8e73f50eb2531c7438dab0d715d060 Mon Sep 17 00:00:00 2001
From: suobset
diff --git a/leaflet.ipynb b/leaflet.ipynb new file mode 100644 index 0000000..e69de29 diff --git a/leaflet.py b/leaflet.py index 83326ef..dfbee73 100644 --- a/leaflet.py +++ b/leaflet.py @@ -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") \ No newline at end of file +# Save the map as an HTML file +m.save('./map.html') diff --git a/map.html b/map.html new file mode 100644 index 0000000..8e8a1a8 --- /dev/null +++ b/map.html @@ -0,0 +1,178 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + +
+ +