Learn everything about Analytics

Home » Geospatial Analysis | Getting Started With Folium In Python!

Geospatial Analysis | Getting Started With Folium In Python!

This article was published as a part of the Data Science Blogathon

Introduction

Often we come across some situations, where the data is best to be shown on a map for better understanding. The data we deal with in our day-to-day lives is usually confined to rows and columns, bar charts, pie charts, and histograms.

But, today with more data-gathering resources and more computational power, it is possible for companies to use location and map data as well, in order to meet their data and analytics needs.

getting started

Importance of Maps and Location-based Data

Maps and location data make it easier to visualize data and hidden patterns, which might have been earlier unrecognizable in spreadsheets/excel.

For example, a dataset contains food delivery locations in a city, along with the order costs, items ordered, and other parameters. This data when visualized on a map will help identify factors like distance, proximity, clusters, etc.

Location-based data plotted on maps gives you the option of portraying a lot more information, and with easier visibility.

What is Geospatial Analysis?

Geospatial Analysis is a technique of building and analyzing map-based visualizations made from GPS data, sensors, mobile devices, satellite imagery, and other sources. Visuals can be maps, cartograms, graphs, etc.

The recognizable maps make it easy to understand and act upon. Location-based events are easily understandable using geospatial analysis. Location aspects often dictate various trends.

For example, a residential area in a city having more expensive properties will likely have people with higher incomes, and they will spend higher amounts of money.

Applications and Uses of Geospatial Analysis

There are various uses of Geospatial analysis.

They can be used to map natural resources, or track weather phenomena like rainfall, snow or humidity, air pressure etc. With the location of past weather phenomenon, one can analyze trends and understand/predict future instances.

For telecommunication data also, we can use geospatial analysis and understand connection strength, subscriber spread, and other parameters. Network strengths fluctuate over time and using maps to visualize the data is the most efficient way.

We can also use maps to plot various commercial data, sales of a shop by the outlets, for example, all Mc Donalds in a city or region can plot their sales on the map. This will help in analyzing the most profitable locations and make better decisions.

Urban planning and city planning can also take the help of map-based analysis techniques. The growing population in cities often needs have increased electricity and water needs, and demand and supply in large cities keep on varying.

City councils and Municipal corporations do have the necessary data in many cases, but not a good way to visualize and analyze the data. If one has the electricity demand per region in a city, plotted on a map, one can determine which regions need an urgent upgrade and more supply. All aspects of urban planning can be done easily with proper geospatial analysis.

Introduction to Folium

Folium is a python library that can be used to visualize geospatial data. The simple commands in Folium make it the best choice to make plots on maps. Folium has a number of built-in tilesets from Mapbox, OpenStreetMap, and Stamen and also supports custom tilesets.

Installation of Folium :

pip install folium

Now, after Folium is installed, we now get started.

import numpy as np
import pandas as pd

We import NumPy and pandas.

# Create a map
kol = folium.Map(location=[22.57, 88.36], tiles='openstreetmap', zoom_start=12)
kol

We created a basic map of Kolkata in python.

geospatial analysis | kolkata map

It can be zoomed in and out and moved around, I will share the Kaggle live notebook link at the end of the article. Let us now plot some interesting locations. In folium, to plot locations if you know the map coordinates is very easy.

#add marker for a place

#victoria memorial
tooltip_1 = "This is Victoria Memorial"
tooltip_2 ="This is Eden Gardens"

folium.Marker(
    [22.54472, 88.34273], popup="Victoria Memorial", tooltip=tooltip_1).add_to(kol)

folium.Marker(
    [22.56487826917627, 88.34336378854425], popup="Eden Gardens", tooltip=tooltip_2).add_to(kol)

kol

Let us look at the plot.

Add marker

Now, let us add a different type of marker to our map.

folium.Marker(
    location=[22.55790780507432, 88.35087264462007],
    popup="Indian Museum",
    icon=folium.Icon(color="red", icon="info-sign"),
).add_to(kol)

kol

Here are the results of the above code. To know about the types of markers, you can check the documentation.

geospatial analysis | marker type

Let us now look at a different type of map, Stamen Toner.

kol2 = folium.Map(location=[22.55790780507432, 88.35087264462007], tiles="Stamen Toner", zoom_start=13)
kol2

Now, let us look at the output generated.

geospatial analysis | stamen toner

Adding markers to the map serves the purpose of labelling and identifying something. With labelling, one can mark any particular point of interest on the map.

Let us now add circles to our map.

#adding circle

folium.Circle(
    location=[22.585728381244373, 88.41462932675563],
    radius=1500,
    popup="Salt Lake",
    color="blue",
    fill=True,
).add_to(kol2)

folium.Circle(
    location=[22.56602918189088, 88.36508424354102],
    radius=2000,
    popup="Old Kolkata",
    color="red",
    fill=True,
).add_to(kol2)


kol2

Let us have a look at the output.

add circle

The map is movable and interactable. Usage of circles can be used for zoning and zone marking purposes in the case of real-life data.

Let us now work on the map of India.

# Create a map
india = folium.Map(location=[20.180862078886562, 78.77642751195584], tiles='openstreetmap', zoom_start=5)
india

To, choose any specific place on the map, we can change the coordinates and edit the zoom_start parameter.

Map India
#adding 3 locations, Mumbai, Delhi and Kolkata
loc= [(19.035698150834815, 72.84981409864244),(28.61271068361265, 77.22359851696532) ,
      (22.564213404457185, 88.35872006950966)]

We will take three cities in India, and plot a line between them.

folium.PolyLine(locations = loc,
                line_opacity = 0.5).add_to(india)

india

Let us take a look at the output.

geospatial analysis | polyline

In this way, we can plot some basic data based on coordinates.

Now, let us work with a Kaggle dataset, having Indian states’ population centres as per 2011 census data. Let us proceed.

df_state=pd.read_csv("/kaggle/input/indian-census-data-with-geospatial-indexing/state wise centroids_2011.csv")
df_state.head()

The data looks like this :

Indian states

The data has 35 entries, now let us plot the data.

#creating a new map for India, for all states population centres to be plotted
# Create a map
india2 = folium.Map(location=[20.180862078886562, 78.77642751195584], tiles='openstreetmap', zoom_start=4.5)
#adding the markers

for i in range (0,35):
    state=df_state["State"][i]
    lat=df_state["Latitude"][i]
    long=df_state["Longitude"][i]
    folium.Marker(
    [lat, long], popup=state, tooltip=state).add_to(india2)

india2

Now, let us look at the plot.

geospatial analysis | population centre India

The plot is generated, and the location of each of the markers is the population centre for the respective state/UT.

Conclusion

Using maps we can understand and analyse data more easily and efficiently. With Folium, we get an easy way to plot points on the map and make sense of our data.

About me

Hi, I am Prateek Majumder, I am a Data Science and Analytics enthusiast. You can connect with me on :

Linkedin

Github

Kaggle

Full code of the exercise on Kaggle

Thank You.

 

The media shown in this article are not owned by Analytics Vidhya and is used at the Author’s discretion.

You can also read this article on our Mobile APP Get it on Google Play