'''
Choropleth maps of US counties.

GeoJSON via the US Census and Eric Celeste:

http://eric.clst.org/Stuff/USGeoJSON

'''

import folium
import pandas as pd

county_data = r'us_county_data.csv'
county_geo = r'us_counties_20m_topo.json'

# Read into Dataframe, cast to string for consistency.
df = pd.read_csv(county_data, na_values=[' '])
df['FIPS_Code'] = df['FIPS_Code'].astype(str)


def set_id(fips):
    '''Modify FIPS code to match GeoJSON property'''
    if fips == '0':
        return None
    elif len(fips) <= 4:
        return ''.join(['0500000US0', fips])
    else:
        return ''.join(['0500000US', fips])

# Apply set_id, drop NaN.
df['GEO_ID'] = df['FIPS_Code'].apply(set_id)
df.dropna(subset=['GEO_ID'], inplace=True)

# Number of employed with auto scale.
map_1 = folium.Map(location=[48, -102], zoom_start=3)
map_1.geo_json(geo_path=county_geo, data_out='data1.json', data=df,
               columns=['GEO_ID', 'Employed_2011'],
               key_on='feature.id',
               fill_color='YlOrRd', fill_opacity=0.7, line_opacity=0.3,
               topojson='objects.us_counties_20m')
map_1.save(outfile='map_1.html')

# Unemployment with custom defined scale.
map_2 = folium.Map(location=[40, -99], zoom_start=4)
map_2.geo_json(geo_path=county_geo, data_out='data2.json', data=df,
               columns=['GEO_ID', 'Unemployment_rate_2011'],
               key_on='feature.id',
               threshold_scale=[0, 5, 7, 9, 11, 13],
               fill_color='YlGnBu', line_opacity=0.3,
               legend_name='Unemployment Rate 2011 (%)',
               topojson='objects.us_counties_20m')
map_2.save(outfile='map_2.html')

# Median Household income.
map_3 = folium.Map(location=[40, -99], zoom_start=4)
map_3.geo_json(geo_path=county_geo, data_out='data3.json', data=df,
               columns=['GEO_ID', 'Median_Household_Income_2011'],
               key_on='feature.id',
               fill_color='PuRd', line_opacity=0.3,
               legend_name='Median Household Income 2011 ($)',
               topojson='objects.us_counties_20m')
map_3.save(outfile='map_3.html')

@@ -831,24 +831,26 @@
 )%0Amap_1.
-geo_json
+choropleth
 (geo_pat
@@ -902,32 +902,34 @@
 %0A               
+  
 columns=%5B'GEO_ID
@@ -941,32 +941,34 @@
 mployed_2011'%5D,%0A
+  
                k
@@ -994,32 +994,34 @@
 %0A               
+  
 fill_color='YlOr
@@ -1053,32 +1053,34 @@
 ne_opacity=0.3,%0A
+  
                t
@@ -1249,24 +1249,26 @@
 )%0Amap_2.
-geo_json
+choropleth
 (geo_pat
@@ -1320,32 +1320,34 @@
 %0A               
+  
 columns=%5B'GEO_ID
@@ -1383,32 +1383,34 @@
 %0A               
+  
 key_on='feature.
@@ -1429,16 +1429,18 @@
         
+ 
+ 
 threshol
@@ -1469,16 +1469,18 @@
 1, 13%5D,%0A
+  
         
@@ -1532,32 +1532,34 @@
 %0A               
+ 
+ 
 legend_name='Une
@@ -1580,24 +1580,26 @@
  2011 (%25)',%0A
+  
             
@@ -1761,16 +1761,18 @@
 p_3.
-geo_json
+choropleth
 (geo
@@ -1828,32 +1828,34 @@
 %0A               
+ 
+ 
 columns=%5B'GEO_ID
@@ -1897,32 +1897,34 @@
 %0A               
+  
 key_on='feature.
@@ -1920,32 +1920,34 @@
 n='feature.id',%0A
+  
                f
@@ -1989,32 +1989,34 @@
 %0A               
+  
 legend_name='Med
@@ -2043,24 +2043,26 @@
  2011 ($)',%0A
+  
             

Fix PEP8 formatting