
[英]GeoPandas Polygon Coordinates Not Displaying Properly on Folium Map
[英]Folium initial map coordinates location not working
我正在使用 folium 到 plot 一些坐标,每次运行代码时,它都会在一些默认坐标而不是我定义的坐标处打开 map。
我试过了:
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()
我希望浏览器在 [41.399231, 2.168904] 坐标处打开并绑定到我在 points 变量中使用的坐标。 相反,我去了非洲肯尼亚附近的某个地方。 无论我用作配置的输入坐标如何,这种情况都会发生。
终端没有提示错误。 每次运行代码时,我都可以使用缩放来重新定位感兴趣的区域。
我认为这可能是因为我使用 map 作为参数并将配置覆盖为默认值。 当我没有 plot 任何标记时它会起作用。
问题:如何在 map 中添加标记和线,而无需为我在routes_processed
中的坐标的每次迭代创建一个新标记和线,并且它保持在定义的位置 [41.399231,2.168904] 的中心?
我正在使用 MSI/第 11 代 Intel(R) Core(TM) i7-11800H 上的 Windows 10 Pro。
使用带有 Python 3.9.15 和 folium 版本 0.14.0 和 pandas 1.5.2 的 conda 环境。
从 VSCode、powershell 终端运行并提示到 Chrome 109.0.5414.75(Build oficial)(64 位)。
我认为这个问题对于用户来说是一个有用的代码,作为使用 folium 进行可视化的示例。 错误原因是基本的经纬度颠倒了,但是提问者给了我回答问题的机会。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.