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