A Look at Folium
By: Christos Maglaras
Tags: Folium, Python, Maps

Folium is a feature rich library used to create interactive maps in Python, visualizing geospatial data. Folium is built on leaflet.js, and is often preferred for its much simpler syntax and installation process, while having all the same capabilities. Below we will explore some of the features while creating a map around Chicago.

Noun, a thin leaflike structure, e.g., in some rocks or in the cerebellum of the brain.

!pip install folium

import folium

from folium import plugins

Just a blank map

It's easy to get started with folium, just begin with a pip install and import folium. Also import plugins from folium, as we will be needing third party extensions for some extra functionality later.

Chicago = folium.Map(location=[41.878876, -87.635918],

zoom_start = 13,

control_scale=True)

This one is easier on the eyes

All that's needed to create your first map is the above snippet, passing into Map the coordinates in decimal format, the zoom level (higher is closer), and control_scale as True for overlays to function correctly later.

Chicago = folium.Map(location=[41.878876, -87.635918],

zoom_start = 13,

control_scale = True,

tiles = "OpenStreetMap")

I learned that tooltips disappear in screenshots

Tiles are styles for the map, such as satellite view or greyscale. We will encounter a few more examples of styles further below.

folium.Marker([41.878876, -87.635918],

popup="Aka Willis Tower",

tooltip='Sears Tower',

icon=folium.Icon(icon="cloud",

color='green')

).add_to(Chicago)

Sears Tower in green, The Bean in red, and Lincoln park purple

One of the most basic features is creating a marker, as shown above. A tooltip can be added to show information while hovering over the pinned location, and a popup will show information in a bubble on click. There are also different icons and colors built in for the markers, as well as the ability to create your own. Remember not to miss the .add_to() at the end to specify what map to add the marker to.

folium.Circle(radius=100,

location=[41.882638554916284,

-87.62330984352772],

popup="Aka Cloud Gate",

tooltip="The Bean",

color="red",

fill=True).add_to(Chicago) folium.CircleMarker(location=[41.922854499259714,

-87.64905956817513],

radius=65,

popup="Lincoln Park",

color="orange",

fill=True,

fill_color="purple").add_to(Chicago)

When did they change the name again?

The two compliments of the standard marker are the circlemarker and the circle, which are mostly alike with a few differences. Circlemarker is used more as a standard marker to indicate small points on the map, while circle is used to draw attention to larger areas.

Chicago.add_child(folium.LatLngPopup())

LatLngPopup pops up the current latitude and longitude of the cursor wherever the map is clicked.

Chicago.add_child(folium.ClickForMarker(popup="Marked Site"))

ClickForMarker is fairly self explanatory, clicking the map will now drop a pin at that location. Using this along with the LatLngPopup is probably;y not used often as the effect of the popup covers the pin, forcing you to click elsewhere.

folium.raster_layers.TileLayer('Open Street Map').add_to(Chicago)

folium.raster_layers.TileLayer('Stamen Terrain').add_to(Chicago)

folium.raster_layers.TileLayer('Stamen Toner').add_to(Chicago)

folium.raster_layers.TileLayer('Stamen Watercolor').add_to(Chicago)

folium.raster_layers.TileLayer('CartoDB Positron').add_to(Chicago)

folium.raster_layers.TileLayer('CartoDB Dark_Matter').add_to(Chicago) folium.LayerControl().add_to(Chicago)

Two very different styles

Here we add an additional layer on top of our map to place some interactive GUI elements. Each layer adds an entry to a dropdown allowing you to change map styles much more easily through the map interface.