{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.3.0.dev\n" ] } ], "source": [ "import os\n", "import folium\n", "\n", "print(folium.__version__)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import cartopy.crs as ccrs\n", "import matplotlib.pyplot as plt\n", "import cartopy.feature as cfeature\n", "\n", "LAND = cfeature.NaturalEarthFeature('physical', 'land', '50m',\n", " edgecolor='face',\n", " facecolor=cfeature.COLORS['land'])\n", "\n", "\n", "def make_map(bbox, projection=ccrs.Mercator()):\n", " fig, ax = plt.subplots(figsize=(8, 6),\n", " subplot_kw=dict(projection=projection))\n", " ax.set_extent(bbox)\n", " ax.add_feature(LAND, facecolor='0.25')\n", " ax.coastlines(resolution='50m')\n", " return fig, ax" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy.ma as ma\n", "from scipy.io import loadmat\n", "\n", "fname = os.path.join('data', 'mercator_temperature.mat')\n", "merc = loadmat(fname, squeeze_me=True)\n", "\n", "x, y = merc['x'], merc['y']\n", "bbox = [x.min(), x.max(), y.min(), y.max()]\n", "\n", "level = 10 # 10 meters temperature.\n", "data = ma.masked_invalid(merc['temp'][level, ...])\n", "\n", "fig, ax = make_map(bbox=bbox)\n", "cs = ax.pcolormesh(x, y, data, transform=ccrs.PlateCarree())\n", "fig.savefig('GS.png', transparent=True)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "from PIL import Image, ImageChops\n", "\n", "\n", "def trim(img):\n", " border = Image.new(img.mode, img.size, img.getpixel((0, 0)))\n", " diff = ImageChops.difference(img, border)\n", " diff = ImageChops.add(diff, diff, 2.0, -100)\n", " bbox = diff.getbbox()\n", " if bbox:\n", " img = img.crop(bbox)\n", " return np.array(img)\n", "\n", "img = Image.open(\"GS.png\")\n", "img = trim(img)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from folium import plugins\n", "\n", "\n", "m = folium.Map(location=[y.mean(), x.mean()], zoom_start=5)\n", "\n", "plugins.ImageOverlay(img,\n", " [[y.min(), x.min()], [y.max(), x.max()]],\n", " opacity=0.5).add_to(m)\n", "\n", "\n", "m.save(os.path.join('results', 'GulfStreamImageOverlay_0.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bypass the figure creation entirely:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/filipe/miniconda3/envs/FOLIUM/lib/python3.5/site-packages/numpy/ma/core.py:852: RuntimeWarning: invalid value encountered in greater_equal\n", " return umath.absolute(a) * self.tolerance >= umath.absolute(b)\n" ] } ], "source": [ "def colorize(array, cmap='rainbow'):\n", " normed_data = (array - array.min()) / (array.max() - array.min())\n", " cm = plt.cm.get_cmap(cmap)\n", " return cm(normed_data)\n", "\n", "colored_data = colorize(data, cmap='rainbow')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(location=[y.mean(), x.mean()], zoom_start=5)\n", "\n", "plugins.ImageOverlay(np.flipud(colored_data),\n", " [[y.min(), x.min()], [y.max(), x.max()]],\n", " opacity=0.5).add_to(m)\n", "\n", "m.save(os.path.join('results', 'GulfStreamImageOverlay_1.html'))\n", "\n", "m" ] } ], "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.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }