Folium initial map coordinates location not working

Question:

I’m using folium to plot some coordinates and every time I run the code it opens the map at some default coordinates instead of the ones I’ve defined.

I tried:

import folium
import pandas as pd

# includes only an example route, dict contains N keys with each being a route. Not relevant to the issue
routes_processed = {0: [(41.4178016, 2.149055),
                        (41.419455, 2.1498049),
                        (41.4195666, 2.1499949),
                        (41.4195266, 2.1514833),
                        (41.4198416, 2.151785),
                        (41.4201266, 2.1517766),
                        (41.4204183, 2.1502616)]}

m = folium.Map(location=[41.399231, 2.168904])

def add_marker(points, m):
    for point in [points[0], points[-1]]:
        folium.Marker(point).add_to(m)   # add the lines

def add_line(points, m):
    folium.PolyLine(points, weight=5, opacity=1).add_to(m)   # create optimal zoom

def fit_bounds(points, m):
    df = pd.DataFrame(points).rename(columns={0:'Lon', 1:'Lat'})[['Lat', 'Lon']]
    sw = df[['Lat', 'Lon']].min().values.tolist()
    ne = df[['Lat', 'Lon']].max().values.tolist()
    m.fit_bounds([sw, ne])

for key, value in routes_processed.items():
    add_marker(value, m)
    add_line(value, m)
    fit_bounds(value, m)

m.show_in_browser()

I followed this tutorial and this other one.

I expected the browser to open at [41.399231, 2.168904] coordinates and bounded to the coordinates I use in the points variable. Instead I get somewhere around Kenya, Africa. This happens consistently, no matter the input coordinates that I use as config.

No error is prompted in the terminal. I can use zoom to relocate the area of interest every time I run the code.

I asume it can be because I’m using the map as parameter and overwritting the configuration to default. It works when I don’t plot any marker.

Question: how can I add markers and lines to the map without creating a new one for every iteration of my coordinates in routes_processed and that it is kept centered at the defined location [41.399231, 2.168904] ?

Environment

I’m working with a Windows 10 Pro on a MSI / 11th Gen Intel(R) Core(TM) i7-11800H.

Using conda environment with Python 3.9.15 with folium version 0.14.0 and pandas 1.5.2.

Running from VSCode, powershell terminal and promted to Chrome 109.0.5414.75 (Build oficial) (64 bits).

Asked By: Victor Cuadrat

||

Answers:

I think this question is a useful code as a user as an example of visualization using folium. The cause of the error is that the elementary latitude and longitude are reversed, but the questioner has given me the opportunity to answer the question.

import folium
import pandas as pd

# includes only an example route, dict contains N keys with each being a route. Not relevant to the issue
routes_processed = {0: [(41.4178016, 2.149055),
                        (41.419455, 2.1498049),
                        (41.4195666, 2.1499949),
                        (41.4195266, 2.1514833),
                        (41.4198416, 2.151785),
                        (41.4201266, 2.1517766),
                        (41.4204183, 2.1502616)]}

m = folium.Map(location=[41.419, 2.15])

def add_marker(points, m):
    for point in [points[0], points[-1]]:
        folium.Marker(point).add_to(m)   # add the lines

def add_line(points, m):
    folium.PolyLine(points, weight=5, opacity=1).add_to(m)   # create optimal zoom

def fit_bounds(points, m):
    df = pd.DataFrame(points).rename(columns={0:'Lat', 1:'Lon'})[['Lat', 'Lon']] #update
    sw = df[['Lat', 'Lon']].min().values.tolist()
    ne = df[['Lat', 'Lon']].max().values.tolist()
    m.fit_bounds([sw, ne])

for key, value in routes_processed.items():
    add_marker(value, m)
    add_line(value, m)
    fit_bounds(value, m)

#m.show_in_browser()
m

enter image description here

Answered By: r-beginners