是否可以在Folium地图中绘制带箭头的线条?

akr*_*amo 6 python maps folium jupyter-notebook

我在Jupyter Notebook Server 4.2.1上使用Python 2.7.11运行Folium 0.2.1'

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

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 有没有办法在线上添加箭头?

小智 9

您可以使用正多边形标记在终点处绘制三角形...

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

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

  • 绝对是个好主意!我用 ``` from math import atan # returns radiansrotation = 90*atan(latitude_difference/longitude_difference) ``` 完成了这个 (3认同)

小智 6

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

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

我希望有人会发现此片段有帮助。\n祝你有美好的一天!=)

\n