Folium
Folium
Folium is a powerful Python library that helps you create several types of
Leaflet maps. By default, Folium creates a map in a separate HTML file.
Since Folium results are interactive, this library is very useful for
dashboard building. You can also create inline Jupyter maps in Folium.
Folium builds on the data wrangling strengths of the Python ecosystem and the mapping
strengths of the Leaflet.js library. Using Folium, you can manipulate your data in Python,
Folium enables you to generate a base map of specified width and height with either default
tilesets (i.e., map styles) or a custom tileset URL. The following tilesets are available by
OpenStreetMap
•Mapbox Bright
•Cloudmade
•Mapbo
Plotting maps with Folium is easier than you think. Folium provides
the folium.Map() class which takes location parameter in terms of latitude and
longitude and generates a map around it. So, let’s plot a map of Delhi with
latitude and longitude 15.5057 as 80.0499 and respectively:
# import folium package
import folium
# visualize map
my_ma
# save as html
my_map2.save
my_map3.
Add a line to the map
folium.Marker([15.5378, 79.9681],
popup = 'RGUKT-CAMPUS1').add_to(my_map4)
folium.Marker([15.5378, 79.9681],
popup = 'RGUKT-CAMPUS2').add_to(my_map4)
my_map4
maps are interactive. You can zoom in and out by clicking the positive and
negative buttons in the top-left corner of the map. You can also drag the map and
see different regions.
Let’s try to customize this map now. First, we’ll reduce the height and width of
the map, and then we’ll change the zoom level.
We can resize our map by using the branca library in Python. It is a spinoff from
Folium that hosts the non-map specific features. We can use its Figure class for
resizing our maps and pass the desired width and height in pixels:
Next, let’s move to our second problem and change the zoom level. By default,
you can zoom in and out as much as you want. Also, the starting zoom level is
fixed to 10 (the map is plotted with a 10x zoom). Now, you might be thinking –
how can this be a problem?
Well, imagine you want to plot a map of a small area. Here, you need to mention
the appropriate starting zoom level so that the map focuses only on that region.
Also, if you don’t want them to zoom in or out much and lose focus on the map,
then you can restrict it. For doing this, Folium gives us three parameters –
zoom_start, min_zoom, and max_zoom.
m1=folium.Map(width=550,height=350,location=[28.644800,
77.216721],zoom_start=11,min_zoom=8,max_zoom=14)
fig.add_child(m1)
m1
Each tileset shows different features of a map and is suitable for different
purposes. For example, Stamen Terrain features hill shading and natural
vegetation colors. It showcases advanced labeling and linework generalization of
dual-carriageway roads. And, CartoDB Dark Matter shows the CartoDB Positron
map in dark mode.
Since now we know that each tileset provides information in a different way and
serves a different purpose, we can layer them over one another to get more
information by just plotting a single map. We can do this by adding different
layers of tiles to a single map:
fig2=Figure(width=550,height=350)
m2=folium.Map(location=[28.644800, 77.216721])
fig2.add_child(m2)
folium.TileLayer('Stamen Terrain').add_to(m2)
folium.TileLayer('Stamen Toner').add_to(m2)
folium.TileLayer('Stamen Water Color').add_to(m2)
folium.TileLayer('cartodbpositron').add_to(m2)
folium.TileLayer('cartodbdark_matter').add_to(m2)
folium.LayerControl().add_to(m2)
m2
import folium
m=folium.Map(width=550,height=350,location=[15.5057,
80.0499],zoom_start=25,min_zoom=8,max_zoom=14)
m = folium.Map(location=[15.5057, 80.0499],
zoom_start=12, control_scale=True)
# Add marker
# Run: help(folium.Icon) for more info about icons
folium.Marker(
location=[15.477068, 80.050183],
popup='Kumpula Campus',
icon=folium.Icon(color='green', icon='ok-sign'),
).add_to(m)
#Show map