How to overlay an image on a folium interactive map with Python ?

Published: June 05, 2023

Tags: Python; Folium;

DMCA.com Protection Status

In this article, we will discuss how to overlay an image on a folium interactive map with Python. Folium is a powerful library for creating dynamic maps in Python. By combining it with the power of image overlays, you can easily add custom images to your map and make it even more visually appealing.

Create a basic map using folium by following the installation process

First, you'll need to install the Folium library. This can be done using pip:

pip install folium

or

conda install folium -c conda-forge

Next, you need to import the Folium library into your Python script.

import folium

To create a folium interactive map use the folium.Map() function passing in the coordinates of the location where you want to center your map. For example:

m = folium.Map(location=[30, 0],zoom_start=2)

m

It will create the following map:

How to overlay an image on a folium interactive map with Python ?
How to overlay an image on a folium interactive map with Python ?

Overlay an image with folium

You can use any type of image that is supported by Folium, including GeoTIFFs and PNGs.

Now that you have a folium interactive map, you are ready to overlay an image onto it. To do this, you'll need to create an ImageOverlay object. This object will take the image that you want to overlay as well as a bounding box for where it will be placed on the map. You can also set the opacity of the image and adjust other parameters such as its position and size.

Once you have created your ImageOverlay, all you need to do is add it to your folium map. This can be done with the add_child method, passing in your ImageOverlay object:

folium_map.add_child(image_overlay)

And that's all there is to it! You now have an interactive map with a custom image overlay. You can use this technique to add any type of image you want to your Folium map, allowing you to create unique and visually appealing maps.

Example that I created using the following image Fire-Radiative-Power-Mercator-2022-09-01

import folium

m = folium.Map([30,0], zoom_start=2, crs='EPSG3857')

merc = "Fire_Radiative_Power_Mercator_2022_09_01.png"

folium.TileLayer(
        tiles = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
        attr = 'Esri',
        name = 'Esri Satellite',
        overlay = False,
        control = True
       ).add_to(m)

if not os.path.isfile(merc):
    print(f"File {merc} not found!")
else:
    img = folium.raster_layers.ImageOverlay(
        name="NOAA SNPP VIIRS Ibands FRP",
        image=merc,
        bounds=[[[-85,-180]],[85,180]],
        opacity=0.75,
        interactive=True,
        cross_origin=False,
        zindex=1,
    )

    folium.Popup("Fire Radiative Power").add_to(img)

    img.add_to(m)
    folium.LayerControl().add_to(m)

m

The code above will produce the following image:

How to overlay an image on a folium interactive map with Python ?
How to overlay an image on a folium interactive map with Python ?

References

Links Site
folium Quickstart python-visualization.github.io
ImageOverlay python-visualization.github.io
Image

of