{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.5.0+27.g2d457b0.dirty\n" ] } ], "source": [ "import os\n", "import folium\n", "\n", "print(folium.__version__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# How to use ImageOverlay\n", "\n", "It may happen that you want to draw an image on you map. Here are example on how to do that.\n", "\n", "\n", "## Using an image from disk\n", "\n", "If you have a static image file on your disk, you can simply draw it on the map." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "merc = os.path.join('data', 'Mercator_projection_SW.png')\n", "\n", "m = folium.Map([37, 0], zoom_start=1, tiles='stamentoner')\n", "\n", "img = folium.raster_layers.ImageOverlay(\n", " name='Mercator projection SW',\n", " image=merc,\n", " bounds=[[-82, -180], [82, 180]],\n", " opacity=0.6,\n", " interactive=True,\n", " cross_origin=False,\n", " zindex=1,\n", ")\n", "\n", "folium.Popup('I am an image').add_to(img)\n", "\n", "img.add_to(m)\n", "\n", "folium.LayerControl().add_to(m)\n", "\n", "m.save(os.path.join('results', 'ImageOverlay_0.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A few remarks:\n", "\n", "* Note that your image has to be in Mercator projection format.\n", "\n", " The image we've used is based on https://en.wikipedia.org/wiki/File:Mercator_projection_SW.jpg ; that you can find in wikipedia's article on Mercator Projection (https://en.wikipedia.org/wiki/Mercator_projection).\n", "\n", "\n", "You can also provide simply URL. In this case, the image will not be embedded in folium's output." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([37, 0], zoom_start=1, tiles='stamentoner')\n", "\n", "folium.raster_layers.ImageOverlay(\n", " image='https://upload.wikimedia.org/wikipedia/commons/f/f4/Mercator_projection_SW.jpg',\n", " name='Mercator projection SW',\n", " bounds=[[-82, -180], [82, 180]],\n", " opacity=1,\n", " interactive=False,\n", " cross_origin=False,\n", " zindex=1,\n", " alt='Wikipedia File:Mercator projection SW.jpg'\n", ").add_to(m)\n", "\n", "folium.LayerControl().add_to(m)\n", "\n", "\n", "m.save(os.path.join('results', 'ImageOverlay_1.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This works exactly the same way if you want to put a JPG intead of a PNG." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "merc = os.path.join('data', 'Mercator_projection_SW.jpg')\n", "\n", "m = folium.Map([37, 0], zoom_start=1, tiles='stamentoner')\n", "\n", "folium.raster_layers.ImageOverlay(\n", " name='I am a jpeg',\n", " image=merc,\n", " bounds=[[-82, -180], [82, 180]],\n", " opacity=0.6,\n", ").add_to(m)\n", "\n", "folium.LayerControl().add_to(m)\n", "\n", "m.save(os.path.join('results', 'ImageOverlay_2.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## In creating an image with numpy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now you may wish to create your own image, based on another python computation.\n", "For this, you'll need to have numpy installed on your machine.\n", "\n", "Let's creater an image to draw a rectangle in the bounds `[[0, -60], [60, 60]]`." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "image = np.zeros((61, 61))\n", "image[0, :] = 1.0\n", "image[60, :] = 1.0\n", "image[:, 0] = 1.0\n", "image[:, 60] = 1.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can draw it on the map in using:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([37, 0], zoom_start=2)\n", "\n", "folium.raster_layers.ImageOverlay(\n", " image=image,\n", " bounds=[[0, -60], [60, 60]],\n", " colormap=lambda x: (1, 0, 0, x),\n", ").add_to(m)\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that you need to provide a colormap of the form `lambda x: (R,G,B,A)` where `R,G,B,A` are floats between 0 and 1.\n", "\n", "Now, let's try to add a line at latitude 45°, and add a polyline to verify it's well rendered. We'll need to specify `origin='lower` to inform folium that the first lines of the array are to be plotted at the bottom of the image (see `numpy.imshow`, it's the same principle)." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "image = np.zeros((61, 1))\n", "\n", "image[45, :] = 1.0\n", "\n", "\n", "m = folium.Map([37, 0], zoom_start=3)\n", "\n", "folium.raster_layers.ImageOverlay(\n", " image=image,\n", " bounds=[[0, -60], [60, 60]],\n", " colormap=lambda x: (1, 0, 0, x),\n", " origin='lower',\n", ").add_to(m)\n", "\n", "folium.PolyLine([[45, -60], [45, 60]]).add_to(m)\n", "\n", "m.save(os.path.join('results', 'ImageOverlay_3.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But even with `origin='lower'`, the red line is not at the good latitude. This is due to Mercator projection used in Leaflet (and most other map systems).\n", "\n", "You can read wikipedia's article on Mercator Projection (https://en.wikipedia.org/wiki/Mercator_projection), or simply let folium do the job, in precising that you want the mercator stuff to be handled." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([37, 0], zoom_start=3)\n", "\n", "folium.PolyLine([[45, -60], [45, 60]]).add_to(m)\n", "\n", "folium.raster_layers.ImageOverlay(\n", " image=image,\n", " bounds=[[0, -60], [60, 60]],\n", " origin='lower',\n", " colormap=lambda x: (1, 0, 0, x),\n", " mercator_project=True,\n", ").add_to(m)\n", "\n", "m.save(os.path.join('results', 'ImageOverlay_4.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This time, the lines are properly positionned (at the precision of the array)." ] } ], "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.2" } }, "nbformat": 4, "nbformat_minor": 1 }