Visualizing Geospatial data using Folium in Python (Update: 5/14/2021 00:56)
Folium gives you the power to visualize data in the most compelling way primarily built for Geospatial maps.
Greetings! So, in this article, we are going to see what different types of maps Folium can create. Yes, all of us are tempted to choose the best frameworks and create charts that are helpful and remarkable, but more than that, the colors we choose, the theme we choose for our map, and how we design it matters. Folium is more powerful than other frameworks like Geopandas, base map, etc in python as folium is really compact and easy to use. But personally, I’m attached to Geopandas as it has been my partner for more than 3 years until I started with Folium! Geopandas is highly flexible and can work easily on core i3 computers.
Each folium map that you may create takes time at least 1 min before showing up if that region, whether like the US or San Francisco, is not present in your cache (i.e. you’re creating that region’s map for the first time). Computers with a core having > i3 are recommended for these maps.
What is Folium?
“Folium is a Python library used for visualizing geospatial data. It is easy to use and yet a powerful library. Folium is a Python wrapper for Leaflet.js which is a leading open-source JavaScript library for plotting interactive maps.”
Environment: Anaconda + Jupyter Notebook on Windows
- Installation of Folium
- Creating some basic maps
- Dual Maps
- Layers & Tiles in Folium
How to install Folium?
Folium can be installed in 2 ways. Using pip
or conda
if you prefer working in anaconda.
Pip — using pip it’s simple like that! Just open up your command prompt or terminal and type this in!
pip install folium
Conda — Using conda just paste anyone from below into your command prompt and download!
conda install -c conda-forge folium
conda install -c conda-forge/label/gcc7 folium
conda install -c conda-forge/label/cf201901 folium
conda install -c conda-forge/label/cf202003 folium
Note: Conda-forge is a community that helps the process of installation but recently when I tried, the installation process failed. Don’t worry, many times the issue arises due to some technical issues which is usually debugged as soon as possible on their GitHub but if it keeps on occurring, try installation using pip. Ps: Pip never fails! ; )
Creating some basic Maps
A basic folium map can be created with simply 2 lines of code as shown below:
You need to provide the [longitude, latitude] of the region you want to show a map show. You can get the points from any site but, the one which I use is GPS coordinates ( no affiliates). This is quick and up to the point. You add the place and you get the coordinates. By default, Folium uses ‘Open Street Map’ as tiles for the map. In the latter section, I will show how you can add other tiles and what are some ways to add custom tiles from Mapbox.
What are tilesets?
Folium is a framework providing the most versatility in creating charts and maps. The default styling in folium maps is the open street maps(OSM) if you want to use other themes like terrain, stamen toner, cartoDB light or dark, or the satellite view you need to add tilesets from the tiles provided by folium. Creating maps using the tiles parameter is one of the most important features. Custom tiles can be added through Mapbox which I’m going to show in the later section but before that, let us see what are tiles? and how we can use them?
Tiles: Stamen Toner, Stamen terrain maps, Mapbox bright Maps, etc.
This is an interactive tileset where you can change the tiles according to your comfort. Clicking on the tiles icon located at the top-right will show you the options which we’ve included in the code above.
Dual Maps.
The dual map plugins provided by Folium are quicker than others and are quite simple.
1st Line: This line declares a map with given coordinates and is stored into m.
2nd & 3rd Lines: You’re adding the first map which will be on the left and the 3rd line adds the map which will add the map on the right.
Folium Markers — Adding London Stations
London underground, simply known as underground has metropolitan railways since 1863. This is indeed called underground but around 55% of it lies on the ground. The system today has 373 stations and is 260 miles long. This was called a ‘tube’ starting from the early 20th century. Adding styles and features to your map is simple. Let’s take a look at the London Streetmap. You want to mark all the stations in London at present 2021.
The map is created using the json
file on the 2nd line. This file does the marking of boundaries on London city.
I will add the station data set over it and marking the stations. For doing this, we need one most important thing which [Latitude, Longitude]. For every data set when you’re covering it over a map you need to have latitude and longitude. In most cases, this is enough but some frameworks like plotyly or cartopy might demand the postal codes as well. I got this data set for the official site by London which you can easily get on the web.
Let us start with coding now,
Step #1 — Open the JSON file. This file holds the polygons or the boundaries of London.
geo = json.load(open(r'C:DataSets\inner_london_polygons.json'))