Visualizing geospatial data using Folium

In this blog, I will discuss a very interesting data visualization library in python which is Folium. I will show you how to use it along with examples.  

Folium is a powerful data visualization library in Python that was built primarily to help people visualize geospatial data.
I will be using a Jupyter notebook to showcase the examples. You can also use the same. You can either download Jupyter notebook locally or can try online.  

Below are the steps and examples of using the library –

1. We will start by importing the folium library:

!conda install -c conda-forge folium=0.5.0 --yes
import folium

print('Folium installed and imported!')

2. World map creation:

Creating a world map with Folium is pretty straightforward. You simply call the map function and that is all.

# define the world map 
world_map = folium.Map()  

# display world map 
world_map

What is really interesting about the maps created by Folium is that they are interactive, so you can zoom in and out after the map is rendered.

3. Showing map of a particular location using latitude and longitude

With the help of using latitude and longitude of a location you can create a world map centered around any place in the world using folium. Here, I have given latitude and longitude of India along with tiles and zoom options.  

# define the world map centered around India with a low zoom level
india_map = folium.Map(location=[20.59, 78.96], zoom_start=5, tiles='Stamen terrain')

india_map

Another amazing feature of Folium is that you can create different map styles using the tiles parameter. Different styles of maps can also be created using the tiles option.

  • Stamen maps
  • Street level maps

4. Showing markers

There is an option to superimpose markers as well as a cluster of markers on the top of a map to highlight or show some important places based on some data. For that I have used feature group option provided by folium.Let’s see how we can add a circular mark to Delhi, India.  

# instantiate a feature group for the incidents in the dataframe
incidents = folium.map.FeatureGroup()

# loop through the dataset and add each to the incidents feature group
incidents.add_child(
        folium.features.CircleMarker(
            [28.7, 77.1],  # Delhi's lat long
            radius=5,
            color='yellow',
            fill=True,
            fill_color='blue',
            fill_opacity=0.6,
            popup='Delhi'
        )
)
# add incidents to map
india_map.add_child(incidents)

I gave latitude and longitude of Delhi and you can see that it has been highlighted by a blue marker.

I have also added a popup label into the options, with the help of this our marker will show “Delhi” when clicked on.

5. Choropleth map

We can also create a special type of map called Choropleth map using Folium map library. A choropleth map is a map in which areas are shaded or patterned in proportion to the measurement of the statistical variable being displayed on the map, such as population density or per capita income.

To create a choropleth map, we need a special file known as geojson file. This file consists of lat long values of each state which we need to pass along with the data. You can access the geojson file for Indian states here.  

Loading geojson file

!wget --quiet https://raw.githubusercontent.com/mickeykedia/India-Maps/master/India_Administrative_Maps/states/india_states.geojson -O In-st.geojson
india_geo = r'In-st.geojson'

print('fetched')

Generating choropleth map

# generate choropleth map using the total number of theft cases in India from 2001-2012
india_map.choropleth(
    geo_data=india_geo,
    data=df_state_filter1,
    columns=['STATE/UT', 'Theft (Section 379-382 IPC) - Number of cases registered'],
    key_on='feature.properties.NAME_1',
    fill_color='YlOrRd', 
    fill_opacity=0.7, 
    line_opacity=0.2,
    legend_name='Cities vs Theft representation'
)

# display map
india_map

This will create a map as shown below. But in this I am using a dataset that I got from data.gov.in So it will not be possible for you to try directly.

Infographic

This map represents the number of cases of theft that happened during 2001-2012 in each state of India. The higher the measurement the darker the color.

In this blog, we explored visualization using folium in detail. Now that you have some basic ideas regarding the map visualization, try to create a project and share with us.


Try it yourself and have fun.

Happy Learning 🙂

1 Response

  1. June 25, 2020

    […] have done this observation with the help of jupyter notebook, pandas, matplotlib and folium to process the data and to create visualizations and […]

Leave a Reply

%d bloggers like this: