Search code examples
pythonmapsgeojsongeopandasfolium

TimeSliderChoropleth map with Python Folium won't render geojson polygons individually


I'm trying to render some GeoJSON polygons on a TimeSliderChoropleth map using python Folium.

The work is being done in a Jupyter Notebook.

The code should draw one polygon per timestep on the slider. However, it renders all three polygons for each timestep. The polygons are also not the colors or opacity provided in the styledict.

I was thinking it must be a mistake in the styledict somewhere, however the slider bar registers the correct dates which are given as epochs. I tried several different formats for colors, none of those changed anything. I'm not sure where I'm going wrong with this. Any help would be appreciated.

Minimal reproducible example for a Jupyter Notebook is below.

import folium
import folium.plugins as folium_plugins
from shapely.geometry.polygon import Polygon
import geopandas as gpd


m = folium.Map(location=[33.7490, -84.3880], width=500, height=500, scrollWheelZoom=False, prefer_canvas=True, zoom_start=10)

p1 = Polygon([[-84.688, 33.649], [-84.488, 33.649], [-84.488, 33.449], [-84.688, 33.449], [-84.688, 33.649]])
p2 = Polygon([[-84.488, 33.649], [-84.288, 33.649], [-84.288, 33.449], [-84.488, 33.449], [-84.488, 33.649]])
p3 = Polygon([[-84.288, 33.649], [-84.088, 33.649], [-84.088, 33.449], [-84.288, 33.449], [-84.288, 33.649]])

df_poly = gpd.GeoDataFrame(data={'geometry' : [p1, p2, p3]})
df_poly.crs = 'EPSG:4326'

sd = {
    '0' : {'1617247390': {'color': '#FFEBEB', 'opacity': 0.5}},
    '1' : {'1618247390': {'color': '#0073CF', 'opacity': 0.5}},
    '2' : {'1619247390': {'color': '#FFEBEB', 'opacity': 0.5}}
}

folium.plugins.TimeSliderChoropleth(data=df_poly.to_json(), styledict=sd).add_to(m)

m

Solution

  • It seems prefer_canvas=True is the cause of the problem. Removing that made it work.

    prefer_canvas=True was suggested in some of the documentation as a speed up for maps that have a lot of objects placed in the map. Since this project does, I was attempting to use it. But, it doesn't seem to work with the TimeSliderChoropleth class.