{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.3.0.dev\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": { "collapsed": true }, "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": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# FIXME: This example is broken!!!\n", "\n", "from folium import features\n", "\n", "m = folium.Map([0, 0], zoom_start=3)\n", "\n", "color_line = features.ColorLine(\n", " 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": [ "### WMS" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([40, -100], zoom_start=4)\n", "\n", "w = features.WmsTileLayer(\n", " \"http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi\",\n", " name='test',\n", " format='image/png',\n", " layers='nexrad-n0r-900913',\n", " attr=u\"Weather data © 2012 IEM Nexrad\",\n", " transparent=True\n", ")\n", "\n", "w.add_to(m)\n", "\n", "m.save(os.path.join('results', 'Features_1.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Marker, Icon, Popup" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import branca\n", "\n", "\n", "f = branca.element.Figure(figsize=(8, 8))\n", "m = folium.Map([0, 0], zoom_start=1)\n", "mk = features.Marker([0, 0])\n", "pp = features.Popup('hello')\n", "ic = features.Icon(color='red')\n", "\n", "f.add_child(m)\n", "mk.add_child(ic)\n", "mk.add_child(pp)\n", "m.add_child(mk)\n", "\n", "f.save(os.path.join('results', 'Features_2.html'))\n", "\n", "f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### RegularPolygonMarker" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = branca.element.Figure()\n", "m = folium.Map([0, 0], zoom_start=1)\n", "mk = features.RegularPolygonMarker([0, 0])\n", "mk2 = features.RegularPolygonMarker([0, 45])\n", "\n", "f.add_child(m)\n", "m.add_child(mk)\n", "m.add_child(mk2)\n", "\n", "f.save(os.path.join('results', 'Features_3.html'))\n", "\n", "f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Vega stuff" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# FIXME: This example is broken!!!\n", "\n", "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", "f = branca.element.Figure()\n", "m = folium.Map([0, 0], zoom_start=1)\n", "mk = features.Marker([0, 0])\n", "p = features.Popup('Hello')\n", "v = features.Vega(data, width='100%', height='100%')\n", "\n", "f.add_child(m)\n", "mk.add_child(p)\n", "p.add_child(v)\n", "m.add_child(mk)\n", "\n", "f.save(os.path.join('results', 'Features_4.html'))\n", "\n", "f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Vega div" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 8, "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(multi_iter2, iter_idx='x', height=400, width=600)\n", "data = json.loads(scatter.to_json())\n", "\n", "f = branca.element.Figure()\n", "v = features.Vega(data, height=40, width=600)\n", "f.add_child(v)\n", "\n", "f.save(os.path.join('results', 'Features_5.html'))\n", "\n", "f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A div and a Map\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 9, "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(multi_iter2, iter_idx='x', height=250, width=420)\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(data, position='absolute', left='50%', width='50%', height='50%')\n", "\n", "v2 = features.Vega(data, position='absolute', left='0%', width='50%', height='50%', top='50%')\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_6.html'))\n", "\n", "f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### GeoJson" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 10, "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_7.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Marker Cluster" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N = 100\n", "\n", "data = np.array(\n", " [\n", " np.random.uniform(low=35, high=60, size=N), # Random latitudes in Europe.\n", " np.random.uniform(low=-12, high=30, size=N), # Random longitudes in Europe.\n", " range(N), # Popups text will be simple numbers .\n", " ]\n", ").T\n", "\n", "m = folium.Map([45, 3], zoom_start=4)\n", "mc = features.MarkerCluster()\n", "\n", "for k in range(N):\n", " mk = features.Marker([data[k][0], data[k][1]])\n", " p = features.Popup(str(data[k][2]))\n", " mk.add_child(p)\n", " mc.add_child(mk)\n", "\n", "m.add_child(mc)\n", "\n", "m.save(os.path.join('results', 'Features_8.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Div" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 12, "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(multi_iter2, iter_idx='x', height=250, width=420)\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_9.html'))\n", "\n", "f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### LayerControl" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(tiles=None)\n", "\n", "folium.TileLayer('OpenStreetMap').add_to(m)\n", "folium.TileLayer('stamentoner').add_to(m)\n", "\n", "folium.LayerControl().add_to(m)\n", "\n", "m.save(os.path.join('results', 'Features_10.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Line example" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Coordinates are 15 points on the great circle from Boston to\n", "# San Francisco.\n", "# Reference: http://williams.best.vwh.net/avform.htm#Intermediate\n", "coordinates = [\n", " [42.3581, -71.0636],\n", " [42.82995815, -74.78991444],\n", " [43.17929819, -78.56603306],\n", " [43.40320216, -82.37774519],\n", " [43.49975489, -86.20965845],\n", " [43.46811941, -90.04569087],\n", " [43.30857071, -93.86961818],\n", " [43.02248456, -97.66563267],\n", " [42.61228259, -101.41886832],\n", " [42.08133868, -105.11585198],\n", " [41.4338549, -108.74485069],\n", " [40.67471747, -112.29609954],\n", " [39.8093434, -115.76190821],\n", " [38.84352776, -119.13665678],\n", " [37.7833, -122.4167]]\n", "\n", "\n", "# Create the map and add the line\n", "m = folium.Map(location=[41.9, -97.3], zoom_start=4)\n", "folium.PolyLine(coordinates, color='#FF0000', weight=5).add_to(m)\n", "\n", "m.save(os.path.join('results', 'Features_11.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.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }