ipyleaflet is a BSD-licensed jupyter interactive widget bringing geospatial data analysis to the Jupyter notebook.
Installation: (beta version)
conda install -c conda-forge ipyleaflet
In [1]:
from ipyleaflet import (
Map,
Polyline, Polygon, Rectangle, Circle, CircleMarker,
GeoJSON
)
from ipywidgets import IntSlider, jslink
In [2]:
center = [34.6252978589571, -77.34580993652344]
zoom = 10
In [3]:
m = Map(center=center, zoom=zoom, layout={'width': '600px', 'height': '300px'})
m
In [4]:
import matplotlib as mpl
import matplotlib.cm
import matplotlib.colors
import numpy as np
def n_colors(n, colormap=mpl.cm.Blues):
data = np.linspace(0.0,1.0,n)
c = [mpl.colors.rgb2hex(d[0:3]) for d in colormap(data)]
return c
def data_to_colors(data, colormap=mpl.cm.Blues):
c = [mpl.colors.rgb2hex(d[0:3]) for d in colormap(mpl.colors.Normalize()(data))]
return c
In [5]:
import json
with open('demo.json') as f:
data = json.load(f)
In [6]:
n_features = len(data['features'])
colors = n_colors(n_features)
print(n_features)
In [7]:
colors
Out[7]:
In [8]:
for feature, color in zip(data['features'], colors):
feature['properties']['style'] = {'color':color, 'weight': 1, 'fillColor':color, 'fillOpacity':0.5}
In [9]:
data['features'][0]
Out[9]:
In [10]:
g = GeoJSON(data=data)
In [11]:
m.add_layer(g)
In [12]:
zoom_slider = IntSlider(description='Zoom', min=2, max=15, value=10)
jslink((zoom_slider, 'value'), (m, 'zoom'))
zoom_slider
In [13]:
m
In [14]:
m.remove_layer(g)
In [15]:
g.close()
In [ ]:
In [ ]:
In [ ]: