{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# How to use ImageOverlay" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It may happen that you want to draw an image on you map. Here are example on how to do that." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import sys" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "sys.path.insert(0,'..')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import folium" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from folium.six import PY3" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from folium import plugins" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## In importing a PNG\n", "\n", "If you have a static image file on your disk, you can simply draw it on the map in doing:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([37,0], zoom_start=1, tiles='stamentoner')\n", "\n", "plugins.ImageOverlay(open('Mercator_projection_SW.png',\n", " 'br' if PY3 else 'r'),\n", " [[-82,-180],[82,180]],\n", " opacity=0.8,\n", " ).add_to(m)\n", "\n", "folium.LayerControl().add_to(m)\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", "* In python 3, be careful to mention explictely the `read mode` of the file (`open(..., 'rb')`). You'll get an error otherwise. \n", "\n", "You can also provide simply a file name. In this case, the image will not be embedded in folium's output : you'll need to keep it on your server when your map will be deployed." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "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", "plugins.ImageOverlay('Mercator_projection_SW.png',\n", " [[-82,-180],[82,180]],\n", " opacity=0.8,\n", " ).add_to(m)\n", "\n", "folium.LayerControl().add_to(m)\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## In importing a JPG" ] }, { "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": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([37,0], zoom_start=1, tiles='stamentoner')\n", "\n", "plugins.ImageOverlay(open('Mercator_projection_SW.jpg',\n", " 'br' if PY3 else 'r'),\n", " [[-82,-180],[82,180]],\n", " opacity=0.8,\n", " ).add_to(m)\n", "\n", "folium.LayerControl().add_to(m)\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." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's creater an image to draw a rectangle in the bounds [[0,-60],[60,60]]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "image = np.zeros((61,61))\n", "image[0,:] = 1.\n", "image[60,:] = 1.\n", "image[:,0] = 1.\n", "image[:,60] = 1." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can draw it on the map in using:" ] }, { "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([37,0], zoom_start=3)\n", "\n", "plugins.ImageOverlay(image,\n", " [[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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "image[45,:] = 1.\n", "\n", "\n", "m = folium.Map([37,0], zoom_start=3)\n", "\n", "plugins.ImageOverlay(image,\n", " [[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", "m\n" ] }, { "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": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 9, "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", "plugins.ImageOverlay(image,\n", " [[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" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This time, the lines are properly positionned (at the precision of the array)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conclusion\n", "\n", "That's all folks !\n", "\n", "Hope it'll be useful to you. Don't hesitate to provide a feedback on what can be improved, which method do you prefer, etc." ] } ], "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.11" } }, "nbformat": 4, "nbformat_minor": 0 }