Skip to content

Instantly share code, notes, and snippets.

@deeplook
Created July 31, 2018 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deeplook/d4e5c77a1493b6dc3eaaa4304ef5f0a9 to your computer and use it in GitHub Desktop.
Save deeplook/d4e5c77a1493b6dc3eaaa4304ef5f0a9 to your computer and use it in GitHub Desktop.
Streaming heatmap example with IPyLeaflet
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Streaming heatmap example\n",
"\n",
"This is an experiment adding a streaming effect to the heatmap example bundled in the [IPyLeaflet](https://github.com/jupyter-widgets/ipyleaflet) package. At the moment the streaming effect is achieved by swapping layers in a map, replacing a layer with outdated data by a newer one. Ideally, there should be one Layer, able to update itself."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"from random import uniform\n",
"\n",
"from ipyleaflet import Map, Heatmap"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Static Heatmap"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def create_random_data(length):\n",
" \"Return a list of some random lat/lon/value triples.\"\n",
" return [[uniform(-80, 80), \n",
" uniform(-180, 180), \n",
" uniform(0, 1000)] for i in range(length)]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "8db798201c734343965d0c75a6ecdabc",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map(basemap={'url': 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', 'max_zoom': 19, 'attribution': 'Map …"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"data = create_random_data(1000)\n",
"m = Map(center=(0, 0), zoom=2)\n",
"heatmap = Heatmap(locations=data, radius=20)\n",
"m.add_layer(heatmap);\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Update data once"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"data2 = create_random_data(1000)\n",
"heatmap2 = Heatmap(locations=data2, radius=20)\n",
"m -= heatmap\n",
"m += heatmap2\n",
"heatmap = heatmap2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Changes are reflected in the map above."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Update data continuously\n",
"\n",
"For this example we do this in a for-loop, but the idea is to do this via some streaming/messaging mechanism."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"for i in range(100):\n",
" data2 = create_random_data(1000)\n",
" heatmap2 = Heatmap(locations=data2, radius=20)\n",
" m -= heatmap\n",
" m += heatmap2\n",
" heatmap = heatmap2\n",
" time.sleep(0.1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Again, changes over some period of time are reflected in the map above."
]
}
],
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@deeplook
Copy link
Author

N.B.: GitHub doesn't render the included output map, but you will see it when running this code locally. But it looks not much different from the one here: https://ipyleaflet.readthedocs.io/en/latest/api_reference/heatmap.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment