Skip to content

How to efficiently display a map with CircleMarker() beyond 1000 rows #1010

@GitAnalyst

Description

@GitAnalyst
import pandas as pd
import folium
from folium.plugins import FastMarkerCluster

file_url = 'http://www2.census.gov/geo/docs/maps-data/data/gazetteer/2016_Gazetteer/2016_Gaz_zcta_national.zip'
#Pandas usually infers zips are numerics, but we lose our leading zeroes so let's go with the object dtype
df = pd.read_csv(file_url, sep='\t', dtype={'GEOID' : object}) 
df.columns = df.columns.str.strip() #some column names have some padding

df = df.sample(1000)

folium_map = folium.Map(location=[38, -97],
                        zoom_start=4.4,
                        tiles='CartoDB dark_matter')

# These two lines should create FastMarkerClusters
FastMarkerCluster(data=list(zip(df['INTPTLAT'].values, df['INTPTLONG'].values))).add_to(folium_map)
folium.LayerControl().add_to(folium_map)

for index, row in df.iterrows():

    # generate the popup message that is shown on click.
    popup_text = "{}<br> ALAND: {:,}<br> AWATER: {:,}"
    popup_text = popup_text.format(
                      index,
                      row["ALAND_SQMI"],
                      row["AWATER_SQMI"]
                      )

    folium.CircleMarker(location=(row["INTPTLAT"],
                                  row["INTPTLONG"]),
                        radius= row['ALAND_SQMI']/100,
                        color="#007849",
                        popup=popup_text,
                        fill=False).add_to(folium_map)
folium_map

fastmarkercluster

Problem description

I use CircleMarker() to describe my data by varying radius and color variables (I presented here a more primitive example where only radius varies). The problem is that the map with CircleMarker() doesn't show up (in Jupyter Notebook) when data has more than 1000 lines. I can save maps with more data points to an HTML file, but it is SLOW to navigate.

Expected Output

So to solve this I wanted to use marker clusters. I tried both MarkerCluster() and FastMarkerCluster(). My understanding is that MarkerCluster() provides with enough flexibility, but for me the plot doesn't show up when there are more rows in data than 1000. In the example here I tried to use FastMarkerCluster(). But how to use it so that my data points would retain all the features (radius, color, popup) and would be hidden until you zoom in? And I assume this should solve the issue of not displaying the map with larger data sets.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions