如何使用 Folium 绘制成对的起点和终点地理空间点之间的路线?
How to plot routes between pairs of starting and ending geospatial points using Folium?
我有一个 pandas 数据帧,其中包含路线起点和终点的经纬度,所以这些是我的列 ('origin_lat, 'origin_lon',destination_lat ','destination_lon').
我可以在 Folium 地图上绘制位置,但我正在寻找一种方法来绘制每个位置之间的路线。
这是我使用的代码:
m = folium.Map([16.7, 81.095], zoom_start=11)
m
# mark each origin as a point
for index, row in df.iterrows():
folium.CircleMarker([row['origin_lat'], row['origin_lng']],
radius=15,
fill_color="#3db7e4", # divvy color
).add_to(m)
for index, row in df.iterrows():
folium.CircleMarker([row['destination_lat'], row['destination_lng']],
radius=15,
fill_color="red", # divvy color
).add_to(m)
#add routes
folium.PolyLine([list(zip(df.origin_lat, df.origin_lng)),list(zip(df.destination_lat, df.destination_lng))], line_opacity = 0.5, color='white',line_weight=5).add_to(m)
我使用的代码是将所有起点位置连接在一起,并将所有目的地位置连接在一起,但我想改为绘制起点和目的地之间的路线。我有什么办法可以解决它吗?
如果我正确理解你的问题,我相信我有你的解决方案
部分进口
import pandas as pd
import numpy as np
import folium
一些示例数据
centroid_lat = 16.7
centroid_lon = 81.095
x = .1
n = 10
o_lats = np.random.uniform(low=centroid_lat - x, high=centroid_lat + x, size=(n,))
o_lons = np.random.uniform(low=centroid_lon - x, high=centroid_lon + x, size=(n,))
d_lats = np.random.uniform(low=centroid_lat - x, high=centroid_lat + x, size=(n,))
d_lons = np.random.uniform(low=centroid_lon - x, high=centroid_lon + x, size=(n,))
df = pd.DataFrame({'origin_lng' : o_lons, 'origin_lat' : o_lats,
'destination_lng': d_lons, 'destination_lat': d_lats})
print(df.head())
destination_lat destination_lng origin_lat origin_lng
0 16.797057 81.074000 16.660164 81.080038
1 16.615371 81.001004 16.772645 80.997770
2 16.784289 81.117082 16.670008 81.032719
3 16.686201 81.184775 16.787999 81.189585
4 16.757704 81.127280 16.720080 81.178466
然后是地图。不需要两个 for 循环,我每次迭代都创建一行
m = folium.Map([centroid_lat, centroid_lon], zoom_start=11)
for _, row in df.iterrows():
folium.CircleMarker([row['origin_lat'], row['origin_lng']],
radius=15,
fill_color="#3db7e4", # divvy color
).add_to(m)
folium.CircleMarker([row['destination_lat'], row['destination_lng']],
radius=15,
fill_color="red", # divvy color
).add_to(m)
folium.PolyLine([[row['origin_lat'], row['origin_lng']],
[row['destination_lat'], row['destination_lng']]]).add_to(m)
m
我有一个 pandas 数据帧,其中包含路线起点和终点的经纬度,所以这些是我的列 ('origin_lat, 'origin_lon',destination_lat ','destination_lon').
我可以在 Folium 地图上绘制位置,但我正在寻找一种方法来绘制每个位置之间的路线。
这是我使用的代码:
m = folium.Map([16.7, 81.095], zoom_start=11)
m
# mark each origin as a point
for index, row in df.iterrows():
folium.CircleMarker([row['origin_lat'], row['origin_lng']],
radius=15,
fill_color="#3db7e4", # divvy color
).add_to(m)
for index, row in df.iterrows():
folium.CircleMarker([row['destination_lat'], row['destination_lng']],
radius=15,
fill_color="red", # divvy color
).add_to(m)
#add routes
folium.PolyLine([list(zip(df.origin_lat, df.origin_lng)),list(zip(df.destination_lat, df.destination_lng))], line_opacity = 0.5, color='white',line_weight=5).add_to(m)
我使用的代码是将所有起点位置连接在一起,并将所有目的地位置连接在一起,但我想改为绘制起点和目的地之间的路线。我有什么办法可以解决它吗?
如果我正确理解你的问题,我相信我有你的解决方案
部分进口
import pandas as pd
import numpy as np
import folium
一些示例数据
centroid_lat = 16.7
centroid_lon = 81.095
x = .1
n = 10
o_lats = np.random.uniform(low=centroid_lat - x, high=centroid_lat + x, size=(n,))
o_lons = np.random.uniform(low=centroid_lon - x, high=centroid_lon + x, size=(n,))
d_lats = np.random.uniform(low=centroid_lat - x, high=centroid_lat + x, size=(n,))
d_lons = np.random.uniform(low=centroid_lon - x, high=centroid_lon + x, size=(n,))
df = pd.DataFrame({'origin_lng' : o_lons, 'origin_lat' : o_lats,
'destination_lng': d_lons, 'destination_lat': d_lats})
print(df.head())
destination_lat destination_lng origin_lat origin_lng
0 16.797057 81.074000 16.660164 81.080038
1 16.615371 81.001004 16.772645 80.997770
2 16.784289 81.117082 16.670008 81.032719
3 16.686201 81.184775 16.787999 81.189585
4 16.757704 81.127280 16.720080 81.178466
然后是地图。不需要两个 for 循环,我每次迭代都创建一行
m = folium.Map([centroid_lat, centroid_lon], zoom_start=11)
for _, row in df.iterrows():
folium.CircleMarker([row['origin_lat'], row['origin_lng']],
radius=15,
fill_color="#3db7e4", # divvy color
).add_to(m)
folium.CircleMarker([row['destination_lat'], row['destination_lng']],
radius=15,
fill_color="red", # divvy color
).add_to(m)
folium.PolyLine([[row['origin_lat'], row['origin_lng']],
[row['destination_lat'], row['destination_lng']]]).add_to(m)
m