Lösungen Folium¶

Exercise 1: Create a map¶

Create a map with the coordinates of your hometown, or your favorite city (Braunschweig does not count as a solution). Save your Solution in the variable my_map.

Adjust the zoom_start parameter so that your place is visible in the center. To find out which coordinates your place has you can use the online tool latlong.net.

Use CartoDB Positron as tileset.

Set the prefer_canvas parameter to True.

Also please add your city as a comment.

In case of problems, please check the Folium API Map object.

In [2]:
# Your City: Gifhorn
my_map = folium.Map(
        location=(52.485291, 10.547030),
        tiles='cartodb positron',
        zoom_start=13,
        prefer_canvas=True
    )
my_map
Out[2]:
Make this Notebook Trusted to load map: File -> Trust Notebook

Exercise 2: Design your own Marker¶

In the following exercise you will design your own marker.

In case of problems, please check the Folium API:

  • Marker
  • Popup
  • Icon
  • Tooltip

2.1: Defining a Tooltip¶

Define a tooltip variable named tooltip with the text More about TU Braunschweig.

In [3]:
tooltip = "More about TU Braunschweig"

2.2: Defining a Popup¶

Define a popup object with HTML text named tu_popup_html and the TU BS Logo.

Address: Universitätspl. 2, 38106 Braunschweig

Logo URL: https://upload.wikimedia.org/wikipedia/commons/thumb/9/9d/Siegel_TU_Braunschweig_transparent.svg/1200px-Siegel_TU_Braunschweig_transparent.svg.png

You can also use the Template from the Explanation.

You can find a HTML reference here. If you write better in Markdown I recommend the following converter and this Markdown reference.

In [4]:
tu_popup_html = folium.Popup(
    '''
    <p>
        <img 
        src="https://upload.wikimedia.org/wikipedia/commons/thumb/9/9d/Siegel_TU_Braunschweig_transparent.svg/1200px-Siegel_TU_Braunschweig_transparent.svg.png"
        alt="TU BS Logo"
        height="100"
        >
    </p>
    <p><strong>Universitätspl. 2</strong></p>
    <p><em>38106 Braunschweigg</em></p>
    <p><small>Germany, DE</small></p>
    <p>Visit: <a href="https://www.tu-bs.de/">tu-bs.de</a> </p>
    ''',
    show=False
)

2.3 Defining an Icon¶

Next, a red icon named tu_icon should be defined.

The color for the glyphicon should be a gray hexcode that you can choose freely. To make the color selection easier you can use the Color Picker.

As glyph, glyphicon-education, should be used.

In [5]:
tu_icon = folium.Icon(
            color='red',
            icon_color='#eeeeee',
            prefix='glyphicon',
            icon='glyphicon-education',
            angle=0
            )

2.4 Defining a Marker¶

All previously created objects should now be combined into one marker named tu_bs_marker.

As the location data use (52.273460, 10.529231).

In [6]:
tu_bs_marker = folium.Marker(
        location=(52.273460, 10.529231),
        popup=tu_popup_html,
        tooltip=tooltip,
        icon=tu_icon
    )
In [7]:
m = folium.Map(
        location=(52.274, 10.53),
        tiles='OpenStreetMap',
        zoom_start=17,
        prefer_canvas=False
    )

tu_bs_marker.add_to(m)
m
Out[7]:
Make this Notebook Trusted to load map: File -> Trust Notebook

Exercise 3: Mapping the Universties in Lower Saxony¶

3.1: Reading the Dataset¶

The data set for this notebook is unis_nd.csv.

Read this into the variable df using the pandas read_csv function.

(I recommend that you take a look at the data set)

In [17]:
df = pd.read_csv('unis_nd.csv')
df.head(2)
Out[17]:
University name Type of university Sponsorship Right of promotion Founding year Number of students Address lat lon plz pic
0 Hochschule für Bildende Künste Braunschweig Artistic university public yes 1963 976.0 Johannes-Selenka-Platz 1 52.257738 10.502315 38118 Braunschweig https://www.hbk-bs.de/fileadmin/_processed_/5/...
1 Technische Universität Carolo-Wilhelmina zu Br... University public yes 1745 17709.0 Universitätspl. 2 52.273550 10.530097 38106 Braunschweig https://upload.wikimedia.org/wikipedia/commons...

3.2: Defining the Map¶

Before we plot the dataset, define a map with the name lower_saxony.

  • The location should be the georaphic centre of Lower Saxony in Wehrenberg 27318 Hoyerhagen (52.806390, 9.135110).
  • Use a suitable tileset from the documentation
  • Use a suitable zoom setting
In [9]:
lower_saxony = folium.Map(
# Georaphical centre Point of Lower Saxony Wehrenberg 27318 Hoyerhagen
        location=(52.806390, 9.135110), 
        tiles='OpenStreetMap',
        zoom_start=7
    )

Detour Factory Functions¶

In [10]:
def popup_factory(adr: str, zipc: str, country: str, pic: str):
    html = '''
        <p><img src="{}" width="300px" height="auto"></p>
        <p><strong>{}</strong></p>
        <p><em>{}</em></p>
        <p><small>{}</small></p>
        '''.format(pic, adr, zipc, country)
    return folium.Popup(html)
In [11]:
def icon_factory(is_public=True):
    icon = folium.Icon(
        color='black' if is_public else 'white',
        icon_color = 'white' if is_public else 'black',
        icon='glyphicon-home'
        )
    return icon
In [12]:
def marker_factory(loc, popup, is_public=True):
    std_tooltip = 'Click for more information'
    std_icon = icon_factory(is_public)
    return folium.Marker(loc, popup=popup,
            icon=std_icon, tooltip=std_tooltip)

3.3 Plotting the Dataset¶

Write a for loop which reads the values from the dataset df and add the markers to the map lower_saxony using the already defined functions popup_factory and marker_factory.

In [14]:
for index, row in df.iterrows():
    pp = popup_factory(
        adr=row['Address'],
        zipc=row['plz'],
        country='Germany, DE',
        pic=row['pic'],
        )
    location = (float(row['lat']), float(row['lon']))
    
    is_public = False
    if row['Sponsorship'] == 'public':
        is_public = True
        
    marker = marker_factory(location, pp, is_public)                     
    marker.add_to(lower_saxony)
In [15]:
lower_saxony
Out[15]:
Make this Notebook Trusted to load map: File -> Trust Notebook