What is Folium in Python?
Folium is a Python Library that allow us to visualize spatial data (geographical data) in an interactive manner (you can drag,zoom,click on the map ).Manipulate your data in Python, then visualize it in on a Leaflet map via folium.
Official Documentation of Folium (Click Here)
Geographical Data: All kinds of data that contain geographical (latitude, longitude, altitude, shape) as part of its feature. (Maps!).
Note that to see the position of any place (place in city,city,district, state,country etc...) on the map, we need latitude and longitude.
Here is a link to a site that I follow to find the position (latitude and longitude) of any place (click here).
Leaflet Map: Leaflet is the leading open-source JavaScript library (leaflet.js) for mobile-friendly interactive maps.It has all the mapping features most developers ever need.
Installation of folium
Installation is same as other libraries in python
$ pip install folium
Or
How do you use(Install) Folium in Jupyter Notebook or Anaconda?
$ conda install folium -c conda-forge
|
What are tile styles of Folium maps?
Before getting started let's understand what is tiles and zooming in map
Tiles are 256x256 pixels.At the outer most zoom level, 0, the entire world can be rendered in a single map tile (consider world map as one tile).Each zoom level doubles in both dimensions, so a single tile is replaced by 4 tiles when zooming in.
This means that about 22 zoom levels are sufficient for most practical purposes. (You can also see it by doing it on Google map, try it just open your google map start with maximum zoom and see the map after 22 zooms.)
Most tiled web maps follow certain Google Maps conventions.
For clarification see the image below where you can see that how tiles and zooms work in a map.

Getting Started
To create a basic map, simply pass your starting coordinates (longitude and latitude) to Folium map function.
#Creating simple map
marker=folium.Map(location=[28.704060,77.102493],
zoom_start=4)
marker
#saving a map in folium (create a html page)
marker.save('index.html')
|
Output

Note
We have not done much here, just pass the position/location (longitude and latitude) in the folium map function and we got our map. We can save the map in Folium. The map will be saved in the form of html file (that you can use later).
#styling of map
mapp= folium.Map(location=[26.920980,75.794220],zoom_start=4,tiles="Stamen Terrain")
mapp
|
Output

Note
The default tiles are set to OpenStreetMap in folium library but Stamen Terrain, Stamen Toner , Mapbox Bright and Mapbox Control Room and many other tiles are built in , in folium.
To get any of these tiles (map style) you just have to pass a parameter tiles in folium map function as we did in above code.
Markers and Popup
There are numerous marker types (see the google map), start with simple style location marker with popup.
#creating a simple marker
folium.Marker([28.609140,77.234138], popup='India Gate', tooltip="click me for more").add_to(marker)
marker
|
Output

Note: As you can see from the output we have created a simple marker on location of India gate Delhi with popup message 'India Gate'.
Creating different markers and icons in map
#Different types of markers and icon
marker=folium.Map(location=[28.704060,77.102493],
zoom_start=10)
#adding blue circle marker to india gate
folium.CircleMarker(
[28.609140,77.234138],
radius=8,
popup='India Gate',
color='#3186cc',
fill=True,
fill_color='#3186cc'
).add_to(marker)
#adding red circle marker to red fort
folium.CircleMarker(
location=[28.6562,77.2410],
radius=10,
popup='red Fort',
color='#ff0033',
fill=True,
).add_to(marker)
#adding icon to the map
folium.Marker(
[28.6304,77.2177],
popup='CP Delhi',
icon=folium.Icon(color='#ff0033', icon='fas fa-shopping-cart')
).add_to(marker)
marker
|
Output
Note
As you can see in the output, we have made three markers here.
The first marker, which is a circle marker, is used in red color for the red fort.
We used the second marker in blue color circle for India Gate.
And the third marker in which we have shown a shopping cart icon, used for the cp (Connaught Place, shopping mart) Delhi .
When you people use Google Map, then you must have seen many icons popping location name.Here is a link of listed icons (click here).
Plotting Geojson/ json and Topojson file on map
Geojson/json files: GeoJSON is a JSON (JavaScript Object Notation) based format designed to represent the geographical features with their non-spatial attributes.The features reflect addresses and places as point’s streets, main roads and borders as line strings and countries, provinces, and land regions as polygons.
TopoJSON: An extension of GeoJSON is TopoJSON that is smaller in size and encodes geospatial topology.
Here we used Indian states json file to define the state boundaries and multiple layers can be visualized on the same map. Link of Indian states Geo json file ( click here ).used here.
#plotting Geojson data on map
marker=folium.Map(location=[28.704060,77.102493],zoom_start=4)
folium.GeoJson('india_states.json').add_to(marker)
marker
|
Output

Note: Here we just Indian state Geojson file to define Indian state boundaries.
Python Tutorial
Machine Learning Tutorial
AI Tutorial