Folium reference: https://python-visualization.github.io/folium/quickstart.html#Getting-Started
import folium
import geopandas as gpd
import pandas as pd
import os
os.chdir('../../')
Type:
>>> tahoeTracts = gpd.read_file('TahoeTracts_geodata.geojson')
tahoeTracts = gpd.read_file('TahoeTracts_geodata.geojson')
Type:
>>> map = folium.Map()
>>> map
map = folium.Map()
map
m = folium.Map(
location = [39.025, -120.032349],
tiles = 'Stamen Terrain',
zoom_start = 9)
m
m = folium.Map(location = [39.025, -120.032349], tiles = 'Stamen Terrain', zoom_start = 10)
folium.GeoJson(tahoeTracts).add_to(m)
m
Paste the above code into the following cell and make the following modifications:
>>> m = folium.Map(location = [39.025, -120.032349], tiles = 'Stamen Terrain', zoom_start = 10 )
>>> folium.GeoJson(tahoeTracts, style_function = lambda feature: {'fillColor': '#00ff00'}).add_to(m)
>>> m
m = folium.Map(location = [39.025, -120.032349], tiles = 'Stamen Terrain', zoom_start = 10 )
folium.GeoJson(tahoeTracts, style_function = lambda features: {'fillColor': '#00ff00'}).add_to(m)
m
To make this easier to read, you can add returns to those long, in-line code snippets, like so:
Type this out manually in the cell below:
>>>
m = folium.Map(
location = [39.025, -120.032349],
tiles = 'Stamen Terrain',
zoom_start = 10
)
>>>
folium.GeoJson(tahoeTracts,
style_function = lambda feature: {'fillColor': '#00ff00'}
).add_to(m)
>>> m
m = folium.Map(location = [39.025, -120.032349],
tiles = 'Stamen Terrain',
zoom_start = 10 )
folium.GeoJson(tahoeTracts,
style_function = lambda features: {'fillColor': '#00ff00'}
).add_to(m)
m
You can also move the style function to a variable, which will make make it easier to read and further customize.
Type:
>>>
style = lambda feature: {'fillColor': '#00ff00',
'color': 'Green',
'weight':2}
>>>
m = folium.Map(
location = [39.025, -120.032349],
tiles = 'Stamen Terrain',
zoom_start = 10
)
>>>
folium.GeoJson(tahoeTracts,
style_function = style
).add_to(m)
>>>m
style = lambda features: {'fillColor': '#00ff00',
'color':'Green', 'weight':2}
m = folium.Map(location = [39.025, -120.032349],
tiles = 'Stamen Terrain',
zoom_start = 10 )
folium.GeoJson(tahoeTracts,
style_function = style
).add_to(m)
m
This method takes a Pandas dataframe and joins it to a GeoJson on the fly to generate a choropleth.
If your GeoJson or shapefile layer already have attributes baked in that you want to display, you can use a more complex alternative method here: https://nbviewer.jupyter.org/github/python-visualization/folium/blob/master/examples/plugin-Search.ipynb
We do have attributes baked in, but I want to use the easier folium.choropleth() method.
Create a pandas dataframe from the tahoeTracts geodataframe:
Type:
>>> tractdata = pd.DataFrame(tahoeTracts)
tractdata = pd.DataFrame(tahoeTracts)
Type the following:
>>>
m = folium.Map(
location = [39.025, -120.32349],
tiles = 'Stamen Terrain',
zoom_start = 10
)
>>>
folium.Choropleth(
geo_data = tahoeTracts,
data = tractdata,
columns = ['GEOID', 'Median Household Income (In 2017 Inflation Adjusted Dollars)'],
key_on = 'feature.properties.GEOID',
fill_color = 'RdYlGn',
fill_opacity = 0.7,
line_opacity = 0.5,
).add_to(m)
>>> m
m = folium.Map(location = [39.025, -120.032349],
tiles = 'Stamen Terrain',
zoom_start = 10)
folium.Choropleth(geo_data = tahoeTracts,
data = tractdata,
columns = ['GEOID', 'Median Household Income (In 2017 Inflation Adjusted Dollars)'],
key_on = 'feature.properties.GEOID',
fill_color = 'RdYlGn',
fill_opacity = 0.7,
line_opacity = 0.5,
).add_to(m)
m
m.save('my_map.html')