Python Folium Library
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.
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.
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)
#saving a map in folium (create a html page)
marker.save(‘index.html’)
marker
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).
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’.
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.
Note: I am sure you must have learned something new in this article. Click here to see the full version of this article.
Thank You for reading !
Resources