{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.3.0+206.g2acfc64.dirty\n" ] } ], "source": [ "import os\n", "import folium\n", "\n", "print(folium.__version__)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "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.save(os.path.join('results', '1000_MarkerCluster0.html'))\n", "\n", "m" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "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": 4, "metadata": { "collapsed": true }, "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": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from folium.plugins import MarkerCluster\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", "marker_cluster = MarkerCluster(\n", " locations=locations, 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.save(os.path.join('results', '1000_MarkerCluster1.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Explicit loop allow for customization in the loop." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 2.04 s, sys: 3.93 ms, total: 2.04 s\n", "Wall time: 2.04 s\n" ] } ], "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": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m.save(os.path.join('results', '1000_MarkerCluster2.html'))\n", "\n", "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": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from folium.plugins import FastMarkerCluster" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 82.6 ms, sys: 0 ns, total: 82.6 ms\n", "Wall time: 81.3 ms\n" ] } ], "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": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m.save(os.path.join('results', '1000_MarkerCluster3.html'))\n", "\n", "m" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 11, "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)],\n", " tiles='Cartodb Positron',\n", " zoom_start=1\n", ")\n", "\n", "FastMarkerCluster(\n", " data=list(zip(lats, lons)),\n", " callback=callback\n", ").add_to(m)\n", "\n", "\n", "m.save(os.path.join('results', '1000_MarkerCluster4.html'))\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.6.2" } }, "nbformat": 4, "nbformat_minor": 1 }