{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import folium\n", "from folium.plugins import MarkerCluster\n", "\n", "\n", "m = folium.Map(location=[44, -73], zoom_start=5)\n", "\n", "marker_cluster = MarkerCluster().add_to(m)\n", "\n", "\n", "folium.Marker(\n", " location=[40.67, -73.94],\n", " popup=\"Add popup text here.\",\n", " icon=folium.Icon(color=\"green\", icon=\"ok-sign\"),\n", ").add_to(marker_cluster)\n", "\n", "folium.Marker(\n", " location=[44.67, -73.94],\n", " popup=\"Add popup text here.\",\n", " icon=folium.Icon(color=\"red\", icon=\"remove-sign\"),\n", ").add_to(marker_cluster)\n", "\n", "folium.Marker(\n", " location=[44.67, -71.94],\n", " popup=\"Add popup text here.\",\n", " icon=None,\n", ").add_to(marker_cluster)\n", "\n", "m" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "size = 100\n", "lons = np.random.randint(-180, 180, size=size)\n", "lats = np.random.randint(-90, 90, size=size)\n", "\n", "locations = list(zip(lats, lons))\n", "popups = [\"lon:{}
lat:{}\".format(lon, lat) for (lat, lon) in locations]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Adding all icons in a single call" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "icon_create_function = \"\"\"\\\n", "function(cluster) {\n", " return L.divIcon({\n", " html: '' + cluster.getChildCount() + '',\n", " className: 'marker-cluster marker-cluster-large',\n", " iconSize: new L.Point(20, 20)\n", " });\n", "}\"\"\"" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from folium.plugins import MarkerCluster\n", "\n", "m = folium.Map(\n", " location=[np.mean(lats), np.mean(lons)], tiles=\"Cartodb Positron\", zoom_start=1\n", ")\n", "\n", "marker_cluster = MarkerCluster(\n", " locations=locations,\n", " popups=popups,\n", " name=\"1000 clustered icons\",\n", " overlay=True,\n", " control=True,\n", " icon_create_function=icon_create_function,\n", ")\n", "\n", "marker_cluster.add_to(m)\n", "\n", "folium.LayerControl().add_to(m)\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Explicit loop allow for customization in the loop." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 17.8 ms, sys: 3.64 ms, total: 21.4 ms\n", "Wall time: 21.8 ms\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "\n", "m = folium.Map(\n", " location=[np.mean(lats), np.mean(lons)],\n", " tiles='Cartodb Positron',\n", " zoom_start=1\n", ")\n", "\n", "marker_cluster = MarkerCluster(\n", " name='1000 clustered icons',\n", " overlay=True,\n", " control=False,\n", " icon_create_function=None\n", ")\n", "\n", "for k in range(size):\n", " location = lats[k], lons[k]\n", " marker = folium.Marker(location=location)\n", " popup = 'lon:{}
lat:{}'.format(location[1], location[0])\n", " folium.Popup(popup).add_to(marker)\n", " marker_cluster.add_child(marker)\n", "\n", "marker_cluster.add_to(m)\n", "\n", "folium.LayerControl().add_to(m);" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`FastMarkerCluster` is not as flexible as MarkerCluster but, like the name suggests, it is faster." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "from folium.plugins import FastMarkerCluster" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 5.66 ms, sys: 0 ns, total: 5.66 ms\n", "Wall time: 4.98 ms\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "\n", "\n", "m = folium.Map(\n", " location=[np.mean(lats), np.mean(lons)],\n", " tiles='Cartodb Positron',\n", " zoom_start=1\n", ")\n", "\n", "FastMarkerCluster(data=list(zip(lats, lons))).add_to(m)\n", "\n", "folium.LayerControl().add_to(m);" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "callback = \"\"\"\\\n", "function (row) {\n", " var icon, marker;\n", " icon = L.AwesomeMarkers.icon({\n", " icon: \"map-marker\", markerColor: \"red\"});\n", " marker = L.marker(new L.LatLng(row[0], row[1]));\n", " marker.setIcon(icon);\n", " return marker;\n", "};\n", "\"\"\"\n", "m = folium.Map(\n", " location=[np.mean(lats), np.mean(lons)], tiles=\"Cartodb Positron\", zoom_start=1\n", ")\n", "\n", "FastMarkerCluster(data=list(zip(lats, lons)), callback=callback).add_to(m)\n", "\n", "\n", "m" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.0" } }, "nbformat": 4, "nbformat_minor": 1 }