Skip to content

Instantly share code, notes, and snippets.

@PBrockmann
Last active April 19, 2016 20:02
Show Gist options
  • Save PBrockmann/5874c88bb61ac2b7af50 to your computer and use it in GitHub Desktop.
Save PBrockmann/5874c88bb61ac2b7af50 to your computer and use it in GitHub Desktop.
sci-wms with folium
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.1.5\n"
]
}
],
"source": [
"import folium\n",
"reload(folium)\n",
"print folium.__version__\n",
"\n",
"from IPython.display import HTML, display\n",
"\n",
"def inline_map(m):\n",
" if isinstance(m, folium.folium.Map):\n",
" m._build_map()\n",
" srcdoc = \"data:text/html;base64,\" + m.HTML.encode('base64')\n",
" embed = HTML('<iframe src=\"{}\" '\n",
" 'style=\"width: {}px; height: {}px; '\n",
" 'border: none\"></iframe>'.format(srcdoc, m.width, m.height))\n",
" else:\n",
" raise ValueError('{!r} is not a folium Map instance.')\n",
" return embed"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe src=\"data:text/html;base64,PCFET0NUWVBFIGh0bWw+CjxoZWFkPgogICA8bWV0YSBodHRwLWVxdWl2PSJjb250ZW50LXR5cGUi\n",
"IGNvbnRlbnQ9InRleHQvaHRtbDsgY2hhcnNldD1VVEYtOCIgLz4KICAgPGxpbmsgcmVsPSJzdHls\n",
"ZXNoZWV0IiBocmVmPSJodHRwczovL2NkbmpzLmNsb3VkZmxhcmUuY29tL2FqYXgvbGlicy9sZWFm\n",
"bGV0LzAuNy43L2xlYWZsZXQuY3NzIiAvPgogICA8c2NyaXB0IHNyYz0iaHR0cHM6Ly9jZG5qcy5j\n",
"bG91ZGZsYXJlLmNvbS9hamF4L2xpYnMvbGVhZmxldC8wLjcuNy9sZWFmbGV0LmpzIj48L3Njcmlw\n",
"dD4KCiAgIDxzY3JpcHQgc3Jj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gkJCQkJc3R5bGVzOiAncGNvbG9yX2dudXBsb3QyJywgCgkJCQkJZm9y\n",
"bWF0OiAnaW1hZ2UvcG5nJywKCQkJCQl0cmFuc3BhcmVudDogdHJ1ZSwKCQkJCQlsYXllcnM6ICd0\n",
"ZW1wJwoJCQkJCX0pOwogICAgICAKCiAgICAgIC8qCiAgICAgIGFkZGl0aW9uIG9mIHRoZSB0aWxl\n",
"IGxheWVycwogICAgICAqLwogICAgICAKCiAgICAgIC8qCiAgICAgIGxpc3Qgb2YgbGF5ZXJzIHRv\n",
"IGJlIGFkZGVkCiAgICAgICovCiAgICAgIHZhciBsYXllcl9saXN0ID0gewogICAgICAKICAgICAg\n",
"InNjaXdtcyI6IHNjaXdtcwoKICAgICAgCiAgICAgIH07CgogICAgICAvKgogICAgICBCb3VuZGlu\n",
"ZyBib3guCiAgICAgICovCiAgICAgIHZhciBzb3V0aFdlc3QgPSBMLmxhdExuZygtOTAsIC0xODAp\n",
"LAogICAgICAgICAgbm9ydGhFYXN0ID0gTC5sYXRMbmcoOTAsIDE4MCksCiAgICAgICAgICBib3Vu\n",
"ZHMgPSBMLmxhdExuZ0JvdW5kcyhzb3V0aFdlc3QsIG5vcnRoRWFzdCk7CgogICAgICAvKgogICAg\n",
"ICBDcmVhdGVzIHRoZSBtYXAgYW5kIGFkZHMgdGhlIHNlbGVjdGVkIGxheWVycwogICAgICAqLwog\n",
"ICAgICB2YXIgbWFwID0gTC5tYXAoJ2ZvbGl1bV8zYmFhOTM3NGUxMzQ0ZDM1YmNmNjhlOGM5NTc5\n",
"MDliNycsIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgY2VudGVyOlsw\n",
"LCAwXSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgem9vbTogMSwKICAg\n",
"ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLy9tYXhCb3VuZHM6IGJvdW5kcywK\n",
"ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgbGF5ZXJzOiBbYmFzZV90aWxl\n",
"XQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSk7CgogICAgICBMLmNvbnRy\n",
"b2wubGF5ZXJzKGJhc2VMYXllciwgbGF5ZXJfbGlzdCkuYWRkVG8obWFwKTsKCiAgICAgIC8vY2x1\n",
"c3RlciBncm91cAogICAgICB2YXIgY2x1c3RlcmVkbWFya2VycyA9IEwubWFya2VyQ2x1c3Rlckdy\n",
"b3VwKCk7CiAgICAgIC8vc2VjdGlvbiBmb3IgYWRkaW5nIGNsdXN0ZXJlZCBtYXJrZXJzCiAgICAg\n",
"IAogICAgICAvL2FkZCB0aGUgY2x1c3RlcmVkIG1hcmtlcnMgdG8gdGhlIGdyb3VwIGFueXdheQog\n",
"ICAgICBtYXAuYWRkTGF5ZXIoY2x1c3RlcmVkbWFya2Vycyk7CgogICAgICAKCiAgICAgIAoKICAg\n",
"ICAgCgogICAgICAKCiAgICAgIAoKICAgICAgCgp2YXIgYXV0b2JvdW5kcyA9IEwuZmVhdHVyZUdy\n",
"b3VwKFtdKS5nZXRCb3VuZHMoKQoKCgoKCm1hcC5maXRCb3VuZHMoYXV0b2JvdW5kcywKICAgIHsi\n",
"cGFkZGluZyI6IFszMCwgMzBdfQopOwoKICAgPC9zY3JpcHQ+Cgo8L2JvZHk+\n",
"\" style=\"width: 600px; height: 400px; border: none\"></iframe>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"map = folium.Map(width=600, height=400, zoom_start=2)\n",
"\n",
"url='http://sci-wms.whoi.edu/wms/datasets/coawst?ELEVATION=15&COLORSCALERANGE=10,30'\n",
"\n",
"# we should have a wms_parameters not only a wms_styles option\n",
"map.add_wms_layer(wms_name='sciwms',\n",
" wms_url=url,\n",
" wms_styles='pcolor_gnuplot2',\n",
" wms_format='image/png',\n",
" wms_layers='temp'\n",
" )\n",
"\n",
"map.add_layers_to_map()\n",
"inline_map(map)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Notes\n",
" * folium is a wrapper of Leaflet that allows to instantiate a Leaflet from a IPython notebook\n",
" * instantiate will be simplify in the next release (see issue https://github.com/python-visualization/folium/issues/212)\n",
" * Have made some changes to actual realease 0.1.5 to produce this example (template changed to take into consideration option styles from Leaflet). Will do a issue/PR on that.\n",
" * Also need to clarify why a styles option is needed. Should be parameters option. See http://leafletjs.com/reference.html#tilelayer-wms\n",
" * I would like to add synchroneaous maps using https://github.com/turban/Leaflet.Sync \n",
" * Seems that sci-wms not built yet over last release of matplotlib because nice \"Perceptually Sequential Uniform colormaps are not available http://matplotlib.org/examples/color/colormaps_reference.html"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
@bakerbd
Copy link

bakerbd commented Apr 19, 2016

I would be very interested in something like this using local data netCDF data.

My first test was to use mplleaflet to create a geojson from pcolormesh but this is slow due to the number of grid points. Maybe something like the mapbox-gl-vt could be used

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