0

I have a mass of coordinates stored in a XML-File. They are parsed in Python and stored into Pandas DataFrame, which I want to use to map all those coordinates on a map.

The whole parsing is working, even the DataFrame is correct. I tried to install folium via AUR, but same error.

#!/usr/bin/env python
import pandas as pd
import requests
from xml.etree import ElementTree
import numpy as np
import folium

xml_data = "coords.xml"

tree = ElementTree.parse(xml_data)
counter = tree.find('counter')

id = []
name = []
latitude = []
longitude = []

for c in tree.findall('counter'):
    id.append(c.attrib['id'])
    name.append(c.find('name').text)
    latitude.append(c.find('latitude').text)
    longitude.append(c.find('longitude').text)

df_counters = pd.DataFrame(
    {'ID' : id,
    'Name' : name,
    'latitude' : latitude,
    'longitude' : longitude
    })
df_counters.head()

locations = df_counters[['latitude', 'longitude']]
locationlist = locations.values.tolist()

map = folium.Map(location=[47.3, 5.2], zoom_start=10)
for point in range(0, len(locationlist)):
    folium.Marker(locationlist[point], popup=df_counters['Name'][point]).add_to(map)
map

The parsed xml file looks like this:

<counter id="10084">
  <name>DSC_00458.JPG</name>
  <latitude>47.4340525</latitude>
  <longitude>8.208285</longitude>
</counter>
<counter id="10085">
  <name>DSC_01287.JPG</name>
  <latitude>47.3563725</latitude>
  <longitude>12.449092499999999</longitude>
</counter>

What I want to reach is, that I map all those coordinates as points on a map. But instead of getting the QT-Window of Folium, there's just the following message in stacktrace:

Traceback (most recent call last):
  File "./map.py", line 40, in <module>
    folium.Marker(locationlist[point], popup=df_counters['Name'][point]).add_to(map)
  File "/home/rtfm/.local/lib/python3.7/site-packages/folium/map.py", line 252, in __init__
    self.location = _validate_coordinates(location)
  File "/home/rtfm/.local/lib/python3.7/site-packages/folium/utilities.py", line 39, in _validate_coordinates
    if _isnan(coordinates):
  File "/home/rtfm/.local/lib/python3.7/site-packages/folium/utilities.py", line 65, in _isnan
    return any(math.isnan(value) for value in _flatten(values))
  File "/home/rtfm/.local/lib/python3.7/site-packages/folium/utilities.py", line 65, in <genexpr>
    return any(math.isnan(value) for value in _flatten(values))
TypeError: must be real number, not str

I can't figure out, where the actual reason for the stacktrace is. Even if I try the original example from here: https://georgetsilva.github.io/posts/mapping-points-with-folium/ I get the same error message back.

  • 1
    Maybe your `latitude` and `longitude` columns are of type `object`. Try `locationlist = locations.astype(float).values.tolist()` to convert them floats. Or perhaps do the cast during the xml parsing process – Bob Haffner Dec 26 '18 at 21:47
  • I made it like this: `locations = df_counters[['latitude', 'longitude']] locationlist = locations.values.tolist() locationlist = [[float(lat),float(lon)] for [lat,lon] in locationlist]` which worked for me. –  Dec 28 '18 at 21:36
  • Are you sure locationlist[point] is a number? It looks like it is still as a string. – Niels Henkens Jan 05 '19 at 15:05
  • Are you sure locationlist[point] is a number? It looks like it is still as a string. – Niels Henkens Jan 05 '19 at 16:28

0 Answers0