Python地图可视化:folium绘制轨迹
folium安装
我是直接使用anaconda prompt安装:pip install folium
绘制路径
folium的坐标系是WGS84坐标系。
import folium
import os
import pandas as pd
import numpy as np
def read_gps_data(path):
P = pd.read_csv(path, header=None, dtype=np.double).values # 读取csv文件,输出为narray
locations_nav = P[:, 0:2].tolist() # narray转换成list
locations_true = P[:, 2:4].tolist()
return locations_nav, locations_true
def draw_gps(locations_nav, locations_true, output_path, file_name):
"""
绘制gps轨迹图
:param locations: list, 需要绘制轨迹的经纬度信息,格式为[[lat1, lon1], [lat2, lon2], ...]
:param output_path: str, 轨迹图保存路径
:param file_name: str, 轨迹图保存文件名
:return: None
"""
m = folium.Map(locations_true[0], zoom_start=30, attr='default') # 中心区域的确定
folium.PolyLine( # polyline方法为将坐标用实线形式连接起来
locations_true, # 将坐标点连接起来
weight=4, # 线的大小为4
color='red', # 线的颜色为红色
opacity=0.8, # 线的透明度
).add_to(m) # 将这条线添加到刚才的区域m内
folium.PolyLine( # polyline方法为将坐标用虚线形式连接起来
locations_nav, # 将坐标点连接起来
weight=2, # 线的大小为2
color='blue', # 线的颜色为蓝色
opacity=0.8, # 线的透明度
dash_array='5' # 虚线频率
).add_to(m) # 将这条线添加到刚才的区域m内
# 起始点,结束点
folium.Marker(locations_true[0], popup='<b>Starting Point</b>').add_to(m)
folium.Marker(locations_true[-1], popup='<b>End Point</b>').add_to(m)
m.save(os.path.join(output_path, file_name)) # 将结果以HTML形式保存到指定路径
if __name__ == '__main__':
path1 = '../gps/385276_pos.csv' # 前两列预估值,后两列真值
locations_nav, locations_true = read_gps_data(path1)
draw_gps(locations_nav, locations_true, '../drawing', '385276.html')
用浏览器打开html文件: