-1

I am trying to create a web map which shows the locations of volcanoes. I am using the Folium library in Python, and designing it with HTML. I am also using data from a Pandas DataFrame. However, when I run my code, I get the following error:

Traceback (most recent call last):
  File "webmap.py", line 20, in <module>
    iframe = folium.IFrame(html=html % (name, name, str(elev)), width=200, height=100)
TypeError: not all arguments converted during string formatting

This is my code:

import folium
import pandas

data = pandas.read_csv("Volcanoes.txt")
latitudes = list(data["LAT"])
longitudes = list(data["LON"])
elevations = list(data["ELEV"])
names = list(data["NAME"])

html = """
Volcano name:<br>
<a href="https://www.google.com/search?q=%%22%%s%%22" target="_blank">%s</a><br>
Height: %s m
"""

map = folium.Map(location=[38.58, -99.09], zoom_start=6, tiles="Stamen Terrain")
fg = folium.FeatureGroup(name="My Map")

for lat, lon, elev, name in zip(latitudes, longitudes, elevations, names):
    iframe = folium.IFrame(html=html %(name, name, str(elev)), width=200, height=100)
    fg.add_child(folium.Marker(location=[lat, lon], popup=folium.Popup(iframe), icon=folium.Icon(color="green")))

map.add_child(fg)
map.save("Map1Advacned.html")

The Pandas DataFrame contains information about each volcano, including its location (latitude and longitude), elevation, and name, which I parsed into a Python array in the first bit of my code.

Does anyone know why this error occurs? Any help would be much appreciated. Thanks in advance!

Gavin Wong
  • 1,254
  • 1
  • 6
  • 15

1 Answers1

0

My supplementary data is Singapore government weather station temperature data. I've manipulated it a to fit into your example

  1. changed location and zoom params as Singapore is in a different place and much smaller ;-)
  2. your core issue is the string substitution into html variable. I much prefer f-strings so have changed it to this and it works.
import folium
import pandas as pd

df = pd.DataFrame({'latitude': [1.3764, 1.256, 1.3337, 1.3135, 1.3399, 1.2799],
 'longitude': [103.8492, 103.679, 103.7768, 103.9625, 103.8878, 103.8703],
 'value': [32.3, 31.7, 32.2, 29.9, 32.1, 32.5],
 'tooltip': ['32.3 Ang Mo Kio Avenue 5 August 09, 2020 at 01:00PM',
  '31.7 Banyan Road August 09, 2020 at 01:00PM',
  '32.2 Clementi Road August 09, 2020 at 01:00PM',
  '29.9 East Coast Parkway August 09, 2020 at 01:00PM',
  '32.1 Kim Chuan Road August 09, 2020 at 01:00PM',
  '32.5 Marina Gardens Drive August 09, 2020 at 01:00PM']})

data = df.copy().rename({'latitude':"LAT",'longitude':"LON",'value':"ELEV",'tooltip':"NAME"}, axis=1)
latitudes = list(data["LAT"])
longitudes = list(data["LON"])
elevations = list(data["ELEV"])
names = list(data["NAME"])

def myhtml(name, elev):
    return f"""
    Volcano name:<br>
    <a href="https://www.google.com/search?q=%%22%{name}%%22" target="_blank">{name}</a><br>
    Height: {elev} m
    """
map = folium.Map(location=[1.34, 103.82], zoom_start=12, tiles="Stamen Terrain")
fg = folium.FeatureGroup(name="My Map")

for lat, lon, elev, name in zip(latitudes, longitudes, elevations, names):
    iframe = folium.IFrame(html=myhtml(name, elev), width=200, height=100)
    fg.add_child(folium.Marker(location=[lat, lon], popup=folium.Popup(iframe), icon=folium.Icon(color="green")))

map.add_child(fg)
map.save("Map1Advacned.html")

Rob Raymond
  • 29,118
  • 3
  • 14
  • 30