0% found this document useful (0 votes)
73 views5 pages

Folium

Folium is a Python library for creating interactive Leaflet maps, which can be saved as HTML files or displayed inline in Jupyter notebooks. It allows users to manipulate data in Python and visualize it with various default tilesets, including OpenStreetMap and Stamen. Users can customize maps by adding markers, lines, and layers, as well as adjusting zoom levels and dimensions using the branca library.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views5 pages

Folium

Folium is a Python library for creating interactive Leaflet maps, which can be saved as HTML files or displayed inline in Jupyter notebooks. It allows users to manipulate data in Python and visualize it with various default tilesets, including OpenStreetMap and Stamen. Users can customize maps by adding markers, lines, and layers, as well as adjusting zoom levels and dimensions using the branca library.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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,

then visualize it in a Leaflet map.

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

default with Folium:

OpenStreetMap

•Mapbox Bright

•Mapbox Control Room

•Stamen (incl. Terrain, Toner, and Watercolor)

•Cloudmade

•Mapbo

Plotting Maps with Folium

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

# Map method of folium return Map object

# Here we pass coordinates of Gfg


# and starting Zoom level = 12
my_map1 = folium.Map(location = [ 15.5057, 80.0499],
zoom_start = 12 )

# visualize map
my_ma

Add a circular marker with popup text.

# import folium package


import folium

my_map2 = folium.Map(location = [28.5011226, 77.4099794],


zoom_start = 12)

# CircleMarker with radius


folium.CircleMarker(location = [28.5011226, 77.4099794],
radius = 50, popup = ' FRI ').add_to(my_map2)

# save as html
my_map2.save

simple_marker for parachute style marker with pop-up text

# import folium package


import folium

my_map3 = folium.Map(location = [28.5011226, 77.4099794],


zoom_start = 15)

# Pass a string in popup parameter


folium.Marker([28.5011226, 77.4099794],
popup = ' RGUKT-ONGOLE ').add_to(my_map3)

my_map3.
Add a line to the map

# import folium package


import folium

my_map4 = folium.Map(location = [15.5378, 79.9681],


zoom_start = 12)

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)

# Add a line to the map by using line method .


# it connect both coordinates by the line
# line_opacity implies intensity of the line

folium.PolyLine(locations = [( 15.5057, 80.0499), (15.5378, 79.9681)],


line_opacity = 0.5).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:

from branca.element import Figure


fig=Figure(width=550,height=350)

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

Layers and Tiles in Folium


A tileset is a collection of raster and vector data broken up into a uniform grid of
square tiles. Each tileset has a different way of representing data in the map.
Folium allows us to create maps with different tiles like Stamen Terrain, Stamen
Toner, Stamen Water Color, CartoDB Positron, and many more. By default, the
tiles are set to OpenStreetMap.

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

You might also like