1

I would like to clusterize my points. Unfortunately I cannot do it with my code it loos like.

 import pandas as pd
 import folium
 from folium import FeatureGroup
 from folium.plugins import MarkerCluster

 map = folium.Map(location=[51.86762,0.8624], tiles='OpenStreetMap', zoom_start=8)

 mCluster = MarkerCluster(control = False).add_to(map)

 fc=folium.FeatureGroup(name="CF",overlay=True)
 df = pd.read_csv("cf_geo.csv")
 for i,row in df.iterrows():
 lat =df.at[i, 'lat']
 lng = df.at[i, 'lng']
 sp = df.at[i, 'sp']

 popup = df.at[i,'sp'] +'<br>' + str(df.at[i, 'street']) + '<br>' + str(df.at[i, 'post code'])
 fc.add_child(folium.Marker(location=[lat,lng], popup=popup, icon = 
 folium.Icon(color='green')))
 mCluster.add_child(fc)

All my items are gone.

I tried these hints:

https://splunktool.com/how-to-add-a-marker-manually-or-with-draw-plugin-folium-python-and-be-able-to-export-it-or-save-it

and other, general sources show rather the most simple way:

https://www.youtube.com/watch?v=n7HUBNmXB5Y&ab_channel=LearningSoftwareSkills

How to add Cluster markers to Choropleth with Folium

How can I make the makrercluster working for the child elements of the folium library?

1 Answer 1

1

The right track for this problem is here:

https://stackoverflow.com/questions/72222835/folium-put-markers-in-marker-clusters-and-in-layers-based-on-a-value

In turn, my code should look like this:

 df = pd.read_csv("cf_geo.csv")
 fc=folium.FeatureGroup(name="CF",overlay=True)  
 cf_cluster = MarkerCluster(name="CF").add_to(map)  
 for i,row in df.iterrows():
 lat =df.at[i, 'lat']
 lng = df.at[i, 'lng']
 sp = df.at[i, 'sp']

 popup = df.at[i,'sp'] +'<br>' + str(df.at[i, 'street']) + '<br>' + str(df.at[i, 
 'post code'])
 cf_marker = folium.Marker(location=[lat,lng], popup=popup, icon = 
 folium.Icon(color='green'))
 cf_cluster.add_child(cf_marker)

where we need to do 3 steps:

  1. Define your own cluster for the particular layer instead of the general definition in my previous code above.

    cf_cluster = MarkerCluster(name="CF").add_to(map)  
    
  2. Define the individual variable for the marker, representing just items from the particular layer, unlike classifying this marker as the child to append to the main map.

     cf_marker = folium.Marker(location=[lat,lng], popup=popup, icon = 
    

    folium.Icon(color='green'))

  3. Append this marker as the child of the cluster defined earlier.

      cf_cluster.add_child(cf_marker)
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.