folium icon indicating copy to clipboard operation
folium copied to clipboard

Folium marker clusters not working as expected

Open Schweboo opened this issue 9 years ago • 13 comments

img_0028 The attached file shows a map on which marker clusters were expected. However, only a few of the markers are appearing as marker clusters, even though all points were created using the same .add_to (marker_cluster) function

Some of the markers have a black outline...which I think means that there are several markers on or around that location. I do not understand why they are not showing as marker_clusters. I would like all markers to be seen initially through markerclusters. This is my code. The problem is in the way I pass latitude and longitude to Folium. If I copy the 'folium.Marker' code replace [latArr[0], lonArr[0] with numbers e.g 54.54, the markerclusters appear as seen above . What am I doing wrong?

marker_cluster = folium.MarkerCluster().add_to(tweet_map)

try:
    latArr = np.array([0],np.float)
    lonArr = np.array([0],np.float)
    latArr[0] = float(latitude)
    lonArr[0] = float(longitude)
    latArr = latArr.round(decimals=5)
    lonArr = lonArr.round(decimals=5)
except:
    print "NaN error?"


folium.Marker([latArr[0], lonArr[0]],
    popup=' ',
    icon=folium.Icon(color=color_code, icon='ok-sign'),
).add_to(marker_cluster)

Schweboo avatar May 11 '17 23:05 Schweboo

The black outline is odd but the cluster seems to be working. Maybe your points are not too close together is some places to actually show them. See the example on cell [3] of http://nbviewer.jupyter.org/github/python-visualization/folium/blob/d2c495b871c09dac656b4fc4f8b153e36f1eed14/examples/Plugins.ipynb

There the icons are all around a smaller area of the globe. At different zoom levels everything is a single cluster.

ocefpaf avatar May 12 '17 20:05 ocefpaf

The cluster does not work at all for any data points in which I used the list code shown. It worked when I hard coded coordinates with numbers. I need to be able to use a list to initiate a cluster. I looked at the example, where the clusters appeared as I zoomed back. No matter what zoom I use, the clusters only show up when I hard code the coordinates.

Some of the points are very close together, between -0.1 and 0.1 difference only. Still no clusters.

Given that my original question was closed by ocepaf, do I need to raise this question again?

Schweboo avatar May 12 '17 21:05 Schweboo

do I need to raise this question again?

No need. I closed because that is particular to your data and not a folium bug.

Please see http://nbviewer.jupyter.org/gist/ocefpaf/e588851f2abcfe3d2d57896734fd2e31 and note the comment regarding the data format for the locations in the docstring.

ocefpaf avatar May 12 '17 21:05 ocefpaf

OK, but still no progress. Thanks. My data is now in this format: print points gives:

[[51.4685415975, -0.199527294926]]

This matches the example you showed me.

my code is:

import folium
.
.
.
marker_cluster = folium.MarkerCluster().add_to(tweet_map)
tweet_map = folium.Map(location=[53, 0], zoom_start=5,control_scale=True)
folium.Marker(location=points,
        popup=' ',
        icon=folium.Icon(color=color_code, icon='ok-sign'),
    ).add_to(marker_cluster)

This shows nothing, but then map.

I tried:

from folium.plugins import MarkerCluster
.
.
.
.
tweet_map = folium.Map(location=[53, 0], zoom_start=5,control_scale=True)
MarkerCluster(locations=points).add_to(tweet_map)

This shows the map, but clusters. Am I doing something wrong

Schweboo avatar May 13 '17 10:05 Schweboo

[[51.4685415975, -0.199527294926]] is a single point and won't produce a cluster. And I cannot debug your snippet without knowing what points is.

I can assure you that a list of lists or array of shape (n,2) will work.

ocefpaf avatar May 13 '17 10:05 ocefpaf

x = float(LATLONG[0])
y = float(LATLONG[1])
points  = [[1,2]]
points[0][0] = x
points[0][1] = y

The function is called whenever a tweet is picked up through the Twitter streaming API

The tweet content is analysed for sentiment (positive/negative). Using geo data from the tweet, markers are posted on a map (GREEN=positive; RED=Negative)

The link ([https://www.schweboo.com/tweet-map/ shows you a map with markers. As the markers build up, you can see the need for clusters. I had assumed that I could add markers to a marker_cluster one by one. Is that an incorrect assumption?

Schweboo avatar May 13 '17 10:05 Schweboo

It is now working. The solution turned out to be very simple.

Folium.Marker([x,y], etc

Where x and y and float numbers. Clusters are now working.

Schweboo avatar May 14 '17 06:05 Schweboo

Have marker clusters been removed? Or is there a new syntax? Using folium.MarkerCluster() results in an error for me.

nathan3leaf avatar Apr 11 '19 02:04 nathan3leaf

@nathan3leaf it’s in folium.plugins

Conengmo avatar Apr 11 '19 05:04 Conengmo

Ah, yes. Sloppy mistake on my part. Thanks!

nathan3leaf avatar Apr 11 '19 18:04 nathan3leaf

Folium master clusters are not working. ` from folium.plugins import MarkerCluster MarkerCluster()

m=folium.Map(location=[34.38,132.45], tiles='CartoDB dark_matter', zoom_start=8) marker_cluster = MarkerCluster().add_to(m) for i in range(len(locations)): folium.Marker(location=locations[i],popup=data['Hostel']).add_to(marker_cluster) folium.LayerControl().add_to(m) m `

where locations is: ` locations=data[['lon','lat']] locations=locations.values.tolist() locations[:5]

[[135.75518200000002, 35.0052769], [135.50725780000002, 34.6447906], [135.5042885, 34.6484984], [135.5065439, 34.64574879999999], [135.51227769999997, 34.6493107]]

` Here is the code I wrote following from examples given by documentation. I can only see the graph but not clusters. Can anybody please help me out? For reference here is the NOTEBOOK Please check the end of the notebook.

Irene-123 avatar Dec 26 '20 10:12 Irene-123

You're missing the Javascript that defines your cluster format. Before you create your MarkerCluster() object, try something like this:

cluster_format_function = """
                                        function(cluster) {
                                        var childCount = cluster.getChildCount();
                                        var c = ' marker-cluster-';
                                        if (childCount < 50) {
                                            c += 'large';
                                            } 
                                        else if (childCount < 300) {
                                            c += 'medium';
                                            } 
                                        else {
                                            c += 'small';
                                            }
        
                                        return new L.DivIcon({ html: '<div><span>' + childCount + '</span></div>', className: 'marker- 
                                        cluster' + c, iconSize: new L.Point(40, 40) });
                                        }"""

Then create your MarkerCluster object and set the icon_create_function parameter to the javascript function you just created: marker_cluster = MarkerCluster(name='Cluster Map Example', icon_create_function=cluster_format_function))

Now continue with your loop to add points to your MarkerCluster object.

nathan3leaf avatar Dec 26 '20 14:12 nathan3leaf

Okay. I have been missing out on icon . Thank you so much for responding

Irene-123 avatar Dec 26 '20 16:12 Irene-123