Introduction to Folium for interactive maps in python

folium_intro

Skill - Introduction to Folium for interactive maps in python

Table of Contents

Skills Required

Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision


In this post we will get a beginner level introduction to Folium library which is extensively used for creating interactive maps in python

Topics covered

  • Initialize a folium map object with map center and zoom
  • Layer controls button to show/hide layers
  • Create tile layers from built-in sources
  • Create tile layers from other sources
  • Save a map object as html file

Installing folium

  • Open command prompt and type python -m pip install folium
    pip install folium

Initialize a folium map object with map center and zoom

Lets create a simple folium map object

import folium
mapObj = folium.Map(location=[21.437730075416685, 77.255859375],
zoom_start=2, tiles='OpenStreetMap')
  • Keep tiles=None to create a map without tiles
  • The options for tile sources in folium can be found here

Layer controls button to show/hide layers

import folium
mapObj = folium.Map()

# add layers control over the map
folium.LayerControl().add_to(mapObj)

Create tile layers from built-in sources

import folium
mapObj = folium.Map()

# add new tile layers
folium.TileLayer('openstreetmap').add_to(mapObj)
folium.TileLayer('stamenterrain', attr="stamenterrain").add_to(mapObj)
folium.TileLayer('stamenwatercolor', attr="stamenwatercolor").add_to(mapObj)

# add layers control over the map
folium.LayerControl().add_to(mapObj)

The options for built-in tile sources in folium can be found here

Create tile layers from other sources

import folium
mapObj = folium.Map()

# add new tile layer from external source
folium.TileLayer('https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png', name='CartoDB.DarkMatter', attr="CartoDB.DarkMatter").add_to(mapObj)

# add layers control over the map
folium.LayerControl().add_to(mapObj)

The options for external tile sources can be explored here

Save a map object as html file

import folium
mapObj = folium.Map()

# save the map object as a html file
mapObj.save('folium_intro.html')

Complete example

import folium

# initialize a map with center and zoom
mapObj = folium.Map(location=[21.437730075416685, 77.255859375],
                     zoom_start=4, tiles=None)

# add tile layers
folium.TileLayer('openstreetmap').add_to(mapObj)
folium.TileLayer('stamenterrain', attr="stamenterrain").add_to(mapObj)
folium.TileLayer('stamenwatercolor', attr="stamenwatercolor").add_to(mapObj)
folium.TileLayer('https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png', name='CartoDB.DarkMatter', attr="CartoDB.DarkMatter").add_to(mapObj)

# add layers control over the map
folium.LayerControl().add_to(mapObj)

# save the map as html file
mapObj.save('folium_intro.html')

Video

The video for this post can be seen here


References


Table of Contents

Comments