{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.5.0+105.g065f6f3.dirty\n" ] } ], "source": [ "import os\n", "import folium\n", "\n", "print(folium.__version__)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "\n", "def sample_data(shape=(73, 145)):\n", " nlats, nlons = shape\n", " lats = np.linspace(-np.pi / 2, np.pi / 2, nlats)\n", " lons = np.linspace(0, 2 * np.pi, nlons)\n", " lons, lats = np.meshgrid(lons, lats)\n", " wave = 0.75 * (np.sin(2 * lats) ** 8) * np.cos(4 * lons)\n", " mean = 0.5 * np.cos(2 * lats) * ((np.sin(2 * lats)) ** 2 + 2)\n", "\n", " lats = np.rad2deg(lats)\n", " lons = np.rad2deg(lons)\n", " data = wave + mean\n", "\n", " return lons, lats, data\n", "\n", "\n", "lon, lat, data = sample_data(shape=(73, 145))\n", "lon -= 180" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "\n", "import matplotlib\n", "\n", "cm = matplotlib.cm.get_cmap('cubehelix')\n", "\n", "normed_data = (data - data.min()) / (data.max() - data.min())\n", "colored_data = cm(normed_data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Bad" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(location=[lat.mean(), lon.mean()], zoom_start=1)\n", "\n", "folium.raster_layers.ImageOverlay(\n", " image=colored_data,\n", " bounds=[[lat.min(), lon.min()], [lat.max(), lon.max()]],\n", " opacity=0.25\n", ").add_to(m)\n", "\n", "m.save(os.path.join('results', 'GeodedeticImageOverlay_0.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Good" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(location=[lat.mean(), lon.mean()], zoom_start=1)\n", "\n", "folium.raster_layers.ImageOverlay(\n", " image=colored_data,\n", " bounds=[[lat.min(), lon.min()], [lat.max(), lon.max()]],\n", " mercator_project=True,\n", " opacity=0.25\n", ").add_to(m)\n", "\n", "m.save(os.path.join('results', 'GeodedeticImageOverlay_1.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Same as above but with cartopy" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import cartopy.crs as ccrs\n", "from cartopy.img_transform import warp_array\n", "\n", "source_extent = [lon.min(), lon.max(), lat.min(), lat.max()]\n", "\n", "new_data = warp_array(colored_data,\n", " target_proj=ccrs.GOOGLE_MERCATOR,\n", " source_proj=ccrs.PlateCarree(),\n", " target_res=data.shape,\n", " source_extent=source_extent,\n", " target_extent=None,\n", " mask_extrapolated=False)\n", "\n", "\n", "m = folium.Map(location=[lat.mean(), lon.mean()], zoom_start=1)\n", "\n", "folium.raster_layers.ImageOverlay(\n", " image=new_data[0],\n", " bounds=[[lat.min(), lon.min()], [lat.max(), lon.max()]],\n", " opacity=0.25\n", ").add_to(m)\n", "\n", "m.save(os.path.join('results', 'GeodedeticImageOverlay_2.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "TODO: Try [rasterio](https://github.com/mapbox/rasterio/blob/ca75cf0a842943c1b3da4522e6ea3500215130fd/docs/reproject.rst). Rasterio can warp images and arrays." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Compare to original" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import IFrame\n", "\n", "url = 'http://scitools.org.uk/cartopy/docs/latest/gallery/waves.html'\n", "IFrame(url, width=900, height=750)" ] } ], "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": 1 }