#!/usr/bin/env python # coding: utf-8 # In[1]: import geopandas as gpd, folium, requests, pandas as pd, fiona, branca, json, numpy as np # In[2]: income = pd.read_csv("https://raw.githubusercontent.com/pri-data/50-states/master/data/income-counties-states-national.csv") income.columns # In[3]: gdf = gpd.read_file("https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json") statesabbr=pd.read_json("https://gist.githubusercontent.com/mshafrir/2646763/raw/8b0dbb93521f5d6889502305335104218454c2bf/states_titlecase.json") income = pd.read_csv("https://raw.githubusercontent.com/pri-data/50-states/master/data/income-counties-states-national.csv") income['income-2015']=pd.to_numeric(income['income-2015'], errors='coerce') income=income.groupby(by=['state'], as_index=False).median() income['State']=pd.merge(income, statesabbr, left_on='state', right_on='abbreviation')['name'] income.head() # income.dropna(subset=['income-2015'])[['state','income-2015']].groupby(by='state') # In[4]: gdf1=gdf.merge(income, left_on='name', right_on='State', how='left').fillna(0) # In[5]: gdf1.crs = fiona.crs.from_epsg(4326) gdf1.head() # In[6]: gdf1=gdf1[gdf1['name']!='District of Columbia'] # In[7]: roads = gpd.read_file(r"/Users/jasonbaker/Downloads/tl_2016_us_primaryroads/tl_2016_us_primaryroads.shp") roads = roads.to_crs(gdf1.crs) interstates = roads.loc[roads['RTTYP']=='I'] interstates.loc[:,'geometry']=interstates.geometry.simplify(tolerance=0.01, preserve_topology=False) interstates = interstates.dissolve(by='FULLNAME', aggfunc='first', as_index=False) interstates.loc[:,'FULLNAME'] = interstates['FULLNAME'].str.replace('- ','-') interstates.head() # In[8]: variable = 'income-2015' gdf1=gdf1.sort_values(by=variable, ascending=True) colormap = folium.LinearColormap(colors=["red","orange","yellow","green"],vmin=gdf1.loc[gdf1[variable]>0, variable].min(), vmax=gdf1.loc[gdf1[variable]>0, variable].max()).to_step(n=5) colormap.caption = "2015 US Median Household Income" # In[9]: centroid=gdf1.geometry.centroid m=folium.Map(location=[centroid.y.mean(), centroid.x.mean()], zoom_start=4, tiles='stamentoner') folium.GeoJson(gdf1[['geometry','name',variable]], name="United States of America", style_function=lambda x: {"weight":2, 'color':'black','fillColor':colormap(x['properties'][variable]), 'fillOpacity':0.2}, highlight_function=lambda x: {'weight':3, 'color':'black'}, smooth_factor=2.0, tooltip=folium.features.Tooltip(fields=['name',variable,], aliases=['State','2015 Median Income'], labels=True, sticky=True, toLocaleString=True ) ).add_to(m) folium.GeoJson(interstates, name='US Interstate Highways', style_function=lambda x: {'weight':2,'color':'#5BDAE9','dashArray':'1 3'}, highlight_function=lambda x: {'weight':5,'color':'yellow', 'dashArray':'1 7'}, tooltip=folium.features.Tooltip(fields=['FULLNAME',],labels=False, toLocaleString=True), smooth_factor=5.0, ).add_to(m) colormap.add_to(m) folium.LayerControl(autoZIndex=False, collapsed=False).add_to(m) m # In[ ]: # In[ ]: