{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.7.0+12.gfdf158a.dirty\n" ] } ], "source": [ "import os\n", "import folium\n", "\n", "print(folium.__version__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ColorLine" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "x = np.linspace(0, 2*np.pi, 300)\n", "\n", "lats = 20 * np.cos(x)\n", "lons = 20 * np.sin(x)\n", "colors = np.sin(5 * x)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from folium import features\n", "\n", "m = folium.Map([0, 0], zoom_start=3)\n", "\n", "\n", "color_line = features.ColorLine(\n", " positions=list(zip(lats, lons)),\n", " colors=colors,\n", " colormap=['y', 'orange', 'r'],\n", " weight=10)\n", "\n", "color_line.add_to(m)\n", "\n", "m.save(os.path.join('results', 'Features_0.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Marker, Icon, Popup" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([0, 0], zoom_start=1)\n", "mk = features.Marker([0, 0])\n", "pp = folium.Popup('hello')\n", "ic = features.Icon(color='red')\n", "\n", "mk.add_child(ic)\n", "mk.add_child(pp)\n", "m.add_child(mk)\n", "\n", "m.save(os.path.join('results', 'Features_1.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Vega popup" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import json\n", "import vincent\n", "\n", "N = 100\n", "\n", "multi_iter2 = {\n", " 'x': np.random.uniform(size=(N,)),\n", " 'y': np.random.uniform(size=(N,)),\n", "}\n", "\n", "scatter = vincent.Scatter(multi_iter2, iter_idx='x', height=100, width=200)\n", "data = json.loads(scatter.to_json())\n", "\n", "m = folium.Map([0, 0], zoom_start=1)\n", "mk = features.Marker([0, 0])\n", "p = folium.Popup('Hello')\n", "v = features.Vega(data, width='100%', height='100%')\n", "\n", "mk.add_child(p)\n", "p.add_child(v)\n", "m.add_child(mk)\n", "\n", "m.save(os.path.join('results', 'Features_2.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Vega-Lite popup" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from altair import Chart, load_dataset\n", "\n", "# load built-in dataset as a pandas DataFrame\n", "cars = load_dataset('cars')\n", "\n", "scatter = Chart(cars).mark_circle().encode(\n", " x='Horsepower',\n", " y='Miles_per_Gallon',\n", " color='Origin',\n", ")\n", "\n", "vega = folium.features.VegaLite(\n", " scatter,\n", " width='100%',\n", " height='100%',\n", ")\n", "\n", "m = folium.Map(location=[-27.5717, -48.6256])\n", "\n", "marker = folium.features.Marker([-27.57, -48.62])\n", "\n", "popup = folium.Popup()\n", "\n", "vega.add_to(popup)\n", "popup.add_to(marker)\n", "\n", "marker.add_to(m)\n", "\n", "m.save(os.path.join('results', 'Features_3.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Vega div and a Map\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import branca\n", "\n", "\n", "N = 100\n", "\n", "multi_iter2 = {\n", " 'x': np.random.uniform(size=(N,)),\n", " 'y': np.random.uniform(size=(N,)),\n", "}\n", "\n", "scatter = vincent.Scatter(\n", " multi_iter2,\n", " iter_idx='x',\n", " height=250,\n", " width=420\n", ")\n", "data = json.loads(scatter.to_json())\n", "\n", "f = branca.element.Figure()\n", "\n", "# Create two maps.\n", "m = folium.Map(location=[0, 0],\n", " tiles='stamenwatercolor',\n", " zoom_start=1,\n", " position='absolute',\n", " left='0%',\n", " width='50%',\n", " height='50%')\n", "\n", "m2 = folium.Map(location=[46, 3],\n", " tiles='OpenStreetMap',\n", " zoom_start=4,\n", " position='absolute',\n", " left='50%',\n", " width='50%',\n", " height='50%',\n", " top='50%')\n", "\n", "# Create two Vega.\n", "v = features.Vega(\n", " data,\n", " position='absolute',\n", " left='50%',\n", " width='50%',\n", " height='50%'\n", ")\n", "\n", "v2 = features.Vega(\n", " data,\n", " position='absolute',\n", " left='0%',\n", " width='50%',\n", " height='50%',\n", " top='50%'\n", ")\n", "\n", "f.add_child(m)\n", "f.add_child(m2)\n", "f.add_child(v)\n", "f.add_child(v2)\n", "\n", "f.save(os.path.join('results', 'Features_4.html'))\n", "\n", "f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Vega-Lite div and a Map" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "N = 100\n", "\n", "multi_iter2 = pd.DataFrame({\n", " 'x': np.random.uniform(size=(N,)),\n", " 'y': np.random.uniform(size=(N,)),\n", "})\n", "\n", "scatter = Chart(multi_iter2).mark_circle().encode(x='x', y='y')\n", "scatter.width = 420\n", "scatter.height = 250\n", "data = json.loads(scatter.to_json())\n", "\n", "f = branca.element.Figure()\n", "\n", "# Create two maps.\n", "m = folium.Map(\n", " location=[0, 0],\n", " tiles='stamenwatercolor',\n", " zoom_start=1,\n", " position='absolute',\n", " left='0%',\n", " width='50%',\n", " height='50%'\n", ")\n", "\n", "m2 = folium.Map(\n", " location=[46, 3],\n", " tiles='OpenStreetMap',\n", " zoom_start=4,\n", " position='absolute',\n", " left='50%',\n", " width='50%',\n", " height='50%',\n", " top='50%')\n", "\n", "\n", "# Create two Vega.\n", "v = features.VegaLite(\n", " data,\n", " position='absolute',\n", " left='50%',\n", " width='50%',\n", " height='50%'\n", ")\n", "\n", "v2 = features.VegaLite(\n", " data,\n", " position='absolute',\n", " left='0%',\n", " width='50%',\n", " height='50%',\n", " top='50%'\n", ")\n", "\n", "f.add_child(m)\n", "f.add_child(m2)\n", "f.add_child(v)\n", "f.add_child(v2)\n", "\n", "f.save(os.path.join('results', 'Features_5.html'))\n", "\n", "f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### GeoJson" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N = 1000\n", "\n", "lons = +5 - np.random.normal(size=N)\n", "lats = 48 - np.random.normal(size=N)\n", "\n", "data = {\n", " 'type': 'FeatureCollection',\n", " 'features': [\n", " {\n", " 'type': 'Feature',\n", " 'geometry': {\n", " 'type': 'MultiPoint',\n", " 'coordinates': [[lon, lat] for (lat, lon) in zip(lats, lons)],\n", " },\n", " 'properties': {'prop0': 'value0'}\n", " },\n", " ],\n", "}\n", "\n", "m = folium.Map([48, 5], zoom_start=6)\n", "m.add_child(features.GeoJson(data))\n", "\n", "m.save(os.path.join('results', 'Features_6.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Div" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N = 100\n", "\n", "multi_iter2 = {\n", " 'x': np.random.uniform(size=(N,)),\n", " 'y': np.random.uniform(size=(N,)),\n", "}\n", "\n", "scatter = vincent.Scatter(\n", " multi_iter2,\n", " iter_idx='x',\n", " height=250,\n", " width=420\n", ")\n", "data = json.loads(scatter.to_json())\n", "\n", "f = branca.element.Figure()\n", "\n", "d1 = f.add_subplot(1, 2, 1)\n", "d2 = f.add_subplot(1, 2, 2)\n", "\n", "d1.add_child(folium.Map([0, 0], tiles='stamenwatercolor', zoom_start=1))\n", "d2.add_child(folium.Map([46, 3], tiles='OpenStreetMap', zoom_start=5))\n", "\n", "f.save(os.path.join('results', 'Features_7.html'))\n", "\n", "f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### LayerControl" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(tiles=None)\n", "\n", "folium.raster_layers.TileLayer('OpenStreetMap').add_to(m)\n", "folium.raster_layers.TileLayer('stamentoner').add_to(m)\n", "\n", "folium.LayerControl().add_to(m)\n", "\n", "m.save(os.path.join('results', 'Features_8.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.7.1" } }, "nbformat": 4, "nbformat_minor": 1 }