添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

在Folium地图中,是否可以用箭头来画线?

10 人关注

我在Jupyter Notebook Server 4.2.1上运行Folium 0.2.1' 和Python 2.7.11。

我正试图在地图上绘制线条,这些线条有一个箭头来表达方向。

import folium
#DFW, LGA coordinates
coordinates=[(32.900908, -97.040335),(40.768571, -73.861603)]
m = folium.Map(location=[32.900908, -97.040335], zoom_start=4)
#line going from dfw to lga
aline=folium.PolyLine(locations=coordinates,weight=2,color = 'blue')
m.add_children(aline)
有没有办法在线条上添加一个箭头?

python
maps
jupyter-notebook
folium
akrishnamo
akrishnamo
发布于 2016-08-23
3 个回答
user3456239
user3456239
发布于 2022-03-16
已采纳
0 人赞同

你可以用一个普通的多边形标记,在终点处画一个三角形...

folium.RegularPolygonMarker(location=(32.900908, -97.040335), fill_color='blue', number_of_sides=3, radius=10, rotation=???).add_to(m)

你必须用一些三角学的方法来计算旋转的角度,使三角形指向正确的方向。 任何此类标记的初始点都指向正东。

绝对是个好主意!我用```from math import atan # returns radians rotation = 90*atan(latitude_difference/longitude_difference) ```完成这个任务。
Tobias Triesch
Tobias Triesch
发布于 2022-03-16
0 人赞同

我可能来的有点晚,但我对其他被这个问题困扰的人有另一个建议。我建议使用 pyproj 包的 Geod 类,它可以进行大地测量和大圆计算。我们可以用它来获得LineString中某一块的前后方位角。然后我们为每一块添加一个小的多边形标记(或类似的东西)在一端。

from pyproj import Geod
# loop your lines
for line in lines.itertuples():
    # format coordinates and draw line
    loc = [[j for j in reversed(i)] for i in line.geometry.coords]
    folium.PolyLine(loc, color="red").add_to(m)
    # get pieces of the line
    pairs = [(loc[idx], loc[idx-1]) for idx, val in enumerate(loc) if idx != 0]
    # get rotations from forward azimuth of the line pieces and add an offset of 90°
    geodesic = Geod(ellps='WGS84')
    rotations = [geodesic.inv(pair[0][1], pair[0][0], pair[1][1], pair[1][0])[0]+90 for pair in pairs]
    # create your arrow
    for pair, rot in zip(pairs, rotations):
        folium.RegularPolygonMarker(location=pair[0], color='red', fill=True, fill_color='red', fill_opacity=1,
                                    number_of_sides=3, rotation=rot).add_to(m)

我希望有人会发现这个片段有帮助。 祝你有个愉快的一天!=)

Marcos
Marcos
发布于 2022-03-16
0 人赞同

我找到了一个解决这个问题的办法。 我用三角法计算我自己的箭点,然后用这些箭点调用折线函数。

    def arrow_points_calculate(self, ini_lat, ini_long, heading):
    lenght_scale = 0.00012
    sides_scale = 0.000025
    sides_angle = 25
    latA= ini_lat
    longA = ini_long
    latB = lenght_scale * math.cos(math.radians(heading)) + latA
    longB = lenght_scale * math.sin(math.radians(heading)) + longA
    latC = sides_scale * math.cos(math.radians(heading + 180 - sides_angle)) + latB
    longC = sides_scale * math.sin(math.radians(heading + 180 - sides_angle)) + longB
    latD = sides_scale * math.cos(math.radians(heading + 180 + sides_angle)) + latB
    longD = sides_scale * math.sin(math.radians(heading + 180 + sides_angle)) + longB
    pointA = (latA, longA)
    pointB = (latB, longB)
    pointC = (latC, longC)
    pointD = (latD, longD)
    point = [pointA, pointB, pointC, pointD, pointB]
    return point
folium.PolyLine(locations=points, color="purple").add_to(
                        position_plot)