Folium - The answer to Geospatial Visualization

Folium - The answer to Geospatial Visualization

ยท

3 min read

What is Folium? ๐Ÿค”

Folium is a powerful Python library that helps you create several types of Leaflet maps. It enables both the binding of data to a map for choropleth visualizations as well as passing rich vector/raster/HTML visualizations as markers on the map.

Folium builds on the data wrangling strengths of the Python ecosystem and the mapping strengths of the leaflet.js library. Manipulate your data in Python, then visualize it on a Leaflet map via folium.

The library has several built-in tilesets from OpenStreetMap, Mapbox, and Stamen, and supports custom tilesets with Mapbox or Cloudmade API keys. Folium supports both GeoJSON and TopoJSON overlays, as well as the binding of data to those overlays to create choropleth maps with color-brewer color schemes.

Why should I use Folium? ๐Ÿคท

Folium was developed for the sole purpose of visualizing geospatial data. While other libraries are available to visualize such kinds of data, such as plotly, they might have limitations you won't be aware of such as maximum API calls for a certain period of time and the fact they weren't built for the sole purpose of geospatial visualization is where folium shines.

Folium is also easy to use and is completely free.

Let's have a glimpse at how things are done with Folium ๐Ÿค“

You can use either of the commands below to install folium

pip install folium

conda install -c conda-forge folium --yes

- Let's create a basic map of the world.

import folium

world_map = folium.Map()

world_map

In the code snippet above we import the folium library then assign the Folium Map object to the world_map variable and then display it. (The image below is the result of the code snippet above)

cropped_world_map.png One really fun thing about folium is that maps you create are really interactive, you can zoom in and out to have better look at things.

You can customize this default definition of the world map by specifying the center of your map and the initial zoom level.

All locations on a map are defined by their respective Latitude and Longitude values. So you can create a map and pass in a center of Latitude and Longitude values of [0, 0].

For a defined center, you can also define the initial zoom level into that location when the map is rendered. The higher the zoom level the more the map is zoomed in to the center.

- Let's center the map around Mexico.

Here we would set the zoom level to 6 and also let's try out one of folium's tilesets by adding the "tiles" attribute and set it to "Stamen Terrain", this lets us create a nice looking map. ๐Ÿคซ (NOTE: not adding tiles would set the map to the default white and blue look)

# define Mexico's geolocation coordinates
mexico_latitude = 23.6345 
mexico_longitude = -102.5528

# define the world map centered around Mexico with a higher zoom level
mexico_map = folium.Map(location=[mexico_latitude, mexico_longitude], zoom_start=6, tiles='Stamen Terrain')

# display Mexico map
mexico_map

Stamen Terrain are maps that feature hill shading and natural vegetation colors. They showcase advanced labeling and linework generalization of dual-carriageway roads.

The code snippet above is self-explanatory as you can see, here is the output:

steman_terrain_mexico.png

Next Up: The Fun parts

To avoid this becoming too long and inevitably boring, I would be releasing the second part in just a few hours from now.

You can also try using the zoom controllers that show up at the top right-hand corner of the output when you run any of the code snippets above, just to have a feel of the interactive nature of Folium's maps.

See you soon! ๐Ÿ˜๐Ÿ‚