ipyleaflet https://github.com/ellisonbg/ipyleaflet

A Jupyter - leaflet bridge

ipyleaflet is a BSD-licensed jupyter interactive widget bringing geospatial data analysis to the Jupyter notebook.

  • Originally authored by Brian Granger
  • MIT Licensed

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


Mirror the output


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)


20

In [7]:
colors


Out[7]:
['#f7fbff',
 '#edf4fc',
 '#e3eef8',
 '#d8e7f5',
 '#cee0f2',
 '#c2d9ee',
 '#b2d2e8',
 '#a0cbe2',
 '#8cc0dd',
 '#75b4d8',
 '#63a8d3',
 '#519ccc',
 '#4090c5',
 '#3282be',
 '#2474b7',
 '#1966ad',
 '#0e59a2',
 '#084b93',
 '#083d7f',
 '#08306b']

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]:
{'geometry': {'coordinates': [[[[-77.39940577291571, 34.5780540544853],
     [-77.39940610066606, 34.57805427015046],
     [-77.39940640189788, 34.57805446836596],
     [-77.39940680648922, 34.57805473459376],
     [-77.39940687019617, 34.57805477651398],
     [-77.39940689381065, 34.57805472199389],
     [-77.3994073308358, 34.57805432604519],
     [-77.39940731720131, 34.578054179242265],
     [-77.39940728534575, 34.578053836252096],
     [-77.39940728147378, 34.57805380828763],
     [-77.39940726378504, 34.57805363205166],
     [-77.39940725501071, 34.57805361742678],
     [-77.39940719593868, 34.57805349818902],
     [-77.39940711258689, 34.57805350039165],
     [-77.39940706262787, 34.57805349996589],
     [-77.3994070238417, 34.578053501157314],
     [-77.39940689549977, 34.57805350509971],
     [-77.3994068702403, 34.5780535173504],
     [-77.39940683257294, 34.57805352755014],
     [-77.39940665584797, 34.57805351246129],
     [-77.39940650171212, 34.578053517196004],
     [-77.39940646539253, 34.578053518311656],
     [-77.39940610067742, 34.578053945657025]]]],
  'type': 'MultiPolygon'},
 'id': 'demo0',
 'properties': {'style': {'color': '#f7fbff',
   'fillColor': '#f7fbff',
   'fillOpacity': 0.5,
   'weight': 1}},
 'type': 'Feature'}

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 [ ]: